diff options
Diffstat (limited to '')
-rw-r--r-- | NW4RTools/BrresWriter.cs | 99 |
1 files changed, 58 insertions, 41 deletions
diff --git a/NW4RTools/BrresWriter.cs b/NW4RTools/BrresWriter.cs index b93a71d..cf48cdd 100644 --- a/NW4RTools/BrresWriter.cs +++ b/NW4RTools/BrresWriter.cs @@ -42,6 +42,12 @@ namespace NW4RTools { return Output.GetBuffer(); } + + private void LogPosition(string text) { + Debug.Send("0x{0:X} : {1}", CurrentPos, text); + OffsetMap.Add(CurrentPos, text); + } + // OK, here's how I'm going to code this: kinda like how BrawlLib works, first I'll calculate the size of // each written element, and use that to build up a list of offsets. Then, I'll actually write out the data. // This code will also handle building the string table. @@ -106,7 +112,7 @@ namespace NW4RTools { // Now do each ResDict in the File foreach (var kv in File) { - OffsetMap.Add(CurrentPos, "ResDict: " + kv.Key); + LogPosition("ResDict: " + kv.Key); RootDictOffsets[kv.Value] = CurrentPos; CurrentPos += GetSizeForResDict((kv.Value as ICollection).Count); @@ -132,7 +138,7 @@ namespace NW4RTools { // ... and done with that. Build the string table and go! AlignCalcPos(4); - OffsetMap.Add(CurrentPos, "String Table"); + LogPosition("String Table"); CalculateStringTable(); } @@ -169,61 +175,61 @@ namespace NW4RTools { // This is used later to calculate the MDL0 block size easily int startPos = CurrentPos; - OffsetMap.Add(CurrentPos, "Model: " + kv.Key); + LogPosition("Model: " + kv.Key); ModelOffsets.Add(model, CurrentPos); CurrentPos += 0x4C; - OffsetMap.Add(CurrentPos, "Model Info Struct for: " + kv.Key); + LogPosition("Model Info Struct for: " + kv.Key); CurrentPos += 0x40; - OffsetMap.Add(CurrentPos, "Matrix ID to Node ID Data for: " + kv.Key); + LogPosition("Matrix ID to Node ID Data for: " + kv.Key); calcInfo.MatrixIDtoNodeID = CurrentPos; CurrentPos += 4 + (model.MatrixIDtoNodeID.Length * 4); - OffsetMap.Add(CurrentPos, "ResDict: ByteCode"); + LogPosition("ResDict: ByteCode"); calcInfo.Bytecode = CurrentPos; CurrentPos += GetSizeForResDict(model.Bytecode.Count); - OffsetMap.Add(CurrentPos, "ResDict: Nodes"); + LogPosition("ResDict: Nodes"); calcInfo.Nodes = CurrentPos; CurrentPos += GetSizeForResDict(model.Nodes.Count); - OffsetMap.Add(CurrentPos, "ResDict: VertexPosData"); + LogPosition("ResDict: VertexPosData"); calcInfo.VtxPosData = CurrentPos; CurrentPos += GetSizeForResDict(model.VtxPosData.Count); if (model.VtxNrmData.Count > 0) { - OffsetMap.Add(CurrentPos, "ResDict: VertexNrmData"); + LogPosition("ResDict: VertexNrmData"); calcInfo.VtxNrmData = CurrentPos; CurrentPos += GetSizeForResDict(model.VtxNrmData.Count); } if (model.VtxClrData.Count > 0) { - OffsetMap.Add(CurrentPos, "ResDict: VertexClrData"); + LogPosition("ResDict: VertexClrData"); calcInfo.VtxClrData = CurrentPos; CurrentPos += GetSizeForResDict(model.VtxClrData.Count); } if (model.VtxTexCoordData.Count > 0) { - OffsetMap.Add(CurrentPos, "ResDict: VertexTexCoordData"); + LogPosition("ResDict: VertexTexCoordData"); calcInfo.VtxTexCoordData = CurrentPos; CurrentPos += GetSizeForResDict(model.VtxTexCoordData.Count); } - OffsetMap.Add(CurrentPos, "ResDict: Materials"); + LogPosition("ResDict: Materials"); calcInfo.Materials = CurrentPos; CurrentPos += GetSizeForResDict(model.Materials.Count); - OffsetMap.Add(CurrentPos, "ResDict: Shaders"); + LogPosition("ResDict: Shaders"); calcInfo.Shaders = CurrentPos; CurrentPos += GetSizeForResDict(model.Shaders.Count); - OffsetMap.Add(CurrentPos, "ResDict: Shapes"); + LogPosition("ResDict: Shapes"); calcInfo.Shapes = CurrentPos; CurrentPos += GetSizeForResDict(model.Shapes.Count); if (model.PairingLookupByTexture.Count > 0) { - OffsetMap.Add(CurrentPos, "ResDict: Texture Lookup"); + LogPosition("ResDict: Texture Lookup"); calcInfo.PairingLookupByTexture = CurrentPos; CurrentPos += GetSizeForResDict(model.PairingLookupByTexture.Count); } @@ -250,7 +256,7 @@ namespace NW4RTools { private void CalculatePairings(Model m, ResDict<List<TexMatPairing>> dict) { foreach (var kv in dict) { - OffsetMap.Add(CurrentPos, "Texture/Material Pairing List for: " + kv.Key); + LogPosition("Texture/Material Pairing List for: " + kv.Key); PairingOffsets.Add(kv.Value, CurrentPos); CurrentPos += 4 + (kv.Value.Count * 8); @@ -262,7 +268,7 @@ namespace NW4RTools { AddString(kv.Key); ByteCode bc = kv.Value; - OffsetMap.Add(CurrentPos, "ByteCode: " + kv.Key); + LogPosition("ByteCode: " + kv.Key); BytecodeOffsets.Add(kv.Value, CurrentPos); foreach (var insn in bc.Instructions) { @@ -296,7 +302,7 @@ namespace NW4RTools { foreach (var kv in m.Nodes) { AddString(kv.Key); - OffsetMap.Add(CurrentPos, "Node: " + kv.Key); + LogPosition("Node: " + kv.Key); NodeOffsets.Add(kv.Value, CurrentPos); CurrentPos += 0xD0; @@ -307,7 +313,7 @@ namespace NW4RTools { foreach (var kv in m.Materials) { AddString(kv.Key); - OffsetMap.Add(CurrentPos, "Material: " + kv.Key); + LogPosition("Material: " + kv.Key); MaterialOffsets.Add(kv.Value, CurrentPos); // Base material struct @@ -336,7 +342,7 @@ namespace NW4RTools { // Texture Infos if (kv.Value.TextureInfos.Count > 0) - OffsetMap.Add(CurrentPos, "Material Texture Infos: " + kv.Key); + LogPosition("Material Texture Infos: " + kv.Key); for (int i = 0; i < kv.Value.TextureInfos.Count; i++) { TextureInfoOffsets[kv.Value.TextureInfos[i]] = CurrentPos; @@ -345,7 +351,7 @@ namespace NW4RTools { // Display Lists AlignCalcPos(0x20); - OffsetMap.Add(CurrentPos, "Material Display Lists: " + kv.Key); + LogPosition("Material Display Lists: " + kv.Key); MaterialDLOffsets[kv.Value] = CurrentPos; CurrentPos += 0x20 + 0x80 + 0x40 + 0xA0; } @@ -391,7 +397,7 @@ namespace NW4RTools { // This is a new shader, add it! calcInfo.UniqueShaders.Add(kv.Value); - OffsetMap.Add(CurrentPos, "Shader: " + kv.Key); + LogPosition("Shader: " + kv.Key); ShaderOffsets.Add(kv.Value, CurrentPos); CurrentPos += 0x200; @@ -403,7 +409,7 @@ namespace NW4RTools { foreach (var kv in m.Shapes) { AddString(kv.Key); - OffsetMap.Add(CurrentPos, "Shape: " + kv.Key); + LogPosition("Shape: " + kv.Key); ShapeOffsets.Add(kv.Value, CurrentPos); CurrentPos += 0x68; @@ -416,12 +422,12 @@ namespace NW4RTools { AlignCalcPos(0x20); ShapeDL1Offsets.Add(kv.Value, CurrentPos); - OffsetMap.Add(CurrentPos, "Shape DL 1: " + kv.Key); + LogPosition("Shape DL 1: " + kv.Key); CurrentPos += (int)kv.Value.DLBufferSize1; AlignCalcPos(0x20); ShapeDL2Offsets.Add(kv.Value, CurrentPos); - OffsetMap.Add(CurrentPos, "Shape DL 2: " + kv.Key); + LogPosition("Shape DL 2: " + kv.Key); CurrentPos += (int)kv.Value.DLBufferSize2; // Should this line be after the final alignment? @@ -436,7 +442,7 @@ namespace NW4RTools { foreach (var kv in m.VtxPosData) { AddString(kv.Key); - OffsetMap.Add(CurrentPos, "VertexPosData: " + kv.Key); + LogPosition("VertexPosData: " + kv.Key); VtxPosOffsets.Add(kv.Value, CurrentPos); // Main data @@ -446,7 +452,7 @@ namespace NW4RTools { CurrentPos += 0x18; AlignCalcPos(0x20); - OffsetMap.Add(CurrentPos, "Data: " + kv.Key); + LogPosition("Data: " + kv.Key); CurrentPos += kv.Value.EntryCount * kv.Value.EntrySize; AlignCalcPos(0x20); } @@ -456,14 +462,14 @@ namespace NW4RTools { foreach (var kv in m.VtxNrmData) { AddString(kv.Key); - OffsetMap.Add(CurrentPos, "VertexNrmData: " + kv.Key); + LogPosition("VertexNrmData: " + kv.Key); VtxNrmOffsets.Add(kv.Value, CurrentPos); // Main data CurrentPos += 0x20; AlignCalcPos(0x20); - OffsetMap.Add(CurrentPos, "Data: " + kv.Key); + LogPosition("Data: " + kv.Key); CurrentPos += kv.Value.EntryCount * kv.Value.EntrySize; AlignCalcPos(0x20); } @@ -473,14 +479,14 @@ namespace NW4RTools { foreach (var kv in m.VtxClrData) { AddString(kv.Key); - OffsetMap.Add(CurrentPos, "VertexClrData: " + kv.Key); + LogPosition("VertexClrData: " + kv.Key); VtxClrOffsets.Add(kv.Value, CurrentPos); // Main data CurrentPos += 0x20; AlignCalcPos(0x20); - OffsetMap.Add(CurrentPos, "Data: " + kv.Key); + LogPosition("Data: " + kv.Key); CurrentPos += kv.Value.EntryCount * kv.Value.EntrySize; AlignCalcPos(0x20); } @@ -490,7 +496,7 @@ namespace NW4RTools { foreach (var kv in m.VtxTexCoordData) { AddString(kv.Key); - OffsetMap.Add(CurrentPos, "VertexTexCoordData: " + kv.Key); + LogPosition("VertexTexCoordData: " + kv.Key); VtxTexCoordOffsets.Add(kv.Value, CurrentPos); // Main data @@ -500,7 +506,7 @@ namespace NW4RTools { CurrentPos += 0x10; AlignCalcPos(0x20); - OffsetMap.Add(CurrentPos, "Data: " + kv.Key); + LogPosition("Data: " + kv.Key); CurrentPos += kv.Value.EntryCount * kv.Value.EntrySize; AlignCalcPos(0x20); } @@ -523,14 +529,14 @@ namespace NW4RTools { Debug.Send("Current: {0}", kv.Key); - OffsetMap.Add(CurrentPos, "Texture: " + kv.Key); + LogPosition("Texture: " + kv.Key); TextureOffsets.Add(kv.Value, CurrentPos); CurrentPos += 0x30; AlignCalcPos(0x20); - OffsetMap.Add(CurrentPos, "Texture Data for: " + kv.Key); + LogPosition("Texture Data for: " + kv.Key); TextureDataOffsets.Add(kv.Value, CurrentPos); CurrentPos += texture.GetDataSize(); @@ -650,7 +656,7 @@ namespace NW4RTools { Output.WriteInt32(calcInfo.Materials - startPos); Output.WriteInt32(calcInfo.Shaders - startPos); Output.WriteInt32(calcInfo.Shapes - startPos); - Output.WriteInt32(calcInfo.PairingLookupByTexture - startPos); + Output.WriteInt32((calcInfo.PairingLookupByTexture == 0) ? 0 : (calcInfo.PairingLookupByTexture - startPos)); // Pairing lookup by palette, unhandled atm Output.WriteInt32(0); // Unknown extra data @@ -673,8 +679,7 @@ namespace NW4RTools { Output.WriteUInt32((uint)model.MatrixIDtoNodeID.Length); Output.WriteByte(model.UsesNrmMtxArray ? (byte)1 : (byte)0); Output.WriteByte(model.UsesTexMtxArray ? (byte)1 : (byte)0); - // Padding - Output.WriteInt16(0); + Output.AddPadding(2); Output.WriteInt32(calcInfo.MatrixIDtoNodeID - infoStructPos); Output.WriteVec3(model.Minimum); Output.WriteVec3(model.Maximum); @@ -700,7 +705,8 @@ namespace NW4RTools { WriteResDict<Material>(model.Materials, MaterialOffsets); WriteResDict<Shader>(model.Shaders, ShaderOffsets); WriteResDict<Shape>(model.Shapes, ShapeOffsets); - WriteResDict<List<TexMatPairing>>(model.PairingLookupByTexture, PairingOffsets); + if (model.PairingLookupByTexture.Count > 0) + WriteResDict<List<TexMatPairing>>(model.PairingLookupByTexture, PairingOffsets); // TODO: Palette pairing lookups @@ -735,6 +741,7 @@ namespace NW4RTools { private void WriteBytecode(Model m) { foreach (var kv in m.Bytecode) { + Debug.Send("Writing bytecode {0} @ offset {1:X}", kv.Key, Output.Position); ByteCode bc = kv.Value; foreach (var insn in bc.Instructions) { @@ -787,6 +794,7 @@ namespace NW4RTools { int currentIndex = 0; foreach (var kv in m.Nodes) { + Debug.Send("Writing node {0} @ offset {1:X}", kv.Key, Output.Position); Node node = kv.Value; int startPos = Output.Position; @@ -831,6 +839,7 @@ namespace NW4RTools { int currentIndex = 0; foreach (var kv in m.Materials) { + Debug.Send("Writing material {0} @ offset {1:X}", kv.Key, Output.Position); Material mat = kv.Value; int startPos = Output.Position; @@ -897,8 +906,13 @@ namespace NW4RTools { // ResTlutObj Output.WriteUInt32(mat.TlutFlag); - Output.WriteBytes(mat.TlutObj); - Output.AddPadding(0x60 - mat.TlutObj.Length); + + if (mat.TlutObj == null) { + Output.AddPadding(0x60); + } else { + Output.WriteBytes(mat.TlutObj); + Output.AddPadding(0x60 - mat.TlutObj.Length); + } // ResTexSrt // this one is a bit of a pain @@ -1032,6 +1046,7 @@ namespace NW4RTools { int currentIndex = 0; foreach (var shader in ModelCalcInfos[m].UniqueShaders) { + Debug.Send("Writing shader @ offset {0:X}", Output.Position); int startPos = Output.Position; // Size, model offset, index @@ -1058,6 +1073,7 @@ namespace NW4RTools { int currentIndex = 0; foreach (var kv in m.Shapes) { + Debug.Send("Writing shape {0} @ offset {1:X}", kv.Key, Output.Position); Shape shape = kv.Value; int startPos = Output.Position; @@ -1142,6 +1158,7 @@ namespace NW4RTools { int currentIndex = 0; foreach (var kv in dict) { + Debug.Send("Writing vtxdata {0} @ offset {1:X}", kv.Key, Output.Position); int startPos = Output.Position; int structSize = 0x20; |