Browse Source

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
console
Nico de Poel 5 years ago
parent
commit
e416868650
  1. 4
      Assets/Scripts/CallbackHandler.cs
  2. 2
      Assets/Scripts/Modules/RenderModule.cs
  3. 16
      Assets/Scripts/Modules/SystemModule.cs
  4. 21
      Assets/Scripts/QuakeParms.cs
  5. 14
      Assets/Scripts/UniQuake.cs

4
Assets/Scripts/CallbackHandler.cs

@ -8,9 +8,9 @@ public abstract class CallbackHandler<THandler>
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()
{

2
Assets/Scripts/Modules/RenderModule.cs

@ -17,7 +17,7 @@ public class RenderModule : CallbackHandler<RenderModule>
var callbacks = new Callbacks
{
target = SelfPtr,
target = TargetPtr,
UploadAliasModel = CreateCallback<UploadAliasModelCallback>(Callback_UploadAliasModel),
};

16
Assets/Scripts/Modules/SystemModule.cs

@ -18,12 +18,12 @@ public class SystemModule: CallbackHandler<SystemModule>
var callbacks = new Callbacks
{
target = SelfPtr,
target = TargetPtr,
SysPrint = CreateCallback<SysPrintCallback>(Callback_SysPrint),
SysError = CreateCallback<SysErrorCallback>(Callback_SysError),
SysQuit = CreateCallback<SysQuitCallback>(Callback_SysQuit),
SysFloatTime = CreateCallback<SysFloatTimeCallback>(Callback_SysFloatTime),
SysDoubleTime = CreateCallback<SysDoubleTimeCallback>(Callback_SysDoubleTime),
SysFileOpenRead = CreateCallback<SysFileOpenReadCallback>(Callback_SysFileOpenRead),
SysFileOpenWrite = CreateCallback<SysFileOpenWriteCallback>(Callback_SysFileOpenWrite),
@ -60,7 +60,7 @@ public class SystemModule: CallbackHandler<SystemModule>
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<SystemModule>
}
[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;
}

21
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<QuakeParms>());
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);

14
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_InitFunc>("UniQuake_Init");
UniQuake_Update = LoadLibraryFunction<UniQuake_UpdateFunc>("UniQuake_Update");
UniQuake_Shutdown = LoadLibraryFunction<UniQuake_ShutdownFunc>("UniQuake_Shutdown");
UniQuake_SetFmodSystem = LoadLibraryFunction<UniQuake_SetFmodSystemFunc>("UniQuake_SetFmodSystem");
// UniQuake_SetFmodSystem = LoadLibraryFunction<UniQuake_SetFmodSystemFunc>("UniQuake_SetFmodSystem");
}
private TDelegate LoadLibraryFunction<TDelegate>(string functionName)

Loading…
Cancel
Save