Skip to content

Commit 19f1156

Browse files
committed
Replaced raw pointer with shared_ptr for CClientIFP
1 parent 19aae47 commit 19f1156

File tree

10 files changed

+66
-30
lines changed

10 files changed

+66
-30
lines changed

Client/mods/deathmatch/logic/CClientGame.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,7 +4090,7 @@ bool CClientGame::BlendAnimationHierarchyHandler ( SIFPAnimations ** pOutIFPAni
40904090
if ( pClientPed->isNextAnimationCustom () )
40914091
{
40924092
const SString & strBlockName = pClientPed->GetNextAnimationCustomBlockName ( );
4093-
CClientIFP * pIFP = GetIFPPointerFromMap ( strBlockName );
4093+
std::shared_ptr < CClientIFP > pIFP = GetIFPPointerFromMap ( strBlockName );
40944094
if ( pIFP )
40954095
{
40964096
const SString & strAnimationName = pClientPed->GetNextAnimationCustomName ( );
@@ -6859,7 +6859,7 @@ void CClientGame::RestreamModel ( unsigned short usModel )
68596859

68606860
}
68616861

6862-
void CClientGame::InsertIFPPointerToMap ( const SString & strBlockName, CClientIFP * pIFP )
6862+
void CClientGame::InsertIFPPointerToMap ( const SString & strBlockName, const std::shared_ptr < CClientIFP > & pIFP )
68636863
{
68646864
const SString mapKey = strBlockName.ToLower ( );
68656865
if ( m_mapOfIfpPointers.count ( mapKey ) == 0 )
@@ -6873,17 +6873,18 @@ void CClientGame::RemoveIFPPointerFromMap ( const SString & strBlockName )
68736873
m_mapOfIfpPointers.erase ( strBlockName.ToLower ( ) );
68746874
}
68756875

6876-
CClientIFP * CClientGame::GetIFPPointerFromMap ( const SString & strBlockName )
6876+
std::shared_ptr < CClientIFP > CClientGame::GetIFPPointerFromMap ( const SString & strBlockName )
68776877
{
68786878
const SString mapKey = strBlockName.ToLower ( );
6879-
std::map < SString, CClientIFP * >::iterator it = m_mapOfIfpPointers.find ( mapKey );
6879+
auto it = m_mapOfIfpPointers.find ( mapKey );
68806880
if ( it != m_mapOfIfpPointers.end ( ) )
68816881
{
68826882
return it->second;
68836883
}
68846884
return nullptr;
68856885
}
68866886

6887+
68876888
void CClientGame::InsertPedPointerToMap ( CClientPed * pPed )
68886889
{
68896890
if ( m_mapOfPedPointers.count ( pPed ) == 0 )
@@ -6917,7 +6918,7 @@ CClientPed * CClientGame::GetClientPedByClump ( const RpClump & Clump )
69176918
return nullptr;
69186919
}
69196920

6920-
void CClientGame::OnClientIFPUnload ( const CClientIFP & IFP )
6921+
void CClientGame::OnClientIFPUnload ( const std::shared_ptr < CClientIFP > & IFP )
69216922
{
69226923
// remove IFP animations from replaced animations of peds/players
69236924
for ( auto it = m_mapOfPedPointers.begin(); it != m_mapOfPedPointers.end(); it++ )

Client/mods/deathmatch/logic/CClientGame.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,15 +592,15 @@ class CClientGame
592592
void SetFileCacheRoot ( void );
593593
const char* GetFileCacheRoot ( void ) { return m_strFileCacheRoot; }
594594

595-
void InsertIFPPointerToMap ( const SString & strBlockName, CClientIFP * pIFP );
595+
void InsertIFPPointerToMap ( const SString & strBlockName, const std::shared_ptr < CClientIFP > & pIFP );
596596
void RemoveIFPPointerFromMap ( const SString & strBlockName );
597-
CClientIFP * GetIFPPointerFromMap ( const SString & strBlockName );
597+
std::shared_ptr < CClientIFP > GetIFPPointerFromMap ( const SString & strBlockName );
598598

599599
void InsertPedPointerToMap ( CClientPed * pPed );
600600
void RemovePedPointerFromMap ( CClientPed * pPed );
601601
CClientPed * GetClientPedByClump ( const RpClump & Clump );
602602

603-
void OnClientIFPUnload ( const CClientIFP & IFP );
603+
void OnClientIFPUnload ( const std::shared_ptr < CClientIFP > & IFP );
604604
void DeleteIFPAnimations ( SIFPAnimations * pIFPAnimations );
605605

606606
private:
@@ -817,7 +817,7 @@ class CClientGame
817817
SharedUtil::CAsyncTaskScheduler* m_pAsyncTaskScheduler;
818818

819819
// (SString) Key is custom block name that is supplied to engineLoadIFP
820-
std::map < SString, CClientIFP * > m_mapOfIfpPointers;
820+
std::map < SString, std::shared_ptr < CClientIFP > > m_mapOfIfpPointers;
821821

822822
std::map < CClientPed *, bool > m_mapOfPedPointers;
823823
};

Client/mods/deathmatch/logic/CClientIFP.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ void CClientIFP::UnloadIFP ( void )
5959

6060
m_bisIFPLoaded = false;
6161

62-
// Remove IFP from map, so we can indicate that it does not exist
63-
g_pClientGame->RemoveIFPPointerFromMap ( m_strBlockName );
64-
65-
// Remove IFP animations from replaced animations of peds/players
66-
g_pClientGame->OnClientIFPUnload ( *this );
67-
6862
// When all animations from this IFP block stop playing, and
6963
// the reference count reaches zero, IFP animations will be unloaded
7064
m_pIFPAnimations->bUnloadOnZeroReferences = true;

Client/mods/deathmatch/logic/CClientIFP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CClientIFP: public CClientEntity, FileLoader
1515

1616
virtual eClientEntityType GetType ( void ) const { return CCLIENTIFP; }
1717

18+
const SString & GetBlockName ( void ) { return m_strBlockName; }
1819
bool isIFPLoaded ( void ) { return m_bisIFPLoaded; }
1920
bool LoadIFP ( const char* szFilePath, SString strBlockName );
2021
void UnloadIFP ( void );

Client/mods/deathmatch/logic/CClientPed.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6191,12 +6191,12 @@ CAnimBlendAssociation * CClientPed::GetFirstAnimation ( void )
61916191
return NULL;
61926192
}
61936193

6194-
bool CClientPed::ReplaceAnimation ( CAnimBlendHierarchy * pInternalAnimHierarchy, CClientIFP * pIFP, CAnimBlendHierarchySAInterface * pCustomAnimHierarchy )
6194+
bool CClientPed::ReplaceAnimation ( CAnimBlendHierarchy * pInternalAnimHierarchy, const std::shared_ptr < CClientIFP > & pIFP, CAnimBlendHierarchySAInterface * pCustomAnimHierarchy )
61956195
{
61966196
if ( pIFP->isIFPLoaded ( ) )
61976197
{
61986198
SReplacedAnimation replacedAnimation;
6199-
replacedAnimation.pIFP = pIFP;
6199+
replacedAnimation.pIFP = pIFP;
62006200
replacedAnimation.pAnimationHierarchy = pCustomAnimHierarchy;
62016201

62026202
m_mapOfReplacedAnimations [ pInternalAnimHierarchy->GetInterface () ] = replacedAnimation;
@@ -6210,12 +6210,11 @@ void CClientPed::RestoreAnimation ( CAnimBlendHierarchy * pInternalAnimHierarchy
62106210
m_mapOfReplacedAnimations.erase ( pInternalAnimHierarchy->GetInterface () );
62116211
}
62126212

6213-
void CClientPed::RestoreAnimations ( const CClientIFP & IFP )
6213+
void CClientPed::RestoreAnimations ( const std::shared_ptr < CClientIFP > & IFP )
62146214
{
62156215
for ( auto const& x : m_mapOfReplacedAnimations )
62166216
{
6217-
const CClientIFP & replacedAnimationIFP = *x.second.pIFP;
6218-
if ( std::addressof ( IFP ) == std::addressof ( replacedAnimationIFP ) )
6217+
if ( std::addressof ( *IFP.get ( ) ) == std::addressof ( *x.second.pIFP.get ( ) ) )
62196218
{
62206219
m_mapOfReplacedAnimations.erase ( x.first );
62216220
}

Client/mods/deathmatch/logic/CClientPed.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class CClientIFP;
122122

123123
struct SReplacedAnimation
124124
{
125-
CClientIFP * pIFP;
125+
std::shared_ptr < CClientIFP > pIFP;
126126
CAnimBlendHierarchySAInterface * pAnimationHierarchy;
127127
};
128128

@@ -478,9 +478,9 @@ class CClientPed : public CClientStreamElement, public CAntiCheatModule
478478
inline const SString & GetNextAnimationCustomBlockName ( void ) { return m_strCustomIFPBlockName; }
479479
inline const SString & GetNextAnimationCustomName ( void ) { return m_strCustomIFPAnimationName; }
480480

481-
bool ReplaceAnimation ( CAnimBlendHierarchy * pInternalAnimHierarchy, CClientIFP * pIFP, CAnimBlendHierarchySAInterface * pCustomAnimHierarchy );
481+
bool ReplaceAnimation ( CAnimBlendHierarchy * pInternalAnimHierarchy, const std::shared_ptr < CClientIFP > & pIFP, CAnimBlendHierarchySAInterface * pCustomAnimHierarchy );
482482
void RestoreAnimation ( CAnimBlendHierarchy * pInternalAnimHierarchy );
483-
void RestoreAnimations ( const CClientIFP & IFP );
483+
void RestoreAnimations ( const std::shared_ptr < CClientIFP > & IFP );
484484
void RestoreAnimations ( CAnimBlock & animationBlock );
485485
void RestoreAllAnimations ( void );
486486
SReplacedAnimation * getReplacedAnimation ( CAnimBlendHierarchySAInterface * pInternalHierarchyInterface );

Client/mods/deathmatch/logic/CElementDeleter.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,42 @@ void CElementDeleter::DeleteRecursive ( class CClientEntity* pElement )
7777
pElement->Unlink ();
7878
}
7979

80+
// If DeleteElementSpecial returns true then do not access pElement
81+
// because element has been deleted
82+
bool CElementDeleter::DeleteElementSpecial ( CClientEntity* pElement )
83+
{
84+
// Mark us as being deleted, unlink from parent and unlink from manager classes eventually
85+
pElement->SetBeingDeleted ( true );
86+
pElement->SetParent ( NULL );
87+
pElement->Unlink ();
88+
89+
if ( IS_IFP ( pElement ) )
90+
{
91+
DeleteIFP ( pElement );
92+
return true;
93+
}
94+
return false;
95+
}
96+
97+
98+
void CElementDeleter::DeleteIFP ( CClientEntity * pElement )
99+
{
100+
CClientIFP & IFP = static_cast < CClientIFP& > ( *pElement );
101+
const SString & strBlockName = IFP.GetBlockName ( );
102+
std::shared_ptr < CClientIFP > pIFP = g_pClientGame->GetIFPPointerFromMap ( strBlockName );
103+
if ( pIFP )
104+
{
105+
// Remove IFP from map, so we can indicate that it does not exist
106+
g_pClientGame->RemoveIFPPointerFromMap ( strBlockName );
107+
108+
// Remove IFP animations from replaced animations of peds/players
109+
g_pClientGame->OnClientIFPUnload ( pIFP );
110+
111+
//delete pElement; //------------------- REMOVE THIS LATER
112+
printf ("done calling g_pClientGame->OnClientIFPUnload\n");
113+
}
114+
}
115+
80116

81117
void CElementDeleter::DoDeleteAll ( void )
82118
{
@@ -89,8 +125,11 @@ void CElementDeleter::DoDeleteAll ( void )
89125
{
90126
CClientEntity* pEntity = *iter;
91127

92-
// Delete the entity and put the next element in the list in the iterator
93-
delete pEntity;
128+
if ( !DeleteElementSpecial ( pEntity ) )
129+
{
130+
// Delete the entity and put the next element in the list in the iterator
131+
delete pEntity;
132+
}
94133
iter = m_List.erase ( iter );
95134
}
96135

Client/mods/deathmatch/logic/CElementDeleter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class CElementDeleter
2525

2626
void Delete ( class CClientEntity* pElement );
2727
void DeleteRecursive ( class CClientEntity* pElement );
28+
bool DeleteElementSpecial ( CClientEntity* pElement );
29+
void DeleteIFP ( CClientEntity * pElement );
2830

2931
void DoDeleteAll ( void );
3032

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ bool CStaticFunctionDefinitions::SetPedAnimation ( CClientEntity& Entity, const
21352135
}
21362136
else
21372137
{
2138-
CClientIFP * pIFP = g_pClientGame->GetIFPPointerFromMap ( szBlockName );
2138+
std::shared_ptr < CClientIFP > pIFP = g_pClientGame->GetIFPPointerFromMap ( szBlockName );
21392139

21402140
// Is this a custom animation block?
21412141
if ( pIFP )

Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ int CLuaEngineDefs::EngineLoadIFP ( lua_State* luaVM )
312312
if ( g_pClientGame->GetIFPPointerFromMap ( strBlockName ) == nullptr )
313313
{
314314
// Create a IFP element
315-
CClientIFP* pIFP = new CClientIFP ( m_pManager, INVALID_ELEMENT_ID );
315+
std::shared_ptr < CClientIFP > pIFP ( new CClientIFP ( m_pManager, INVALID_ELEMENT_ID ) );
316316

317317
// Try to load the IFP file
318318
if ( pIFP->LoadIFP ( strPath, strBlockName ) )
@@ -324,13 +324,13 @@ int CLuaEngineDefs::EngineLoadIFP ( lua_State* luaVM )
324324
pIFP->SetParent ( pRoot );
325325

326326
// Return the IFP element
327-
lua_pushelement ( luaVM, pIFP );
327+
lua_pushelement ( luaVM, pIFP.get ( ) );
328328
return 1;
329329
}
330330
else
331331
{
332332
// Delete it again
333-
delete pIFP;
333+
//delete pIFP;
334334
argStream.SetCustomError ( strFile, "Error loading IFP" );
335335
}
336336
}
@@ -531,7 +531,7 @@ int CLuaEngineDefs::EngineReplaceAnimation ( lua_State* luaVM )
531531
CClientPed& Ped = static_cast < CClientPed& > ( *pEntity );
532532

533533
CAnimBlock * pInternalBlock = g_pGame->GetAnimManager ()->GetAnimationBlock ( strInternalBlockName );
534-
CClientIFP * pCustomIFP = g_pClientGame->GetIFPPointerFromMap ( strCustomBlockName );
534+
std::shared_ptr < CClientIFP > pCustomIFP = g_pClientGame->GetIFPPointerFromMap ( strCustomBlockName );
535535
if ( pInternalBlock && pCustomIFP )
536536
{
537537
CAnimBlendHierarchy * pInternalAnimHierarchy = g_pGame->GetAnimManager ()->GetAnimation ( strInternalAnimName, pInternalBlock );

0 commit comments

Comments
 (0)