From da62abefcf211d0c6670b5ebb70345ffd60b1ec0 Mon Sep 17 00:00:00 2001 From: dexy Date: Sun, 15 Mar 2020 02:25:09 +1100 Subject: [PATCH] Adjusted padding for BoundGeometry Materials array --- CodeWalker.Core/GameFiles/Resources/Bounds.cs | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/CodeWalker.Core/GameFiles/Resources/Bounds.cs b/CodeWalker.Core/GameFiles/Resources/Bounds.cs index 8120fbd..41ae8d1 100644 --- a/CodeWalker.Core/GameFiles/Resources/Bounds.cs +++ b/CodeWalker.Core/GameFiles/Resources/Bounds.cs @@ -1066,7 +1066,7 @@ namespace CodeWalker.GameFiles this.Octants = reader.ReadBlockAt(this.OctantsPointer, this.OctantItemsPointer); - this.Materials = reader.ReadStructsAt(this.MaterialsPointer, this.MaterialsCount); + this.Materials = reader.ReadStructsAt(this.MaterialsPointer, (this.MaterialsCount < 4) ? 4u : MaterialsCount); this.MaterialColours = reader.ReadStructsAt(this.MaterialColoursPointer, this.MaterialColoursCount); @@ -1074,6 +1074,27 @@ namespace CodeWalker.GameFiles + if ((MaterialsPointer != 0) && (MaterialsCount < 4)) + { + //for (var i = MaterialsCount; i < 4; i++) + //{ + // var m = Materials[i]; + // if ((m.Data1 != 0) || (m.Data2 != 0)) + // { }//no hit + //} + + + //the read array was padded, so remove the padding from this array. will re-add padding in BuildMaterials... + var mats = new BoundMaterial_s[MaterialsCount]; + for (int i = 0; i < MaterialsCount; i++) + { + mats[i] = Materials[i]; + } + Materials = mats; + + } + + //if (Vertices2Count != VerticesCount) //{ }//no hit here //if (Unknown_9Ch != 0) @@ -1114,7 +1135,7 @@ namespace CodeWalker.GameFiles this.MaterialsPointer = (ulong)(this.MaterialsBlock != null ? this.MaterialsBlock.FilePosition : 0); this.MaterialColoursPointer = (ulong)(this.MaterialColoursBlock != null ? this.MaterialColoursBlock.FilePosition : 0); this.PolygonMaterialIndicesPointer = (ulong)(this.PolygonMaterialIndicesBlock != null ? this.PolygonMaterialIndicesBlock.FilePosition : 0); - this.MaterialsCount = (byte)(this.MaterialsBlock != null ? this.MaterialsBlock.ItemCount : 0); + //this.MaterialsCount = (byte)(this.MaterialsBlock != null ? this.MaterialsBlock.ItemCount : 0);//this is updated by BuildMaterials, and the array could include padding! this.MaterialColoursCount = (byte)(this.MaterialColoursBlock != null ? this.MaterialColoursBlock.ItemCount : 0); @@ -1750,6 +1771,14 @@ namespace CodeWalker.GameFiles } } + MaterialsCount = (byte)matlist.Count; + + //add padding to the array for writing + for (int i = matlist.Count; i < 4; i++) + { + matlist.Add(new BoundMaterial_s()); + } + Materials = matlist.ToArray(); PolygonMaterialIndices = polymats.ToArray(); }