From f03f5f85a331ed3cef700e2c4b18c49a3ae62330 Mon Sep 17 00:00:00 2001 From: Luciano Ciccariello Date: Fri, 13 Dec 2019 22:18:23 +0000 Subject: [PATCH] Refactor Mdlx --- OpenKh.Kh2/Mdlx.Model.cs | 250 +++++++++++++++++++-------------------- OpenKh.Kh2/Mdlx.cs | 2 +- 2 files changed, 122 insertions(+), 130 deletions(-) diff --git a/OpenKh.Kh2/Mdlx.Model.cs b/OpenKh.Kh2/Mdlx.Model.cs index 54c8ef9dc..68d40e056 100644 --- a/OpenKh.Kh2/Mdlx.Model.cs +++ b/OpenKh.Kh2/Mdlx.Model.cs @@ -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 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(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(), + Bones = new List(), + }; - private const int ReservedArea = 0x90; - public List SubModels; + var dmaChains = For(header.DmaChainCount, () => BinaryMapping.ReadObject(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(); + 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 ReadDmaChain(Stream stream, DmaChainHeader dmaChainHeader) + { + var dmaVifs = new List(); + 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(); + var alaxi = new List(); + var alaxref = new List(); + + 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(stream); - header.SubModel = new SubModel() + if (alv1[i] == -1) { - DmaVifs = new List(), - Bones = new List(), - }; - - var dmaChains = For(header.DmaChainCount, () => BinaryMapping.ReadObject(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 ReadDmaChain(Stream stream, DmaChainHeader dmaChainHeader) - { - var dmaVifs = new List(); - var count1a = stream.SetPosition(dmaChainHeader.Unk14).ReadInt32(); - var alv1 = For(count1a, () => stream.ReadInt32()).ToList(); - - var offsetDmaTag = new List(); - var alaxi = new List(); - var alaxref = new List(); - - 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() + }; } } } diff --git a/OpenKh.Kh2/Mdlx.cs b/OpenKh.Kh2/Mdlx.cs index 558afea6c..7d4c80657 100644 --- a/OpenKh.Kh2/Mdlx.cs +++ b/OpenKh.Kh2/Mdlx.cs @@ -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; } }