Skip to content

Commit

Permalink
Cleaned up the Melee code a bit more.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Oct 25, 2023
1 parent 5b47852 commit 972c12e
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 136 deletions.
53 changes: 40 additions & 13 deletions FinModelUtility/Formats/Dat/src/schema/DObj.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
using fin.data.queues;

using schema.binary;
using schema.binary;

namespace dat.schema {
/// <summary>
/// Data object.
/// </summary>
[BinarySchema]
public partial class DObjData : IBinaryConvertible {
public uint StringOffset { get; set; }
public uint NextObjectOffset { get; set; }
public uint MaterialStructOffset { get; set; }
public uint MeshStructOffset { get; set; }
}
public partial class DObj : IBinaryDeserializable {
private readonly Dat dat_;

public DObj(Dat dat) {
this.dat_ = dat;
}

public class DObj {
public DObjData Data { get; } = new();
[BinarySchema]
public partial class DObjHeader : IBinaryConvertible {
public uint StringOffset { get; set; }
public uint NextDObjOffset { get; set; }
public uint MObjOffset { get; set; }
public uint FirstPObjOffset { get; set; }
}

public DObjHeader Header { get; } = new();
public string Name { get; set; }

public PObj? FirstPObj { get; set; }
public DObj? NextDObj { get; private set; }
public MObj? MObj { get; private set; }
public PObj? FirstPObj { get; private set; }

public IEnumerable<PObj> PObjs {
get {
Expand All @@ -29,5 +35,26 @@ public IEnumerable<PObj> PObjs {
}
}
}

public void Read(IBinaryReader br) {
this.Header.Read(br);

if (this.Header.FirstPObjOffset != 0) {
br.Position = this.Header.FirstPObjOffset;
this.FirstPObj = new PObj(this.dat_);
this.FirstPObj.Read(br);
}

if (this.Header.MObjOffset != 0) {
br.Position = this.Header.MObjOffset;
this.MObj = br.ReadNew<MObj>();
}

if (this.Header.NextDObjOffset != 0) {
br.Position = this.Header.NextDObjOffset;
this.NextDObj = new DObj(this.dat_);
this.NextDObj.Read(br);
}
}
}
}
115 changes: 9 additions & 106 deletions FinModelUtility/Formats/Dat/src/schema/Dat.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System.Diagnostics;
using System.Numerics;
using System.Numerics;

using CommunityToolkit.HighPerformance;

using fin.color;
using fin.model;
using fin.schema.vector;
using fin.util.asserts;
using fin.util.color;
using fin.util.enums;

using gx;

Expand All @@ -22,10 +17,7 @@ namespace dat.schema {
// FObj: keyframe descriptor
// IObj: image
// JObj: joint (bone)
// MObj: material
// PObj: primitive
// SObj: Scene object
// TObj: texture

/// <summary>
/// References:
Expand Down Expand Up @@ -154,13 +146,10 @@ uint jObjDataOffset
jObj.Name = br.ReadStringNT();
}

var jObjFlags = jObjData.Flags;
var isSpline = jObjFlags.CheckFlag(JObjFlags.SPLINE);
var isParticleJoint = jObjFlags.CheckFlag(JObjFlags.PTCL);
var isDObj = !isSpline && !isParticleJoint;

if (isDObj) {
this.ReadDObjIntoJObj_(br, jObj, jObj.Data.ObjectStructOffset);
if (jObj.Data.FirstDObjOffset != 0) {
br.Position = jObj.Data.FirstDObjOffset;
jObj.FirstDObj = new DObj(this);
jObj.FirstDObj.Read(br);
}

var firstChildOffset = jObj.Data.FirstChildBoneOffset;
Expand All @@ -176,60 +165,35 @@ uint jObjDataOffset

br.PopLocalSpace();
}

private void ReadDObjIntoJObj_(IBinaryReader br,
JObj jObj,
uint objectStructOffset) {
if (!this.AssertNullOrValidPointer_(objectStructOffset)) {
return;
}

var dObj = new DObj();
jObj.DObjs.Add(dObj);

br.Position = objectStructOffset;
dObj.Data.Read(br);

if (dObj.Data.MeshStructOffset != 0) {
br.Position = dObj.Data.MeshStructOffset;

var firstPObj = new PObj(this);
firstPObj.Read(br);

dObj.FirstPObj = firstPObj;
}

this.ReadDObjIntoJObj_(br, jObj, dObj.Data.NextObjectOffset);
}
}

public static class BinaryReaderExtensions {
public static Vector2 ReadVector2(this IBinaryReader br,
VertexDescriptorData descriptor) {
VertexDescriptor descriptor) {
var vec2 = new Vector2();
br.ReadIntoVector(descriptor,
new Span<Vector2>(ref vec2).Cast<Vector2, float>());
return vec2;
}

public static Vector3 ReadVector3(this IBinaryReader br,
VertexDescriptorData descriptor) {
VertexDescriptor descriptor) {
var vec3 = new Vector3();
br.ReadIntoVector(descriptor,
new Span<Vector3>(ref vec3).Cast<Vector3, float>());
return vec3;
}

public static Vector4 ReadVector4(this IBinaryReader br,
VertexDescriptorData descriptor) {
VertexDescriptor descriptor) {
var vec4 = new Vector4();
br.ReadIntoVector(descriptor,
new Span<Vector4>(ref vec4).Cast<Vector4, float>());
return vec4;
}

public static void ReadIntoVector(this IBinaryReader br,
VertexDescriptorData descriptor,
VertexDescriptor descriptor,
Span<float> floats) {
Asserts.True(floats.Length >= descriptor.ComponentCount);

Expand Down Expand Up @@ -362,65 +326,4 @@ private static RootNodeType GetTypeFromName_(string name) {
return RootNodeType.Undefined;
}
}

[BinarySchema]
public partial class VertexDescriptorData : IBinaryConvertible {
public GxAttribute Attribute { get; set; }

[IntegerFormat(SchemaIntegerType.UINT32)]
public GxAttributeType AttributeType { get; set; }


[IntegerFormat(SchemaIntegerType.UINT32)]
public GxComponentCount ComponentCountType { get; set; }

[Ignore]
public int ComponentCount => this.Attribute switch {
GxAttribute.POS => this.ComponentCountType switch {
GxComponentCount.POS_XY => 2,
GxComponentCount.POS_XYZ => 3,
},
GxAttribute.NRM => this.ComponentCountType switch {
GxComponentCount.NRM_XYZ => 3,
},
GxAttribute.TEX0 or GxAttribute.TEX1 => this.ComponentCountType switch {
GxComponentCount.TEX_S => 1,
GxComponentCount.TEX_ST => 2,
},
};


[IntegerFormat(SchemaIntegerType.UINT32)]
public uint RawComponentType { get; set; }

[Ignore]
public GxComponentType AxesComponentType
=> (GxComponentType) this.RawComponentType;

[Ignore]
public ColorComponentType ColorComponentType
=> (ColorComponentType) this.RawComponentType;


public byte Scale { get; set; }

public byte Padding { get; set; }

public ushort Stride { get; set; }

public uint ArrayOffset { get; set; }
}

public class VertexDescriptor {
public VertexDescriptorData Data { get; } = new();
}

public enum ColorComponentType : uint {
RGB565,
RGB888,
RGBX8888,
RGBA4444,
RGBA6,
RGBA8888,
}
}
17 changes: 15 additions & 2 deletions FinModelUtility/Formats/Dat/src/schema/JObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public partial class JObjData : IBinaryConvertible {
public JObjFlags Flags { get; set; }
public uint FirstChildBoneOffset { get; set; }
public uint NextSiblingBoneOffset { get; set; }
public uint ObjectStructOffset { get; set; }
public uint FirstDObjOffset { get; set; }
public Vector3f RotationRadians { get; } = new();
public Vector3f Scale { get; } = new();
public Vector3f Position { get; } = new();
Expand All @@ -65,12 +65,25 @@ public partial class JObjData : IBinaryConvertible {
public uint UnknownPointer { get; set; }
}

/// <summary>
/// Joint object.
/// </summary>
public class JObj {
public JObjData Data { get; } = new();

public string Name { get; set; }

public List<DObj> DObjs { get; } = new();
public DObj FirstDObj { get; set; }

public IEnumerable<DObj> DObjs {
get {
var current = this.FirstDObj;
while (current != null) {
yield return current;
current = current.NextDObj;
}
}
}

public List<JObj> Children { get; } = new();
}
Expand Down
21 changes: 20 additions & 1 deletion FinModelUtility/Formats/Dat/src/schema/MObj.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using schema.binary;
using schema.binary.attributes;

namespace dat.schema {
/// <summary>
Expand All @@ -7,6 +8,24 @@ namespace dat.schema {
/// Shamelessly copied from:
/// https://github.com/jam1garner/Smash-Forge/blob/c0075bca364366bbea2d3803f5aeae45a4168640/Smash%20Forge/Filetypes/Melee/DAT.cs#L1256
/// </summary>
public class MObj {
[BinarySchema]
public partial class MObj : IBinaryDeserializable {
public uint Unk1 { get; set; }
public uint Unk2 { get; set; }

private uint tObjOffset_;

[Ignore]
private bool hasTObj_ => this.tObjOffset_ != 0;

[RIfBoolean(nameof(hasTObj_))]
[RAtPosition(nameof(tObjOffset_))]
public TObj? TObj { get; set; }

// TODO: What is this used for, is this another MObj?
public uint MaterialOffset { get; private set; }

public uint Unk3 { get; set; }
public uint Unk4 { get; set; }
}
}
24 changes: 11 additions & 13 deletions FinModelUtility/Formats/Dat/src/schema/PObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ public void Read(IBinaryReader br) {
// Reads vertex descriptors
while (true) {
var vertexDescriptor = new VertexDescriptor();
var vertexDescriptorData = vertexDescriptor.Data;
vertexDescriptorData.Read(br);
vertexDescriptor.Read(br);

if (vertexDescriptorData.Attribute == GxAttribute.NULL) {
if (vertexDescriptor.Attribute == GxAttribute.NULL) {
break;
}

Expand Down Expand Up @@ -124,13 +123,12 @@ public void Read(IBinaryReader br) {
IColor? color = null;

foreach (var vertexDescriptor in this.VertexDescriptors) {
var vertexDescriptorData = vertexDescriptor.Data;
var vertexAttribute = vertexDescriptorData.Attribute;
var vertexFormat = vertexDescriptorData.AttributeType;
var vertexAttribute = vertexDescriptor.Attribute;
var vertexFormat = vertexDescriptor.AttributeType;

if (vertexAttribute == GxAttribute.CLR0 &&
vertexFormat == GxAttributeType.DIRECT) {
switch (vertexDescriptor.Data.ColorComponentType) {
switch (vertexDescriptor.ColorComponentType) {
case ColorComponentType.RGB565: {
color = ColorUtil.ParseRgb565(br.ReadUInt16());
break;
Expand Down Expand Up @@ -193,8 +191,8 @@ public void Read(IBinaryReader br) {
_ => throw new NotImplementedException(),
};

var offset = vertexDescriptorData.ArrayOffset +
vertexDescriptorData.Stride * value;
var offset = vertexDescriptor.ArrayOffset +
vertexDescriptor.Stride * value;

switch (vertexAttribute) {
case GxAttribute.PNMTXIDX: {
Expand All @@ -204,25 +202,25 @@ public void Read(IBinaryReader br) {
case GxAttribute.POS: {
position = br.SubreadAt(
offset,
sbr => sbr.ReadVector3(vertexDescriptorData));
sbr => sbr.ReadVector3(vertexDescriptor));
break;
}
case GxAttribute.NRM: {
normal = br.SubreadAt(
offset,
sbr => sbr.ReadVector3(vertexDescriptorData));
sbr => sbr.ReadVector3(vertexDescriptor));
break;
}
case GxAttribute.TEX0: {
uv0 = br.SubreadAt(
offset,
sbr => sbr.ReadVector2(vertexDescriptorData));
sbr => sbr.ReadVector2(vertexDescriptor));
break;
}
case GxAttribute.TEX1: {
uv1 = br.SubreadAt(
offset,
sbr => sbr.ReadVector2(vertexDescriptorData));
sbr => sbr.ReadVector2(vertexDescriptor));
break;
}
default: throw new NotImplementedException();
Expand Down
Loading

0 comments on commit 972c12e

Please sign in to comment.