1111using UnityEditor . Modules ;
1212using UnityEditorInternal ;
1313using UnityEngine ;
14+ using System . Runtime . InteropServices ;
15+ using UnityEngine . Scripting ;
1416
1517namespace UnityEditor
1618{
@@ -317,30 +319,48 @@ public static void ExtractAllClassesThatAreUserExtendedScripts(string path, out
317319 originalClassNameSpacesArray = originalNamespaces . ToArray ( ) ;
318320 }
319321
322+ struct GetAssemblyResolverData
323+ {
324+ public IAssemblyResolver Resolver ;
325+ public string [ ] SearchDirs ;
326+ }
327+
328+ static GetAssemblyResolverData GetAssemblyResolver ( BuildTarget targetPlatform , bool isEditor , string assemblyPathName , string [ ] searchDirs )
329+ {
330+ var target = ModuleManager . GetTargetStringFromBuildTarget ( targetPlatform ) ;
331+ var extension = ModuleManager . GetCompilationExtension ( target ) ;
332+ var extraPaths = extension . GetCompilerExtraAssemblyPaths ( isEditor , assemblyPathName ) ;
333+ if ( extraPaths != null && extraPaths . Length > 0 )
334+ {
335+ var dirs = new List < string > ( searchDirs ) ;
336+ dirs . AddRange ( extraPaths ) ;
337+ searchDirs = dirs . ToArray ( ) ;
338+ }
339+
340+ var assemblyResolver = extension . GetAssemblyResolver ( isEditor , assemblyPathName , searchDirs ) ;
341+
342+ return new GetAssemblyResolverData
343+ {
344+ Resolver = assemblyResolver ,
345+ SearchDirs = searchDirs
346+ } ;
347+ }
348+
320349 /// Extract information about all types in the specified assembly, searchDirs might be used to resolve dependencies.
321350 static public AssemblyTypeInfoGenerator . ClassInfo [ ] ExtractAssemblyTypeInfo ( BuildTarget targetPlatform , bool isEditor , string assemblyPathName , string [ ] searchDirs )
322351 {
323352 try
324353 {
325- var target = ModuleManager . GetTargetStringFromBuildTarget ( targetPlatform ) ;
326- var extension = ModuleManager . GetCompilationExtension ( target ) ;
327- var extraPaths = extension . GetCompilerExtraAssemblyPaths ( isEditor , assemblyPathName ) ;
328- if ( extraPaths != null && extraPaths . Length > 0 )
329- {
330- var dirs = new List < string > ( searchDirs ) ;
331- dirs . AddRange ( extraPaths ) ;
332- searchDirs = dirs . ToArray ( ) ;
333- }
334- var assemblyResolver = extension . GetAssemblyResolver ( isEditor , assemblyPathName , searchDirs ) ;
354+ var assemblyResolverData = GetAssemblyResolver ( targetPlatform , isEditor , assemblyPathName , searchDirs ) ;
335355
336356 AssemblyTypeInfoGenerator gen ;
337- if ( assemblyResolver == null )
357+ if ( assemblyResolverData . Resolver == null )
338358 {
339- gen = new AssemblyTypeInfoGenerator ( assemblyPathName , searchDirs ) ;
359+ gen = new AssemblyTypeInfoGenerator ( assemblyPathName , assemblyResolverData . SearchDirs ) ;
340360 }
341361 else
342362 {
343- gen = new AssemblyTypeInfoGenerator ( assemblyPathName , assemblyResolver ) ;
363+ gen = new AssemblyTypeInfoGenerator ( assemblyPathName , assemblyResolverData . Resolver ) ;
344364 }
345365
346366 return gen . GatherClassInfo ( ) ;
@@ -351,6 +371,101 @@ static public AssemblyTypeInfoGenerator.ClassInfo[] ExtractAssemblyTypeInfo(Buil
351371 }
352372 }
353373
374+ [ StructLayout ( LayoutKind . Sequential ) ]
375+ public struct RuntimeInitializeOnLoadMethodsData
376+ {
377+ public RuntimeInitializeClassInfo [ ] classInfos ;
378+ public int methodsCount ;
379+ }
380+
381+ static void FindRuntimeInitializeOnLoadMethodAttributes ( TypeDefinition type ,
382+ string assemblyName ,
383+ ref List < RuntimeInitializeClassInfo > classInfoList ,
384+ ref int methodCount )
385+ {
386+ if ( ! type . HasMethods )
387+ return ;
388+
389+ foreach ( var method in type . Methods )
390+ {
391+ // RuntimeInitializeOnLoadMethod only works on static methods.
392+ if ( ! method . IsStatic )
393+ continue ;
394+
395+ foreach ( var attribute in method . CustomAttributes )
396+ {
397+ if ( attribute . AttributeType . FullName == "UnityEngine.RuntimeInitializeOnLoadMethodAttribute" )
398+ {
399+ RuntimeInitializeLoadType loadType = RuntimeInitializeLoadType . AfterSceneLoad ;
400+
401+ if ( attribute . ConstructorArguments != null && attribute . ConstructorArguments . Count > 0 )
402+ loadType = ( RuntimeInitializeLoadType ) attribute . ConstructorArguments [ 0 ] . Value ;
403+
404+ RuntimeInitializeClassInfo classInfo = new RuntimeInitializeClassInfo ( ) ;
405+
406+ classInfo . assemblyName = assemblyName ;
407+ classInfo . className = type . FullName ;
408+ classInfo . methodNames = new [ ] { method . Name } ;
409+ classInfo . loadTypes = new [ ] { loadType } ;
410+ classInfoList . Add ( classInfo ) ;
411+ methodCount ++ ;
412+ }
413+ }
414+ }
415+ }
416+
417+ [ RequiredByNativeCode ]
418+ public static RuntimeInitializeOnLoadMethodsData ExtractPlayerRuntimeInitializeOnLoadMethods ( BuildTarget targetPlatform , string [ ] assemblyPaths , string [ ] searchDirs )
419+ {
420+ var classInfoList = new List < RuntimeInitializeClassInfo > ( ) ;
421+ int methodCount = 0 ;
422+
423+ foreach ( var assemblyPath in assemblyPaths )
424+ {
425+ try
426+ {
427+ var assemblyResolverData = GetAssemblyResolver ( targetPlatform , false , assemblyPath , searchDirs ) ;
428+
429+ if ( assemblyResolverData . Resolver == null )
430+ {
431+ var resolver = new DefaultAssemblyResolver ( ) ;
432+ foreach ( var searchDir in searchDirs )
433+ resolver . AddSearchDirectory ( searchDir ) ;
434+
435+ assemblyResolverData . Resolver = resolver ;
436+ }
437+
438+ var assembly = AssemblyDefinition . ReadAssembly ( assemblyPath , new ReaderParameters
439+ {
440+ AssemblyResolver = assemblyResolverData . Resolver
441+ } ) ;
442+
443+ var assemblyName = assembly . Name . Name ;
444+
445+ foreach ( var module in assembly . Modules )
446+ {
447+ foreach ( var type in module . Types )
448+ {
449+ FindRuntimeInitializeOnLoadMethodAttributes ( type ,
450+ assemblyName ,
451+ ref classInfoList ,
452+ ref methodCount ) ;
453+ }
454+ }
455+ }
456+ catch ( Exception ex )
457+ {
458+ throw new Exception ( "ExtractPlayerRuntimeInitializeOnLoadMethods: Failed to process " + assemblyPath + ", " + ex ) ;
459+ }
460+ }
461+
462+ var data = new RuntimeInitializeOnLoadMethodsData ( ) ;
463+ data . classInfos = classInfoList . ToArray ( ) ;
464+ data . methodsCount = methodCount ;
465+
466+ return data ;
467+ }
468+
354469 internal static Type [ ] GetTypesFromAssembly ( Assembly assembly )
355470 {
356471 if ( assembly == null )
0 commit comments