Browse Source
Split all of the callback modules' native interop boilerplate code off into separate partial class files, to keep the actual logic code clean and easy to read.
console
Split all of the callback modules' native interop boilerplate code off into separate partial class files, to keep the actual logic code clean and easy to read.
console
6 changed files with 233 additions and 211 deletions
-
61Assets/Scripts/Modules/RenderModule.Interop.cs
-
3Assets/Scripts/Modules/RenderModule.Interop.cs.meta
-
60Assets/Scripts/Modules/RenderModule.cs
-
161Assets/Scripts/Modules/SystemModule.Interop.cs
-
3Assets/Scripts/Modules/SystemModule.Interop.cs.meta
-
156Assets/Scripts/Modules/SystemModule.cs
@ -0,0 +1,61 @@ |
|||||
|
using System; |
||||
|
using System.Runtime.InteropServices; |
||||
|
using AOT; |
||||
|
using UnityEngine; |
||||
|
|
||||
|
public partial class RenderModule: CallbackHandler<RenderModule> |
||||
|
{ |
||||
|
private const int MaxAliasFrames = 256; // Should match MAXALIASFRAMES
|
||||
|
|
||||
|
private void BuildCallbacks() |
||||
|
{ |
||||
|
var callbacks = new Callbacks |
||||
|
{ |
||||
|
target = TargetPtr, |
||||
|
|
||||
|
UploadAliasModel = CreateCallback<UploadAliasModelCallback>(Callback_UploadAliasModel), |
||||
|
}; |
||||
|
|
||||
|
RegisterCallbacks(callbacks); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// This matches unity_modcalls_t from mod_uniquake.c in native code.
|
||||
|
/// </summary>
|
||||
|
[StructLayout(LayoutKind.Sequential, Pack = 0)] |
||||
|
private class Callbacks |
||||
|
{ |
||||
|
public IntPtr target; |
||||
|
|
||||
|
public IntPtr UploadAliasModel; |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)] |
||||
|
private delegate int UploadAliasModelCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string name, |
||||
|
QAliasHeader header, IntPtr frames, [MarshalAs(UnmanagedType.LPArray, SizeConst = MaxAliasFrames)] |
||||
|
IntPtr[] poseVerts, IntPtr triangles, IntPtr stVerts); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(UploadAliasModelCallback))] |
||||
|
private static int Callback_UploadAliasModel(IntPtr target, string name, QAliasHeader header, IntPtr frames, |
||||
|
IntPtr[] poseVerts, IntPtr triangles, IntPtr stVerts) |
||||
|
{ |
||||
|
if (header == null) |
||||
|
{ |
||||
|
Debug.LogWarning($"Uploading invalid alias model, name = {name}"); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
if (frames != IntPtr.Zero) |
||||
|
header.frames = frames.ToStructArray<QAliasFrameDesc>(header.numFrames); |
||||
|
|
||||
|
var poseVertices = new QTriVertex[header.numFrames][]; |
||||
|
for (int i = 0; i < header.numFrames && i < MaxAliasFrames; ++i) |
||||
|
{ |
||||
|
poseVertices[i] = poseVerts[i].ToStructArray<QTriVertex>(header.numVerts); |
||||
|
} |
||||
|
|
||||
|
return GetSelf(target).UploadAliasModel(name, header, poseVertices, |
||||
|
triangles.ToStructArray<QTriangle>(header.numTriangles), |
||||
|
stVerts.ToStructArray<QSTVert>(header.numVerts)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
fileFormatVersion: 2 |
||||
|
guid: 32320d2ca59f42cc8cc141d36574d8a7 |
||||
|
timeCreated: 1618133895 |
||||
@ -0,0 +1,161 @@ |
|||||
|
using System; |
||||
|
using System.Runtime.InteropServices; |
||||
|
using AOT; |
||||
|
|
||||
|
public partial class SystemModule: CallbackHandler<SystemModule> |
||||
|
{ |
||||
|
private void BuildCallbacks() |
||||
|
{ |
||||
|
var callbacks = new Callbacks |
||||
|
{ |
||||
|
target = TargetPtr, |
||||
|
|
||||
|
SysPrint = CreateCallback<SysPrintCallback>(Callback_SysPrint), |
||||
|
SysError = CreateCallback<SysErrorCallback>(Callback_SysError), |
||||
|
SysQuit = CreateCallback<SysQuitCallback>(Callback_SysQuit), |
||||
|
SysDoubleTime = CreateCallback<SysDoubleTimeCallback>(Callback_SysDoubleTime), |
||||
|
|
||||
|
SysFileOpenRead = CreateCallback<SysFileOpenReadCallback>(Callback_SysFileOpenRead), |
||||
|
SysFileOpenWrite = CreateCallback<SysFileOpenWriteCallback>(Callback_SysFileOpenWrite), |
||||
|
SysFileClose = CreateCallback<SysFileCloseCallback>(Callback_SysFileClose), |
||||
|
SysFileSeek = CreateCallback<SysFileSeekCallback>(Callback_SysFileSeek), |
||||
|
SysFileRead = CreateCallback<SysFileReadCallback>(Callback_SysFileRead), |
||||
|
SysFileWrite = CreateCallback<SysFileWriteCallback>(Callback_SysFileWrite), |
||||
|
SysFileTime = CreateCallback<SysFileTimeCallback>(Callback_SysFileTime), |
||||
|
SysMkDir = CreateCallback<SysMkDirCallback>(Callback_SysMkDir), |
||||
|
}; |
||||
|
|
||||
|
RegisterCallbacks(callbacks); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// This matches struct unity_syscalls_s from uniquake.h in native code.
|
||||
|
/// </summary>
|
||||
|
[StructLayout(LayoutKind.Sequential, Pack = 0)] |
||||
|
private class Callbacks |
||||
|
{ |
||||
|
public IntPtr target; |
||||
|
|
||||
|
public IntPtr SysPrint; |
||||
|
public IntPtr SysError; |
||||
|
public IntPtr SysQuit; |
||||
|
public IntPtr SysDoubleTime; |
||||
|
|
||||
|
public IntPtr SysFileOpenRead; |
||||
|
public IntPtr SysFileOpenWrite; |
||||
|
public IntPtr SysFileClose; |
||||
|
public IntPtr SysFileSeek; |
||||
|
public IntPtr SysFileRead; |
||||
|
public IntPtr SysFileWrite; |
||||
|
public IntPtr SysFileTime; |
||||
|
public IntPtr SysMkDir; |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate void SysPrintCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string message); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysPrintCallback))] |
||||
|
private static void Callback_SysPrint(IntPtr target, string message) |
||||
|
{ |
||||
|
GetSelf(target).Print(message); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate void SysErrorCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string message); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysErrorCallback))] |
||||
|
private static void Callback_SysError(IntPtr target, string message) |
||||
|
{ |
||||
|
GetSelf(target).Error(message); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate void SysQuitCallback(IntPtr target); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysQuitCallback))] |
||||
|
private static void Callback_SysQuit(IntPtr target) |
||||
|
{ |
||||
|
GetSelf(target).Quit(); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate double SysDoubleTimeCallback(IntPtr target); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysDoubleTimeCallback))] |
||||
|
private static double Callback_SysDoubleTime(IntPtr target) |
||||
|
{ |
||||
|
return GetSelf(target).DoubleTime(); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate int SysFileOpenReadCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string path, out int handle); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysFileOpenReadCallback))] |
||||
|
private static int Callback_SysFileOpenRead(IntPtr target, string path, out int handle) |
||||
|
{ |
||||
|
return GetSelf(target).FileOpenRead(path, out handle); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate int SysFileOpenWriteCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string path); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysFileOpenWriteCallback))] |
||||
|
private static int Callback_SysFileOpenWrite(IntPtr target, string path) |
||||
|
{ |
||||
|
return GetSelf(target).FileOpenWrite(path); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate void SysFileCloseCallback(IntPtr target, int handle); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysFileCloseCallback))] |
||||
|
private static void Callback_SysFileClose(IntPtr target, int handle) |
||||
|
{ |
||||
|
GetSelf(target).FileClose(handle); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate void SysFileSeekCallback(IntPtr target, int handle, int position); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysFileSeekCallback))] |
||||
|
private static void Callback_SysFileSeek(IntPtr target, int handle, int position) |
||||
|
{ |
||||
|
GetSelf(target).FileSeek(handle, position); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate int SysFileReadCallback(IntPtr target, int handle, IntPtr dest, int count); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysFileReadCallback))] |
||||
|
private static int Callback_SysFileRead(IntPtr target, int handle, IntPtr dest, int count) |
||||
|
{ |
||||
|
return GetSelf(target).FileRead(handle, dest, count); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate int SysFileWriteCallback(IntPtr target, int handle, IntPtr data, int count); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysFileWriteCallback))] |
||||
|
private static int Callback_SysFileWrite(IntPtr target, int handle, IntPtr data, int count) |
||||
|
{ |
||||
|
return GetSelf(target).FileWrite(handle, data, count); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate int SysFileTimeCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string path); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysFileTimeCallback))] |
||||
|
private static int Callback_SysFileTime(IntPtr target, string path) |
||||
|
{ |
||||
|
return GetSelf(target).FileTime(path); |
||||
|
} |
||||
|
|
||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
||||
|
private delegate void SysMkDirCallback(IntPtr target, [MarshalAs(UnmanagedType.LPStr)] string path); |
||||
|
|
||||
|
[MonoPInvokeCallback(typeof(SysMkDirCallback))] |
||||
|
private static void Callback_SysMkDir(IntPtr target, string path) |
||||
|
{ |
||||
|
GetSelf(target).MkDir(path); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
fileFormatVersion: 2 |
||||
|
guid: 0a237031e45b41deaed58a305beb273f |
||||
|
timeCreated: 1618133546 |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue