@@ -28,12 +28,11 @@ public partial class DllManipulator
2828 private static readonly Dictionary < string , NativeDll > _dlls = new Dictionary < string , NativeDll > ( ) ;
2929 private static readonly Dictionary < MethodInfo , DynamicMethod > _nativeFunctionMocks = new Dictionary < MethodInfo , DynamicMethod > ( ) ;
3030 private static readonly Dictionary < NativeFunctionSignature , Type > _delegateTypesForNativeFunctionSignatures = new Dictionary < NativeFunctionSignature , Type > ( ) ;
31- private static NativeFunction [ ] _nativeFunctions = null ;
32- private static int _nativeFunctionsCount = 0 ;
31+ private static List < NativeFunction > _mockedNativeFunctions = new List < NativeFunction > ( ) ;
3332 private static int _createdDelegateTypes = 0 ;
3433 private static int _lastNativeCallIndex = 0 ; //Use with synchronization
3534
36- public static void SetUnityContext ( int unityMainThreadId , string assetsPath )
35+ internal static void SetUnityContext ( int unityMainThreadId , string assetsPath )
3736 {
3837 DllManipulator . _unityMainThreadId = unityMainThreadId ;
3938 DllManipulator . _assetsPath = assetsPath ;
@@ -79,19 +78,10 @@ public static void UnloadAll()
7978 LowLevelPluginManager . OnBeforeDllUnload ( dll ) ;
8079
8180 bool success = SysUnloadDll ( dll . handle ) ;
82- dll . handle = IntPtr . Zero ;
83-
84- //Reset error states at unload
85- dll . loadingError = false ;
86- dll . symbolError = false ;
87-
88- foreach ( var func in dll . functions )
89- {
90- func . @delegate = null ;
91- }
92-
9381 if ( ! success )
9482 Debug . LogWarning ( $ "Error while unloading DLL \" { dll . name } \" at path \" { dll . path } \" ") ;
83+
84+ dll . ResetAsUnloaded ( ) ;
9585 }
9686 }
9787 }
@@ -101,14 +91,13 @@ public static void UnloadAll()
10191 }
10292 }
10393
104- public static void ForgetAllDlls ( )
94+ internal static void ForgetAllDlls ( )
10595 {
10696 _dlls . Clear ( ) ;
107- _nativeFunctions = null ;
108- _nativeFunctionsCount = 0 ;
97+ _mockedNativeFunctions . Clear ( ) ;
10998 }
11099
111- public static void ClearCrashLogs ( )
100+ internal static void ClearCrashLogs ( )
112101 {
113102 if ( Options . enableCrashLogs )
114103 {
@@ -140,7 +129,7 @@ public static IList<NativeDllInfo> GetUsedDllsInfos()
140129 return dllInfos ;
141130 }
142131
143- public static IEnumerable < MethodInfo > FindNativeFunctionsToMock ( Assembly assembly )
132+ internal static IEnumerable < MethodInfo > FindNativeFunctionsToMock ( Assembly assembly )
144133 {
145134 var allTypes = assembly . GetTypes ( ) ;
146135 foreach ( var type in allTypes )
@@ -162,19 +151,19 @@ public static IEnumerable<MethodInfo> FindNativeFunctionsToMock(Assembly assembl
162151 }
163152 }
164153
165- public static string ApplyDirectoryPathMacros ( string path )
154+ private static string ApplyDirectoryPathMacros ( string path )
166155 {
167156 return path
168157 . Replace ( DLL_PATH_PATTERN_ASSETS_MACRO , _assetsPath )
169158 . Replace ( DLL_PATH_PATTERN_PROJECT_MACRO , _assetsPath + "/../" ) ;
170159 }
171160
172- public static void MockNativeFunction ( MethodInfo function )
161+ internal static void MockNativeFunction ( MethodInfo function )
173162 {
174163 var methodMock = GetNativeFunctionMockMethod ( function ) ;
175- Memory . MarkForNoInlining ( function ) ;
164+ Detour . MarkForNoInlining ( function ) ;
176165 PrepareDynamicMethod ( methodMock ) ;
177- Memory . DetourMethod ( function , methodMock ) ;
166+ Detour . DetourMethod ( function , methodMock ) ;
178167 }
179168
180169 /// <summary>
@@ -202,9 +191,8 @@ private static DynamicMethod GetNativeFunctionMockMethod(MethodInfo nativeMethod
202191
203192 var nativeFunction = new NativeFunction ( nativeFunctionSymbol , dll ) ;
204193 dll . functions . Add ( nativeFunction ) ;
205- var nativeFunctionIndex = _nativeFunctionsCount ;
206- AddNativeFunction ( nativeFunction ) ;
207- nativeFunction . index = nativeFunctionIndex ;
194+ var nativeFunctionIndex = _mockedNativeFunctions . Count ;
195+ _mockedNativeFunctions . Add ( nativeFunction ) ;
208196
209197 var parameters = nativeMethod . GetParameters ( ) ;
210198 var parameterTypes = parameters . Select ( x => x . ParameterType ) . ToArray ( ) ;
@@ -247,9 +235,9 @@ private static void GenerateNativeFunctionMockBody(ILGenerator il, ParameterInfo
247235 il . BeginExceptionBlock ( ) ; //Start lock clause: lock, try { ... }, finally { release }
248236 }
249237
250- il . Emit ( OpCodes . Ldsfld , Field_NativeFunctions . Value ) ; //Load NativeFunction object
238+ il . Emit ( OpCodes . Ldsfld , Field_MockedNativeFunctions . Value ) ; //Load NativeFunction object
251239 il . EmitFastI4Load ( nativeFunctionIndex ) ;
252- il . Emit ( OpCodes . Ldelem_Ref ) ;
240+ il . Emit ( OpCodes . Call , Method_List_NativeFunction_get_Item . Value ) ;
253241
254242 if ( Options . loadingMode == DllLoadingMode . Lazy ) //If lazy mode, load the function. Otherwise we assume it's already loaded
255243 {
@@ -276,9 +264,9 @@ private static void GenerateNativeFunctionMockBody(ILGenerator il, ParameterInfo
276264 }
277265 il . Emit ( OpCodes . Call , Method_WriteNativeCrashLog . Value ) ;
278266
279- il . Emit ( OpCodes . Ldsfld , Field_NativeFunctions . Value ) ; //Once again load native function, previous one was consumed by log method
267+ il . Emit ( OpCodes . Ldsfld , Field_MockedNativeFunctions . Value ) ; //Once again load native function, previous one was consumed by log method
280268 il . EmitFastI4Load ( nativeFunctionIndex ) ;
281- il . Emit ( OpCodes . Ldelem_Ref ) ;
269+ il . Emit ( OpCodes . Call , Method_List_NativeFunction_get_Item . Value ) ;
282270 }
283271
284272 il . Emit ( OpCodes . Ldfld , Field_NativeFunctionDelegate . Value ) ;
@@ -326,24 +314,6 @@ private static void PrepareDynamicMethod(DynamicMethod method)
326314 }
327315 }
328316
329- /// <summary>
330- /// Adds <paramref name="nativeFunction"/> to <see cref="_nativeFunctions"/> list
331- /// </summary>
332- private static void AddNativeFunction ( NativeFunction nativeFunction )
333- {
334- if ( _nativeFunctions == null )
335- _nativeFunctions = new NativeFunction [ 4 ] ;
336-
337- if ( _nativeFunctionsCount == _nativeFunctions . Length )
338- {
339- var newArray = new NativeFunction [ _nativeFunctions . Length * 2 ] ;
340- Array . Copy ( _nativeFunctions , newArray , _nativeFunctions . Length ) ;
341- _nativeFunctions = newArray ;
342- }
343-
344- _nativeFunctions [ _nativeFunctionsCount ++ ] = nativeFunction ;
345- }
346-
347317 private static Type CreateDelegateTypeForNativeFunctionSignature ( NativeFunctionSignature functionSignature , string functionName )
348318 {
349319 if ( _customDelegateTypesModule == null )
0 commit comments