Skip to content

Commit a43c381

Browse files
committed
feat(DaedalusVm): add various modding features
1. Adds support for passing execution flags to the VM 2. Adds support for enabling the symbol access trap 3. Adds support for marking symbol as using local variables (recursion support)
1 parent 9f63626 commit a43c381

File tree

7 files changed

+65
-6
lines changed

7 files changed

+65
-6
lines changed

ZenKit/DaedalusScript.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ public DaedalusSymbol(UIntPtr handle)
9696
public bool IsMerged => Native.ZkDaedalusSymbol_getIsMerged(Handle);
9797
public bool IsGenerated => Native.ZkDaedalusSymbol_getIsGenerated(Handle);
9898
public bool HasReturn => Native.ZkDaedalusSymbol_getHasReturn(Handle);
99+
100+
public bool UseAccessTrap
101+
{
102+
get => Native.ZkDaedalusSymbol_getAccessTrapEnabled(Handle);
103+
set => Native.ZkDaedalusSymbol_setAccessTrapEnabled(Handle, value);
104+
}
105+
106+
public bool UseLocalVariables
107+
{
108+
get => Native.ZkDaedalusSymbol_getLocalVariablesEnabled(Handle);
109+
set => Native.ZkDaedalusSymbol_setLocalVariablesEnabled(Handle, value);
110+
}
111+
99112
public string Name => Native.ZkDaedalusSymbol_getName(Handle).MarshalAsString() ?? string.Empty;
100113
public int Address => Native.ZkDaedalusSymbol_getAddress(Handle);
101114
public int Parent => Native.ZkDaedalusSymbol_getParent(Handle);

ZenKit/DaedalusVm.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66

77
namespace ZenKit
88
{
9+
public static class DaedalusExecutionFlags
10+
{
11+
public const byte Default = 0;
12+
public const byte AllowNullInstanceAccess = 1 << 1;
13+
public const byte IgnoreConstSpecifier = 1 << 2;
14+
}
15+
916
public class DaedalusVm : DaedalusScript
1017
{
18+
public delegate void AccessTrapCallback(DaedalusSymbol sym);
19+
1120
public delegate void ExternalDefaultFunction(DaedalusVm vm, DaedalusSymbol sym);
1221

1322
public delegate TR ExternalFunc<out TR>();
@@ -73,18 +82,21 @@ public delegate void ExternalFuncV<in TP0, in TP1, in TP2, in TP3, in TP4, in TP
7382

7483
private readonly Native.Callbacks.ZkDaedalusVmExternalDefaultCallback _nativeExternalCallbackDefault =
7584
_nativeExternalCallbackDefaultHandle;
85+
86+
private readonly Native.Callbacks.ZkDaedalusVmTrapCallback _nativeTrapCallback =
87+
_nativeTrapCallbackHandler;
7688

77-
public DaedalusVm(string path) : base(Native.ZkDaedalusVm_loadPath(path), true)
89+
public DaedalusVm(string path, byte flags = DaedalusExecutionFlags.Default) : base(Native.ZkDaedalusVm_loadPath(path, flags), true)
7890
{
7991
if (Handle == UIntPtr.Zero) throw new Exception("Failed to load DaedalusVm");
8092
}
8193

82-
public DaedalusVm(Read r) : base(Native.ZkDaedalusVm_load(r.Handle), true)
94+
public DaedalusVm(Read r, byte flags = DaedalusExecutionFlags.Default) : base(Native.ZkDaedalusVm_load(r.Handle, flags), true)
8395
{
8496
if (Handle == UIntPtr.Zero) throw new Exception("Failed to load DaedalusVm");
8597
}
8698

87-
public DaedalusVm(Vfs vfs, string name) : base(Native.ZkDaedalusVm_loadVfs(vfs.Handle, name), true)
99+
public DaedalusVm(Vfs vfs, string name, byte flags = DaedalusExecutionFlags.Default) : base(Native.ZkDaedalusVm_loadVfs(vfs.Handle, name, flags), true)
88100
{
89101
if (Handle == UIntPtr.Zero) throw new Exception("Failed to load DaedalusVm");
90102
}
@@ -350,6 +362,13 @@ public void RegisterExternalDefault(ExternalDefaultFunction cb)
350362
Native.ZkDaedalusVm_registerExternalDefault(Handle, _nativeExternalCallbackDefault,
351363
GCHandle.ToIntPtr(gcHandle));
352364
}
365+
366+
public void SetAccessTrapCallback(AccessTrapCallback cb)
367+
{
368+
var gcHandle = GCHandle.Alloc(cb);
369+
_externalCallbacks.Add(gcHandle);
370+
Native.ZkDaedalusVm_setAccessTrapCallback(Handle, _nativeTrapCallback, GCHandle.ToIntPtr(gcHandle));
371+
}
353372

354373
public void RegisterExternal<TR>(string name, ExternalFunc<TR> cb)
355374
{
@@ -1023,6 +1042,14 @@ private static void _nativeExternalCallbackDefaultHandle(IntPtr ctx, UIntPtr vm,
10231042
cb(new DaedalusVm(vm), new DaedalusSymbol(sym));
10241043
}
10251044

1045+
[MonoPInvokeCallback]
1046+
private static void _nativeTrapCallbackHandler(IntPtr ctx, UIntPtr sym)
1047+
{
1048+
var gcHandle = GCHandle.FromIntPtr(ctx);
1049+
var cb = (AccessTrapCallback)gcHandle.Target;
1050+
cb(new DaedalusSymbol(sym));
1051+
}
1052+
10261053
private delegate void ExternalFunc(DaedalusVm vm);
10271054
}
10281055
}

ZenKit/Native.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4189,7 +4189,19 @@ public static extern void ZkDaedalusScript_enumerateInstanceSymbols(UIntPtr slf,
41894189

41904190
[DllImport(DllName)]
41914191
public static extern bool ZkDaedalusSymbol_getHasReturn(UIntPtr slf);
4192+
4193+
[DllImport(DllName)]
4194+
public static extern void ZkDaedalusSymbol_setAccessTrapEnabled(UIntPtr slf, bool enable);
4195+
4196+
[DllImport(DllName)]
4197+
public static extern bool ZkDaedalusSymbol_getAccessTrapEnabled(UIntPtr slf);
41924198

4199+
[DllImport(DllName)]
4200+
public static extern void ZkDaedalusSymbol_setLocalVariablesEnabled(UIntPtr slf, bool enable);
4201+
4202+
[DllImport(DllName)]
4203+
public static extern bool ZkDaedalusSymbol_getLocalVariablesEnabled(UIntPtr slf);
4204+
41934205
[DllImport(DllName)]
41944206
public static extern IntPtr ZkDaedalusSymbol_getName(UIntPtr slf);
41954207

@@ -4227,13 +4239,13 @@ public static extern void ZkDaedalusScript_enumerateInstanceSymbols(UIntPtr slf,
42274239
public static extern void ZkDaedalusInstance_setUserPointer(UIntPtr slf, IntPtr ptr);
42284240

42294241
[DllImport(DllName)]
4230-
public static extern UIntPtr ZkDaedalusVm_load(UIntPtr buf);
4242+
public static extern UIntPtr ZkDaedalusVm_load(UIntPtr buf, byte flags);
42314243

42324244
[DllImport(DllName)]
4233-
public static extern UIntPtr ZkDaedalusVm_loadPath([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(GothicStringMarshaller))] string path);
4245+
public static extern UIntPtr ZkDaedalusVm_loadPath([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(GothicStringMarshaller))] string path, byte flags);
42344246

42354247
[DllImport(DllName)]
4236-
public static extern UIntPtr ZkDaedalusVm_loadVfs(UIntPtr vfs, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(GothicStringMarshaller))] string name);
4248+
public static extern UIntPtr ZkDaedalusVm_loadVfs(UIntPtr vfs, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(GothicStringMarshaller))] string name, byte flags);
42374249

42384250
[DllImport(DllName)]
42394251
public static extern void ZkDaedalusVm_del(UIntPtr slf);
@@ -4320,6 +4332,10 @@ public static extern void ZkDaedalusVm_registerExternalDefault(UIntPtr slf,
43204332
ZkDaedalusVmExternalDefaultCallback cb,
43214333
IntPtr ctx);
43224334

4335+
[DllImport(DllName)]
4336+
public static extern void ZkDaedalusVm_setAccessTrapCallback(UIntPtr slf, ZkDaedalusVmTrapCallback cb,
4337+
IntPtr ctx);
4338+
43234339
[DllImport(DllName)]
43244340
public static extern void ZkDaedalusVm_printStackTrace(UIntPtr slf);
43254341

@@ -7899,6 +7915,9 @@ public class Callbacks
78997915

79007916
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
79017917
public delegate void ZkDaedalusVmExternalDefaultCallback(IntPtr ctx, UIntPtr vm, UIntPtr sym);
7918+
7919+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
7920+
public delegate void ZkDaedalusVmTrapCallback(IntPtr ctx, UIntPtr sym);
79027921

79037922
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
79047923
public delegate void ZkLogger(IntPtr ctx, LogLevel lvl, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(GothicStringMarshaller))] string name, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(GothicStringMarshaller))] string message);
5.32 KB
Binary file not shown.

ZenKit/runtimes/linux-x64/native/libzenkitcapi.so

100644100755
8.73 KB
Binary file not shown.

ZenKit/runtimes/osx-x64/native/libzenkitcapi.dylib

100644100755
2.36 KB
Binary file not shown.
8.32 KB
Binary file not shown.

0 commit comments

Comments
 (0)