Skip to content

Commit bd36d6b

Browse files
committed
feat(*): partially implement materialization API
1 parent 606a5a3 commit bd36d6b

22 files changed

Lines changed: 1285 additions & 82 deletions

ZenKit/Boxes.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@
22
using System.Collections.Generic;
33
using System.Numerics;
44
using System.Runtime.InteropServices;
5+
using ZenKit.Util;
56

67
namespace ZenKit
78
{
9+
[Serializable]
810
[StructLayout(LayoutKind.Sequential)]
911
public struct AxisAlignedBoundingBox
1012
{
1113
public Vector3 Min;
1214
public Vector3 Max;
1315
}
1416

15-
public class OrientedBoundingBox
17+
namespace Materialized
18+
{
19+
[Serializable]
20+
public struct OrientedBoundingBox
21+
{
22+
public Vector3 Center;
23+
public Tuple<Vector3, Vector3, Vector3> Axes;
24+
public Vector3 HalfWidth;
25+
public List<OrientedBoundingBox> Children;
26+
}
27+
}
28+
29+
/// <summary>
30+
/// The interface to native oriented bounding boxes.
31+
/// </summary>
32+
public class OrientedBoundingBox : IMaterializing<Materialized.OrientedBoundingBox>
1633
{
1734
private readonly UIntPtr _handle;
1835

@@ -48,6 +65,22 @@ public List<OrientedBoundingBox> Children
4865
}
4966
}
5067

68+
/// <summary>
69+
/// Fully loads this native object into a C# serializable object, disassociated
70+
/// from the underlying native implementation.
71+
/// </summary>
72+
/// <returns>This native object in a pure C# representation.</returns>
73+
public Materialized.OrientedBoundingBox Materialize()
74+
{
75+
return new Materialized.OrientedBoundingBox
76+
{
77+
Center = Center,
78+
HalfWidth = HalfWidth,
79+
Axes = Axes,
80+
Children = Children.ConvertAll(obb => obb.Materialize())
81+
};
82+
}
83+
5184
public OrientedBoundingBox GetChild(ulong i)
5285
{
5386
var handle = Native.ZkOrientedBoundingBox_getChild(_handle, i);

ZenKit/BspTree.cs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
using System.Collections.Generic;
33
using System.Numerics;
44
using System.Runtime.InteropServices;
5+
using ZenKit.Util;
56

67
namespace ZenKit
78
{
9+
[Serializable]
810
public enum BspTreeType
911
{
1012
Indoor = 0,
1113
Outdoor = 1
1214
}
1315

16+
[Serializable]
1417
[StructLayout(LayoutKind.Sequential, Size = 60)]
1518
public struct BspNode
1619
{
@@ -23,7 +26,31 @@ public struct BspNode
2326
public int ParentIndex;
2427
}
2528

26-
public class BspSector
29+
namespace Materialized
30+
{
31+
[Serializable]
32+
public struct BspSector
33+
{
34+
public string Name;
35+
public uint[] NodeIndices;
36+
public uint[] PortalPolygonIndices;
37+
}
38+
39+
[Serializable]
40+
public struct BspTree
41+
{
42+
public BspTreeType Type;
43+
public uint[] PolygonIndices;
44+
public uint[] LeafPolygonIndices;
45+
public uint[] PortalPolygonIndices;
46+
public Vector3[] LightPoints;
47+
public ulong[] LeafNodeIndices;
48+
public BspNode[] Nodes;
49+
public List<BspSector> Sectors;
50+
}
51+
}
52+
53+
public class BspSector : IMaterializing<Materialized.BspSector>
2754
{
2855
private readonly UIntPtr _handle;
2956

@@ -35,13 +62,29 @@ internal BspSector(UIntPtr handle)
3562
public string Name => Native.ZkBspSector_getName(_handle).MarshalAsString() ??
3663
throw new Exception("Failed to load bsp sector name");
3764

38-
public uint[] NodeIndices => Native.ZkBspSector_getNodeIndices(_handle, out var count).MarshalAsArray<uint>(count);
65+
public uint[] NodeIndices =>
66+
Native.ZkBspSector_getNodeIndices(_handle, out var count).MarshalAsArray<uint>(count);
3967

4068
public uint[] PortalPolygonIndices =>
4169
Native.ZkBspSector_getPortalPolygonIndices(_handle, out var count).MarshalAsArray<uint>(count);
70+
71+
/// <summary>
72+
/// Fully loads this native object into a C# serializable object, disassociated
73+
/// from the underlying native implementation.
74+
/// </summary>
75+
/// <returns>This native object in a pure C# representation.</returns>
76+
public Materialized.BspSector Materialize()
77+
{
78+
return new Materialized.BspSector
79+
{
80+
Name = Name,
81+
NodeIndices = NodeIndices,
82+
PortalPolygonIndices = PortalPolygonIndices
83+
};
84+
}
4285
}
4386

44-
public class BspTree
87+
public class BspTree : IMaterializing<Materialized.BspTree>
4588
{
4689
private readonly UIntPtr _handle;
4790

@@ -86,6 +129,26 @@ public List<BspSector> Sectors
86129
}
87130
}
88131

132+
/// <summary>
133+
/// Fully loads this native object into a C# serializable object, disassociated
134+
/// from the underlying native implementation.
135+
/// </summary>
136+
/// <returns>This native object in a pure C# representation.</returns>
137+
public Materialized.BspTree Materialize()
138+
{
139+
return new Materialized.BspTree
140+
{
141+
Type = Type,
142+
PolygonIndices = PolygonIndices,
143+
LeafPolygonIndices = LeafPolygonIndices,
144+
PortalPolygonIndices = PortalPolygonIndices,
145+
LightPoints = LightPoints,
146+
LeafNodeIndices = LeafNodeIndices,
147+
Nodes = Nodes,
148+
Sectors = Sectors.ConvertAll(sector => sector.Materialize())
149+
};
150+
}
151+
89152
public BspSector GetSector(ulong i)
90153
{
91154
return new BspSector(Native.ZkBspTree_getSector(_handle, i));

ZenKit/CutsceneLibrary.cs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
using System;
22
using System.Collections.Generic;
3+
using ZenKit.Util;
34

45
namespace ZenKit
56
{
6-
public class CutsceneMessage
7+
namespace Materialized
8+
{
9+
[Serializable]
10+
public struct CutsceneMessage
11+
{
12+
public uint Type;
13+
public string Text;
14+
public string Name;
15+
}
16+
17+
[Serializable]
18+
public struct CutsceneBlock
19+
{
20+
public string Name;
21+
public CutsceneMessage Message;
22+
}
23+
24+
[Serializable]
25+
public struct CutsceneLibrary
26+
{
27+
public List<CutsceneBlock> Blocks;
28+
}
29+
}
30+
31+
public class CutsceneMessage : IMaterializing<Materialized.CutsceneMessage>
732
{
833
private readonly UIntPtr _handle;
934

@@ -19,9 +44,19 @@ internal CutsceneMessage(UIntPtr handle)
1944

2045
public string Name => Native.ZkCutsceneMessage_getName(_handle).MarshalAsString() ??
2146
throw new Exception("Failed to get cutscene message name");
47+
48+
public Materialized.CutsceneMessage Materialize()
49+
{
50+
return new Materialized.CutsceneMessage
51+
{
52+
Type = Type,
53+
Text = Text,
54+
Name = Name
55+
};
56+
}
2257
}
2358

24-
public class CutsceneBlock
59+
public class CutsceneBlock : IMaterializing<Materialized.CutsceneBlock>
2560
{
2661
private readonly UIntPtr _handle;
2762

@@ -43,9 +78,18 @@ public CutsceneMessage Message
4378
: throw new Exception("Failed to load cutscene block message");
4479
}
4580
}
81+
82+
public Materialized.CutsceneBlock Materialize()
83+
{
84+
return new Materialized.CutsceneBlock
85+
{
86+
Name = Name,
87+
Message = Message.Materialize()
88+
};
89+
}
4690
}
4791

48-
public class CutsceneLibrary
92+
public class CutsceneLibrary : IMaterializing<Materialized.CutsceneLibrary>
4993
{
5094
private readonly UIntPtr _handle;
5195

@@ -83,6 +127,14 @@ public List<CutsceneBlock> Blocks
83127
}
84128
}
85129

130+
public Materialized.CutsceneLibrary Materialize()
131+
{
132+
return new Materialized.CutsceneLibrary
133+
{
134+
Blocks = Blocks.ConvertAll(block => block.Materialize())
135+
};
136+
}
137+
86138
~CutsceneLibrary()
87139
{
88140
Native.ZkCutsceneLibrary_del(_handle);

ZenKit/DaedalusInstance.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System;
22
using ZenKit.Daedalus;
3+
using ZenKit.Util;
34

45
namespace ZenKit
56
{
6-
public enum DaedalusInstanceType {
7+
[Serializable]
8+
public enum DaedalusInstanceType
9+
{
710
GuildValues = 0,
811
Npc = 1,
912
Mission = 2,
@@ -25,12 +28,22 @@ public enum DaedalusInstanceType {
2528
FightAi = 18,
2629
SoundEffect = 19,
2730
SoundSystem = 20,
28-
Invalid = 20,
31+
Invalid = 20
32+
}
33+
34+
namespace Materialized
35+
{
36+
[Serializable]
37+
public struct DaedalusInstance
38+
{
39+
public DaedalusInstanceType Type;
40+
public uint Index;
41+
}
2942
}
3043

31-
public class DaedalusInstance
44+
public class DaedalusInstance : IMaterializing<Materialized.DaedalusInstance>
3245
{
33-
public DaedalusInstance(UIntPtr handle)
46+
protected DaedalusInstance(UIntPtr handle)
3447
{
3548
Handle = handle;
3649
}
@@ -40,6 +53,15 @@ public DaedalusInstance(UIntPtr handle)
4053
public DaedalusInstanceType Type => Native.ZkDaedalusInstance_getType(Handle);
4154
public uint Index => Native.ZkDaedalusInstance_getIndex(Handle);
4255

56+
public Materialized.DaedalusInstance Materialize()
57+
{
58+
return new Materialized.DaedalusInstance
59+
{
60+
Type = Type,
61+
Index = Index
62+
};
63+
}
64+
4365
public static DaedalusInstance FromNative(UIntPtr handle)
4466
{
4567
return Native.ZkDaedalusInstance_getType(handle) switch
@@ -64,9 +86,10 @@ public static DaedalusInstance FromNative(UIntPtr handle)
6486
DaedalusInstanceType.FightAi => new FightAiInstance(handle),
6587
DaedalusInstanceType.SoundEffect => new SoundEffectInstance(handle),
6688
DaedalusInstanceType.SoundSystem => new SoundSystemInstance(handle),
67-
DaedalusInstanceType.Svm => throw new InvalidOperationException("Svm Instances are currently not supported"),
89+
DaedalusInstanceType.Svm => throw new InvalidOperationException(
90+
"Svm Instances are currently not supported"),
6891
_ => new DaedalusInstance(handle)
6992
};
7093
}
7194
}
72-
}
95+
}

ZenKit/Font.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
using System.Collections.Generic;
33
using System.Numerics;
44
using System.Runtime.InteropServices;
5+
using ZenKit.Util;
56

67
namespace ZenKit
78
{
9+
[Serializable]
810
[StructLayout(LayoutKind.Sequential)]
911
public struct FontGlyph
1012
{
@@ -13,7 +15,18 @@ public struct FontGlyph
1315
public Vector2 bottomRight;
1416
}
1517

16-
public class Font
18+
namespace Materialized
19+
{
20+
[Serializable]
21+
public struct Font
22+
{
23+
public string Name;
24+
public uint Height;
25+
public List<FontGlyph> Glyphs;
26+
}
27+
}
28+
29+
public class Font : IMaterializing<Materialized.Font>
1730
{
1831
private readonly UIntPtr _handle;
1932

@@ -58,6 +71,16 @@ public List<FontGlyph> Glyphs
5871
}
5972
}
6073

74+
public Materialized.Font Materialize()
75+
{
76+
return new Materialized.Font
77+
{
78+
Name = Name,
79+
Height = Height,
80+
Glyphs = Glyphs
81+
};
82+
}
83+
6184
~Font()
6285
{
6386
Native.ZkFont_del(_handle);

ZenKit/GameVersion.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using System;
2+
13
namespace ZenKit
24
{
5+
[Serializable]
36
public enum GameVersion
47
{
58
Gothic1 = 0,

0 commit comments

Comments
 (0)