77using System . Linq ;
88using System . Reflection ;
99using System . Collections . Generic ;
10- using System . Diagnostics ;
1110using Mono . Cecil ;
12- using UnityEditor . Build ;
1311using UnityEditor . Modules ;
1412using UnityEditorInternal ;
1513using UnityEngine ;
1614using System . Runtime . InteropServices ;
17- using UnityEditor . VisualStudioIntegration ;
1815using UnityEngine . Scripting ;
1916using Debug = UnityEngine . Debug ;
2017using Unity . Profiling ;
2421
2522namespace UnityEditor
2623{
27- internal partial class AssemblyHelper
24+ internal class AssemblyHelper
2825 {
2926 static Dictionary < string , bool > managedToDllType = new Dictionary < string , bool > ( ) ;
3027 static BuildPlayerDataExtractor m_BuildPlayerDataExtractor = new BuildPlayerDataExtractor ( ) ;
3128
32- static public string [ ] GetNamesOfAssembliesLoadedInCurrentDomain ( )
33- {
34- var assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
35- var locations = new List < string > ( ) ;
36- foreach ( var a in assemblies )
37- {
38- try
39- {
40- locations . Add ( a . Location ) ;
41- }
42- catch ( NotSupportedException )
43- {
44- //we have some "dynamic" assmeblies that do not have a filename
45- }
46- }
47- return locations . ToArray ( ) ;
48- }
49-
50- static public string ExtractInternalAssemblyName ( string path )
51- {
52- try
53- {
54- AssemblyDefinition definition = AssemblyDefinition . ReadAssembly ( path ) ;
55- return definition . Name . Name ;
56- }
57- catch
58- {
59- return "" ;
60- }
61- }
62-
63- static AssemblyDefinition GetAssemblyDefinitionCached ( string path , Dictionary < string , AssemblyDefinition > cache )
64- {
65- if ( cache . ContainsKey ( path ) )
66- return cache [ path ] ;
67-
68- AssemblyDefinition definition = AssemblyDefinition . ReadAssembly ( path ) ;
69- cache [ path ] = definition ;
70- return definition ;
71- }
72-
73- static private bool CouldBelongToDotNetOrWindowsRuntime ( string assemblyPath )
74- {
75- return assemblyPath . IndexOf ( "mscorlib.dll" ) != - 1 ||
76- assemblyPath . IndexOf ( "System." ) != - 1 ||
77- assemblyPath . IndexOf ( "Microsoft." ) != - 1 ||
78- assemblyPath . IndexOf ( "Windows." ) != - 1 ||
79- assemblyPath . IndexOf ( "WinRTLegacy.dll" ) != - 1 ||
80- assemblyPath . IndexOf ( "platform.dll" ) != - 1 ;
81- }
82-
83- static private bool IgnoreAssembly ( string assemblyPath , BuildTarget target , ScriptingImplementation scriptingImplementation )
84- {
85- #pragma warning disable 618
86- if ( target == BuildTarget . WSAPlayer || scriptingImplementation == ScriptingImplementation . CoreCLR )
87- {
88- if ( CouldBelongToDotNetOrWindowsRuntime ( assemblyPath ) )
89- return true ;
90- }
91- else if ( target == BuildTarget . XboxOne )
92- {
93- var profile = PlayerSettings . GetApiCompatibilityLevel ( NamedBuildTarget . XboxOne ) ;
94- if ( profile == ApiCompatibilityLevel . NET_4_6 || profile == ApiCompatibilityLevel . NET_Standard_2_0 )
95- {
96- if ( CouldBelongToDotNetOrWindowsRuntime ( assemblyPath ) )
97- return true ;
98- }
99- }
100-
101- return IsInternalAssembly ( assemblyPath ) ;
102- }
103-
104- static private void AddReferencedAssembliesRecurse ( string assemblyPath , List < string > alreadyFoundAssemblies , string [ ] allAssemblyPaths , string [ ] foldersToSearch , Dictionary < string , AssemblyDefinition > cache , BuildTarget target , ScriptingImplementation scriptingImplementation )
105- {
106- if ( IgnoreAssembly ( assemblyPath , target , scriptingImplementation ) )
107- return ;
108-
109- if ( ! File . Exists ( assemblyPath ) )
110- return ;
111-
112- AssemblyDefinition assembly = GetAssemblyDefinitionCached ( assemblyPath , cache ) ;
113- if ( assembly == null )
114- throw new System . ArgumentException ( "Referenced Assembly " + Path . GetFileName ( assemblyPath ) + " could not be found!" ) ;
115-
116- // Ignore it if we already added the assembly
117- if ( alreadyFoundAssemblies . IndexOf ( assemblyPath ) != - 1 )
118- return ;
119-
120- alreadyFoundAssemblies . Add ( assemblyPath ) ;
121-
122- var architectureSpecificPlugins = PluginImporter . GetImporters ( target ) . Where ( i =>
123- {
124- var cpu = i . GetPlatformData ( target , "CPU" ) ;
125- return ! string . IsNullOrEmpty ( cpu ) && ! string . Equals ( cpu , "AnyCPU" , StringComparison . InvariantCultureIgnoreCase ) ;
126- } ) . Select ( i => Path . GetFileName ( i . assetPath ) ) . Distinct ( ) ;
127-
128- // Go through all referenced assemblies
129- foreach ( AssemblyNameReference referencedAssembly in assembly . MainModule . AssemblyReferences )
130- {
131- // Special cases for Metro
132- if ( referencedAssembly . Name == "BridgeInterface" ) continue ;
133- if ( referencedAssembly . Name == "WinRTBridge" ) continue ;
134- if ( referencedAssembly . Name == "UnityEngineProxy" ) continue ;
135- if ( IgnoreAssembly ( referencedAssembly . Name + ".dll" , target , scriptingImplementation ) ) continue ;
136-
137- string foundPath = FindAssemblyName ( referencedAssembly . FullName , referencedAssembly . Name , allAssemblyPaths , foldersToSearch , cache ) ;
138-
139- if ( foundPath == "" )
140- {
141- // Ignore architecture specific plugin references
142- var found = false ;
143- foreach ( var extension in new [ ] { ".dll" , ".winmd" } )
144- {
145- if ( architectureSpecificPlugins . Any ( p => string . Equals ( p , referencedAssembly . Name + extension , StringComparison . InvariantCultureIgnoreCase ) ) )
146- {
147- found = true ;
148- break ;
149- }
150- }
151- if ( found )
152- continue ;
153- throw new System . ArgumentException ( string . Format ( "The Assembly {0} is referenced by {1} ('{2}'). But the dll is not allowed to be included or could not be found." ,
154- referencedAssembly . Name ,
155- assembly . MainModule . Assembly . Name . Name ,
156- assemblyPath ) ) ;
157- }
158-
159- AddReferencedAssembliesRecurse ( foundPath , alreadyFoundAssemblies , allAssemblyPaths , foldersToSearch , cache , target , scriptingImplementation ) ;
160- }
161- }
162-
163- static string FindAssemblyName ( string fullName , string name , string [ ] allAssemblyPaths , string [ ] foldersToSearch , Dictionary < string , AssemblyDefinition > cache )
164- {
165- // Search in provided assemblies
166- for ( int i = 0 ; i < allAssemblyPaths . Length ; i ++ )
167- {
168- if ( ! File . Exists ( allAssemblyPaths [ i ] ) )
169- continue ;
170-
171- AssemblyDefinition definition = GetAssemblyDefinitionCached ( allAssemblyPaths [ i ] , cache ) ;
172- if ( definition . MainModule . Assembly . Name . Name == name )
173- return allAssemblyPaths [ i ] ;
174- }
175-
176- // Search in GAC
177- foreach ( string folder in foldersToSearch )
178- {
179- string pathInGacFolder = Path . Combine ( folder , name + ".dll" ) ;
180- if ( File . Exists ( pathInGacFolder ) )
181- return pathInGacFolder ;
182- }
183- return "" ;
184- }
185-
186- [ RequiredByNativeCode ]
187- static public string [ ] FindAssembliesReferencedBy ( string [ ] paths , string [ ] foldersToSearch , BuildTarget target , ScriptingImplementation scriptingImplementation )
188- {
189- List < string > unique = new List < string > ( ) ;
190- string [ ] allAssemblyPaths = paths ;
191-
192- var cache = new Dictionary < string , AssemblyDefinition > ( ) ;
193- for ( int i = 0 ; i < paths . Length ; i ++ )
194- AddReferencedAssembliesRecurse ( paths [ i ] , unique , allAssemblyPaths , foldersToSearch , cache , target , scriptingImplementation ) ;
195-
196- for ( int i = 0 ; i < paths . Length ; i ++ )
197- unique . Remove ( paths [ i ] ) ;
198-
199- return unique . ToArray ( ) ;
200- }
201-
20229 static public bool IsUnityEngineModule ( AssemblyDefinition assembly )
20330 {
20431 return assembly . CustomAttributes . Any ( a => a . AttributeType . FullName == typeof ( UnityEngineModuleAssembly ) . FullName ) ;
@@ -209,33 +36,6 @@ static public bool IsUnityEngineModule(Assembly assembly)
20936 return assembly . GetCustomAttributes ( typeof ( UnityEngineModuleAssembly ) , false ) . Length > 0 ;
21037 }
21138
212- private static bool IsTypeAUserExtendedScript ( TypeReference type )
213- {
214- if ( type == null || type . FullName == "System.Object" )
215- return false ;
216-
217- try
218- {
219- var typeDefinition = type . Resolve ( ) ;
220- var attributes = typeDefinition . CustomAttributes ;
221- for ( var i = 0 ; i < attributes . Count ; i ++ )
222- {
223- if ( attributes [ i ] . Constructor . DeclaringType . FullName == "UnityEngine.ExtensionOfNativeClassAttribute" )
224- return true ;
225- }
226-
227- if ( typeDefinition . BaseType != null )
228- return IsTypeAUserExtendedScript ( typeDefinition . BaseType ) ;
229- }
230- catch ( AssemblyResolutionException )
231- {
232- // just eat exception if we fail to load assembly here.
233- // failure should be handled better in other places.
234- }
235-
236- return false ;
237- }
238-
23939 public static string [ ] GetDefaultAssemblySearchPaths ( )
24040 {
24141 // Add the path to all available precompiled assemblies
@@ -359,36 +159,6 @@ public static bool IsInternalAssembly(string file)
359159 return ModuleUtils . GetAdditionalReferencesForUserScripts ( ) . Any ( p => p . Equals ( file ) ) ;
360160 }
361161
362- const int kDefaultDepth = 10 ;
363- internal static ICollection < string > FindAssemblies ( string basePath )
364- {
365- return FindAssemblies ( basePath , kDefaultDepth ) ;
366- }
367-
368- internal static ICollection < string > FindAssemblies ( string basePath , int maxDepth )
369- {
370- var assemblies = new List < string > ( ) ;
371-
372- if ( 0 == maxDepth )
373- return assemblies ;
374-
375- try
376- {
377- DirectoryInfo directory = new DirectoryInfo ( basePath ) ;
378- assemblies . AddRange ( directory . GetFiles ( )
379- . Where ( file => IsManagedAssembly ( file . FullName ) )
380- . Select ( file => file . FullName ) ) ;
381- foreach ( DirectoryInfo subdirectory in directory . GetDirectories ( ) )
382- assemblies . AddRange ( FindAssemblies ( subdirectory . FullName , maxDepth - 1 ) ) ;
383- }
384- catch ( Exception )
385- {
386- // Return what we have now
387- }
388-
389- return assemblies ;
390- }
391-
392162 /// <summary>
393163 /// Performs a depth-first-search topological sort on the input assemblies,
394164 /// based on the outgoing assembly references from each assembly. The
0 commit comments