@@ -28,12 +28,11 @@ public partial class DllManipulator
28
28
private static readonly Dictionary < string , NativeDll > _dlls = new Dictionary < string , NativeDll > ( ) ;
29
29
private static readonly Dictionary < MethodInfo , DynamicMethod > _nativeFunctionMocks = new Dictionary < MethodInfo , DynamicMethod > ( ) ;
30
30
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 > ( ) ;
33
32
private static int _createdDelegateTypes = 0 ;
34
33
private static int _lastNativeCallIndex = 0 ; //Use with synchronization
35
34
36
- public static void SetUnityContext ( int unityMainThreadId , string assetsPath )
35
+ internal static void SetUnityContext ( int unityMainThreadId , string assetsPath )
37
36
{
38
37
DllManipulator . _unityMainThreadId = unityMainThreadId ;
39
38
DllManipulator . _assetsPath = assetsPath ;
@@ -79,19 +78,10 @@ public static void UnloadAll()
79
78
LowLevelPluginManager . OnBeforeDllUnload ( dll ) ;
80
79
81
80
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
-
93
81
if ( ! success )
94
82
Debug . LogWarning ( $ "Error while unloading DLL \" { dll . name } \" at path \" { dll . path } \" ") ;
83
+
84
+ dll . ResetAsUnloaded ( ) ;
95
85
}
96
86
}
97
87
}
@@ -101,14 +91,13 @@ public static void UnloadAll()
101
91
}
102
92
}
103
93
104
- public static void ForgetAllDlls ( )
94
+ internal static void ForgetAllDlls ( )
105
95
{
106
96
_dlls . Clear ( ) ;
107
- _nativeFunctions = null ;
108
- _nativeFunctionsCount = 0 ;
97
+ _mockedNativeFunctions . Clear ( ) ;
109
98
}
110
99
111
- public static void ClearCrashLogs ( )
100
+ internal static void ClearCrashLogs ( )
112
101
{
113
102
if ( Options . enableCrashLogs )
114
103
{
@@ -140,7 +129,7 @@ public static IList<NativeDllInfo> GetUsedDllsInfos()
140
129
return dllInfos ;
141
130
}
142
131
143
- public static IEnumerable < MethodInfo > FindNativeFunctionsToMock ( Assembly assembly )
132
+ internal static IEnumerable < MethodInfo > FindNativeFunctionsToMock ( Assembly assembly )
144
133
{
145
134
var allTypes = assembly . GetTypes ( ) ;
146
135
foreach ( var type in allTypes )
@@ -162,19 +151,19 @@ public static IEnumerable<MethodInfo> FindNativeFunctionsToMock(Assembly assembl
162
151
}
163
152
}
164
153
165
- public static string ApplyDirectoryPathMacros ( string path )
154
+ private static string ApplyDirectoryPathMacros ( string path )
166
155
{
167
156
return path
168
157
. Replace ( DLL_PATH_PATTERN_ASSETS_MACRO , _assetsPath )
169
158
. Replace ( DLL_PATH_PATTERN_PROJECT_MACRO , _assetsPath + "/../" ) ;
170
159
}
171
160
172
- public static void MockNativeFunction ( MethodInfo function )
161
+ internal static void MockNativeFunction ( MethodInfo function )
173
162
{
174
163
var methodMock = GetNativeFunctionMockMethod ( function ) ;
175
- Memory . MarkForNoInlining ( function ) ;
164
+ Detour . MarkForNoInlining ( function ) ;
176
165
PrepareDynamicMethod ( methodMock ) ;
177
- Memory . DetourMethod ( function , methodMock ) ;
166
+ Detour . DetourMethod ( function , methodMock ) ;
178
167
}
179
168
180
169
/// <summary>
@@ -202,9 +191,8 @@ private static DynamicMethod GetNativeFunctionMockMethod(MethodInfo nativeMethod
202
191
203
192
var nativeFunction = new NativeFunction ( nativeFunctionSymbol , dll ) ;
204
193
dll . functions . Add ( nativeFunction ) ;
205
- var nativeFunctionIndex = _nativeFunctionsCount ;
206
- AddNativeFunction ( nativeFunction ) ;
207
- nativeFunction . index = nativeFunctionIndex ;
194
+ var nativeFunctionIndex = _mockedNativeFunctions . Count ;
195
+ _mockedNativeFunctions . Add ( nativeFunction ) ;
208
196
209
197
var parameters = nativeMethod . GetParameters ( ) ;
210
198
var parameterTypes = parameters . Select ( x => x . ParameterType ) . ToArray ( ) ;
@@ -247,9 +235,9 @@ private static void GenerateNativeFunctionMockBody(ILGenerator il, ParameterInfo
247
235
il . BeginExceptionBlock ( ) ; //Start lock clause: lock, try { ... }, finally { release }
248
236
}
249
237
250
- il . Emit ( OpCodes . Ldsfld , Field_NativeFunctions . Value ) ; //Load NativeFunction object
238
+ il . Emit ( OpCodes . Ldsfld , Field_MockedNativeFunctions . Value ) ; //Load NativeFunction object
251
239
il . EmitFastI4Load ( nativeFunctionIndex ) ;
252
- il . Emit ( OpCodes . Ldelem_Ref ) ;
240
+ il . Emit ( OpCodes . Call , Method_List_NativeFunction_get_Item . Value ) ;
253
241
254
242
if ( Options . loadingMode == DllLoadingMode . Lazy ) //If lazy mode, load the function. Otherwise we assume it's already loaded
255
243
{
@@ -276,9 +264,9 @@ private static void GenerateNativeFunctionMockBody(ILGenerator il, ParameterInfo
276
264
}
277
265
il . Emit ( OpCodes . Call , Method_WriteNativeCrashLog . Value ) ;
278
266
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
280
268
il . EmitFastI4Load ( nativeFunctionIndex ) ;
281
- il . Emit ( OpCodes . Ldelem_Ref ) ;
269
+ il . Emit ( OpCodes . Call , Method_List_NativeFunction_get_Item . Value ) ;
282
270
}
283
271
284
272
il . Emit ( OpCodes . Ldfld , Field_NativeFunctionDelegate . Value ) ;
@@ -326,24 +314,6 @@ private static void PrepareDynamicMethod(DynamicMethod method)
326
314
}
327
315
}
328
316
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
-
347
317
private static Type CreateDelegateTypeForNativeFunctionSignature ( NativeFunctionSignature functionSignature , string functionName )
348
318
{
349
319
if ( _customDelegateTypesModule == null )
0 commit comments