-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.cs
More file actions
380 lines (302 loc) · 12.3 KB
/
Copy pathUtils.cs
File metadata and controls
380 lines (302 loc) · 12.3 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
377
378
379
380
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Preprocessor
{
public static class Utils
{
public static string ToCSharp(this string source)
{
if (source == "as")
source = "@as";
return source
//Fix compatibility with C#
.Replace('/', '.')
.Replace("`1", "")
.Replace("`2", "")
//.Replace("&", "")
//Replace types to standard format
.Replace("System.Void", "void")
.Replace("System.String", "string")
.Replace("System.Int16", "short")
.Replace("System.Int32", "int")
.Replace("System.Int64", "long")
.Replace("System.Guid", "Guid")
.Replace("System.Collections.Generic.List", "List")
.Replace("System.Collections.Generic.Dictionary", "Dictionary")
.Replace("System.Collections.Generic.ICollection", "ICollection")
.Replace("System.Collections.Generic.IEnumerable", "IEnumerable")
.Replace("System.Byte", "byte")
.Replace("System.Single", "float")
.Replace("System.Boolean", "bool")
.Replace("System.Collections.IEnumerable", "IEnumerable")
.Replace("Albion.Common.Time.GameTimeStamp", "GameTimeStamp")
.Replace("UnityEngine.RaycastHit", "RaycastHit");
}
public static bool IsEnum(this string type, Dictionary<string, string> dict)
{
return dict.ContainsKey(type);
}
public static void Empty(string directory) => Empty(new System.IO.DirectoryInfo(directory));
public static void Empty(this System.IO.DirectoryInfo directory)
{
foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();
foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
}
public static string TypesAndNames(this ParameterMapping[] parameters, Dictionary<string, string> dict = null)
{
if (parameters.Length == 0)
return "";
string outString = "";
foreach (ParameterMapping param in parameters)
{
if (dict == null)
outString += string.Format("{0} {1} {2}, ", param.Type.GetModifier(), param.Type.RemoveModifiers(), param.ObfuscatedName);
else
{
outString += string.Format("{0} {1} {2}, ", param.Type.GetModifier(), param.Type.RemoveModifiers().TryGetWrappedType(dict), param.ObfuscatedName);
}
}
return outString.Substring(0, outString.Length - 2);
}
public static string GenerateWhere(this GenericParameterMapping[] mapping)
{
StringBuilder builder = new StringBuilder();
foreach (var item in mapping)
{
builder.AppendFormat("where {0}:{1} ", item.ObfuscatedName, item.Constraints[0]); //TODO: Add index param, if more constraints is availible
}
return builder.ToString();
}
public static string GetGenericNames(this GenericParameterMapping[] mapping)
{
if (mapping.Length == 0)
throw new Exception("Can't happen");
StringBuilder builder = new StringBuilder();
foreach (var item in mapping)
{
builder.AppendFormat("{0}, ", item.ObfuscatedName);
}
string raw = builder.ToString();
return raw.Substring(0, raw.Length - 2);
}
public static string GetModifier(this string str)
{
if (str.Contains("&"))
{
return "out";
}
return "";
}
public static string RemoveModifiers(this string str)
{
return str.Replace("&", "");
}
public static string Names(this ParameterMapping[] parameters)
{
if (parameters.Length == 0)
return "";
string outString = "";
foreach (ParameterMapping param in parameters)
{
outString += string.Format("{0} {1}, ", param.Type.GetModifier(), param.ObfuscatedName);
}
return outString.Substring(0, outString.Length - 2);
}
public static string TypedNames(this ParameterMapping[] parameters)
{
if (parameters.Length == 0)
return "";
string outString = "";
foreach (ParameterMapping param in parameters)
{
outString += string.Format("{0} {2}{1}, ", param.Type.GetModifier(), param.ObfuscatedName, param.Type.GetModifier() != "" ? "" : "(" + param.Type + ")");
}
return outString.Substring(0, outString.Length - 2);
}
public static string Typeofs(this ParameterMapping[] parameters)
{
if (parameters.Length == 0)
return "";
string outString = "";
foreach (ParameterMapping param in parameters)
{
outString += string.Format("typeof({0}), ", param.Type.RemoveModifiers());
}
return outString.Substring(0, outString.Length - 2);
}
public static bool IsArray(this string obfuscatedType)
{
return obfuscatedType.Contains("[]") && !obfuscatedType.Contains("float");
}
public static bool IsList(this string obfuscatedType)
{
return obfuscatedType.Contains("List<");
}
public static bool IsEnumerable(this string obfuscatedType)
{
return obfuscatedType.Contains("IEnumerable<");
}
public static bool IsCollection(this string obfuscatedType)
{
return obfuscatedType.Contains("ICollection<");
}
public static string TryGetWrappedType(this string obfuscatedType, Dictionary<string, string> dict)
{
if (dict.ContainsKey(obfuscatedType))
{
return dict[obfuscatedType];
}
if (obfuscatedType.Contains("[]"))
{
string obfListType = obfuscatedType.Substring(0, obfuscatedType.IndexOf("[]"));
if (dict.ContainsKey(obfListType))
{
return dict[obfListType] + "[]";
}
}
if (obfuscatedType.Contains("<") && obfuscatedType.Contains(">"))
{
int ios = obfuscatedType.IndexOf("<") + 1;
int ioe = obfuscatedType.IndexOf(">");
string obfListType = obfuscatedType.Substring(ios, ioe - ios);
string obfContainer = obfuscatedType.Substring(0, ios - 1);
string returnType = (dict.ContainsKey(obfContainer) ? dict[obfContainer] : obfContainer) + "<";
returnType += (dict.ContainsKey(obfListType) ? dict[obfListType] : obfListType) +">";
return returnType;
}
return obfuscatedType;
}
public static string GetGenericClass(this TypeMapping mapping)
{
if (mapping.BaseType.Contains("ValueType"))
return "struct";
else
return "class";
}
public static string GetGenericWhere(this TypeMapping mapping)
{
if (mapping.GenericParameters.Length == 0)
return "";
StringBuilder builder = new StringBuilder();
foreach (var item in mapping.GenericParameters)
{
builder.AppendFormat("where {0}:{1} ", item.ObfuscatedName, item.Constraints[0]);
}
return builder.ToString().Substring(0, builder.Length - 1);
}
public static string GetGenericNames(this TypeMapping mapping)
{
if (mapping.GenericParameters.Length == 0)
return "";
StringBuilder builder = new StringBuilder("<");
foreach (var item in mapping.GenericParameters)
{
builder.AppendFormat("{0}, ", item.ObfuscatedName);
}
return builder.ToString().Substring(0, builder.Length - 2) + ">";
}
public static string GetGenericBaseTypes(this TypeMapping mapping, Dictionary<string, string> dict)
{
if (mapping.BaseType == null || mapping.BaseType.Contains("System."))
return "";
string baseType = mapping.BaseType;
if (dict.ContainsKey(baseType))
{
baseType = dict[baseType];
} else
{
return "";
}
StringBuilder builder = new StringBuilder(" : ");
builder.AppendFormat("{0}", baseType);
return builder.ToString();
}
public static string GetBaseHolder(this TypeMapping mapping, Dictionary<string, string> dict)
{
if (mapping.BaseType == null || mapping.BaseType.Contains("System."))
return "";
if (dict.ContainsKey(mapping.BaseType))
{
return " : base(instance)";
}
else
{
return "";
}
}
public static string GetGenericTypes(this TypeMapping mapping)
{
if (mapping.GenericParameters.Length == 0)
return "";
StringBuilder builder = new StringBuilder("<");
foreach (var item in mapping.GenericParameters)
{
builder.AppendFormat("{0}, ", item.Constraints[0]);
}
return builder.ToString().Substring(0, builder.Length - 2) + ">";
}
public static string TryGetListConvertType(this string obfuscatedType, Dictionary<string, string> dict)
{
if (dict.ContainsKey(obfuscatedType))
{
return dict[obfuscatedType];
}
if (obfuscatedType.Contains("[]"))
{
string obfListType = obfuscatedType.Substring(0, obfuscatedType.IndexOf("[]"));
if (dict.ContainsKey(obfListType))
{
return dict[obfListType];
}
}
if (obfuscatedType.Contains("<") && obfuscatedType.Contains(">"))
{
int ios = obfuscatedType.IndexOf("<") + 1;
int ioe = obfuscatedType.IndexOf(">");
string obfSubstring = obfuscatedType.Substring(ios, ioe - ios);
if (dict.ContainsKey(obfSubstring))
{
return dict[obfSubstring];
}
}
return obfuscatedType;
}
public static string[] Lines(this string source)
{
return source.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
}
public static string Beautify(string source)
{
//TODO: Fix the bug-out when line contains {} and it's not suited for indenting (example ClusterExitDescriptor, ItemDescriptor)
int indent = 0;
string[] lines = source.Lines();
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
int bracketEndCount = line.Count(x => x == '}');
int bracketStartCount = line.Count(x => x == '{');
if (bracketEndCount != bracketStartCount)
indent -= bracketEndCount;
line = line.Trim();
line = Regex.Replace(line, @"\s+", " ");
line = Indent(indent) + line;
lines[i] = line;
if (bracketEndCount != bracketStartCount)
indent += bracketStartCount;
}
StringBuilder builder = new StringBuilder();
foreach (var line in lines)
{
builder.AppendLine(line);
}
return builder.ToString();
}
public static string Indent(int count)
{
return "".PadLeft(count * 4); //4 spaces per indent level
}
}
}