Skip to content

Commit 8bbee96

Browse files
committed
Cleanup
1 parent 4fbe635 commit 8bbee96

File tree

5 files changed

+39
-53
lines changed

5 files changed

+39
-53
lines changed

scripts/Memory.cs renamed to scripts/Detour.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace UnityNativeTool.Internal
1212
{
1313
/// <summary>A low level memory helper</summary>
14-
internal static class Memory
14+
internal static class Detour
1515
{
1616
static readonly HashSet<PlatformID> WindowsPlatformIDSet = new HashSet<PlatformID>
1717
{

scripts/DllManipulator.ReflectionCache.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45
using System.Reflection.Emit;
@@ -14,8 +15,8 @@ public partial class DllManipulator
1415
private static readonly Type[] MARSHAL_AS_ATTRIBUTE_CTOR_PARAMETERS = { typeof(UnmanagedType) };
1516

1617

17-
private static readonly Lazy<FieldInfo> Field_NativeFunctions = new Lazy<FieldInfo>(
18-
() => typeof(DllManipulator).GetField(nameof(_nativeFunctions), BindingFlags.NonPublic | BindingFlags.Static));
18+
private static readonly Lazy<FieldInfo> Field_MockedNativeFunctions = new Lazy<FieldInfo>(
19+
() => typeof(DllManipulator).GetField(nameof(_mockedNativeFunctions), BindingFlags.NonPublic | BindingFlags.Static));
1920

2021
private static readonly Lazy<FieldInfo> Field_NativeFunctionDelegate = new Lazy<FieldInfo>(
2122
() => typeof(NativeFunction).GetField(nameof(NativeFunction.@delegate), BindingFlags.Public | BindingFlags.Instance));
@@ -29,6 +30,10 @@ public partial class DllManipulator
2930
private static readonly Lazy<MethodInfo> Method_WriteNativeCrashLog = new Lazy<MethodInfo>(
3031
() => typeof(DllManipulator).GetMethod(nameof(WriteNativeCrashLog), BindingFlags.NonPublic | BindingFlags.Static));
3132

33+
34+
private static readonly Lazy<MethodInfo> Method_List_NativeFunction_get_Item = new Lazy<MethodInfo>(
35+
() => typeof(List<NativeFunction>).GetMethod("get_Item", BindingFlags.Public | BindingFlags.Instance));
36+
3237
/// <summary>
3338
/// <see cref="ReaderWriterLockSlim.EnterReadLock()"/>
3439
/// </summary>

scripts/DllManipulator.cs

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

scripts/NativeDll.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ public NativeDll(string name, string path)
1717
this.name = name;
1818
this.path = path;
1919
}
20+
21+
public void ResetAsUnloaded()
22+
{
23+
handle = IntPtr.Zero;
24+
loadingError = false;
25+
symbolError = false;
26+
27+
foreach (var func in functions)
28+
{
29+
func.@delegate = null;
30+
}
31+
}
2032
}
2133

2234
public class NativeDllInfo

scripts/NativeFunction.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ internal class NativeFunction
3434
{
3535
public readonly NativeFunctionIdentity identity;
3636
public NativeDll containingDll;
37-
public Delegate @delegate = null;
3837
public Type delegateType = null;
39-
public int index = -1;
38+
public Delegate @delegate = null;
4039

4140
public NativeFunction(string symbol, NativeDll containingDll)
4241
{

0 commit comments

Comments
 (0)