Skip to content

Commit

Permalink
fix(*): fix Unity compatibility
Browse files Browse the repository at this point in the history
Removes most callback-based C-APIs and updates the remaining ones for compatibility with Unity.
  • Loading branch information
lmichaelis committed Jan 4, 2024
1 parent 5041edf commit bbd9e09
Show file tree
Hide file tree
Showing 31 changed files with 268 additions and 767 deletions.
9 changes: 2 additions & 7 deletions ZenKit/Boxes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,8 @@ public List<IOrientedBoundingBox> Children
get
{
var children = new List<IOrientedBoundingBox>();

Native.ZkOrientedBoundingBox_enumerateChildren(_handle, (_, box) =>
{
children.Add(new OrientedBoundingBox(box));
return false;
}, UIntPtr.Zero);

var count = ChildCount;
for (var i = 0;i < count; ++i) children.Add(GetChild(i));
return children;
}
}
Expand Down
26 changes: 5 additions & 21 deletions ZenKit/BspTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,7 @@ public List<Vector3> LightPoints
get
{
var points = new List<Vector3>();

Native.ZkBspTree_enumerateLightPoints(_handle, (_, v) =>
{
points.Add(v);
return false;
}, UIntPtr.Zero);

for (var i = 0;i < LightPointCount; ++i) points.Add(GetLightPoint(i));
return points;
}
}
Expand All @@ -205,13 +199,8 @@ public List<BspNode> Nodes
get
{
var nodes = new List<BspNode>();

Native.ZkBspTree_enumerateNodes(_handle, (_, node) =>
{
nodes.Add(Marshal.PtrToStructure<BspNode>(node));
return false;
}, UIntPtr.Zero);

var count = NodeCount;
for (var i = 0;i < count; ++i) nodes.Add(GetNode(i));
return nodes;
}
}
Expand All @@ -223,13 +212,8 @@ public List<IBspSector> Sectors
get
{
var sectors = new List<IBspSector>();

Native.ZkBspTree_enumerateSectors(_handle, (_, sector) =>
{
sectors.Add(new BspSector(sector));
return false;
}, UIntPtr.Zero);

var count = SectorCount;
for (var i = 0;i < count; ++i) sectors.Add(GetSector(i));
return sectors;
}
}
Expand Down
11 changes: 4 additions & 7 deletions ZenKit/CutsceneLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using ZenKit.Util;

namespace ZenKit
Expand Down Expand Up @@ -179,13 +180,9 @@ public List<ICutsceneBlock> Blocks
get
{
var blocks = new List<ICutsceneBlock>();

Native.ZkCutsceneLibrary_enumerateBlocks(_handle, (_, block) =>
{
blocks.Add(new CutsceneBlock(block));
return false;
}, UIntPtr.Zero);

var count =(int)Native.ZkCutsceneLibrary_getBlockCount(_handle);
for (var i = 0; i < count; ++i)
blocks.Add(new CutsceneBlock(Native.ZkCutsceneLibrary_getBlockByIndex(_handle, (ulong)i)));
return blocks;
}
}
Expand Down
28 changes: 16 additions & 12 deletions ZenKit/DaedalusScript.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ZenKit.Util;

namespace ZenKit
{
Expand Down Expand Up @@ -172,13 +173,7 @@ public List<DaedalusSymbol> Symbols
get
{
var symbols = new List<DaedalusSymbol>();

Native.ZkDaedalusScript_enumerateSymbols(Handle, (_, symbol) =>
{
symbols.Add(new DaedalusSymbol(symbol));
return false;
}, UIntPtr.Zero);

for (var i = 0;i < SymbolCount; ++i) symbols.Add(GetSymbolByIndex(i)!);
return symbols;
}
}
Expand All @@ -197,11 +192,9 @@ public List<DaedalusSymbol> GetInstanceSymbols(string className)
{
var symbols = new List<DaedalusSymbol>();

Native.ZkDaedalusScript_enumerateInstanceSymbols(Handle, className, (_, symbol) =>
{
symbols.Add(new DaedalusSymbol(symbol));
return false;
}, UIntPtr.Zero);
var gch = GCHandle.Alloc(symbols);
Native.ZkDaedalusScript_enumerateInstanceSymbols(Handle, className, InstanceSymbolsEnumerator, GCHandle.ToIntPtr(gch));
gch.Free();

return symbols;
}
Expand All @@ -228,5 +221,16 @@ public DaedalusInstruction GetInstruction(int address)
var sym = Native.ZkDaedalusScript_getSymbolByName(Handle, name);
return sym == UIntPtr.Zero ? null : new DaedalusSymbol(sym);
}


private static readonly Native.Callbacks.ZkDaedalusSymbolEnumerator InstanceSymbolsEnumerator = _enumerateInstanceSymbolsHandler;

[MonoPInvokeCallback]
private static bool _enumerateInstanceSymbolsHandler(IntPtr ctx, UIntPtr ptr)
{
var list = (List<DaedalusSymbol>)GCHandle.FromIntPtr(ctx).Target;
list.Add(new DaedalusSymbol(ptr));
return false;
}
}
}
41 changes: 32 additions & 9 deletions ZenKit/DaedalusVm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ZenKit.Daedalus;
using ZenKit.Util;

namespace ZenKit
{
Expand Down Expand Up @@ -65,10 +66,13 @@ public delegate void ExternalFuncV<in TP0, in TP1, in TP2, in TP3, in TP4, in TP
public delegate void ExternalFuncV<in TP0, in TP1, in TP2, in TP3, in TP4, in TP5, in TP6, in TP7, in TP8,
in TP9>(TP0 p0, TP1 p1, TP2 p2, TP3 p3, TP4 p4, TP5 p5, TP6 p6, TP7 p7, TP8 p8, TP9 p9);

private readonly List<Native.Callbacks.ZkDaedalusVmExternalCallback> _externalCallbacks =
new List<Native.Callbacks.ZkDaedalusVmExternalCallback>();
private readonly List<GCHandle> _externalCallbacks = new List<GCHandle>();

private Native.Callbacks.ZkDaedalusVmExternalDefaultCallback? _externalDefaultCallback;
private readonly Native.Callbacks.ZkDaedalusVmExternalCallback _nativeExternalCallback =
_nativeExternalCallbackHandler;

private readonly Native.Callbacks.ZkDaedalusVmExternalDefaultCallback _nativeExternalCallbackDefault =
_nativeExternalCallbackDefaultHandle;

public DaedalusVm(string path) : base(Native.ZkDaedalusVm_loadPath(path), true)
{
Expand Down Expand Up @@ -121,6 +125,7 @@ public DaedalusInstance? GlobalItem

protected override void Delete()
{
_externalCallbacks.ForEach(handle => handle.Free());
Native.ZkDaedalusVm_del(Handle);
}

Expand Down Expand Up @@ -340,8 +345,10 @@ public void Call<TP0, TP1, TP2, TP3>(int symId, TP0 p0, TP1 p1, TP2 p2, TP3 p3)

public void RegisterExternalDefault(ExternalDefaultFunction cb)
{
_externalDefaultCallback = (_0, vm, sym) => cb(new DaedalusVm(vm), new DaedalusSymbol(sym));
Native.ZkDaedalusVm_registerExternalDefault(Handle, _externalDefaultCallback, UIntPtr.Zero);
var gcHandle = GCHandle.Alloc(cb);
_externalCallbacks.Add(gcHandle);
Native.ZkDaedalusVm_registerExternalDefault(Handle, _nativeExternalCallbackDefault,
GCHandle.ToIntPtr(gcHandle));
}

public void RegisterExternal<TR>(string name, ExternalFunc<TR> cb)
Expand Down Expand Up @@ -682,12 +689,28 @@ private void RegisterExternalUnsafe(string name, ExternalFunc cb)

private void RegisterExternalUnsafe(DaedalusSymbol sym, ExternalFunc cb)
{
Native.Callbacks.ZkDaedalusVmExternalCallback handle = (_, vm) => cb(new DaedalusVm(vm));
_externalCallbacks.Add(handle);

Native.ZkDaedalusVm_registerExternal(Handle, sym.Handle, handle, UIntPtr.Zero);
var gcHandle = GCHandle.Alloc(cb);
_externalCallbacks.Add(gcHandle);
Native.ZkDaedalusVm_registerExternal(Handle, sym.Handle, _nativeExternalCallback,
GCHandle.ToIntPtr(gcHandle));
}

private delegate void ExternalFunc(DaedalusVm vm);

[MonoPInvokeCallback]
private static void _nativeExternalCallbackHandler(IntPtr ctx, UIntPtr vm)
{
var gcHandle = GCHandle.FromIntPtr(ctx);
var cb = (ExternalFunc)gcHandle.Target;
cb(new DaedalusVm(vm));
}

[MonoPInvokeCallback]
private static void _nativeExternalCallbackDefaultHandle(IntPtr ctx, UIntPtr vm, UIntPtr sym)
{
var gcHandle = GCHandle.FromIntPtr(ctx);
var cb = (ExternalDefaultFunction)gcHandle.Target;
cb(new DaedalusVm(vm), new DaedalusSymbol(sym));
}
}
}
9 changes: 2 additions & 7 deletions ZenKit/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,8 @@ public List<FontGlyph> Glyphs
get
{
var glyphs = new List<FontGlyph>(GlyphCount);

Native.ZkFont_enumerateGlyphs(_handle, (_, glyph) =>
{
glyphs.Add(Marshal.PtrToStructure<FontGlyph>(glyph));
return false;
}, UIntPtr.Zero);

var count = GlyphCount;
for (var i = 0;i < count; ++i) glyphs.Add(GetGlyph(i));
return glyphs;
}
}
Expand Down
22 changes: 18 additions & 4 deletions ZenKit/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ZenKit.Util;
using ZenKit.Vobs;

namespace ZenKit
{
Expand All @@ -16,13 +19,16 @@ public static class Logger
{
public delegate void Callback(LogLevel level, string name, string message);

private static readonly List<Native.Callbacks.ZkLogger> _callbacks = new List<Native.Callbacks.ZkLogger>();
private static GCHandle? _handler = null;
private static readonly Native.Callbacks.ZkLogger NativeHandler = _nativeCallbackHandler;

public static void Set(LogLevel lvl, Callback callback)
{
Native.Callbacks.ZkLogger cb = (_, level, name, message) => callback(level, name, message);
_callbacks.Add(cb);
Native.ZkLogger_set(lvl, cb, UIntPtr.Zero);
var handler = GCHandle.Alloc(callback);
Native.ZkLogger_set(lvl, NativeHandler, GCHandle.ToIntPtr(handler));

_handler?.Free();
_handler = handler;
}

public static void SetDefault(LogLevel level)
Expand All @@ -34,5 +40,13 @@ public static void Log(LogLevel level, string name, string message)
{
Native.ZkLogger_log(level, name, message);
}

[MonoPInvokeCallback]
private static void _nativeCallbackHandler(IntPtr ctx, LogLevel level, string name, string message)
{
var gcHandle = GCHandle.FromIntPtr(ctx);
var cb = (Callback)gcHandle.Target;
cb(level, name, message);
}
}
}
45 changes: 10 additions & 35 deletions ZenKit/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,8 @@ public List<IMaterial> Materials
get
{
var materials = new List<IMaterial>();

Native.ZkMesh_enumerateMaterials(_handle, (_, material) =>
{
materials.Add(new Material(material));
return false;
}, UIntPtr.Zero);

var count = MaterialCount;
for (var i = 0;i < count; ++i) materials.Add(GetMaterial(i));
return materials;
}
}
Expand All @@ -321,13 +316,8 @@ public List<Vector3> Positions
get
{
var positions = new List<Vector3>();

Native.ZkMesh_enumeratePositions(_handle, (_, v) =>
{
positions.Add(v);
return false;
}, UIntPtr.Zero);

var count = PositionCount;
for (var i = 0;i < count; ++i) positions.Add(GetPosition(i));
return positions;
}
}
Expand All @@ -339,13 +329,8 @@ public List<Vertex> Features
get
{
var features = new List<Vertex>();

Native.ZkMesh_enumerateVertices(_handle, (_, v) =>
{
features.Add(Marshal.PtrToStructure<Vertex>(v));
return false;
}, UIntPtr.Zero);

var count = FeatureCount;
for (var i = 0;i < count; ++i) features.Add(GetFeature(i));
return features;
}
}
Expand All @@ -357,13 +342,8 @@ public List<ILightMap> LightMap
get
{
var lightMaps = new List<ILightMap>();

Native.ZkMesh_enumerateLightMaps(_handle, (_, lm) =>
{
lightMaps.Add(new LightMap(lm));
return false;
}, UIntPtr.Zero);

var count = LightMapCount;
for (var i = 0;i < count; ++i) lightMaps.Add(GetLightMap(i));
return lightMaps;
}
}
Expand All @@ -375,13 +355,8 @@ public List<IPolygon> Polygons
get
{
var polygons = new List<IPolygon>();

Native.ZkMesh_enumeratePolygons(_handle, (_, poly) =>
{
polygons.Add(new Polygon(poly));
return false;
}, UIntPtr.Zero);

var count = PolygonCount;
for (var i = 0;i < count; ++i) polygons.Add(GetPolygon(i));
return polygons;
}
}
Expand Down
9 changes: 2 additions & 7 deletions ZenKit/ModelAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,8 @@ public List<AnimationSample> Samples
get
{
var samples = new List<AnimationSample>();

Native.ZkModelAnimation_enumerateSamples(_handle, (_, sample) =>
{
samples.Add(Marshal.PtrToStructure<AnimationSample>(sample));
return false;
}, UIntPtr.Zero);

var count = SampleCount;
for (var i = 0;i < count; ++i) samples.Add(GetSample(i));
return samples;
}
}
Expand Down
9 changes: 2 additions & 7 deletions ZenKit/ModelHierarchy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,8 @@ public List<IModelHierarchyNode> Nodes
get
{
var nodes = new List<IModelHierarchyNode>();

Native.ZkModelHierarchy_enumerateNodes(_handle, (_, node) =>
{
nodes.Add(Marshal.PtrToStructure<ModelHierarchyNode>(node));
return false;
}, UIntPtr.Zero);

var count = NodeCount;
for (var i = 0;i < count; ++i) nodes.Add(GetNode(i));
return nodes;
}
}
Expand Down
Loading

0 comments on commit bbd9e09

Please sign in to comment.