-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathVRModuleManagerEditor.cs
More file actions
677 lines (580 loc) · 23.6 KB
/
VRModuleManagerEditor.cs
File metadata and controls
677 lines (580 loc) · 23.6 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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//========= Copyright 2016-2024, HTC Corporation. All rights reserved. ===========
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using HTC.UnityPlugin.Vive;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditorInternal.VR;
using UnityEngine;
using Assembly = System.Reflection.Assembly;
#if UNITY_2018_1_OR_NEWER
using UnityEditor.PackageManager;
using PackageInfo = UnityEditor.PackageManager.PackageInfo;
using UnityEditor.Build;
#endif
#if UNITY_2017_3_OR_NEWER
using UnityEditor.Compilation;
#endif
namespace HTC.UnityPlugin.VRModuleManagement
{
// This script manage define symbols used by VIU
public class VRModuleManagerEditor : AssetPostprocessor
#if UNITY_2017_1_OR_NEWER
, UnityEditor.Build.IActiveBuildTargetChanged
#endif
{
public class SymbolRequirement
{
public class ReqFieldInfo
{
public string typeName = string.Empty;
public string name = string.Empty;
public BindingFlags bindingAttr = BindingFlags.Default;
}
public class ReqMethodInfo
{
public string typeName = string.Empty;
public string name = string.Empty;
public BindingFlags bindingAttr;
public string[] argTypeNames = null;
public ParameterModifier[] argModifiers = null;
}
public string symbol = string.Empty;
public string[] symbols = null;
public string[] reqTypeNames = null;
public string[] reqAnyTypeNames = null;
public string[] reqFileNames = null;
public string[] reqAnyFileNames = null;
public ReqFieldInfo[] reqFields = null;
public ReqFieldInfo[] reqAnyFields = null;
public ReqMethodInfo[] reqMethods = null;
public ReqMethodInfo[] reqAnyMethods = null;
public Func<SymbolRequirement, bool> validateFunc = null;
public static Dictionary<string, Type> s_foundTypes;
public static void ResetFoundTypes()
{
if (s_foundTypes != null)
{
s_foundTypes.Clear();
}
}
public void FindRequiredTypesInAssembly(Assembly assembly)
{
if (reqTypeNames != null)
{
foreach (var name in reqTypeNames)
{
TryAddTypeFromAssembly(name, assembly);
}
}
if (reqAnyTypeNames != null)
{
foreach (var name in reqAnyTypeNames)
{
TryAddTypeFromAssembly(name, assembly);
}
}
if (reqFields != null)
{
foreach (var field in reqFields)
{
TryAddTypeFromAssembly(field.typeName, assembly);
}
}
if (reqAnyFields != null)
{
foreach (var field in reqAnyFields)
{
TryAddTypeFromAssembly(field.typeName, assembly);
}
}
if (reqMethods != null)
{
foreach (var method in reqMethods)
{
TryAddTypeFromAssembly(method.typeName, assembly);
if (method.argTypeNames != null)
{
foreach (var typeName in method.argTypeNames)
{
TryAddTypeFromAssembly(typeName, assembly);
}
}
}
}
if (reqAnyMethods != null)
{
foreach (var method in reqAnyMethods)
{
TryAddTypeFromAssembly(method.typeName, assembly);
if (method.argTypeNames != null)
{
foreach (var typeName in method.argTypeNames)
{
TryAddTypeFromAssembly(typeName, assembly);
}
}
}
}
}
private bool TryAddTypeFromAssembly(string name, Assembly assembly)
{
if (string.IsNullOrEmpty(name) || RequiredTypeFound(name)) { return false; }
var type = assembly.GetType(name);
if (type == null) { return false; }
if (s_foundTypes == null) { s_foundTypes = new Dictionary<string, Type>(); }
s_foundTypes.Add(name, type);
return true;
}
private bool RequiredTypeFound(string name)
{
return s_foundTypes == null ? false : s_foundTypes.ContainsKey(name);
}
public bool Validate()
{
if (s_foundTypes == null) { return false; }
if (reqTypeNames != null)
{
foreach (var name in reqTypeNames)
{
if (!s_foundTypes.ContainsKey(name)) { return false; }
}
}
if (reqAnyTypeNames != null)
{
var found = false;
foreach (var name in reqAnyTypeNames)
{
if (s_foundTypes.ContainsKey(name))
{
found = true;
break;
}
}
if (!found) { return false; }
}
if (reqFileNames != null)
{
foreach (var requiredFile in reqFileNames)
{
if (!DoesFileExist(requiredFile))
{
return false;
}
}
}
if (reqAnyFileNames != null)
{
var found = false;
foreach (var requiredFile in reqAnyFileNames)
{
var files = Directory.GetFiles(Application.dataPath, requiredFile, SearchOption.AllDirectories);
if (files != null && files.Length > 0)
{
found = true;
break;
}
}
if (!found) { return false; }
}
if (reqFields != null)
{
foreach (var field in reqFields)
{
Type type;
if (!s_foundTypes.TryGetValue(field.typeName, out type)) { return false; }
if (type.GetField(field.name, field.bindingAttr) == null) { return false; }
}
}
if (reqAnyFields != null)
{
var found = false;
foreach (var field in reqAnyFields)
{
Type type;
if (!s_foundTypes.TryGetValue(field.typeName, out type)) { continue; }
if (type.GetField(field.name, field.bindingAttr) == null) { continue; }
found = true;
break;
}
if (!found) { return false; }
}
if (reqMethods != null)
{
foreach (var method in reqMethods)
{
Type type;
if (!s_foundTypes.TryGetValue(method.typeName, out type)) { return false; }
var argTypes = new Type[method.argTypeNames == null ? 0 : method.argTypeNames.Length];
for (int i = argTypes.Length - 1; i >= 0; --i)
{
if (!s_foundTypes.TryGetValue(method.argTypeNames[i], out argTypes[i])) { return false; }
}
if (type.GetMethod(method.name, method.bindingAttr, null, CallingConventions.Any, argTypes, method.argModifiers ?? new ParameterModifier[0]) == null) { return false; }
}
}
if (reqAnyMethods != null)
{
var found = false;
foreach (var method in reqAnyMethods)
{
Type type;
if (!s_foundTypes.TryGetValue(method.typeName, out type)) { continue; }
if (method.argTypeNames == null)
{
continue;
}
bool isAllArgTypesFound = true;
var argTypes = new Type[method.argTypeNames.Length];
for (int i = argTypes.Length - 1; i >= 0; --i)
{
if (!s_foundTypes.TryGetValue(method.argTypeNames[i], out argTypes[i]))
{
isAllArgTypesFound = false;
break;
}
}
if (!isAllArgTypesFound)
{
continue;
}
if (type.GetMethod(method.name, method.bindingAttr, null, CallingConventions.Any, argTypes, method.argModifiers ?? new ParameterModifier[0]) == null) { continue; }
found = true;
break;
}
if (!found) { return false; }
}
if (validateFunc != null)
{
if (!validateFunc(this)) { return false; }
}
return true;
}
}
public abstract class SymbolRequirementCollection : List<SymbolRequirement> { }
private static List<SymbolRequirement> s_symbolReqList;
private static HashSet<string> s_referencedAssemblyNameSet;
static VRModuleManagerEditor()
{
s_symbolReqList = new List<SymbolRequirement>();
foreach (var type in Assembly.GetAssembly(typeof(SymbolRequirementCollection)).GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(SymbolRequirementCollection))))
{
s_symbolReqList.AddRange((SymbolRequirementCollection)Activator.CreateInstance(type));
}
s_symbolReqList.Add(new SymbolRequirement()
{
symbol = "VIU_PLUGIN",
validateFunc = (req) => true,
});
s_symbolReqList.Add(new SymbolRequirement()
{
symbol = "VIU_PACKAGE",
validateFunc = (req) => VIUSettingsEditor.PackageManagerHelper.IsPackageInList(VIUSettingsEditor.VIUPackageName),
});
s_symbolReqList.Add(new SymbolRequirement()
{
symbol = "VIU_SIUMULATOR_SUPPORT",
validateFunc = (req) => Vive.VIUSettingsEditor.supportSimulator,
});
// Obsolete symbol, will be removed in all condition
s_symbolReqList.Add(new SymbolRequirement()
{
symbol = "VIU_EXTERNAL_CAMERA_SWITCH",
validateFunc = (req) => false,
});
// Obsolete symbol, will be removed in all condition
s_symbolReqList.Add(new SymbolRequirement()
{
symbol = "VIU_BINDING_INTERFACE_SWITCH",
validateFunc = (req) => false,
});
#if !UNITY_2017_1_OR_NEWER
EditorUserBuildSettings.activeBuildTargetChanged += UpdateScriptingDefineSymbols;
#endif
}
#if UNITY_2017_1_OR_NEWER
public int callbackOrder { get { return 0; } }
public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
{
UpdateScriptingDefineSymbols();
}
#endif
//[DidReloadScripts]
[MenuItem("Tool/VIU/UpdateScriptingDefineSymbols")]
public static void UpdateScriptingDefineSymbols()
{
if (!s_isUpdatingScriptingDefineSymbols)
{
s_isUpdatingScriptingDefineSymbols = true;
EditorApplication.update += DoUpdateScriptingDefineSymbols;
}
}
// From UnityEditor.AssetPostprocessor
public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string assetPath in deletedAssets)
{
string deletedFileName = Path.GetFileName(assetPath);
bool isFound = s_symbolReqList.Exists((req) =>
{
if (req == null || req.reqFileNames == null)
{
return false;
}
foreach (string fileName in req.reqFileNames)
{
if (fileName == deletedFileName)
{
return true;
}
}
return false;
});
if (isFound)
{
if (!s_delayCallRemoveRegistered)
{
s_delayCallRemoveRegistered = true;
EditorApplication.delayCall += RemoveAllVIUSymbols;
}
break;
}
}
}
private static bool s_isUpdatingScriptingDefineSymbols = false;
private static void DoUpdateScriptingDefineSymbols()
{
if (EditorApplication.isPlaying) { EditorApplication.update -= DoUpdateScriptingDefineSymbols; return; }
// some symbolRequirement depends on installed packages (only works when UNITY_2018_1_OR_NEWER)
Vive.VIUSettingsEditor.PackageManagerHelper.PreparePackageList();
if (Vive.VIUSettingsEditor.PackageManagerHelper.isPreparingList) { return; }
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (!IsReferenced(assembly))
{
continue;
}
try
{
foreach (var symbolReq in s_symbolReqList)
{
symbolReq.FindRequiredTypesInAssembly(assembly);
}
}
catch (ReflectionTypeLoadException e)
{
Debug.LogWarning(e);
Debug.LogWarning("load assembly " + assembly.FullName + " fail");
}
catch (Exception e)
{
Debug.LogError(e);
}
}
var defineSymbols = GetDefineSymbols();
var defineSymbolsChanged = false;
var validSymbols = new HashSet<string>();
var invalidSymbols = new HashSet<string>();
foreach (var symbolReq in s_symbolReqList)
{
if (symbolReq.Validate())
{
if (!string.IsNullOrEmpty(symbolReq.symbol))
{
invalidSymbols.Remove(symbolReq.symbol);
validSymbols.Add(symbolReq.symbol);
}
if (symbolReq.symbols != null)
{
foreach (var symbol in symbolReq.symbols)
{
if (!string.IsNullOrEmpty(symbol))
{
invalidSymbols.Remove(symbol);
validSymbols.Add(symbol);
}
}
}
}
else
{
if (!string.IsNullOrEmpty(symbolReq.symbol) && !validSymbols.Contains(symbolReq.symbol))
{
invalidSymbols.Add(symbolReq.symbol);
}
if (symbolReq.symbols != null)
{
foreach (var symbol in symbolReq.symbols)
{
if (!string.IsNullOrEmpty(symbol) && !validSymbols.Contains(symbol))
{
invalidSymbols.Add(symbol);
}
}
}
}
}
foreach (var symbol in invalidSymbols)
{
if (defineSymbols.RemoveAll((s) => s == symbol) > 0)
{
defineSymbolsChanged = true;
}
}
foreach (var symbol in validSymbols)
{
if (!defineSymbols.Contains(symbol))
{
defineSymbols.Add(symbol);
defineSymbolsChanged = true;
}
}
if (defineSymbolsChanged)
{
SetDefineSymbols(defineSymbols);
}
SymbolRequirement.ResetFoundTypes();
s_isUpdatingScriptingDefineSymbols = false;
EditorApplication.update -= DoUpdateScriptingDefineSymbols;
}
private static bool s_delayCallRemoveRegistered;
private static void RemoveAllVIUSymbols()
{
s_delayCallRemoveRegistered = false;
EditorApplication.delayCall -= RemoveAllVIUSymbols;
var defineSymbols = GetDefineSymbols();
foreach (var symbolReq in s_symbolReqList)
{
defineSymbols.Remove(symbolReq.symbol);
}
SetDefineSymbols(defineSymbols);
}
private static List<string> GetDefineSymbols()
{
#if UNITY_6000_0_OR_NEWER
return new List<string>(PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget))).Split(';'));
#else
return new List<string>(PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget)).Split(';'));
#endif
}
private static void SetDefineSymbols(List<string> symbols)
{
#if UNITY_6000_0_OR_NEWER
PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget)), string.Join(";", symbols.ToArray()));
#else
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget), string.Join(";", symbols.ToArray()));
#endif
}
private static bool IsReferenced(Assembly assembly)
{
return GetReferencedAssemblyNameSet().Contains(assembly.GetName().Name);
}
private static HashSet<string> GetReferencedAssemblyNameSet()
{
if (s_referencedAssemblyNameSet != null)
{
return s_referencedAssemblyNameSet;
}
s_referencedAssemblyNameSet = new HashSet<string>();
Assembly playerAssembly = typeof(VRModule).Assembly;
Assembly editorAssembly = typeof(VRModuleManagerEditor).Assembly;
// C# player referenced assemblies
foreach (AssemblyName asmName in playerAssembly.GetReferencedAssemblies())
{
s_referencedAssemblyNameSet.Add(asmName.Name);
}
// C# editor referenced assemblies
foreach (AssemblyName asmName in editorAssembly.GetReferencedAssemblies())
{
s_referencedAssemblyNameSet.Add(asmName.Name);
}
#if UNITY_2018_1_OR_NEWER
// Unity player referenced assemblies
UnityEditor.Compilation.Assembly playerUnityAsm = FindUnityAssembly(playerAssembly.GetName().Name, AssembliesType.Player);
if (playerUnityAsm != null)
{
foreach (UnityEditor.Compilation.Assembly asm in playerUnityAsm.assemblyReferences)
{
s_referencedAssemblyNameSet.Add(asm.name);
}
}
else
{
Debug.LogWarning("Player assembly not found.");
}
// Unity editor referenced assemblies
UnityEditor.Compilation.Assembly editorUnityAsm = FindUnityAssembly(editorAssembly.GetName().Name, AssembliesType.Editor);
if (editorUnityAsm != null)
{
foreach (UnityEditor.Compilation.Assembly asm in editorUnityAsm.assemblyReferences)
{
s_referencedAssemblyNameSet.Add(asm.name);
}
}
else
{
Debug.LogWarning("Editor assembly not found.");
}
#elif UNITY_2017_3_OR_NEWER
UnityEditor.Compilation.Assembly[] assemblies = CompilationPipeline.GetAssemblies();
foreach (UnityEditor.Compilation.Assembly asm in assemblies)
{
s_referencedAssemblyNameSet.Add(asm.name);
}
#endif
return s_referencedAssemblyNameSet;
}
#if UNITY_2018_1_OR_NEWER
private static UnityEditor.Compilation.Assembly FindUnityAssembly(string name, AssembliesType type)
{
UnityEditor.Compilation.Assembly foundAssembly = null;
UnityEditor.Compilation.Assembly[] assemblies = CompilationPipeline.GetAssemblies(type);
foreach (UnityEditor.Compilation.Assembly asm in assemblies)
{
if (asm.name == name)
{
foundAssembly = asm;
break;
}
}
return foundAssembly;
}
#endif
private static bool DoesFileExist(string fileName)
{
string[] fileNamesInAsset = Directory.GetFiles(Application.dataPath, fileName, SearchOption.AllDirectories);
if (fileNamesInAsset != null && fileNamesInAsset.Length > 0)
{
return true;
}
#if UNITY_2018_1_OR_NEWER
PackageCollection packages = VIUSettingsEditor.PackageManagerHelper.GetPackageList();
foreach (UnityEditor.PackageManager.PackageInfo package in packages)
{
if (package == null)
{
continue;
}
if (package.source == PackageSource.BuiltIn)
{
continue;
}
var resolvedPath = package.resolvedPath.Trim();
if (string.IsNullOrEmpty(resolvedPath))
{
continue;
}
string[] fileNamesInPackage = Directory.GetFiles(resolvedPath, fileName, SearchOption.AllDirectories);
if (fileNamesInPackage != null && fileNamesInPackage.Length > 0)
{
return true;
}
}
#endif
return false;
}
}
}