You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.7 KiB
102 lines
2.7 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
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 const int MaxArgs = 50; // Corresponds with MAX_NUM_ARGVS in engine
|
|
|
|
private QuakeParms quakeParms;
|
|
private SysCalls sysCalls;
|
|
|
|
void Start()
|
|
{
|
|
string[] arguments =
|
|
{
|
|
"",
|
|
"-window",
|
|
"-width", "1440",
|
|
"-height", "1080",
|
|
};
|
|
|
|
IntPtr[] argv = new IntPtr[MaxArgs];
|
|
for (int i = 0; i < arguments.Length && i < MaxArgs; ++i)
|
|
{
|
|
argv[i] = Marshal.StringToHGlobalAnsi(arguments[i]);
|
|
}
|
|
|
|
quakeParms = new QuakeParms
|
|
{
|
|
baseDir = Application.persistentDataPath,
|
|
cacheDir = null,
|
|
argc = arguments.Length,
|
|
argv = argv,
|
|
memBase = Marshal.AllocHGlobal(MemSize),
|
|
memSize = MemSize,
|
|
};
|
|
|
|
sysCalls = new SysCalls(this);
|
|
|
|
UniQuake_Init(sysCalls.CallbacksPtr, sysCalls.SelfPtr, quakeParms);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
UniQuake_Update(Time.deltaTime);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
sysCalls.Destroy();
|
|
|
|
if (quakeParms != null)
|
|
{
|
|
if (quakeParms.memBase != IntPtr.Zero)
|
|
{
|
|
Marshal.FreeHGlobal(quakeParms.memBase);
|
|
quakeParms.memBase = IntPtr.Zero;
|
|
}
|
|
|
|
if (quakeParms.argv != null)
|
|
{
|
|
for (int i = 0; i < quakeParms.argv.Length; ++i)
|
|
{
|
|
if (quakeParms.argv[i] != IntPtr.Zero)
|
|
{
|
|
Marshal.FreeHGlobal(quakeParms.argv[i]);
|
|
quakeParms.argv[i] = IntPtr.Zero;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
|
private static extern void UniQuake_Init(IntPtr callbacks, IntPtr callbackTarget, QuakeParms parms);
|
|
|
|
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
|
|
private static extern void UniQuake_Update(float deltaTime);
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
|
private class QuakeParms
|
|
{
|
|
[MarshalAs(UnmanagedType.LPStr)]
|
|
public string baseDir;
|
|
|
|
[MarshalAs(UnmanagedType.LPStr)]
|
|
public string cacheDir;
|
|
|
|
public int argc;
|
|
|
|
[MarshalAs(UnmanagedType.LPArray, SizeConst = MaxArgs)]
|
|
public IntPtr[] argv;
|
|
|
|
public IntPtr memBase;
|
|
|
|
public int memSize;
|
|
}
|
|
}
|