From e41686865016196b611c15bbb2c580dfd675f8ab Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 10 Apr 2021 18:16:49 +0200 Subject: [PATCH] Modifications to make UniQuake work with a new engine DLL based on QuakeSpasm: - Extended QuakeParms with additional fields required by QuakeSpasm, and marshal it to a fixed memory block so that the engine can hold on to a reference. - Increased heap size to 128 MB - Some minor renaming and refactoring --- Assets/Scripts/CallbackHandler.cs | 4 ++-- Assets/Scripts/Modules/RenderModule.cs | 2 +- Assets/Scripts/Modules/SystemModule.cs | 16 ++++++++-------- Assets/Scripts/QuakeParms.cs | 21 ++++++++++++++++++++- Assets/Scripts/UniQuake.cs | 14 ++++++++------ 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/CallbackHandler.cs b/Assets/Scripts/CallbackHandler.cs index f525c70..14cc8a5 100644 --- a/Assets/Scripts/CallbackHandler.cs +++ b/Assets/Scripts/CallbackHandler.cs @@ -8,9 +8,9 @@ public abstract class CallbackHandler private GCHandle selfHandle; private GCHandle callbacksHandle; - public IntPtr ToIntPtr => callbacksHandle.AddrOfPinnedObject(); + public IntPtr CallbacksPtr => callbacksHandle.AddrOfPinnedObject(); - protected IntPtr SelfPtr => GCHandle.ToIntPtr(selfHandle); + protected IntPtr TargetPtr => GCHandle.ToIntPtr(selfHandle); protected CallbackHandler() { diff --git a/Assets/Scripts/Modules/RenderModule.cs b/Assets/Scripts/Modules/RenderModule.cs index 9726902..6dc56cf 100644 --- a/Assets/Scripts/Modules/RenderModule.cs +++ b/Assets/Scripts/Modules/RenderModule.cs @@ -17,7 +17,7 @@ public class RenderModule : CallbackHandler var callbacks = new Callbacks { - target = SelfPtr, + target = TargetPtr, UploadAliasModel = CreateCallback(Callback_UploadAliasModel), }; diff --git a/Assets/Scripts/Modules/SystemModule.cs b/Assets/Scripts/Modules/SystemModule.cs index 6910210..24edcf0 100644 --- a/Assets/Scripts/Modules/SystemModule.cs +++ b/Assets/Scripts/Modules/SystemModule.cs @@ -18,12 +18,12 @@ public class SystemModule: CallbackHandler var callbacks = new Callbacks { - target = SelfPtr, + target = TargetPtr, SysPrint = CreateCallback(Callback_SysPrint), SysError = CreateCallback(Callback_SysError), SysQuit = CreateCallback(Callback_SysQuit), - SysFloatTime = CreateCallback(Callback_SysFloatTime), + SysDoubleTime = CreateCallback(Callback_SysDoubleTime), SysFileOpenRead = CreateCallback(Callback_SysFileOpenRead), SysFileOpenWrite = CreateCallback(Callback_SysFileOpenWrite), @@ -60,7 +60,7 @@ public class SystemModule: CallbackHandler public IntPtr SysPrint; public IntPtr SysError; public IntPtr SysQuit; - public IntPtr SysFloatTime; + public IntPtr SysDoubleTime; public IntPtr SysFileOpenRead; public IntPtr SysFileOpenWrite; @@ -117,15 +117,15 @@ public class SystemModule: CallbackHandler } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - private delegate double SysFloatTimeCallback(IntPtr target); + private delegate double SysDoubleTimeCallback(IntPtr target); - [MonoPInvokeCallback(typeof(SysFloatTimeCallback))] - private static double Callback_SysFloatTime(IntPtr target) + [MonoPInvokeCallback(typeof(SysDoubleTimeCallback))] + private static double Callback_SysDoubleTime(IntPtr target) { - return GetSelf(target).FloatTime(); + return GetSelf(target).DoubleTime(); } - private double FloatTime() + private double DoubleTime() { return uq.CurrentTime; } diff --git a/Assets/Scripts/QuakeParms.cs b/Assets/Scripts/QuakeParms.cs index c492c07..f983db9 100644 --- a/Assets/Scripts/QuakeParms.cs +++ b/Assets/Scripts/QuakeParms.cs @@ -10,7 +10,7 @@ public class QuakeParms public string baseDir; [MarshalAs(UnmanagedType.LPStr)] - public string cacheDir; + public string userDir; public int argc; @@ -21,6 +21,19 @@ public class QuakeParms public int memSize; + public int numCpus; + + public int errState; + + private IntPtr nativeBlock; + + public IntPtr ToNativePtr() + { + nativeBlock = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(this, nativeBlock, false); + return nativeBlock; + } + public void SetArguments(string[] arguments) { argc = arguments.Length; @@ -39,6 +52,12 @@ public class QuakeParms public void Destroy() { + if (nativeBlock != IntPtr.Zero) + { + Marshal.FreeHGlobal(nativeBlock); + nativeBlock = IntPtr.Zero; + } + if (memBase != IntPtr.Zero) { Marshal.FreeHGlobal(memBase); diff --git a/Assets/Scripts/UniQuake.cs b/Assets/Scripts/UniQuake.cs index 93e6a05..6b33500 100644 --- a/Assets/Scripts/UniQuake.cs +++ b/Assets/Scripts/UniQuake.cs @@ -16,7 +16,7 @@ public class UniQuake: MonoBehaviour #endif #endif - private const int MemSize = 0x4000000; // 64 MB of heap space + private const int MemSize = 0x8000000; // 128 MB of heap space private IntPtr libraryHandle; @@ -73,7 +73,9 @@ public class UniQuake: MonoBehaviour quakeParms = new QuakeParms { baseDir = Application.persistentDataPath, - cacheDir = null, + userDir = null, // TODO set this if user prefs need to be stored somewhere specific, otherwise this will be the same as baseDir + numCpus = SystemInfo.processorCount, + errState = 0, }; quakeParms.SetArguments(arguments.ToArray()); quakeParms.AllocateMemory(MemSize); @@ -82,8 +84,8 @@ public class UniQuake: MonoBehaviour try { - UniQuake_SetFmodSystem(AudioManager.Instance.FmodSystem.handle); - UniQuake_Init(quakeParms, systemModule.ToIntPtr, renderModule.ToIntPtr); + // UniQuake_SetFmodSystem(AudioManager.Instance.FmodSystem.handle); + UniQuake_Init(quakeParms.ToNativePtr(), systemModule.CallbacksPtr, renderModule.CallbacksPtr); initialized = true; } catch (QuakeException ex) @@ -149,7 +151,7 @@ public class UniQuake: MonoBehaviour } [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - private delegate void UniQuake_InitFunc(QuakeParms parms, IntPtr sysCalls, IntPtr glCalls); + private delegate void UniQuake_InitFunc(IntPtr parms, IntPtr sysCalls, IntPtr glCalls); private UniQuake_InitFunc UniQuake_Init; [UnmanagedFunctionPointer(CallingConvention.Cdecl)] @@ -176,7 +178,7 @@ public class UniQuake: MonoBehaviour UniQuake_Init = LoadLibraryFunction("UniQuake_Init"); UniQuake_Update = LoadLibraryFunction("UniQuake_Update"); UniQuake_Shutdown = LoadLibraryFunction("UniQuake_Shutdown"); - UniQuake_SetFmodSystem = LoadLibraryFunction("UniQuake_SetFmodSystem"); + // UniQuake_SetFmodSystem = LoadLibraryFunction("UniQuake_SetFmodSystem"); } private TDelegate LoadLibraryFunction(string functionName)