From 5782ab0e1a135a845728389afc4eca9d10a0474f Mon Sep 17 00:00:00 2001 From: xypwn <54681180+xypwn@users.noreply.github.com> Date: Wed, 6 Nov 2024 04:14:21 +0100 Subject: [PATCH] Resolve #11 There seems to be a situation where a remapMeshBones item is out of bounds. I don't exactly know why because I don't understand the file format, but throwing an error is better than panicking. --- extractor/unit/extractor.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/extractor/unit/extractor.go b/extractor/unit/extractor.go index fe3d34c..42f6a0f 100644 --- a/extractor/unit/extractor.go +++ b/extractor/unit/extractor.go @@ -178,15 +178,19 @@ func loadBoneMap(ctx extractor.Context) (*bones.BoneInfo, error) { return boneInfo, err } -func remapMeshBones(mesh *unit.Mesh, mapping unit.SkeletonMap) { +func remapMeshBones(mesh *unit.Mesh, mapping unit.SkeletonMap) error { for i := range mesh.BoneIndices { for j := range mesh.BoneIndices[i] { + if int(mesh.BoneIndices[i][j]) >= len(mapping.RemapList[0]) { + return fmt.Errorf("remapMeshBones: remap list item out of bounds") + } if mesh.BoneWeights[i][j] > 0 { remapIndex := mapping.RemapList[0][mesh.BoneIndices[i][j]] mesh.BoneIndices[i][j] = uint8(mapping.BoneIndices[remapIndex]) } } } + return nil } // Adds the unit's skeleton to the gltf document @@ -450,7 +454,9 @@ func ConvertOpts(ctx extractor.Context, imgOpts *ImageOptions) error { if bonesEnabled { if len(unitInfo.SkeletonMaps) > 0 { - remapMeshBones(&mesh, unitInfo.SkeletonMaps[0]) + if err := remapMeshBones(&mesh, unitInfo.SkeletonMaps[0]); err != nil { + return err + } } skin = gltf.Index(addSkeleton(doc, unitInfo, boneInfo)) weights = modeler.WriteWeights(doc, mesh.BoneWeights)