Skip to content

Commit 3d33aac

Browse files
committed
Lazily load StubLluiPlugin
This removes warning about StubLluiPlugin being not found, when it wouldn't be anyhow usefull
1 parent 8819394 commit 3d33aac

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

scripts/DllManipulatorScript.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ private void OnEnable()
4848
DontDestroyOnLoad(gameObject);
4949

5050
var timer = System.Diagnostics.Stopwatch.StartNew();
51+
52+
LowLevelPluginManager.ResetStubPlugin();
53+
5154
DllManipulator.SetUnityContext(Thread.CurrentThread.ManagedThreadId, Application.dataPath);
5255
DllManipulator.Options = Options;
5356

@@ -75,8 +78,6 @@ private void OnEnable()
7578
}
7679
}
7780

78-
LowLevelPluginManager.Initialize();
79-
8081
if (DllManipulator.Options.loadingMode == DllLoadingMode.Preload)
8182
DllManipulator.LoadAll();
8283

@@ -94,7 +95,7 @@ private void OnDestroy()
9495

9596
DllManipulator.UnloadAll();
9697
DllManipulator.ForgetAllDlls();
97-
DllManipulator.ClearCrashLogs();
98+
DllManipulator.ClearCrashLogs();
9899
}
99100
}
100101
}

scripts/LowLevelPluginManager.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,51 @@
1212
namespace UnityNativeTool.Internal
1313
{
1414
[DisableMocking]
15-
class LowLevelPluginManager
15+
static class LowLevelPluginManager
1616
{
17+
private static bool _triedLoadingStubPlugin = false;
1718
private static IntPtr _unityInterfacePtr = IntPtr.Zero;
18-
public static void Initialize()
19-
{
20-
try
21-
{
22-
_unityInterfacePtr = GetUnityInterfacesPtr();
23-
if (_unityInterfacePtr == IntPtr.Zero)
24-
throw new Exception($"{nameof(GetUnityInterfacesPtr)} returned null");
25-
}
26-
catch(DllNotFoundException)
27-
{
28-
Debug.LogWarning("StubLluiPlugin not found. UnityPluginLoad and UnityPluginUnload callbacks won't fire. You may comment out this warning if you don't care about these callbacks.");
29-
}
30-
}
3119

3220
public static void OnDllLoaded(NativeDll dll)
3321
{
34-
if (_unityInterfacePtr == IntPtr.Zero)
35-
return;
22+
if (_triedLoadingStubPlugin && _unityInterfacePtr == IntPtr.Zero)
23+
return;
3624

3725
var unityPluginLoadFunc = new NativeFunction("UnityPluginLoad", dll) {
3826
delegateType = typeof(UnityPluginLoadDel)
3927
};
4028

4129
DllManipulator.LoadTargetFunction(unityPluginLoadFunc, true);
42-
if (unityPluginLoadFunc.@delegate != null)
30+
if (unityPluginLoadFunc.@delegate == null)
31+
return;
32+
33+
if (!_triedLoadingStubPlugin)
34+
{
35+
try
36+
{
37+
_unityInterfacePtr = GetUnityInterfacesPtr();
38+
if (_unityInterfacePtr == IntPtr.Zero)
39+
throw new Exception($"{nameof(GetUnityInterfacesPtr)} returned null");
40+
}
41+
catch (DllNotFoundException)
42+
{
43+
Debug.LogWarning("StubLluiPlugin not found. UnityPluginLoad and UnityPluginUnload callbacks won't fire. If you didn't install UnityNativeTool from .unitypackage or it didn't contain the compiled plugin, you'll need to compile it manually. Alternatively, you may comment out this warning if you don't care about these callbacks.");
44+
}
45+
finally
46+
{
47+
_triedLoadingStubPlugin = true;
48+
}
49+
}
50+
51+
if (_unityInterfacePtr != IntPtr.Zero)
4352
((UnityPluginLoadDel)unityPluginLoadFunc.@delegate)(_unityInterfacePtr);
4453
}
4554

4655
public static void OnBeforeDllUnload(NativeDll dll)
4756
{
57+
if (_unityInterfacePtr == IntPtr.Zero)
58+
return;
59+
4860
var unityPluginUnloadFunc = new NativeFunction("UnityPluginUnload", dll) {
4961
delegateType = typeof(UnityPluginUnloadDel)
5062
};
@@ -53,6 +65,12 @@ public static void OnBeforeDllUnload(NativeDll dll)
5365
if (unityPluginUnloadFunc.@delegate != null)
5466
((UnityPluginUnloadDel)unityPluginUnloadFunc.@delegate)();
5567
}
68+
69+
public static void ResetStubPlugin()
70+
{
71+
_triedLoadingStubPlugin = false;
72+
_unityInterfacePtr = IntPtr.Zero;
73+
}
5674

5775
delegate void UnityPluginLoadDel(IntPtr unityInterfaces);
5876
delegate void UnityPluginUnloadDel();

0 commit comments

Comments
 (0)