From ad674b46ec3c437c3dd44c60feb51218d3f3e853 Mon Sep 17 00:00:00 2001 From: ccp-intern <180460643+ccp-intern@users.noreply.github.com> Date: Thu, 6 Aug 2026 13:33:51 +0200 Subject: [PATCH] fix crash regarding model index --- trinity/Tr2GrannyAnimation.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/trinity/Tr2GrannyAnimation.cpp b/trinity/Tr2GrannyAnimation.cpp index 0dbe67aa5..fc16f5c55 100644 --- a/trinity/Tr2GrannyAnimation.cpp +++ b/trinity/Tr2GrannyAnimation.cpp @@ -82,7 +82,7 @@ Tr2GrannyAnimation::Tr2GrannyAnimation( IRoot* lockobj ) : m_debugRenderSkeleton( false ), m_debugRenderJointNames( false ), m_baseLayer( 1.f ), - m_modelIndex( 0 ), + m_modelIndex( -1 ), m_animationEnabled( true ), m_boneBoundsInitialized( false ), m_additiveMode( false ), @@ -591,7 +591,7 @@ void Tr2GrannyAnimation::RebuildCachedData( BlueAsyncRes* p ) if( cmfData->meshes.size() > 0 ) { - m_modelIndex = cmfData->meshes[0].skeleton; + m_modelIndex = cmfData->meshes[0].skeleton == 0xff ? -1 : cmfData->meshes[0].skeleton; } } else if( !m_model.empty() ) @@ -654,6 +654,7 @@ void Tr2GrannyAnimation::RebuildCachedData( BlueAsyncRes* p ) } else { + m_modelIndex = -1; m_pose.boneTransforms.clear(); m_pose.skeleton = nullptr; m_tmpPose.boneTransforms.clear(); @@ -787,6 +788,7 @@ void Tr2GrannyAnimation::RebuildCachedData( BlueAsyncRes* p ) } else { + m_modelIndex = -1; m_skeleton = nullptr; m_worldPose = nullptr; m_localPose = nullptr; @@ -864,6 +866,11 @@ bool Tr2GrannyAnimation::InitializeBoundingInfo() return false; } + if( m_modelIndex == -1 ) + { + return false; + } + auto& skeleton = cmfData->skeletons[m_modelIndex]; auto boneBindings = cmfData->meshes[0].boneBindings; @@ -1002,7 +1009,7 @@ bool Tr2GrannyAnimation::GetDynamicBounds( Vector4& boundingSphere, Vector3& aab } const granny_file_info* fi = GetFileInfo(); - if( fi ) + if( fi && m_modelIndex != -1 ) { aabbMin += *reinterpret_cast( fi->Models[m_modelIndex]->InitialPlacement.Position ); aabbMax += *reinterpret_cast( fi->Models[m_modelIndex]->InitialPlacement.Position ); @@ -1042,7 +1049,12 @@ void Tr2GrannyAnimation::RenderBones( const Matrix& modelTransform, const Tr2Ani return; } - auto& skeleton = GetCMFData()->skeletons[m_modelIndex]; + const cmf::Skeleton* skeletonPtr = GetSkeleton(); + if( !skeletonPtr ) + { + return; + } + const cmf::Skeleton& skeleton = *skeletonPtr; for( int boneIdx = 0; boneIdx < boneCount; boneIdx++ ) { @@ -1080,7 +1092,7 @@ void Tr2GrannyAnimation::RenderBones( const Matrix& modelTransform, const Tr2Ani Vector3 initialPlacement( 0, 0, 0 ); const granny_file_info* fi = GetFileInfo(); Matrix initialTranslation; - if( fi ) + if( fi && m_modelIndex != -1 ) { initialPlacement = *reinterpret_cast( fi->Models[m_modelIndex]->InitialPlacement.Position ); } @@ -1188,7 +1200,7 @@ void Tr2GrannyAnimation::RenderDynamicBounds( const Matrix& modelTransform ) Vector3 initialPlacement( 0, 0, 0 ); const granny_file_info* fi = GetFileInfo(); Matrix initialTranslation; - if( fi ) + if( fi && m_modelIndex != -1 ) { initialPlacement = *reinterpret_cast( fi->Models[m_modelIndex]->InitialPlacement.Position ); } @@ -1428,7 +1440,7 @@ const std::vector& Tr2GrannyAnimation::GetWorldTransforms() const granny_model* Tr2GrannyAnimation::GetGrannyModel() const { granny_file_info* fi = GetFileInfo(); - if( !fi ) + if( !fi || m_modelIndex == -1 ) { return nullptr; } @@ -1854,6 +1866,7 @@ void Tr2GrannyAnimation::Cleanup() m_pose.skeleton = nullptr; m_skeletonBoneIndices.clear(); m_worldTransforms.clear(); + m_boneBounds.clear(); #if WITH_GRANNY if( m_localPose ) @@ -1883,6 +1896,7 @@ void Tr2GrannyAnimation::Cleanup() m_skeleton = nullptr; #endif + m_modelIndex = -1; m_boneList.clear(); if( m_meshBoneMatrixList )