From 64b4ac812b06d3ffd4b8d852d0faa506b4bd7279 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 12 Apr 2021 12:34:58 +0200 Subject: [PATCH] Split off native interop code for the main UniQuake class into a separate partial class file as well --- Assets/Scripts/UniQuake.Interop.cs | 78 +++++++++++++++++++++++ Assets/Scripts/UniQuake.Interop.cs.meta | 3 + Assets/Scripts/UniQuake.cs | 84 ++----------------------- 3 files changed, 87 insertions(+), 78 deletions(-) create mode 100644 Assets/Scripts/UniQuake.Interop.cs create mode 100644 Assets/Scripts/UniQuake.Interop.cs.meta diff --git a/Assets/Scripts/UniQuake.Interop.cs b/Assets/Scripts/UniQuake.Interop.cs new file mode 100644 index 0000000..400f772 --- /dev/null +++ b/Assets/Scripts/UniQuake.Interop.cs @@ -0,0 +1,78 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; +using UnityEngine; + +public partial class UniQuake +{ +#if UNITY_EDITOR + private const string DllPath = "Plugins/windows/x86_64/uniquake.dll"; +#elif UNITY_STANDALONE_WIN + #if UNITY_64 + private const string DllPath = "Plugins/x86_64/uniquake.dll"; + #else + private const string DllPath = "Plugins/x86/uniquake.dll"; + #endif +#endif + + private IntPtr libraryHandle; + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate void UniQuake_InitFunc(IntPtr parms, IntPtr sysCalls, IntPtr glCalls); + private UniQuake_InitFunc UniQuake_Init; + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate void UniQuake_UpdateFunc(float deltaTime); + private UniQuake_UpdateFunc UniQuake_Update; + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate void UniQuake_ShutdownFunc(); + private UniQuake_ShutdownFunc UniQuake_Shutdown; + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate void UniQuake_SetFmodSystemFunc(IntPtr fmodSystem); + private UniQuake_SetFmodSystemFunc UniQuake_SetFmodSystem; + + private void LoadLibrary() + { + string dllFile = Path.Combine(Application.dataPath, DllPath); + + // Experimental code to allow running multiple instances of Quake next to each other + // string dllName = Path.GetFileNameWithoutExtension(dllFile); + // string dllExt = Path.GetExtension(dllFile); + // string dllCopy = Path.Combine(Application.persistentDataPath, $"{dllName}{GetInstanceID()}{dllExt}"); + // File.Copy(dllFile, dllCopy, true); + // dllFile = dllCopy; + + libraryHandle = SystemLibrary.LoadLibrary(dllFile); + if (libraryHandle == IntPtr.Zero) + { + throw new DllNotFoundException($"Failed to load UniQuake library from path: {dllFile}, last error = {Marshal.GetLastWin32Error()}"); + } + + UniQuake_Init = LoadLibraryFunction("UniQuake_Init"); + UniQuake_Update = LoadLibraryFunction("UniQuake_Update"); + UniQuake_Shutdown = LoadLibraryFunction("UniQuake_Shutdown"); + UniQuake_SetFmodSystem = LoadLibraryFunction("UniQuake_SetFmodSystem"); + } + + private TDelegate LoadLibraryFunction(string functionName) + { + IntPtr procAddress = SystemLibrary.GetProcAddress(libraryHandle, functionName); + if (procAddress == IntPtr.Zero) + { + throw new DllNotFoundException($"Could not find library function: {functionName}"); + } + + return Marshal.GetDelegateForFunctionPointer(procAddress); + } + + private void FreeLibrary() + { + if (libraryHandle != IntPtr.Zero) + { + SystemLibrary.FreeLibrary(libraryHandle); + libraryHandle = IntPtr.Zero; + } + } +} diff --git a/Assets/Scripts/UniQuake.Interop.cs.meta b/Assets/Scripts/UniQuake.Interop.cs.meta new file mode 100644 index 0000000..d0c5a21 --- /dev/null +++ b/Assets/Scripts/UniQuake.Interop.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a22391b0c55941c6b4c6fb0f8510d88e +timeCreated: 1618223248 \ No newline at end of file diff --git a/Assets/Scripts/UniQuake.cs b/Assets/Scripts/UniQuake.cs index 869d02a..21e87c4 100644 --- a/Assets/Scripts/UniQuake.cs +++ b/Assets/Scripts/UniQuake.cs @@ -1,25 +1,13 @@ using System; using System.Collections.Generic; -using System.IO; -using System.Runtime.InteropServices; using System.Text; using UnityEngine; -public class UniQuake: MonoBehaviour +public partial class UniQuake: MonoBehaviour { -#if UNITY_EDITOR - private const string DllPath = "Plugins/windows/x86_64/uniquake.dll"; -#elif UNITY_STANDALONE_WIN - #if UNITY_64 - private const string DllPath = "Plugins/x86_64/uniquake.dll"; - #else - private const string DllPath = "Plugins/x86/uniquake.dll"; - #endif -#endif + private const int DefaultMemSize = 0x8000000; // 128 MB of heap space - private const int MemSize = 0x8000000; // 128 MB of heap space - - private IntPtr libraryHandle; + public int memorySize = DefaultMemSize; private QuakeParms quakeParms; private SystemModule systemModule; @@ -36,7 +24,8 @@ public class UniQuake: MonoBehaviour /// public double CurrentTime => Time.timeAsDouble - startTime; - private Action logHandler; + private Action logHandler; + private StringBuilder logBuffer = new StringBuilder(); void Start() { @@ -83,7 +72,7 @@ public class UniQuake: MonoBehaviour errState = 0, }; quakeParms.SetArguments(arguments.ToArray()); - quakeParms.AllocateMemory(MemSize); + quakeParms.AllocateMemory(memorySize); startTime = Time.timeAsDouble; @@ -169,8 +158,6 @@ public class UniQuake: MonoBehaviour Debug.Log(log); } - private StringBuilder logBuffer = new StringBuilder(); - private void CollectLog(string log) { logBuffer.Append(log); @@ -184,65 +171,6 @@ public class UniQuake: MonoBehaviour logBuffer.Clear(); } } - - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - private delegate void UniQuake_InitFunc(IntPtr parms, IntPtr sysCalls, IntPtr glCalls); - private UniQuake_InitFunc UniQuake_Init; - - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - private delegate void UniQuake_UpdateFunc(float deltaTime); - private UniQuake_UpdateFunc UniQuake_Update; - - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - private delegate void UniQuake_ShutdownFunc(); - private UniQuake_ShutdownFunc UniQuake_Shutdown; - - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - private delegate void UniQuake_SetFmodSystemFunc(IntPtr fmodSystem); - private UniQuake_SetFmodSystemFunc UniQuake_SetFmodSystem; - - private void LoadLibrary() - { - string dllFile = Path.Combine(Application.dataPath, DllPath); - - // Experimental code to allow running multiple instances of Quake next to each other - // string dllName = Path.GetFileNameWithoutExtension(dllFile); - // string dllExt = Path.GetExtension(dllFile); - // string dllCopy = Path.Combine(Application.persistentDataPath, $"{dllName}{GetInstanceID()}{dllExt}"); - // File.Copy(dllFile, dllCopy, true); - // dllFile = dllCopy; - - libraryHandle = SystemLibrary.LoadLibrary(dllFile); - if (libraryHandle == IntPtr.Zero) - { - throw new DllNotFoundException($"Failed to load UniQuake library from path: {dllFile}, last error = {Marshal.GetLastWin32Error()}"); - } - - UniQuake_Init = LoadLibraryFunction("UniQuake_Init"); - UniQuake_Update = LoadLibraryFunction("UniQuake_Update"); - UniQuake_Shutdown = LoadLibraryFunction("UniQuake_Shutdown"); - UniQuake_SetFmodSystem = LoadLibraryFunction("UniQuake_SetFmodSystem"); - } - - private TDelegate LoadLibraryFunction(string functionName) - { - IntPtr procAddress = SystemLibrary.GetProcAddress(libraryHandle, functionName); - if (procAddress == IntPtr.Zero) - { - throw new DllNotFoundException($"Could not find library function: {functionName}"); - } - - return Marshal.GetDelegateForFunctionPointer(procAddress); - } - - private void FreeLibrary() - { - if (libraryHandle != IntPtr.Zero) - { - SystemLibrary.FreeLibrary(libraryHandle); - libraryHandle = IntPtr.Zero; - } - } } public enum MissionPack