Skip to content

Commit

Permalink
fix(DaedalusInstance): make FromNative nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichaelis committed Nov 12, 2023
1 parent 7cd2480 commit cda712e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
6 changes: 4 additions & 2 deletions ZenKit/DaedalusInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum DaedalusInstanceType
FightAi = 18,
SoundEffect = 19,
SoundSystem = 20,
Invalid = 20
Invalid = 21
}

namespace Materialized
Expand Down Expand Up @@ -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),
Expand Down
26 changes: 13 additions & 13 deletions ZenKit/DaedalusVm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<TR>(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!");
Expand Down

0 comments on commit cda712e

Please sign in to comment.