|
|
|
@ -4,9 +4,9 @@ using System.Runtime.InteropServices; |
|
|
|
using UnityEngine; |
|
|
|
|
|
|
|
public class UniQuake: MonoBehaviour |
|
|
|
{ |
|
|
|
#if UNITY_EDITOR
|
|
|
|
private const string DllPath = "Plugins/windows/x86_64/uniquake.dll"; |
|
|
|
{ |
|
|
|
#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"; |
|
|
|
@ -20,21 +20,21 @@ public class UniQuake: MonoBehaviour |
|
|
|
private IntPtr libraryHandle; |
|
|
|
|
|
|
|
private QuakeParms quakeParms; |
|
|
|
private SysCalls sysCalls; |
|
|
|
private ModCalls modCalls; |
|
|
|
private SystemModule systemModule; |
|
|
|
private RenderModule renderModule; |
|
|
|
|
|
|
|
private bool initialized = false; |
|
|
|
private double startTime; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Time since startup for this particular instance of Quake
|
|
|
|
/// <summary>
|
|
|
|
/// Time since startup for this particular instance of Quake
|
|
|
|
/// </summary>
|
|
|
|
public double CurrentTime => Time.timeAsDouble - startTime; |
|
|
|
|
|
|
|
void Start() |
|
|
|
{ |
|
|
|
sysCalls = new SysCalls(this); |
|
|
|
modCalls = new ModCalls(this); |
|
|
|
{ |
|
|
|
systemModule = new SystemModule(this); |
|
|
|
renderModule = new RenderModule(this); |
|
|
|
|
|
|
|
LoadLibrary(); |
|
|
|
|
|
|
|
@ -57,24 +57,24 @@ public class UniQuake: MonoBehaviour |
|
|
|
|
|
|
|
startTime = Time.timeAsDouble; |
|
|
|
|
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
UniQuake_Init(quakeParms, sysCalls.ToIntPtr, modCalls.ToIntPtr); |
|
|
|
initialized = true; |
|
|
|
} |
|
|
|
UniQuake_Init(quakeParms, systemModule.ToIntPtr, renderModule.ToIntPtr); |
|
|
|
initialized = true; |
|
|
|
} |
|
|
|
catch (QuakeException ex) |
|
|
|
{ |
|
|
|
if (ex.ExitCode == 0) |
|
|
|
Debug.Log(ex.Message); |
|
|
|
else |
|
|
|
Debug.LogException(ex); |
|
|
|
|
|
|
|
Shutdown(); |
|
|
|
{ |
|
|
|
if (ex.ExitCode == 0) |
|
|
|
Debug.Log(ex.Message); |
|
|
|
else |
|
|
|
Debug.LogException(ex); |
|
|
|
|
|
|
|
Shutdown(); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Debug.LogException(ex); |
|
|
|
Shutdown(); |
|
|
|
{ |
|
|
|
Debug.LogException(ex); |
|
|
|
Shutdown(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -83,37 +83,37 @@ public class UniQuake: MonoBehaviour |
|
|
|
if (!initialized) |
|
|
|
return; |
|
|
|
|
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
UniQuake_Update(Time.deltaTime); |
|
|
|
UniQuake_Update(Time.deltaTime); |
|
|
|
} |
|
|
|
catch (QuakeException ex) |
|
|
|
{ |
|
|
|
if (ex.ExitCode == 0) |
|
|
|
Debug.Log(ex.Message); |
|
|
|
else |
|
|
|
Debug.LogException(ex); |
|
|
|
|
|
|
|
Shutdown(); |
|
|
|
{ |
|
|
|
if (ex.ExitCode == 0) |
|
|
|
Debug.Log(ex.Message); |
|
|
|
else |
|
|
|
Debug.LogException(ex); |
|
|
|
|
|
|
|
Shutdown(); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
Debug.LogException(ex); |
|
|
|
Shutdown(); |
|
|
|
{ |
|
|
|
Debug.LogException(ex); |
|
|
|
Shutdown(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void Shutdown() |
|
|
|
{ |
|
|
|
initialized = false; |
|
|
|
UniQuake_Shutdown(); |
|
|
|
Destroy(this); |
|
|
|
private void Shutdown() |
|
|
|
{ |
|
|
|
initialized = false; |
|
|
|
UniQuake_Shutdown(); |
|
|
|
Destroy(this); |
|
|
|
} |
|
|
|
|
|
|
|
private void OnDestroy() |
|
|
|
{ |
|
|
|
modCalls.Destroy(); |
|
|
|
sysCalls.Destroy(); |
|
|
|
renderModule.Destroy(); |
|
|
|
systemModule.Destroy(); |
|
|
|
|
|
|
|
if (quakeParms != null) |
|
|
|
{ |
|
|
|
@ -136,48 +136,48 @@ public class UniQuake: MonoBehaviour |
|
|
|
private delegate void UniQuake_ShutdownFunc(); |
|
|
|
private UniQuake_ShutdownFunc UniQuake_Shutdown; |
|
|
|
|
|
|
|
private void LoadLibrary() |
|
|
|
{ |
|
|
|
string dllFile = Path.Combine(Application.dataPath, DllPath); |
|
|
|
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_InitFunc>("UniQuake_Init"); |
|
|
|
UniQuake_Update = LoadLibraryFunction<UniQuake_UpdateFunc>("UniQuake_Update"); |
|
|
|
UniQuake_Shutdown = LoadLibraryFunction<UniQuake_ShutdownFunc>("UniQuake_Shutdown"); |
|
|
|
private void LoadLibrary() |
|
|
|
{ |
|
|
|
string dllFile = Path.Combine(Application.dataPath, DllPath); |
|
|
|
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_InitFunc>("UniQuake_Init"); |
|
|
|
UniQuake_Update = LoadLibraryFunction<UniQuake_UpdateFunc>("UniQuake_Update"); |
|
|
|
UniQuake_Shutdown = LoadLibraryFunction<UniQuake_ShutdownFunc>("UniQuake_Shutdown"); |
|
|
|
} |
|
|
|
|
|
|
|
private TDelegate LoadLibraryFunction<TDelegate>(string name) |
|
|
|
{ |
|
|
|
IntPtr procAddress = SystemLibrary.GetProcAddress(libraryHandle, name); |
|
|
|
if (procAddress == IntPtr.Zero) |
|
|
|
{ |
|
|
|
throw new DllNotFoundException($"Could not find library function: {name}"); |
|
|
|
} |
|
|
|
|
|
|
|
return Marshal.GetDelegateForFunctionPointer<TDelegate>(procAddress); |
|
|
|
private TDelegate LoadLibraryFunction<TDelegate>(string name) |
|
|
|
{ |
|
|
|
IntPtr procAddress = SystemLibrary.GetProcAddress(libraryHandle, name); |
|
|
|
if (procAddress == IntPtr.Zero) |
|
|
|
{ |
|
|
|
throw new DllNotFoundException($"Could not find library function: {name}"); |
|
|
|
} |
|
|
|
|
|
|
|
return Marshal.GetDelegateForFunctionPointer<TDelegate>(procAddress); |
|
|
|
} |
|
|
|
|
|
|
|
private void FreeLibrary() |
|
|
|
{ |
|
|
|
if (libraryHandle != IntPtr.Zero) |
|
|
|
{ |
|
|
|
SystemLibrary.FreeLibrary(libraryHandle); |
|
|
|
libraryHandle = IntPtr.Zero; |
|
|
|
} |
|
|
|
private void FreeLibrary() |
|
|
|
{ |
|
|
|
if (libraryHandle != IntPtr.Zero) |
|
|
|
{ |
|
|
|
SystemLibrary.FreeLibrary(libraryHandle); |
|
|
|
libraryHandle = IntPtr.Zero; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public class QuakeException: Exception |
|
|
|
{ |
|
|
|
public int ExitCode { get; } |
|
|
|
|
|
|
|
public QuakeException(string message, int exitCode = 0) |
|
|
|
: base(message) |
|
|
|
{ |
|
|
|
ExitCode = exitCode; |
|
|
|
} |
|
|
|
} |
|
|
|
public class QuakeException: Exception |
|
|
|
{ |
|
|
|
public int ExitCode { get; } |
|
|
|
|
|
|
|
public QuakeException(string message, int exitCode = 0) |
|
|
|
: base(message) |
|
|
|
{ |
|
|
|
ExitCode = exitCode; |
|
|
|
} |
|
|
|
} |