diff --git a/scripts/DllManipulator.cs b/scripts/DllManipulator.cs index e61840d..757574d 100644 --- a/scripts/DllManipulator.cs +++ b/scripts/DllManipulator.cs @@ -503,7 +503,7 @@ internal static void LoadTargetFunction(NativeFunction nativeFunction, bool igno if (!ignoreLoadError) { dll.loadingError = true; - Prop_EditorApplication_isPaused.Value?.SetValue(null, true); + DispatchOnMainThread(() => { Prop_EditorApplication_isPaused.Value?.SetValue(null, true); }); throw new NativeDllException($"Could not load DLL \"{dll.name}\" at path \"{dll.path}\"."); } @@ -529,7 +529,7 @@ internal static void LoadTargetFunction(NativeFunction nativeFunction, bool igno if (!ignoreLoadError) { dll.symbolError = true; - Prop_EditorApplication_isPaused.Value?.SetValue(null, true); + DispatchOnMainThread(() => { Prop_EditorApplication_isPaused.Value?.SetValue(null, true); }); throw new NativeDllException($"Could not get address of symbol \"{nativeFunction.identity.symbol}\" in DLL \"{dll.name}\" at path \"{dll.path}\"."); } @@ -568,6 +568,18 @@ private static void InvokeCustomTriggers(List> triggers, methodInfo.Invoke(null, args); } } + + /// + /// Ensure the action is executed on the main thread. Executes immediately if on the main thread already, + /// otherwise the action is added to a queue + /// + private static void DispatchOnMainThread(Action action) + { + if(Thread.CurrentThread.ManagedThreadId == _unityMainThreadId) + action(); + else + DllManipulatorScript.MainThreadTriggerQueue.Enqueue(action); + } /// /// Logs native function's call to file. If that file exists, it is overwritten. One file is maintained for each thread.