Skip to content

Commit 7cf982b

Browse files
committed
Using shared_ptr for CIFPAnimations
1 parent 63398af commit 7cf982b

File tree

8 files changed

+58
-192
lines changed

8 files changed

+58
-192
lines changed

Client/mods/deathmatch/logic/CClientGame.cpp

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3714,14 +3714,14 @@ CAnimBlendAssociationSAInterface * CClientGame::StaticAddAnimationAndSyncHandler
37143714
return g_pClientGame->AddAnimationAndSyncHandler ( pClump, pAnimAssocToSyncWith, animGroup, animID );
37153715
}
37163716

3717-
bool CClientGame::StaticAssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSAInterface * pOutAnimStaticAssoc, SIFPAnimations ** pOutIFPAnimations, RpClump * pClump, CAnimBlendAssocGroupSAInterface * pAnimAssocGroup, AnimationId animID )
3717+
bool CClientGame::StaticAssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSAInterface * pOutAnimStaticAssoc, CAnimBlendAssociationSAInterface * pAnimAssoc, RpClump * pClump, CAnimBlendAssocGroupSAInterface * pAnimAssocGroup, AnimationId animID )
37183718
{
3719-
return g_pClientGame->AssocGroupCopyAnimationHandler ( pOutAnimStaticAssoc, pOutIFPAnimations, pClump, pAnimAssocGroup, animID );
3719+
return g_pClientGame->AssocGroupCopyAnimationHandler ( pOutAnimStaticAssoc, pAnimAssoc, pClump, pAnimAssocGroup, animID );
37203720
}
37213721

3722-
bool CClientGame::StaticBlendAnimationHierarchyHandler ( SIFPAnimations ** pOutIFPAnimations, CAnimBlendHierarchySAInterface ** pOutAnimHierarchy, RpClump * pClump )
3722+
bool CClientGame::StaticBlendAnimationHierarchyHandler ( CAnimBlendAssociationSAInterface * pAnimAssoc, CAnimBlendHierarchySAInterface ** pOutAnimHierarchy, RpClump * pClump )
37233723
{
3724-
return g_pClientGame->BlendAnimationHierarchyHandler ( pOutIFPAnimations, pOutAnimHierarchy, pClump );
3724+
return g_pClientGame->BlendAnimationHierarchyHandler ( pAnimAssoc, pOutAnimHierarchy, pClump );
37253725
}
37263726

37273727
void CClientGame::StaticPreWorldProcessHandler ( void )
@@ -4009,6 +4009,7 @@ bool CClientGame::ChokingHandler ( unsigned char ucWeaponType )
40094009
void CClientGame::CAnimBlendAssocDestructorHandler ( CAnimBlendAssociationSAInterface * pThis )
40104010
{
40114011
//printf("CClientGame::CAnimBlendAssocDestructorHandler called! sAnimID: %d\n", pThis->sAnimID);
4012+
RemoveAnimationAssociationFromMap ( pThis );
40124013
}
40134014

40144015

@@ -4034,11 +4035,12 @@ typedef void (__thiscall* hCAnimBlendStaticAssociation_Init)
40344035
);
40354036

40364037

4037-
bool CClientGame::AssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSAInterface * pOutAnimStaticAssoc, SIFPAnimations ** pOutIFPAnimations, RpClump * pClump, CAnimBlendAssocGroupSAInterface * pAnimAssocGroup, AnimationId animID )
4038+
bool CClientGame::AssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSAInterface * pOutAnimStaticAssoc, CAnimBlendAssociationSAInterface * pAnimAssoc, RpClump * pClump, CAnimBlendAssocGroupSAInterface * pAnimAssocGroup, AnimationId animID )
40384039
{
40394040
auto CAnimBlendStaticAssociation_Init = (hCAnimBlendStaticAssociation_Init)0x4CEC20;
40404041

40414042
bool isCustomAnimationToPlay = false;
4043+
40424044
CAnimManager * pAnimationManager = g_pGame->GetAnimManager();
40434045
auto pOriginalAnimStaticAssoc = pAnimationManager->GetAnimStaticAssociation ( pAnimAssocGroup->groupID, animID );
40444046

@@ -4050,10 +4052,10 @@ bool CClientGame::AssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSA
40504052
{
40514053
if ( pReplacedAnimation->pIFP->isIFPLoaded ( ) )
40524054
{
4053-
SIFPAnimations * pIFPAnimations = pReplacedAnimation->pIFP->GetIFPAnimationsPointer ();
4054-
pIFPAnimations->iReferences ++;
4055+
std::shared_ptr < CIFPAnimations > pIFPAnimations = pReplacedAnimation->pIFP->GetIFPAnimationsPointer ();
4056+
InsertAnimationAssociationToMap ( pAnimAssoc, pIFPAnimations );
4057+
40554058
// Play our custom animation instead of default
4056-
*pOutIFPAnimations = pIFPAnimations;
40574059
CAnimBlendStaticAssociation_Init ( pOutAnimStaticAssoc, pClump, pReplacedAnimation->pAnimationHierarchy );
40584060
isCustomAnimationToPlay = true;
40594061
}
@@ -4074,13 +4076,14 @@ bool CClientGame::AssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSA
40744076
pOutAnimStaticAssoc->sFlags = pOriginalAnimStaticAssoc->sFlags;
40754077

40764078
printf(" CClientGame::AssocGroupCopyAnimationHandler: About to return sAnimGroup: %d | sAnimID: %d !\n", pOutAnimStaticAssoc->sAnimGroup, pOutAnimStaticAssoc->sAnimID);
4079+
40774080
return isCustomAnimationToPlay;
40784081
}
40794082

40804083

4081-
bool CClientGame::BlendAnimationHierarchyHandler ( SIFPAnimations ** pOutIFPAnimations, CAnimBlendHierarchySAInterface ** pOutAnimHierarchy, RpClump * pClump )
4084+
bool CClientGame::BlendAnimationHierarchyHandler ( CAnimBlendAssociationSAInterface * pAnimAssoc, CAnimBlendHierarchySAInterface ** pOutAnimHierarchy, RpClump * pClump )
40824085
{
4083-
bool isCustomAnimationToPlay = false;
4086+
bool isCustomAnimationToPlay = false;
40844087

40854088
CAnimManager * pAnimationManager = g_pGame->GetAnimManager();
40864089
CClientPed * pClientPed = GetClientPedByClump ( *pClump );
@@ -4099,13 +4102,12 @@ bool CClientGame::BlendAnimationHierarchyHandler ( SIFPAnimations ** pOutIFPAni
40994102
{
41004103
if ( pIFP->isIFPLoaded ( ) )
41014104
{
4102-
SIFPAnimations * pIFPAnimations = pIFP->GetIFPAnimationsPointer ();
4103-
pIFPAnimations->iReferences ++;
4105+
std::shared_ptr < CIFPAnimations > pIFPAnimations = pIFP->GetIFPAnimationsPointer ();
4106+
InsertAnimationAssociationToMap ( pAnimAssoc, pIFPAnimations );
41044107

41054108
pClientPed->setCurrentAnimationCustom ( true );
41064109
pClientPed->setNextAnimationNormal ( );
41074110

4108-
*pOutIFPAnimations = pIFPAnimations;
41094111
*pOutAnimHierarchy = pCustomAnimBlendHierarchy;
41104112
isCustomAnimationToPlay = true;
41114113
return isCustomAnimationToPlay;
@@ -6931,44 +6933,12 @@ void CClientGame::OnClientIFPUnload ( const std::shared_ptr < CClientIFP > & IFP
69316933
}
69326934
}
69336935

6934-
void CClientGame::DeleteIFPAnimations ( SIFPAnimations * pIFPAnimations )
6936+
void CClientGame::InsertAnimationAssociationToMap ( CAnimBlendAssociationSAInterface * pAnimAssociation, const std::shared_ptr < CIFPAnimations > & pIFPAnimations )
69356937
{
6936-
if ( pIFPAnimations->iReferences == 0 )
6937-
{
6938-
printf("CClientGame::UnloadIFPAnimations (): iReferences are Zero\n");
6939-
6940-
hCMemoryMgr_Free OLD_CMemoryMgr_Free = (hCMemoryMgr_Free)0x0072F430;
6941-
auto OLD_CAnimBlendHierarchy_RemoveFromUncompressedCache = (hCAnimBlendHierarchy_RemoveFromUncompressedCache)0x004D42A0;
6942-
6943-
for ( size_t i = 0; i < pIFPAnimations->vecAnimations.size(); i++ )
6944-
{
6945-
IFP_Animation * ifpAnimation = &pIFPAnimations->vecAnimations[i];
6946-
6947-
OLD_CAnimBlendHierarchy_RemoveFromUncompressedCache ( (int)&ifpAnimation->Hierarchy );
6948-
6949-
for (unsigned short SequenceIndex = 0; SequenceIndex < ifpAnimation->Hierarchy.m_nSeqCount; SequenceIndex++)
6950-
{
6951-
_CAnimBlendSequence * pSequence = (_CAnimBlendSequence*)((BYTE*)ifpAnimation->Hierarchy.m_pSequences + (sizeof(_CAnimBlendSequence) * SequenceIndex));
6938+
m_mapOfCustomAnimationAssociations [ pAnimAssociation ] = pIFPAnimations;
6939+
}
69526940

6953-
if ( !( (pSequence->m_nFlags >> 3) & 1 ) ) // If ( !OneBigChunkForAllSequences )
6954-
{
6955-
OLD_CMemoryMgr_Free ( pSequence->m_pFrames ); //*(void **)(pThis + 8)); //pSequence->m_pFrames );
6956-
}
6957-
else
6958-
{
6959-
if ( SequenceIndex == 0 )
6960-
{
6961-
// All frames of all sequences are allocated on one memory block, so free that one
6962-
// and break the loop
6963-
OLD_CMemoryMgr_Free ( pSequence->m_pFrames );
6964-
break;
6965-
}
6966-
}
6967-
6968-
}
6969-
delete ifpAnimation->pSequencesMemory;
6970-
}
6971-
delete pIFPAnimations;
6972-
printf("CClientGame::UnloadIFPAnimations (): IFP Animations have been unloaded successfully!\n");
6973-
}
6941+
void CClientGame::RemoveAnimationAssociationFromMap ( CAnimBlendAssociationSAInterface * pAnimAssociation )
6942+
{
6943+
m_mapOfCustomAnimationAssociations.erase ( pAnimAssociation );
69746944
}

Client/mods/deathmatch/logic/CClientGame.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct SMiscGameSettings
8484
class CClientGame
8585
{
8686
friend class CPacketHandler;
87+
typedef std::map < CAnimBlendAssociationSAInterface *, std::shared_ptr < CIFPAnimations > > AnimAssociations_type;
8788

8889
public:
8990
enum eStatus
@@ -506,8 +507,8 @@ class CClientGame
506507
static void StaticCAnimBlendAssocDestructorHandler ( CAnimBlendAssociationSAInterface * pThis );
507508
static CAnimBlendAssociationSAInterface * StaticAddAnimationHandler ( RpClump * pClump, AssocGroupId animGroup, AnimationId animID );
508509
static CAnimBlendAssociationSAInterface * StaticAddAnimationAndSyncHandler( RpClump * pClump, CAnimBlendAssociationSAInterface * pAnimAssocToSyncWith, AssocGroupId animGroup, AnimationId animID );
509-
static bool StaticAssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSAInterface * pOutAnimStaticAssoc, SIFPAnimations ** pOutIFPAnimations, RpClump * pClump, CAnimBlendAssocGroupSAInterface * pAnimAssocGroup, AnimationId animID );
510-
static bool StaticBlendAnimationHierarchyHandler ( SIFPAnimations ** pOutIFPAnimations, CAnimBlendHierarchySAInterface ** pOutAnimHierarchy, RpClump * pClump );
510+
static bool StaticAssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSAInterface * pOutAnimStaticAssoc, CAnimBlendAssociationSAInterface * pAnimAssoc, RpClump * pClump, CAnimBlendAssocGroupSAInterface * pAnimAssocGroup, AnimationId animID );
511+
static bool StaticBlendAnimationHierarchyHandler ( CAnimBlendAssociationSAInterface * pAnimAssoc, CAnimBlendHierarchySAInterface ** pOutAnimHierarchy, RpClump * pClump );
511512
static bool StaticProcessCollisionHandler ( CEntitySAInterface* pThisInterface, CEntitySAInterface* pOtherInterface );
512513
static bool StaticVehicleCollisionHandler ( CVehicleSAInterface* pThisInterface, CEntitySAInterface* pOtherInterface, int iModelIndex, float fDamageImpulseMag, float fCollidingDamageImpulseMag, uint16 usPieceType, CVector vecCollisionPos, CVector vecCollisionVelocity );
513514
static bool StaticVehicleDamageHandler ( CEntitySAInterface* pVehicleInterface, float fLoss, CEntitySAInterface* pAttackerInterface, eWeaponType weaponType, const CVector& vecDamagePos, uchar ucTyre );
@@ -540,8 +541,8 @@ class CClientGame
540541
void CAnimBlendAssocDestructorHandler ( CAnimBlendAssociationSAInterface * pThis );
541542
CAnimBlendAssociationSAInterface * AddAnimationHandler ( RpClump * pClump, AssocGroupId animGroup, AnimationId animID );
542543
CAnimBlendAssociationSAInterface * AddAnimationAndSyncHandler ( RpClump * pClump, CAnimBlendAssociationSAInterface * pAnimAssocToSyncWith, AssocGroupId animGroup, AnimationId animID );
543-
bool AssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSAInterface * pOutAnimStaticAssoc, SIFPAnimations ** pOutIFPAnimations, RpClump * pClump, CAnimBlendAssocGroupSAInterface * pAnimAssocGroup, AnimationId animID );
544-
bool BlendAnimationHierarchyHandler ( SIFPAnimations ** pOutIFPAnimations, CAnimBlendHierarchySAInterface ** pOutAnimHierarchy, RpClump * pClump );
544+
bool AssocGroupCopyAnimationHandler ( CAnimBlendStaticAssociationSAInterface * pOutAnimStaticAssoc, CAnimBlendAssociationSAInterface * pAnimAssoc, RpClump * pClump, CAnimBlendAssocGroupSAInterface * pAnimAssocGroup, AnimationId animID );
545+
bool BlendAnimationHierarchyHandler ( CAnimBlendAssociationSAInterface * pAnimAssoc, CAnimBlendHierarchySAInterface ** pOutAnimHierarchy, RpClump * pClump );
545546
bool ProcessCollisionHandler ( CEntitySAInterface* pThisInterface, CEntitySAInterface* pOtherInterface );
546547
bool VehicleCollisionHandler ( CVehicleSAInterface* pCollidingVehicle, CEntitySAInterface* pCollidedVehicle, int iModelIndex, float fDamageImpulseMag, float fCollidingDamageImpulseMag, uint16 usPieceType, CVector vecCollisionPos, CVector vecCollisionVelocity );
547548
bool VehicleDamageHandler ( CEntitySAInterface* pVehicleInterface, float fLoss, CEntitySAInterface* pAttackerInterface, eWeaponType weaponType, const CVector& vecDamagePos, uchar ucTyre );
@@ -601,7 +602,9 @@ class CClientGame
601602
CClientPed * GetClientPedByClump ( const RpClump & Clump );
602603

603604
void OnClientIFPUnload ( const std::shared_ptr < CClientIFP > & IFP );
604-
void DeleteIFPAnimations ( SIFPAnimations * pIFPAnimations );
605+
606+
void InsertAnimationAssociationToMap ( CAnimBlendAssociationSAInterface * pAnimAssociation, const std::shared_ptr < CIFPAnimations > & pIFPAnimations );
607+
void RemoveAnimationAssociationFromMap ( CAnimBlendAssociationSAInterface * pAnimAssociation );
605608

606609
private:
607610
eStatus m_Status;
@@ -819,7 +822,8 @@ class CClientGame
819822
// (SString) Key is custom block name that is supplied to engineLoadIFP
820823
std::map < SString, std::shared_ptr < CClientIFP > > m_mapOfIfpPointers;
821824

822-
std::map < CClientPed *, bool > m_mapOfPedPointers;
825+
std::map < CClientPed *, bool > m_mapOfPedPointers;
826+
AnimAssociations_type m_mapOfCustomAnimationAssociations;
823827
};
824828

825829
extern CClientGame* g_pClientGame;

Client/mods/deathmatch/logic/CClientIFP.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ CClientIFP::CClientIFP ( class CClientManager* pManager, ElementID ID ) : CClien
2424
m_pManager = pManager;
2525
SetTypeName ( "IFP" );
2626
m_bisIFPLoaded = false;
27-
m_pIFPAnimations = nullptr;
27+
//m_pIFPAnimations = nullptr;
28+
m_pIFPAnimations = std::make_shared < CIFPAnimations > ();
2829
}
2930

3031
CClientIFP::~CClientIFP ( void )
@@ -37,17 +38,13 @@ bool CClientIFP::LoadIFP ( const char* szFilePath, SString strBlockName )
3738
printf ("\nCClientIFP::LoadIFP: szFilePath %s\n szBlockName: %s\n\n", szFilePath, strBlockName.c_str());
3839

3940
m_strBlockName = strBlockName;
40-
m_pIFPAnimations = new SIFPAnimations;
41+
//m_pIFPAnimations ( new CIFPAnimations () );
4142
m_pVecAnimations = &m_pIFPAnimations->vecAnimations;
4243

4344
if ( LoadIFPFile ( szFilePath ) )
4445
{
4546
m_bisIFPLoaded = true;
4647
}
47-
else
48-
{
49-
delete m_pIFPAnimations;
50-
}
5148
return m_bisIFPLoaded;
5249
}
5350

@@ -58,7 +55,7 @@ void CClientIFP::UnloadIFP ( void )
5855
printf ("CClientIFP::UnloadIFP ( ) called!\n");
5956

6057
m_bisIFPLoaded = false;
61-
58+
/*
6259
// When all animations from this IFP block stop playing, and
6360
// the reference count reaches zero, IFP animations will be unloaded
6461
m_pIFPAnimations->bUnloadOnZeroReferences = true;
@@ -67,7 +64,7 @@ void CClientIFP::UnloadIFP ( void )
6764
if ( m_pIFPAnimations->iReferences == 0 )
6865
{
6966
g_pClientGame->DeleteIFPAnimations ( m_pIFPAnimations );
70-
}
67+
}*/
7168
}
7269
}
7370

Client/mods/deathmatch/logic/CClientIFP.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "CClientEntity.h"
77
#include "IFP/IFPLoader.h"
8+
#include "IFP/CIFPAnimations.h"
89
#include <algorithm>
910

1011
class CClientIFP: public CClientEntity, FileLoader
@@ -39,7 +40,7 @@ class CClientIFP: public CClientEntity, FileLoader
3940
size_t getCorrectBoneIndexFromID(int32_t & BoneID);
4041

4142
CAnimBlendHierarchySAInterface * GetAnimationHierarchy ( const SString & strAnimationName );
42-
inline SIFPAnimations * GetIFPAnimationsPointer ( void ) { return m_pIFPAnimations; }
43+
inline std::shared_ptr < CIFPAnimations > GetIFPAnimationsPointer ( void ) { return m_pIFPAnimations; }
4344

4445
// Sorta a hack that these are required by CClientEntity...
4546
void Unlink ( void ) {};
@@ -48,7 +49,7 @@ class CClientIFP: public CClientEntity, FileLoader
4849

4950

5051
private:
51-
SIFPAnimations * m_pIFPAnimations;
52+
std::shared_ptr < CIFPAnimations > m_pIFPAnimations;
5253
SString m_strBlockName;
5354
std::vector < IFP_Animation > * m_pVecAnimations;
5455
std::vector < unsigned char * > m_DummySequencesKeyFrames;

Client/mods/deathmatch/logic/CClientPed.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,8 @@ class CClientPed : public CClientStreamElement, public CAntiCheatModule
469469
inline void setNextAnimationCustom ( const SString & strBlockName, const SString & strAnimationName ) { m_bisNextAnimationCustom = true; m_strCustomIFPBlockName = strBlockName; m_strCustomIFPAnimationName = strAnimationName; }
470470
inline void setCurrentAnimationCustom ( bool bCustom ) { m_bisCurrentAnimationCustom = bCustom; }
471471
inline bool IsCurrentAnimationCustom ( void ) { return m_bisCurrentAnimationCustom; }
472-
inline SIFPAnimations * GetIFPAnimationsPointer ( void ) { return m_pIFPAnimations; }
473-
inline void SetIFPAnimationsPointer ( SIFPAnimations * pIFPAnimations ) { m_pIFPAnimations = pIFPAnimations; }
472+
inline CIFPAnimations * GetIFPAnimationsPointer ( void ) { return m_pIFPAnimations; }
473+
inline void SetIFPAnimationsPointer ( CIFPAnimations * pIFPAnimations ) { m_pIFPAnimations = pIFPAnimations; }
474474

475475
// This will indicate that we have played custom animation, so next animation can be internal GTA animation
476476
// You must call this function after playing a custom animation
@@ -684,7 +684,7 @@ class CClientPed : public CClientStreamElement, public CAntiCheatModule
684684
bool m_bisCurrentAnimationCustom;
685685
SString m_strCustomIFPBlockName;
686686
SString m_strCustomIFPAnimationName;
687-
SIFPAnimations * m_pIFPAnimations;
687+
CIFPAnimations * m_pIFPAnimations;
688688

689689
// Key: Internal GTA animation, Value: Custom Animation
690690
ReplacedAnim_type m_mapOfReplacedAnimations;

Client/mods/deathmatch/logic/IFP/IFPLoader.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,6 @@ enum BoneType
201201
R_TOE_0 = 54
202202
};
203203

204-
struct SIFPAnimations
205-
{
206-
// This is set to true when unloadIFP function is called, it means that animations
207-
// can be unloaded when iReferences reaches zero, if this boolean is set to true
208-
bool bUnloadOnZeroReferences;
209-
// incremented when a new CAnimBlendAssociationSAInteface is created and decremented
210-
// when destructor of that animation is called
211-
DWORD iReferences;
212-
std::vector < IFP_Animation > vecAnimations;
213-
214-
SIFPAnimations()
215-
{
216-
bUnloadOnZeroReferences = false;
217-
iReferences = 0;
218-
}
219-
};
220-
221-
222204
typedef void *(__cdecl* hCMemoryMgr_Malloc)
223205
(
224206
size_t TotalBytesToAllocate

0 commit comments

Comments
 (0)