From cda712e00e4465f66673461bd4d83ab1821bf92a Mon Sep 17 00:00:00 2001 From: Luis Michaelis Date: Sun, 12 Nov 2023 13:07:24 +0100 Subject: [PATCH] fix(DaedalusInstance): make `FromNative` nullable --- ZenKit/DaedalusInstance.cs | 6 ++++-- ZenKit/DaedalusVm.cs | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/ZenKit/DaedalusInstance.cs b/ZenKit/DaedalusInstance.cs index 09bdb19..7d7a4eb 100644 --- a/ZenKit/DaedalusInstance.cs +++ b/ZenKit/DaedalusInstance.cs @@ -28,7 +28,7 @@ public enum DaedalusInstanceType FightAi = 18, SoundEffect = 19, SoundSystem = 20, - Invalid = 20 + Invalid = 21 } namespace Materialized @@ -62,8 +62,10 @@ public Materialized.DaedalusInstance Materialize() }; } - public static DaedalusInstance FromNative(UIntPtr handle) + public static DaedalusInstance? FromNative(UIntPtr handle) { + if (handle == UIntPtr.Zero) return null; + return Native.ZkDaedalusInstance_getType(handle) switch { DaedalusInstanceType.GuildValues => new GuildValuesInstance(handle), diff --git a/ZenKit/DaedalusVm.cs b/ZenKit/DaedalusVm.cs index d74fc3b..ee06487 100644 --- a/ZenKit/DaedalusVm.cs +++ b/ZenKit/DaedalusVm.cs @@ -47,34 +47,34 @@ public DaedalusVm(Vfs vfs, string name) : base(Native.ZkDaedalusVm_loadVfs(vfs.H if (Handle == UIntPtr.Zero) throw new Exception("Failed to load DaedalusVm"); } - public DaedalusInstance GlobalSelf + public DaedalusInstance? GlobalSelf { get => DaedalusInstance.FromNative(Native.ZkDaedalusVm_getGlobalSelf(Handle)); - set => Native.ZkDaedalusVm_setGlobalSelf(Handle, value.Handle); + set => Native.ZkDaedalusVm_setGlobalSelf(Handle, value?.Handle ?? UIntPtr.Zero); } - public DaedalusInstance GlobalOther + public DaedalusInstance? GlobalOther { get => DaedalusInstance.FromNative(Native.ZkDaedalusVm_getGlobalOther(Handle)); - set => Native.ZkDaedalusVm_setGlobalOther(Handle, value.Handle); + set => Native.ZkDaedalusVm_setGlobalOther(Handle, value?.Handle ?? UIntPtr.Zero); } - public DaedalusInstance GlobalVictim + public DaedalusInstance? GlobalVictim { get => DaedalusInstance.FromNative(Native.ZkDaedalusVm_getGlobalVictim(Handle)); - set => Native.ZkDaedalusVm_setGlobalVictim(Handle, value.Handle); + set => Native.ZkDaedalusVm_setGlobalVictim(Handle, value?.Handle ?? UIntPtr.Zero); } - public DaedalusInstance GlobalHero + public DaedalusInstance? GlobalHero { get => DaedalusInstance.FromNative(Native.ZkDaedalusVm_getGlobalHero(Handle)); - set => Native.ZkDaedalusVm_setGlobalHero(Handle, value.Handle); + set => Native.ZkDaedalusVm_setGlobalHero(Handle, value?.Handle ?? UIntPtr.Zero); } - public DaedalusInstance GlobalItem + public DaedalusInstance? GlobalItem { get => DaedalusInstance.FromNative(Native.ZkDaedalusVm_getGlobalItem(Handle)); - set => Native.ZkDaedalusVm_setGlobalItem(Handle, value.Handle); + set => Native.ZkDaedalusVm_setGlobalItem(Handle, value?.Handle ?? UIntPtr.Zero); } protected override void Delete() @@ -96,20 +96,20 @@ public DaedalusInstance InitInstance(string symbolName, DaedalusInstanceType typ public DaedalusInstance InitInstance(DaedalusSymbol symbol, DaedalusInstanceType type) { - return DaedalusInstance.FromNative(Native.ZkDaedalusVm_initInstance(Handle, symbol.Handle, type)); + return DaedalusInstance.FromNative(Native.ZkDaedalusVm_initInstance(Handle, symbol.Handle, type)) ?? throw new InvalidOperationException(); } public void Call(string name) { var sym = GetSymbolByName(name); - if (sym == null) throw new Exception("Symbol not found"); + if (!(sym is { Type: DaedalusDataType.Function })) throw new Exception("Symbol not found"); Native.ZkDaedalusVm_callFunction(Handle, sym.Handle); } public TR Call(string name) { var sym = GetSymbolByName(name); - if (sym == null) throw new Exception("Symbol not found"); + if (!(sym is { Type: DaedalusDataType.Function })) throw new Exception("Symbol not found"); Native.ZkDaedalusVm_callFunction(Handle, sym.Handle); if (!sym.HasReturn) throw new InvalidOperationException("The function does not return anything!");