-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathConfigGen.cs
389 lines (366 loc) · 14.7 KB
/
ConfigGen.cs
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
377
378
379
380
381
382
383
384
385
386
387
388
389
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public partial class ConfigGen
{
private const string autoConfigEditorPath = "ConfigAuto/Configs/";
[InitializeOnLoadMethod]
public static void Init()
{
if (!Directory.Exists(autoConfigEditorPath))
return;
var types = typeof(ConfigGen).Assembly.GetTypes()
.Where(t => t.Name != "Config_" && t.Name.StartsWith("Config_"));
foreach (var type in types)
{
var path = autoConfigEditorPath + $"{type.Name}.cs";
//if (type.Name != "Config_Test") continue;
var str = DoGen(path, type);
File.WriteAllText(path, str);
}
}
private static string DoGen(string path, Type type)
{
var obj = Activator.CreateInstance(type);
var className = type.Name.Split('_', 2)[1];
classFuncs.Clear();
var detailStr = DescObj(obj, type.Name);
var funcsStr = ClassFuncs();
var alltext = @"//本文件为自动生成,请不要手动修改
using System;
using System.Collections.Generic;
namespace ConfigAuto
{
";
alltext += $"{GetTabs(1)}public partial class {type.Name}{Environment.NewLine}{GetTabs(1)}{{";
alltext += $"{Environment.NewLine}{GetTabs(2)}private static {type.Name} _{className};";
alltext += $"{Environment.NewLine}{GetTabs(2)}public static {type.Name} {className}";
alltext += $"{Environment.NewLine}{GetTabs(2)}{{";
alltext += $"{Environment.NewLine}{GetTabs(3)}get";
alltext += $"{Environment.NewLine}{GetTabs(3)}{{";
alltext += $"{Environment.NewLine}{GetTabs(4)}if (_{className} == null)";
alltext += $"{Environment.NewLine}{GetTabs(5)}_{className} = new()";
alltext += $"{Environment.NewLine}{GetTabs(5)}{{";
alltext += detailStr;
alltext += $"{Environment.NewLine}{GetTabs(5)}}};";
alltext += $"{Environment.NewLine}{GetTabs(4)}return _{className};";
alltext += $"{Environment.NewLine}{GetTabs(3)}}}";
alltext += $"{Environment.NewLine}{GetTabs(2)}}}";
alltext += Environment.NewLine;
alltext += funcsStr;
alltext += @"
}
}";
//Debug.Log($"====== DoGen ======" +
// $"{Environment.NewLine}" +
// $"{alltext}" +
// $"{Environment.NewLine}" +
// $"====== End ======");
return alltext;
}
private static List<string> ignoreClass = new() { "items", "baoxiang", "selectedblindbox" };
private static Dictionary<string, string> convertClass = new() { { "guide_chapter", "chapter" }, { "guide_level", "level" }, { "start_actions", "actions" }, { "win_actions", "actions" }, { "lose_actions", "actions" } };
private static string ClassFuncs()
{
var str = "";
foreach (var kv in classFuncs)
{
if (ignoreClass.Contains(kv.Key) || convertClass.ContainsKey(kv.Key))
continue;
var isroot = kv.Key.StartsWith("Config_");
if (!isroot)
{
var classname = kv.Key;
if (classname == "data")
classname = $"Root{classname}";
str += $"{GetTabs(2)}public partial class {classname}{Environment.NewLine}";
str += $"{GetTabs(2)}{{{Environment.NewLine}";
}
foreach (var v in kv.Value)
{
str += $"{GetTabs(isroot ? 2 : 3)}{v}{Environment.NewLine}";
}
if (!isroot)
str += $"{GetTabs(2)}}}{Environment.NewLine}";
}
return str;
}
private static string GetTabs(int count)
{
if (count == 0)
return string.Empty;
var res = "";
for (var i = 0; i < count; i++)
res += "\t";
return res;
}
enum ETreeType
{
ENormal,
EFromArray,
EFromObj,
EValueTuple,
EFromDic,
}
private static Dictionary<string, List<string>> classFuncs = new();
private static string DescObj(object obj, string className, int classDeep = 6, ETreeType treeType = ETreeType.ENormal)
{
var str = "";
var type = obj.GetType();
var fs = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (!classFuncs.TryGetValue(className, out var list))
list = new List<string>();
classFuncs[className] = list;
foreach (var f in fs)
{
var fvalue = f.GetValue(obj);
var vnames = f.Name.Split(new char[] { '<', '>' });
var vname = vnames.Length > 1 ? vnames[1] : vnames[0];
var vtype = fvalue.GetType();
var funcs = "";
var realtype = GetRealType(fvalue, vname);
if (realtype == "data")
realtype = $"Root{realtype}";
if (string.IsNullOrEmpty(realtype))
continue;
funcs = $"public {realtype} {vname} {{get;set;}}";
if (!list.Contains(funcs))
list.Add(funcs);
str += GetObjectStr(fvalue, vname, classDeep, ETreeType.EFromObj);
}
if (classDeep > 6 || treeType == ETreeType.EFromObj)
{
classDeep--;
str += Environment.NewLine + GetTabs(classDeep) + "}";
}
return str;
}
private static string GetObjectStr(object fvalue, string vname, int classDeep, ETreeType treeType = ETreeType.ENormal)
{
var str = "";
var vtype = fvalue.GetType();
var fname = vtype.Name;
if (fname.Contains("AnonymousType"))
{
if (treeType != ETreeType.ENormal)
str += Environment.NewLine + GetTabs(classDeep);
if (treeType != ETreeType.EFromArray)
str += vname + " = ";
str += "new()"
+ Environment.NewLine + GetTabs(classDeep) + "{";
str += DescObj(fvalue, vname, classDeep + 1, ETreeType.EFromObj);
if (treeType != ETreeType.ENormal)
{
return str + ",";
}
}
else if (fname.Contains("Dictionary"))
{
var dic = GetReflexDictionaryKeyValueType(fvalue);
var keytype = "";
var valuetype = "";
foreach (var kv in dic)
{
keytype = GetRealType(kv.Key, vname);
valuetype = GetRealType(kv.Value, vname);
break;
}
str += $"{Environment.NewLine}{GetTabs(classDeep)}{vname} = new Dictionary<{keytype}, {valuetype}>{Environment.NewLine}{GetTabs(classDeep)}{{" + Environment.NewLine;
foreach (var kv in dic)
{
str += $"{GetTabs(classDeep + 1)}{{{GetRealValue(kv.Key)}, ";
var kvvtype = kv.Value.GetType();
if (kvvtype.Name.Contains("AnonymousType"))
{
str += $"new {valuetype} {{";
str += DescObj(kv.Value, vname, classDeep + 2, ETreeType.EFromObj);
str += "}," + Environment.NewLine;
}
else if (kvvtype.IsArray)
str += GetObjectStr(kv.Value, vname, classDeep + 2, ETreeType.EFromDic) + Environment.NewLine + GetTabs(classDeep + 1) + "}," + Environment.NewLine;
else
str += $"{GetRealValue(kv.Value)}}},{Environment.NewLine}";
}
str += GetTabs(classDeep) + "},";
}
else if (vtype.IsArray || fvalue is objectarr)
{
var isobjectarr = fvalue is objectarr;
Array avalues;
if (isobjectarr)
avalues = (fvalue as objectarr).objects;
else
avalues = fvalue as Array;
str += $"{Environment.NewLine}{GetTabs(classDeep)}";
if (treeType != ETreeType.EFromArray && treeType != ETreeType.EFromDic)
str += $"{vname} = ";
if (isobjectarr)
str += $"new object[]";
else
str += $"new()";
str += $"{Environment.NewLine}{GetTabs(classDeep)}{{";
str += Environment.NewLine;
foreach (var v in avalues)
{
var atype = v.GetType();
if (!atype.IsArray && !atype.Name.Contains("AnonymousType") && !atype.Name.StartsWith("ValueTuple"))
str += GetTabs(classDeep + 1);
break;
}
foreach (var v in avalues)
{
str += $"{GetObjectStr(v, vname, classDeep + 1, ETreeType.EFromArray)}";
}
str += $"{Environment.NewLine}{GetTabs(classDeep)}}}";
if (treeType != ETreeType.EFromDic)
str += ",";
}
else
{
if (vtype.Name.StartsWith("ValueTuple"))
{
str += $"{Environment.NewLine}{GetTabs(classDeep)}new()";
str += $"{Environment.NewLine}{GetTabs(classDeep)}{{";
str += DescObj(fvalue, vname, classDeep + 1, ETreeType.EValueTuple);
str += ",";
}
else if (vtype.IsEnum)
str += Environment.NewLine + GetTabs(classDeep) + vname + " = " + GetRealValue(fvalue) + ",";
else if (classDeep == 1)
str += Environment.NewLine + GetTabs(classDeep) + "public " + fname + " " + vname + " = " + GetRealValue(fvalue) + ";";
else if (treeType == ETreeType.EFromArray)
str += GetRealValue(fvalue) + ",";
else if (treeType == ETreeType.EFromObj)
str += Environment.NewLine + GetTabs(classDeep) + vname + " = " + GetRealValue(fvalue) + ",";
else if (treeType == ETreeType.EValueTuple)
str += Environment.NewLine + GetTabs(classDeep) + vname + " = " + GetRealValue(fvalue) + ",";
else
str += Environment.NewLine + GetTabs(classDeep) + vname + " = " + GetRealValue(fvalue) + ";";
}
return str;
}
static Dictionary<object, object> GetReflexDictionaryKeyValueType(object dictionaryVal)
{
Dictionary<object, object> result = new Dictionary<object, object>();
var count = 0;
Type type = dictionaryVal.GetType();
bool b = type.IsGenericType;
Type IDictionaryType = type.GetInterface("IDictionary", false);
if (b && IDictionaryType != null)
{
//获取集合属性的值的属性
PropertyInfo countPro = type.GetProperty("Count");
PropertyInfo keyPro = type.GetProperty("Keys");
PropertyInfo ValuesPro = type.GetProperty("Values");
//获取对应的值
count = (int)countPro.GetValue(dictionaryVal);
object objKey = keyPro.GetValue(dictionaryVal);
object objValue = ValuesPro.GetValue(dictionaryVal);
//获取索引Get方法
MethodInfo keyGet = objKey.GetType().GetMethod("GetEnumerator", BindingFlags.Instance | BindingFlags.Public);
MethodInfo valuesGet = objValue.GetType().GetMethod("GetEnumerator", BindingFlags.Instance | BindingFlags.Public);
var keys = keyGet.Invoke(objKey, null) as IEnumerator;
var values = valuesGet.Invoke(objValue, null) as IEnumerator;
keys.MoveNext();
values.MoveNext();
for (int i = 0; i < count; i++)
{
result.Add(keys.Current, values.Current);
keys.MoveNext();
values.MoveNext();
}
}
return result;
}
private static string GetRealValue(object fvalue)
{
var vtype = fvalue.GetType();
if (vtype == typeof(string))
return $@"@""{fvalue}""";
if (vtype == typeof(bool))
return fvalue.ToString().ToLower();
if (vtype == typeof(Double))
return fvalue + "f";
if (vtype.IsEnum)
return ((int)fvalue).ToString();
return fvalue.ToString();
}
private static string GetRealType(object obj, string className)
{
if (convertClass.TryGetValue(className, out var convertname))
className = convertname;
if (obj is Array avalues)
foreach (var v in avalues)
{
var vtype = v.GetType();
var fname = vtype.Name;
if (fname.Contains("AnonymousType"))
return $"List<{className}>";
else if (vtype.IsArray)
return $"List<{GetRealType(v, className)}>";
else if (fname.StartsWith("ValueTuple"))
return $"List<{className}>";
else if (fname.StartsWith("Dictionary"))
{
var keytype = "";
var valuetype = "";
var dic = GetReflexDictionaryKeyValueType(obj);
foreach (var kv in dic)
{
keytype = GetRealType(kv.Key, className);
valuetype = GetRealType(kv.Value, className);
break;
}
return $"Dictionary<{keytype}, {valuetype}>";
}
else if (fname.Equals("double", StringComparison.CurrentCultureIgnoreCase))
return $"List<float>";
else
return $"List<{vtype.Name}>";
}
else
{
var vtype = obj.GetType();
var fname = vtype.Name;
if (fname.Contains("AnonymousType"))
return className;
else if (obj is objectarr)
return "object[]";
else if (fname.StartsWith("ValueTuple"))
return className;
else if (fname.StartsWith("Dictionary"))
{
var keytype = "";
var valuetype = "";
var dic = GetReflexDictionaryKeyValueType(obj);
foreach (var kv in dic)
{
keytype = GetRealType(kv.Key, className);
valuetype = GetRealType(kv.Value, className);
break;
}
return $"Dictionary<{keytype}, {valuetype}>";
}
else if (fname.Equals("double", StringComparison.CurrentCultureIgnoreCase))
return "float";
else if (vtype.IsEnum)
return "int";
else
return vtype.Name;
}
return null;
}
}
public class objectarr
{
public object[] objects;
public objectarr(params object[] objects)
{
this.objects = objects;
}
}