-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleParser.cs
More file actions
376 lines (300 loc) · 13.8 KB
/
ModuleParser.cs
File metadata and controls
376 lines (300 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEngine;
public class ModuleParser : MonoBehaviour
{
//var test = 'Item[equipped:!AnchorToHand,LookAt[speed:12],Gun]
//Item.equipped = []
//calls Item().Init();
//Init parses equipped
public static ModuleParser instance;
private void Awake() {
instance = this;
}
public class GameObjectWithScript {
public GameObject gObj;
public string script;
public GameObjectWithScript (GameObject gObj, string script) {
this.gObj = gObj;
this.script = script;
}
}
public class ModuleFunc {
public string func;
public Dictionary<string,string> param;
public ModuleFunc (string func, string paramString) {
this.func = func;
if (!String.IsNullOrEmpty(paramString)) {
param = new Dictionary<string, string>();
List<int> colonIndex = new List<int>();
string pattern = @"(\w+(?: \w+)*):";
foreach (Match match in Regex.Matches(paramString, pattern)) {
//Debug.Log("REGEX " + match.Value + " / " + match.Index);
colonIndex.Add(match.Index);
colonIndex.Add(match.Index + match.Value.Length);
}
string paramName = "";
string paramData = "";
int waitToClose = 0;
int paramStep = 0;
var i = 0;
Debug.Log(paramString);
//1. split functions and parameters into two strings
while (paramString.Length > 0) {
if (waitToClose == 0) {
if (colonIndex.Count > 0 && i >= colonIndex[0]) {
paramStep++;
colonIndex.RemoveAt(0);
} else if (paramString[0] == '[') {
waitToClose++;
}
} else {
if (colonIndex.Count > 0 && i >= colonIndex[0]) {
colonIndex.RemoveAt(0);
}
if (paramString[0] == ']') {
waitToClose--;
} else if (paramString[0] == '[') {
waitToClose++;
}
}
if (paramStep == 1) {
paramName += paramString[0].ToString();
} else if (paramStep == 2) {
paramData += paramString[0].ToString();
} else if (paramStep == 3) {
//to-do: this was sloppy way to fix up strings
var paramNameF = paramName.Substring(0,paramName.Length-1);
var paramDataF = paramData.Substring(0,paramData.Length-1);
param.Add(paramNameF,paramDataF);
paramName = paramString[0].ToString();
paramData = "";
paramStep = 1;
}
paramString = paramString.Remove(0,1);
if (paramString.Length == 0) {
//to-do: this was sloppy way to fix up strings
var paramNameF = paramName.Substring(0,paramName.Length-1);
param.Add(paramNameF,paramData);
}
i++;
}
}
// Debug.Log("func: " + func);
// Debug.Log("param: " + param);
}
}
//return list of modules to call
public static Vector3 StringToVector3(string sVector)
{
// Remove the parentheses
sVector = sVector.Substring(1, sVector.Length-2);
// split the items
string[] sArray = sVector.Split(',');
// store as a Vector3
Vector3 result = new Vector3(
float.Parse(sArray[0]),
float.Parse(sArray[1]),
float.Parse(sArray[2]));
return result;
}
public static Vector2 StringToVector2(string sVector)
{
// Remove the parentheses
sVector = sVector.Substring(1, sVector.Length-2);
// split the items
string[] sArray = sVector.Split(',');
// store as a Vector3
Vector2 result = new Vector2(
float.Parse(sArray[0]),
float.Parse(sArray[1]));
return result;
}
public static Vector4 StringToVector4(string sVector)
{
// Remove the parentheses
sVector = sVector.Substring(1, sVector.Length-2);
// split the items
string[] sArray = sVector.Split(',');
// store as a Vector3
Vector4 result = new Vector4(
float.Parse(sArray[0]),
float.Parse(sArray[1]),
float.Parse(sArray[2]),
float.Parse(sArray[3]));
return result;
}
public static void Parse (GameObject gameObject, string itemScript, bool INIT = true) {
ModuleParser.instance.StartCoroutine(instance.ParseRoutine(gameObject,itemScript,INIT));
}
public IEnumerator ParseRoutine (GameObject gameObject, string itemScript, bool INIT = true) {
var _itemScript = itemScript;
List<ModuleFunc> functions = new List<ModuleFunc>();
string functionToAdd = "";
string paramsToAdd = "";
int waitToClose = 0;
//1. split functions and parameters into two strings
while (_itemScript.Length > 0) {
if (waitToClose == 0) {
if (_itemScript[0] == ',') {
functions.Add(new ModuleFunc(functionToAdd,paramsToAdd));
functionToAdd = "";
paramsToAdd = "";
} else if (_itemScript[0] == '[') {
waitToClose++;
} else {
functionToAdd = functionToAdd + _itemScript[0].ToString();
}
} else {
if (_itemScript[0] == ']') {
waitToClose--;;
} else if (_itemScript[0] == '[') {
waitToClose++;
}
if (waitToClose > 0)
paramsToAdd = paramsToAdd + _itemScript[0].ToString();
}
_itemScript = _itemScript.Remove(0,1);
if (_itemScript.Length == 0) {
functions.Add(new ModuleFunc(functionToAdd,paramsToAdd));
}
}
//Debug.Log("function # " + functions.Count);
List<Component> modules = new List<Component>();
int assetsToLoad = 0;
for (var x = 0;x < functions.Count;x++) {
if (functions[x].param != null) {
foreach (var keypair in functions[x].param) {
if (!keypair.Value.Contains("[") && !keypair.Value.Contains("<") && keypair.Value.Contains("/")) {
assetsToLoad++;
var assetType = keypair.Value.Split("/")[0];
// is it an assets/, items/, sfxs/ ?
if (assetType == "assets" || assetType == "items") {
if (AssetManager.HasAsset(keypair.Value)) {
assetsToLoad--;
continue;
} else if (AssetManager.HasKey(keypair.Value)) {
yield return new WaitUntil(()=> AssetManager.HasAsset(keypair.Value));
assetsToLoad--;
continue;
}
Debug.Log("asset needed " + keypair.Value);
//we add an empty entry to kick things off
AssetManager.AddAsset(keypair.Value,null);
//to-do: check if object has any of item left in inventory
var inventoryId = gameObject.GetComponent<ItemInfo>()?.itemId;
FirebaseManager.Get(keypair.Value,(snapshot)=>{
if (snapshot.Exists) {
ActionManager.instance.SpawnItem(inventoryId, snapshot, (gObj, script) =>{
AssetManager.AddAsset(keypair.Value,new GameObjectWithScript(gObj,script));
assetsToLoad--;
});
} else {
#if UNITY_EDITOR
Debug.Log("get local asset = " + keypair.Value.Split("/")[1]);
var localAsset = GameObject.Find(keypair.Value.Split("/")[1]);
var localAssetScript = localAsset.GetComponent<TestModuleScript>().ModuleScript;
AssetManager.AddAsset(keypair.Value,new GameObjectWithScript(localAsset,localAssetScript));
assetsToLoad--;
#else
Debug.Log("Module -> load asset failed");
#endif
}
});
} else if (assetType == "sfxs") {
SFXManager.LoadSfx(keypair.Value,()=>{
assetsToLoad--;
});
} else {
Debug.LogWarning(("Incompatible asset directory " + assetType));
assetsToLoad--;
}
}
}
}
}
Debug.Log("assetsToLoad = " + assetsToLoad);
while (assetsToLoad > 0) {
yield return null;
}
Debug.Log(gameObject.name + " is ready to be parsed");
//1. add functions and split parameters into strings
for (var i = 0; i < functions.Count;i++) {
//1. add functions to gameobject
//to-do: check if script inherits from Module to avoid destructive code
if (gameObject == null)
break;
var func = functions[i].func;
var param = functions[i].param;
Type type = Type.GetType(func);
var module = gameObject.GetComponent(type);
if (module == null) {
module = gameObject.AddComponent(type);
}
//2. set parameters in module script
if (module) {
modules.Add(module);
if (param != null) {
foreach (KeyValuePair<string,string> keypair in param) {
//Debug.Log("Attempting to set property " + keypair.Key + " to " + keypair.Value);
var prop = type.GetProperty(keypair.Key);
if (prop == null) {
Debug.LogWarning("Setting property " + keypair.Key + " failed! Likely does not exist..");
continue;
}
if (keypair.Value.Contains("[")) {
prop.SetValue(module, keypair.Value, null);
} else if (!keypair.Value.Contains("<") && keypair.Value.Contains("/")) {
var assetType = keypair.Value.Split("/")[0];
if (assetType == "assets" || assetType == "items") {
prop.SetValue(module, AssetManager.GetAsset(keypair.Value), null);
} else if (assetType == "sfxs") {
prop.SetValue(module, SFXManager.GetSfx(keypair.Value), null);
}
} else if (keypair.Value.StartsWith ("(") && keypair.Value.EndsWith (")")) {
int freq = keypair.Value.Split(',').Length - 1;
if (freq == 1) {
prop.SetValue(module, StringToVector2(keypair.Value), null);
} else if (freq == 2) {
prop.SetValue(module, StringToVector3(keypair.Value), null);
} else if (freq == 3) {
prop.SetValue(module, StringToVector4(keypair.Value), null);
}
} else if (isInt(prop) && int.TryParse(keypair.Value, out int intValue)) {
prop.SetValue(module, intValue, null);
} else if (isFloat(prop) && float.TryParse(keypair.Value, out float floatValue)) {
prop.SetValue(module, floatValue, null);
} else if (bool.TryParse(keypair.Value, out bool boolValue)) {
prop.SetValue(module, boolValue, null);
} else {
prop.SetValue(module, keypair.Value, null);
}
}
}
}
if (INIT) {
(module as Module).Init();
} else {
(module as Module).Deinit();
}
}
// for (var x = 0; x < modules.Count;x++) {
// if (INIT) {
// (modules[x] as Module).Init();
// } else {
// (modules[x] as Module).Deinit();
// }
// }
yield return null;
}
private bool isInt (PropertyInfo prop) {
return prop.PropertyType == typeof(int) || prop.PropertyType == typeof(int?);
}
private bool isFloat (PropertyInfo prop) {
return prop.PropertyType == typeof(float) || prop.PropertyType == typeof(float?);
}
}