using System; using System.Runtime.InteropServices; using UnityEngine; public class UniQuake: MonoBehaviour { private const string DllName = "uniquake.dll"; private const int MemSize = 0x4000000; // 64 MB of heap space private QuakeParms quakeParms; private SysCalls sysCalls; private ModCalls modCalls; private bool initialized = false; void Start() { string[] arguments = { "", "-window", "-width", "1440", "-height", "1080", "+developer", "1", }; quakeParms = new QuakeParms { baseDir = Application.persistentDataPath, cacheDir = null, }; quakeParms.SetArguments(arguments); quakeParms.AllocateMemory(MemSize); sysCalls = new SysCalls(this); modCalls = new ModCalls(this); try { UniQuake_Init(quakeParms, sysCalls.ToIntPtr, modCalls.ToIntPtr); initialized = true; } catch (QuakeException ex) { if (ex.ExitCode == 0) Debug.Log(ex.Message); else Debug.LogException(ex); Shutdown(); } catch (Exception ex) { Debug.LogException(ex); Shutdown(); } } void Update() { if (!initialized) return; try { UniQuake_Update(Time.deltaTime); } catch (QuakeException ex) { if (ex.ExitCode == 0) Debug.Log(ex.Message); else Debug.LogException(ex); Shutdown(); } catch (Exception ex) { Debug.LogException(ex); Shutdown(); } } private void Shutdown() { initialized = false; UniQuake_Shutdown(); Destroy(this); } private void OnDestroy() { modCalls.Destroy(); sysCalls.Destroy(); if (quakeParms != null) { quakeParms.Destroy(); quakeParms = null; } } [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] private static extern void UniQuake_Init(QuakeParms parms, IntPtr sysCalls, IntPtr modCalls); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] private static extern void UniQuake_Update(float deltaTime); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] private static extern void UniQuake_Shutdown(); } public class QuakeException: Exception { public int ExitCode { get; } public QuakeException(string message, int exitCode = 0) : base(message) { ExitCode = exitCode; } }