Skip to content

Commit

Permalink
Refactor Mdlx
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Feb 15, 2020
1 parent 9e4a434 commit f03f5f8
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 130 deletions.
250 changes: 121 additions & 129 deletions OpenKh.Kh2/Mdlx.Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,162 +40,154 @@ public DmaVif(int texi, int[] alaxi, byte[] bin)
}
}

public class Mdlxfst
private class SubModelHeader
{
private class SubModelHeader
[Data] public int Type { get; set; }
[Data] public int Unk04 { get; set; }
[Data] public int Unk08 { get; set; }
[Data] public int NextOffset { get; set; }
[Data] public short BoneCount { get; set; }
[Data] public short Unk12 { get; set; }
[Data] public int BoneOffset { get; set; }
[Data] public int Off4 { get; set; }
[Data] public short DmaChainCount { get; set; }
[Data] public short Unk1e { get; set; }

public SubModel SubModel { get; set; }
}

private class DmaChainHeader
{
[Data] public int Unk00 { get; set; }
[Data] public int TextureIndex { get; set; }
[Data] public int Unk08 { get; set; }
[Data] public int Unk0c { get; set; }
[Data] public int DmaOffset { get; set; }
[Data] public int Unk14 { get; set; }
[Data] public int DmaLength { get; set; }
[Data] public int Unk1c { get; set; }
}

private static IEnumerable<SubModel> ReadAsModel(Stream stream)
{
var currentOffset = 0;
var nextOffset = ReservedArea;
while (nextOffset != 0)
{
[Data] public int Type { get; set; }
[Data] public int Unk04 { get; set; }
[Data] public int Unk08 { get; set; }
[Data] public int NextOffset { get; set; }
[Data] public short BoneCount { get; set; }
[Data] public short Unk12 { get; set; }
[Data] public int BoneOffset { get; set; }
[Data] public int Off4 { get; set; }
[Data] public short DmaChainCount { get; set; }
[Data] public short Unk1e { get; set; }

public SubModel SubModel { get; set; }
currentOffset += nextOffset;
var subStream = new SubStream(stream, currentOffset, stream.Length - currentOffset);
nextOffset = ReadSubModel(subStream, out var subModel);

yield return subModel;
}
}

private class DmaChainHeader
private static int ReadSubModel(Stream stream, out SubModel subModel)
{
var header = BinaryMapping.ReadObject<SubModelHeader>(stream);
header.SubModel = new SubModel()
{
[Data] public int Unk00 { get; set; }
[Data] public int TextureIndex { get; set; }
[Data] public int Unk08 { get; set; }
[Data] public int Unk0c { get; set; }
[Data] public int DmaOffset { get; set; }
[Data] public int Unk14 { get; set; }
[Data] public int DmaLength { get; set; }
[Data] public int Unk1c { get; set; }
}
DmaVifs = new List<DmaVif>(),
Bones = new List<Bone>(),
};

private const int ReservedArea = 0x90;
public List<SubModel> SubModels;
var dmaChains = For(header.DmaChainCount, () => BinaryMapping.ReadObject<DmaChainHeader>(stream));
for (var j = 0; j < header.DmaChainCount; j++)
{
header.SubModel.DmaVifs.AddRange(ReadDmaChain(stream, dmaChains[j]));
}

public Mdlxfst(Stream stream)
if (header.BoneOffset != 0)
{
SubModels = new List<SubModel>();
stream.Position = header.BoneOffset;
header.SubModel.Bones = For(header.BoneCount, () => ReadBone(stream)).ToList();
}

var currentOffset = ReservedArea;
for (var i = 0; ; i++)
{
var subStreamLength = stream.Length - currentOffset;
var header = ReadSubModel(new SubStream(stream, currentOffset, subStreamLength));
subModel = header.SubModel;

return header.NextOffset;
}

SubModels.Add(header.SubModel);
private static IEnumerable<DmaVif> ReadDmaChain(Stream stream, DmaChainHeader dmaChainHeader)
{
var dmaVifs = new List<DmaVif>();
var count1a = stream.SetPosition(dmaChainHeader.Unk14).ReadInt32();
var alv1 = For(count1a, () => stream.ReadInt32()).ToList();

if (header.NextOffset == 0)
break;
currentOffset += header.NextOffset;
}
}
var offsetDmaTag = new List<int>();
var alaxi = new List<int[]>();
var alaxref = new List<int>();

offsetDmaTag.Add(dmaChainHeader.DmaOffset);

private static SubModelHeader ReadSubModel(Stream stream)
var offsetDmaBase = dmaChainHeader.DmaOffset + 0x10;

for (var i = 0; i < count1a; i++)
{
var header = BinaryMapping.ReadObject<SubModelHeader>(stream);
header.SubModel = new SubModel()
if (alv1[i] == -1)
{
DmaVifs = new List<DmaVif>(),
Bones = new List<Bone>(),
};

var dmaChains = For(header.DmaChainCount, () => BinaryMapping.ReadObject<DmaChainHeader>(stream));
for (var j = 0; j < header.DmaChainCount; j++)
offsetDmaTag.Add(offsetDmaBase + 0x10);
offsetDmaBase += 0x20;
}
else
{
header.SubModel.DmaVifs.AddRange(ReadDmaChain(stream, dmaChains[j]));
offsetDmaBase += 0x10;
}

if (header.BoneOffset != 0)
if (i + 1 == count1a)
{
stream.Position = header.BoneOffset;
header.SubModel.Bones = For(header.BoneCount, () => ReadBone(stream)).ToList();
alaxref.Add(alv1[i]);
alaxi.Add(alaxref.ToArray());
alaxref.Clear();
}

return header;
}

private static IEnumerable<DmaVif> ReadDmaChain(Stream stream, DmaChainHeader dmaChainHeader)
{
var dmaVifs = new List<DmaVif>();
var count1a = stream.SetPosition(dmaChainHeader.Unk14).ReadInt32();
var alv1 = For(count1a, () => stream.ReadInt32()).ToList();

var offsetDmaTag = new List<int>();
var alaxi = new List<int[]>();
var alaxref = new List<int>();

offsetDmaTag.Add(dmaChainHeader.DmaOffset);

var offsetDmaBase = dmaChainHeader.DmaOffset + 0x10;

for (var i = 0; i < count1a; i++)
else if (alv1[i] == -1)
{
if (alv1[i] == -1)
{
offsetDmaTag.Add(offsetDmaBase + 0x10);
offsetDmaBase += 0x20;
}
else
{
offsetDmaBase += 0x10;
}

if (i + 1 == count1a)
{
alaxref.Add(alv1[i]);
alaxi.Add(alaxref.ToArray());
alaxref.Clear();
}
else if (alv1[i] == -1)
{
alaxi.Add(alaxref.ToArray());
alaxref.Clear();
}
else
{
alaxref.Add(alv1[i]);
}
alaxi.Add(alaxref.ToArray());
alaxref.Clear();
}

for (var i = 0; i < offsetDmaTag.Count; i++)
else
{
stream.Position = offsetDmaTag[i];
var count = stream.ReadInt16();
var unknown = stream.ReadInt16();
var offset = stream.ReadInt32();

stream.Position = offset & 0x7FFFFFFF;
var vifPacket = stream.ReadBytes(count * 0x10);
dmaVifs.Add(new DmaVif(dmaChainHeader.TextureIndex, alaxi[i], vifPacket));
alaxref.Add(alv1[i]);
}

return dmaVifs;
}

private static Bone ReadBone(Stream stream)
for (var i = 0; i < offsetDmaTag.Count; i++)
{
var reader = new BinaryReader(stream);
return new Bone
{
Index = reader.ReadInt32(),
Parent = reader.ReadInt32(),
Unk08 = reader.ReadInt32(),
Unk0c = reader.ReadInt32(),
ScaleX = reader.ReadSingle(),
ScaleY = reader.ReadSingle(),
ScaleZ = reader.ReadSingle(),
ScaleW = reader.ReadSingle(),
RotationX = reader.ReadSingle(),
RotationY = reader.ReadSingle(),
RotationZ = reader.ReadSingle(),
RotationW = reader.ReadSingle(),
TranslationX = reader.ReadSingle(),
TranslationY = reader.ReadSingle(),
TranslationZ = reader.ReadSingle(),
TranslationW = reader.ReadSingle()
};
stream.Position = offsetDmaTag[i];
var count = stream.ReadInt16();
var unknown = stream.ReadInt16();
var offset = stream.ReadInt32();

stream.Position = offset & 0x7FFFFFFF;
var vifPacket = stream.ReadBytes(count * 0x10);
dmaVifs.Add(new DmaVif(dmaChainHeader.TextureIndex, alaxi[i], vifPacket));
}

return dmaVifs;
}

private static Bone ReadBone(Stream stream)
{
var reader = new BinaryReader(stream);
return new Bone
{
Index = reader.ReadInt32(),
Parent = reader.ReadInt32(),
Unk08 = reader.ReadInt32(),
Unk0c = reader.ReadInt32(),
ScaleX = reader.ReadSingle(),
ScaleY = reader.ReadSingle(),
ScaleZ = reader.ReadSingle(),
ScaleW = reader.ReadSingle(),
RotationX = reader.ReadSingle(),
RotationY = reader.ReadSingle(),
RotationZ = reader.ReadSingle(),
RotationW = reader.ReadSingle(),
TranslationX = reader.ReadSingle(),
TranslationY = reader.ReadSingle(),
TranslationZ = reader.ReadSingle(),
TranslationW = reader.ReadSingle()
};
}
}
}
2 changes: 1 addition & 1 deletion OpenKh.Kh2/Mdlx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private Mdlx(Stream stream)
MapModel = ReadAsMap(new SubStream(stream, ReservedArea, stream.Length - ReservedArea));
break;
case Entity:
SubModels = new Mdlxfst(stream).SubModels;
SubModels = ReadAsModel(stream).ToList();
break;
}
}
Expand Down

0 comments on commit f03f5f8

Please sign in to comment.