forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cs
More file actions
532 lines (499 loc) · 17 KB
/
Utility.cs
File metadata and controls
532 lines (499 loc) · 17 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using UnityEditor;
using UnityEditor.Scripting.Compilers;
using UnityEditor.Utils;
using UnityEditorInternal;
internal static class Utility
{
[CompilerGenerated]
private static Dictionary<string, int> <>f__switch$map1;
private static string frameworkPath;
private static int kProcessTerminateFlag = 1;
private static int kQueryInformationLimitedFlag = 0x1000;
private static readonly Regex packageNameRegex = new Regex(@"^[A-Za-z0-9\.\-]{2,49}[A-Za-z0-9\-]$", RegexOptions.CultureInvariant | RegexOptions.Compiled);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr objectHandle);
public static string CombinePath(params string[] paths) =>
Paths.Combine(paths).ConvertToWindowsPath();
public static string CombinePath(string path1, string path2) =>
Path.Combine(path1, path2).ConvertToWindowsPath();
public static string ConvertToWindowsPath(this string path) =>
path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
public static void CopyDirectoryContents(string sourceDirectory, string destinationDirectory, bool recursive = false)
{
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
foreach (string str in Directory.GetFiles(sourceDirectory))
{
File.Copy(str, Path.Combine(destinationDirectory, Path.GetFileName(str)), true);
}
if (recursive)
{
foreach (string str2 in Directory.GetDirectories(sourceDirectory))
{
CopyDirectoryContents(str2, Path.Combine(destinationDirectory, Path.GetFileName(str2)), true);
}
}
}
public static void CreateDirectory(string path)
{
Directory.CreateDirectory(path);
while (!Directory.Exists(path))
{
Thread.Sleep(0);
}
}
public static void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
while (Directory.Exists(path))
{
Thread.Sleep(0);
}
}
}
[DllImport("Psapi.dll")]
private static extern bool EnumProcesses([In, Out, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.U4)] uint[] processIds, uint arraySizeBytes, [MarshalAs(UnmanagedType.U4)] out uint bytesCopied);
internal static Version GetDesiredUWPSDK() =>
UWPReferences.GetDesiredSDKVersion();
public static string GetDesiredUWPSDKString()
{
Version desiredUWPSDK = GetDesiredUWPSDK();
string str = desiredUWPSDK.ToString();
if (desiredUWPSDK.Build == -1)
{
str = str + ".0";
}
if (desiredUWPSDK.Revision == -1)
{
str = str + ".0";
}
return str;
}
public static string GetFrameworkPath()
{
if (frameworkPath == null)
{
string[] strArray = new string[] { @"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft\VisualStudio\SxS\VC7", @"HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Microsoft\VisualStudio\SxS\VC7", @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft\VisualStudio\SxS\VC7", @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft\VisualStudio\SxS\VC7" };
foreach (string str in strArray)
{
string str2 = RegistryUtil.GetRegistryStringValue32(str, "FrameworkDir32", null);
if (!string.IsNullOrEmpty(str2))
{
string str3 = RegistryUtil.GetRegistryStringValue32(str, "FrameworkVer32", null);
if (!string.IsNullOrEmpty(str3))
{
frameworkPath = Path.Combine(str2, str3);
break;
}
}
}
if (frameworkPath == null)
{
frameworkPath = Path.Combine(Environment.GetEnvironmentVariable("WINDIR"), @"Microsoft.NET\Framework\v4.0.30319");
}
}
return frameworkPath;
}
public static string GetMSBuildExePath(WSASDK wsaSDK)
{
string str = null;
switch (wsaSDK)
{
case WSASDK.SDK80:
str = Path.Combine(GetMSBuildPath(wsaSDK), "MSBuild.exe");
break;
case WSASDK.SDK81:
case WSASDK.PhoneSDK81:
case WSASDK.UniversalSDK81:
case WSASDK.UWP:
str = Path.Combine(GetMSBuildPath(wsaSDK), @"Bin\MSBuild.exe");
break;
default:
throw new Exception("Unsupported Windows Store Apps SDK: " + wsaSDK);
}
if (string.IsNullOrEmpty(str) || !File.Exists(str))
{
throw new Exception($"Failed to locate suitable MSBuild '{str}'");
}
return str;
}
public static string GetMSBuildPath(string version)
{
string environmentVariable = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
if (string.IsNullOrEmpty(environmentVariable))
{
environmentVariable = Environment.GetEnvironmentVariable("ProgramFiles");
if (string.IsNullOrEmpty(environmentVariable))
{
environmentVariable = @"C:\Program Files (x86)";
}
}
return Path.Combine(environmentVariable, $"MSBuild\{version}");
}
public static string GetMSBuildPath(WSASDK wsaSDK)
{
switch (wsaSDK)
{
case WSASDK.SDK80:
return GetFrameworkPath();
case WSASDK.SDK81:
case WSASDK.PhoneSDK81:
case WSASDK.UniversalSDK81:
case WSASDK.UWP:
return GetMSBuildPath(GetVSVersion());
}
throw new Exception("Unsupported Windows Store Apps SDK: " + wsaSDK);
}
public static string GetPackageName(bool wsa)
{
string str;
if (wsa)
{
str = TryValidatePackageName(PlayerSettings.WSA.packageName);
if (str != null)
{
return str;
}
}
str = TryValidatePackageName(PlayerSettings.productName);
if (str != null)
{
return str;
}
return "DefaultPackageName";
}
public static string GetVsName()
{
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
string str = PlayerSettings.productName.Normalize(NormalizationForm.FormD);
StringBuilder builder = new StringBuilder();
foreach (char ch in str)
{
if (!invalidFileNameChars.Contains<char>(ch) && (CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark))
{
builder.Append(ch);
}
}
return builder.ToString().Normalize(NormalizationForm.FormC);
}
public static string GetVsNamespace()
{
string str2;
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
StringBuilder builder = new StringBuilder();
string vsName = GetVsName();
for (int i = 0; i < vsName.Length; i++)
{
char ch = vsName[i];
if (invalidFileNameChars.Contains<char>(ch))
{
continue;
}
switch (ch)
{
case '\'':
case '(':
case ')':
case '-':
case ' ':
break;
case '.':
{
if (!UseIl2CppScriptingBackend() && ((i + 1) != vsName.Length))
{
goto Label_00A3;
}
builder.Append('_');
continue;
}
default:
goto Label_00D8;
}
builder.Append('_');
continue;
Label_00A3:
if (char.IsNumber(vsName[i + 1]))
{
builder.Append("._");
}
else
{
builder.Append(".");
}
continue;
Label_00D8:
builder.Append(ch);
}
if ((builder.Length > 0) && char.IsNumber(builder[0]))
{
str2 = "_" + builder;
}
else
{
str2 = builder.ToString();
}
if ((str2.Length == 20) && UseIl2CppScriptingBackend())
{
str2 = str2 + "_";
}
return str2;
}
public static string GetVSVersion()
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("VS140COMNTOOLS")))
{
return "14.0";
}
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("VS120COMNTOOLS")))
{
return "12.0";
}
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("VS110COMNTOOLS")))
{
throw new Exception("Failed to locate env variables VS140COMNTOOLS, VS120COMNTOOLS or VS110COMNTOOLS.");
}
return "11.0";
}
public static bool HasExtension(string path, string extension) =>
string.Equals(Path.GetExtension(path), extension, StringComparison.InvariantCultureIgnoreCase);
private static bool IsValidPackageName(string value)
{
if (!packageNameRegex.IsMatch(value))
{
return false;
}
string key = value.ToUpperInvariant();
if (key != null)
{
int num;
if (<>f__switch$map1 == null)
{
Dictionary<string, int> dictionary = new Dictionary<string, int>(0x16) {
{
"CON",
0
},
{
"PRN",
0
},
{
"AUX",
0
},
{
"NUL",
0
},
{
"COM1",
0
},
{
"COM2",
0
},
{
"COM3",
0
},
{
"COM4",
0
},
{
"COM5",
0
},
{
"COM6",
0
},
{
"COM7",
0
},
{
"COM8",
0
},
{
"COM9",
0
},
{
"LPT1",
0
},
{
"LPT2",
0
},
{
"LPT3",
0
},
{
"LPT4",
0
},
{
"LPT5",
0
},
{
"LPT6",
0
},
{
"LPT7",
0
},
{
"LPT8",
0
},
{
"LPT9",
0
}
};
<>f__switch$map1 = dictionary;
}
if (<>f__switch$map1.TryGetValue(key, out num) && (num == 0))
{
return false;
}
}
return true;
}
public static void KillProcesses(Func<string, bool> shouldKill)
{
uint bytesCopied = 0x1000;
uint[] processIds = new uint[bytesCopied];
if (EnumProcesses(processIds, 4 * bytesCopied, out bytesCopied))
{
for (int i = 0; i < bytesCopied; i++)
{
IntPtr hProcess = OpenProcess(kQueryInformationLimitedFlag | kProcessTerminateFlag, false, (int) processIds[i]);
if (hProcess != IntPtr.Zero)
{
int capacity = 0x400;
StringBuilder lpExeName = new StringBuilder(capacity);
QueryFullProcessImageName(hProcess, 0, lpExeName, ref capacity);
if (shouldKill(lpExeName.ToString()))
{
TerminateProcess(hProcess, 0);
}
CloseHandle(hProcess);
}
}
}
}
public static void MoveDirectory(string source, string destination, Func<string, bool> shouldOverwriteFile = null)
{
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination);
}
foreach (string str in Directory.GetFiles(source))
{
string path = Path.Combine(destination, Path.GetFileName(str));
if (File.Exists(path))
{
if ((shouldOverwriteFile != null) && !shouldOverwriteFile(path))
{
continue;
}
File.Delete(path);
}
File.Move(str, path);
}
foreach (string str3 in Directory.GetDirectories(source))
{
MoveDirectory(str3, Path.Combine(destination, Path.GetFileName(str3)), shouldOverwriteFile);
}
Directory.Delete(source, true);
}
[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool QueryFullProcessImageName([In] IntPtr hProcess, [In] int dwFlags, [Out] StringBuilder lpExeName, ref int lpdwSize);
public static int RunAndWait(string fileName, string arguments, out string result, IDictionary<string, string> environmentVariables = null)
{
ProcessStartInfo si = new ProcessStartInfo(fileName, arguments) {
CreateNoWindow = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
if (environmentVariables != null)
{
foreach (KeyValuePair<string, string> pair in environmentVariables)
{
si.EnvironmentVariables.Add(pair.Key, pair.Value);
}
}
using (Program program = new Program(si))
{
program.Start();
program.WaitForExit();
result = program.GetStandardOutputAsString();
return program.ExitCode;
}
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);
public static string TryValidatePackageName(string value)
{
if ((value == null) || (value.Length < 3))
{
return null;
}
StringBuilder builder = new StringBuilder(value.Length);
for (int i = 0; i < value.Length; i++)
{
char c = value[i];
if (char.IsLetterOrDigit(c) || (c == '-'))
{
builder.Append(c);
}
else if ((c == '.') && (i != (value.Length - 1)))
{
builder.Append(c);
}
}
value = builder.ToString();
if (!IsValidPackageName(value))
{
return null;
}
return value;
}
public static bool UseIl2CppScriptingBackend() =>
(PlayerSettings.GetScriptingBackend(BuildTargetGroup.WSA) == ScriptingImplementation.IL2CPP);
public static string AssemblyCSharpFirstPassName =>
("Assembly-CSharp-firstpass" + EditorSettings.Internal_UserGeneratedProjectSuffix);
public static string AssemblyCSharpName =>
("Assembly-CSharp" + EditorSettings.Internal_UserGeneratedProjectSuffix);
public static string AssemblyUnityScriptFirstPassName =>
("Assembly-UnityScript-firstpass" + EditorSettings.Internal_UserGeneratedProjectSuffix);
public static string AssemblyUnityScriptName =>
("Assembly-UnityScript" + EditorSettings.Internal_UserGeneratedProjectSuffix);
}