@@ -51,7 +51,7 @@ public static void ConfigureLib(bool allowUserToInputAddresses)
5151 LibCpp2IlMain . Settings . MetadataFixupFunc = Cpp2IlPluginManager . MetadataFixupFuncs is { } funcs ? ( originalBytes , version ) =>
5252 {
5353 Logger . InfoNewline ( "Received request for metadata fixup from LibCpp2Il. Calling registered plugin fixup functions..." ) ;
54-
54+
5555 foreach ( var func in funcs )
5656 {
5757 try
@@ -68,7 +68,7 @@ public static void ConfigureLib(bool allowUserToInputAddresses)
6868 Logger . ErrorNewline ( $ "Metadata fixup function threw an exception: { e } . Ignoring and trying next fixup function, if any...") ;
6969 }
7070 }
71-
71+
7272 //only get here if every fixup function returns null or throws.
7373 return null ;
7474 } : null ;
@@ -89,16 +89,15 @@ public static void InitializeLibCpp2Il(string assemblyPath, string metadataPath,
8989 try
9090 {
9191#endif
92- if ( ! LibCpp2IlMain . LoadFromFile ( assemblyPath , metadataPath , unityVersion ) )
93- throw new Exception ( "Initialization with LibCpp2Il failed" ) ;
92+ var context = LibCpp2IlMain . LoadFromFileAsContext ( assemblyPath , metadataPath , unityVersion ) ;
93+ OnLibInitialized ( context ) ;
9494#if ! DEBUG
9595 }
9696 catch ( Exception e )
9797 {
9898 throw new LibCpp2ILInitializationException ( "Fatal Exception initializing LibCpp2IL!" , e ) ;
9999 }
100100#endif
101- OnLibInitialized ( ) ;
102101 }
103102
104103 [ MemberNotNull ( nameof ( CurrentAppContext ) ) ]
@@ -112,27 +111,24 @@ public static void InitializeLibCpp2Il(byte[] assemblyData, byte[] metadataData,
112111
113112 try
114113 {
115- if ( ! LibCpp2IlMain . Initialize ( assemblyData , metadataData , unityVersion ) )
116- throw new Exception ( "Initialization with LibCpp2Il failed" ) ;
114+ var context = LibCpp2IlMain . InitializeAsContext ( assemblyData , metadataData , unityVersion ) ;
115+ OnLibInitialized ( context ) ;
117116 }
118117 catch ( Exception e )
119118 {
120119 throw new LibCpp2ILInitializationException ( "Fatal Exception initializing LibCpp2IL!" , e ) ;
121120 }
122-
123- OnLibInitialized ( ) ;
124121 }
125122
126123 [ MemberNotNull ( nameof ( CurrentAppContext ) ) ]
127- private static void OnLibInitialized ( )
124+ private static void OnLibInitialized ( LibCpp2IlContext libContext )
128125 {
129- MiscUtils . Init ( ) ;
130- LibCpp2IlMain . Binary ! . AllCustomAttributeGenerators . ToList ( )
126+ libContext . Binary . AllCustomAttributeGenerators . ToList ( )
131127 . ForEach ( ptr => SharedState . AttributeGeneratorStarts . Add ( ptr ) ) ;
132128
133129 var start = DateTime . Now ;
134130 Logger . InfoNewline ( "Creating application model..." ) ;
135- CurrentAppContext = new ( LibCpp2IlMain . Binary , LibCpp2IlMain . TheMetadata ! ) ;
131+ CurrentAppContext = new ( libContext . Binary , libContext . Metadata ) ;
136132 Logger . InfoNewline ( $ "Application model created in { ( DateTime . Now - start ) . TotalMilliseconds } ms") ;
137133 }
138134
@@ -144,38 +140,11 @@ public static void ResetInternalState()
144140
145141 AsmResolverUtils . Reset ( ) ;
146142
147- LibCpp2IlMain . Reset ( ) ;
148-
149143 CurrentAppContext = null ;
150144 }
151145
152- // public static void PopulateConcreteImplementations()
153- // {
154- // CheckLibInitialized();
155- //
156- // Logger.InfoNewline("Populating Concrete Implementation Table...");
157- //
158- // foreach (var def in LibCpp2IlMain.TheMetadata!.typeDefs)
159- // {
160- // if (def.IsAbstract)
161- // continue;
162- //
163- // var baseTypeReflectionData = def.BaseType;
164- // while (baseTypeReflectionData != null)
165- // {
166- // if (baseTypeReflectionData.baseType == null)
167- // break;
168- //
169- // if (baseTypeReflectionData.isType && baseTypeReflectionData.baseType.IsAbstract && !SharedState.ConcreteImplementations.ContainsKey(baseTypeReflectionData.baseType))
170- // SharedState.ConcreteImplementations[baseTypeReflectionData.baseType] = def;
171- //
172- // baseTypeReflectionData = baseTypeReflectionData.baseType.BaseType;
173- // }
174- // }
175- // }
176-
177146 private static bool IsLibInitialized ( )
178147 {
179- return LibCpp2IlMain . Binary != null && LibCpp2IlMain . TheMetadata != null ;
148+ return CurrentAppContext != null ;
180149 }
181150}
0 commit comments