Skip to content

Commit 6ec9bcc

Browse files
committed
Rewrote CFileReader and some code review improvements
CFileReader is using ifstream to load file into std::vector. No more dynamic memory allocation. & replaced std::vector::push_back with std::vector::emplace_back in CElementDeleter::OnClientIFPElementDestroy & Removed empty destructor from CClientIFP and empty constructor from CIFPAnimations - Suggested by Necktrox Used for (auto& pPed : m_setOfPedPointers) for iterating in CClientGame::OnClientIFPUnload. & using std::find_if in CElementDeleter::DeleteIFP with a lambda function - Suggested by sbx320 Removed #pragma once in CClientIFP.h and CIFPAnimations.h - Suggested by sbx320 and Necktrox
1 parent e467f13 commit 6ec9bcc

File tree

9 files changed

+84
-203
lines changed

9 files changed

+84
-203
lines changed

Client/mods/deathmatch/logic/CClientGame.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6820,17 +6820,17 @@ void CClientGame::RemovePedPointerFromSet(CClientPed* pPed)
68206820
CClientPed* CClientGame::GetClientPedByClump(const RpClump& Clump)
68216821
{
68226822
std::lock_guard<std::mutex> mutexGuardedLock(m_MutexOfPedPointersSet);
6823-
for (auto it = m_setOfPedPointers.begin(); it != m_setOfPedPointers.end(); it++)
6823+
for (auto &pPed : m_setOfPedPointers)
68246824
{
6825-
CEntity* pEntity = (*it)->GetGameEntity();
6825+
CEntity* pEntity = pPed->GetGameEntity();
68266826
if (pEntity != nullptr)
68276827
{
68286828
if (pEntity->GetRpClump() != nullptr)
68296829
{
68306830
const RpClump& entityClump = *pEntity->GetRpClump();
68316831
if (std::addressof(entityClump) == std::addressof(Clump))
68326832
{
6833-
return *it;
6833+
return pPed;
68346834
}
68356835
}
68366836
}
@@ -6842,26 +6842,26 @@ void CClientGame::OnClientIFPUnload(const std::shared_ptr<CClientIFP>& IFP)
68426842
{
68436843
std::lock_guard<std::mutex> mutexGuardedLock(m_MutexOfPedPointersSet);
68446844
IFP->MarkAsUnloading();
6845-
for (auto it = m_setOfPedPointers.begin(); it != m_setOfPedPointers.end(); it++)
6845+
for (auto &pPed : m_setOfPedPointers)
68466846
{
68476847
// Remove IFP animations from replaced animations of peds/players
6848-
(*it)->RestoreAnimations(IFP);
6848+
pPed->RestoreAnimations(IFP);
68496849

68506850
// Make sure that streamed in pulses or changing model does not accidently
68516851
// play our custom animation. We can do that by making the custom animation
68526852
// untriggerable
6853-
if ((*it)->GetCustomAnimationBlockNameHash() == IFP->GetBlockNameHash())
6853+
if (pPed->GetCustomAnimationBlockNameHash() == IFP->GetBlockNameHash())
68546854
{
6855-
if ((*it)->IsCustomAnimationPlaying())
6855+
if (pPed->IsCustomAnimationPlaying())
68566856
{
6857-
(*it)->SetCustomAnimationUntriggerable();
6857+
pPed->SetCustomAnimationUntriggerable();
68586858
}
68596859

68606860
// Important! As we are using a shared_ptr, we need to decrement the reference counter
68616861
// by setting the shared_ptr to nullptr, this will avoid memory leak
6862-
if (!(*it)->IsNextAnimationCustom() && (*it)->IsCurrentAnimationCustom())
6862+
if (!pPed->IsNextAnimationCustom() && pPed->IsCurrentAnimationCustom())
68636863
{
6864-
(*it)->DereferenceCustomAnimationBlock();
6864+
pPed->DereferenceCustomAnimationBlock();
68656865
}
68666866
}
68676867
}

Client/mods/deathmatch/logic/CClientIFP.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,22 @@ CClientIFP::CClientIFP(class CClientManager* pManager, ElementID ID) : CClientEn
1313
m_u32Hashkey = 0;
1414
}
1515

16-
CClientIFP::~CClientIFP(void)
17-
{
18-
// g_pCore->GetConsole ()->Printf( "CClientIFP::~CClientIFP called! BlockName: %s", m_strBlockName.c_str ( ) );
19-
}
20-
21-
bool CClientIFP::LoadIFP(const char* szFilePath, const SString& strBlockName)
16+
bool CClientIFP::LoadIFP(const SString& strFilePath, const SString& strBlockName)
2217
{
2318
m_strBlockName = strBlockName;
2419
m_pVecAnimations = &m_pIFPAnimations->vecAnimations;
2520

26-
if (LoadIFPFile(szFilePath))
21+
if (LoadIFPFile(strFilePath))
2722
{
2823
m_u32Hashkey = HashString(strBlockName.ToLower());
2924
return true;
3025
}
3126
return false;
3227
}
3328

34-
bool CClientIFP::LoadIFPFile(const char* szFilePath)
29+
bool CClientIFP::LoadIFPFile(const SString& strFilePath)
3530
{
36-
CreateLoader(szFilePath);
37-
38-
if (LoadFile())
31+
if (LoadFileToMemory(strFilePath))
3932
{
4033
char Version[4];
4134
ReadBytes(Version, sizeof(Version));
@@ -53,9 +46,9 @@ bool CClientIFP::LoadIFPFile(const char* szFilePath)
5346
ReadIFPVersion1();
5447
}
5548

56-
// We are unloading the data because we don't need to read it anymore.
49+
// We are freeing the file reader memory because we don't need to read it anymore.
5750
// This function does not unload IFP, to unload ifp, use destroyElement from Lua
58-
UnloadFile();
51+
FreeFileReaderMemory();
5952
}
6053
else
6154
{
@@ -279,7 +272,7 @@ void CClientIFP::ReadAnimationNameVersion1(SAnimation& IfpAnimation)
279272
RoundSize(Name.Base.Size);
280273

281274
char szAnimationName[24];
282-
ReadCString(szAnimationName, Name.Base.Size);
275+
ReadStringNullTerminated(szAnimationName, Name.Base.Size);
283276
IfpAnimation.Name = szAnimationName;
284277
IfpAnimation.u32NameHash = HashString(IfpAnimation.Name.ToLower());
285278
}
@@ -301,7 +294,7 @@ CClientIFP::eFrameType CClientIFP::ReadKfrm(void)
301294

302295
void CClientIFP::ReadAnimationHeaderVersion2(SAnimationHeaderV2& AnimationNode, bool bAnp3)
303296
{
304-
ReadCString((char*)&AnimationNode.Name, sizeof(SAnimationHeaderV2::Name));
297+
ReadStringNullTerminated((char*)&AnimationNode.Name, sizeof(SAnimationHeaderV2::Name));
305298
ReadBuffer<int32_t>(&AnimationNode.TotalObjects);
306299
if (bAnp3)
307300
{
@@ -354,9 +347,7 @@ void CClientIFP::ReadKrtsFramesAsCompressed(std::unique_ptr<CAnimBlendSequence>&
354347
CompressedKrt0->Rotation.Y = static_cast<int16_t>(((-Krts.Rotation.Y) * 4096.0f));
355348
CompressedKrt0->Rotation.Z = static_cast<int16_t>(((-Krts.Rotation.Z) * 4096.0f));
356349
CompressedKrt0->Rotation.W = static_cast<int16_t>((Krts.Rotation.W * 4096.0f));
357-
358350
CompressedKrt0->Time = static_cast<int16_t>((Krts.Time * 60.0f + 0.5f));
359-
360351
CompressedKrt0->Translation.X = static_cast<int16_t>((Krts.Translation.X * 1024.0f));
361352
CompressedKrt0->Translation.Y = static_cast<int16_t>((Krts.Translation.Y * 1024.0f));
362353
CompressedKrt0->Translation.Z = static_cast<int16_t>((Krts.Translation.Z * 1024.0f));
@@ -375,9 +366,7 @@ void CClientIFP::ReadKrt0FramesAsCompressed(std::unique_ptr<CAnimBlendSequence>&
375366
CompressedKrt0->Rotation.Y = static_cast<int16_t>(((-Krt0.Rotation.Y) * 4096.0f));
376367
CompressedKrt0->Rotation.Z = static_cast<int16_t>(((-Krt0.Rotation.Z) * 4096.0f));
377368
CompressedKrt0->Rotation.W = static_cast<int16_t>((Krt0.Rotation.W * 4096.0f));
378-
379369
CompressedKrt0->Time = static_cast<int16_t>((Krt0.Time * 60.0f + 0.5f));
380-
381370
CompressedKrt0->Translation.X = static_cast<int16_t>((Krt0.Translation.X * 1024.0f));
382371
CompressedKrt0->Translation.Y = static_cast<int16_t>((Krt0.Translation.Y * 1024.0f));
383372
CompressedKrt0->Translation.Z = static_cast<int16_t>((Krt0.Translation.Z * 1024.0f));
@@ -396,7 +385,6 @@ void CClientIFP::ReadKr00FramesAsCompressed(std::unique_ptr<CAnimBlendSequence>&
396385
CompressedKr00->Rotation.Y = static_cast<int16_t>(((-Kr00.Rotation.Y) * 4096.0f));
397386
CompressedKr00->Rotation.Z = static_cast<int16_t>(((-Kr00.Rotation.Z) * 4096.0f));
398387
CompressedKr00->Rotation.W = static_cast<int16_t>((Kr00.Rotation.W * 4096.0f));
399-
400388
CompressedKr00->Time = static_cast<int16_t>((Kr00.Time * 60.0f + 0.5f));
401389
}
402390
}

Client/mods/deathmatch/logic/CClientIFP.h

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
#pragma once
2-
31
#ifndef __CCLIENTIFP_H
42
#define __CCLIENTIFP_H
53

64
#include "CClientEntity.h"
75
#include "CFileReader.h"
86
#include "CIFPAnimations.h"
9-
#include <algorithm>
107

118
class CClientIFP : public CClientEntity, CFileReader
129
{
@@ -132,14 +129,12 @@ class CClientIFP : public CClientEntity, CFileReader
132129
struct SIFPHeaderV2
133130
{
134131
int32_t OffsetEOF;
135-
char InternalFilSName[24];
132+
char InternalFileName[24];
136133
int32_t TotalAnimations;
137134
};
138135

139136
struct SAnimationHeaderV2
140137
{
141-
// 24 + 4 + 4 + 4 = 36 bytes
142-
143138
char Name[24];
144139
int32_t TotalObjects;
145140
int32_t FrameSize;
@@ -192,16 +187,29 @@ class CClientIFP : public CClientEntity, CFileReader
192187
R_FOOT = 53,
193188
R_TOE_0 = 54
194189
};
190+
195191
CClientIFP(class CClientManager* pManager, ElementID ID);
196-
~CClientIFP(void);
197192

198193
virtual eClientEntityType GetType(void) const { return CCLIENTIFP; }
199194

200195
void MarkAsUnloading(void) { m_bUnloading = true; }
201196
bool IsUnloading(void) { return m_bUnloading; }
202197

203-
bool LoadIFP(const char* szFilePath, const SString& strBlockName);
204-
bool LoadIFPFile(const char* szFilePath);
198+
bool LoadIFP(const SString& strFilePath, const SString& strBlockName);
199+
200+
const SString& GetBlockName(void) { return m_strBlockName; }
201+
const unsigned int& GetBlockNameHash(void) { return m_u32Hashkey; }
202+
203+
CAnimBlendHierarchySAInterface* GetAnimationHierarchy(const SString& strAnimationName);
204+
std::shared_ptr<CIFPAnimations> GetIFPAnimationsPointer(void) { return m_pIFPAnimations; }
205+
206+
// Sorta a hack that these are required by CClientEntity...
207+
void Unlink(void){};
208+
void GetPosition(CVector& vecPosition) const {};
209+
void SetPosition(const CVector& vecPosition){};
210+
211+
private:
212+
bool LoadIFPFile(const SString& strFilePath);
205213
void ReadIFPVersion1(void);
206214
void ReadIFPVersion2(bool bAnp3);
207215

@@ -251,18 +259,6 @@ class CClientIFP : public CClientEntity, CFileReader
251259
std::string GetCorrectBoneNameFromName(std::string const& BoneName);
252260
std::string GetCorrectBoneNameFromID(int32_t& BoneID);
253261

254-
const SString& GetBlockName(void) { return m_strBlockName; }
255-
const unsigned int& GetBlockNameHash(void) { return m_u32Hashkey; }
256-
257-
CAnimBlendHierarchySAInterface* GetAnimationHierarchy(const SString& strAnimationName);
258-
std::shared_ptr<CIFPAnimations> GetIFPAnimationsPointer(void) { return m_pIFPAnimations; }
259-
260-
// Sorta a hack that these are required by CClientEntity...
261-
void Unlink(void){};
262-
void GetPosition(CVector& vecPosition) const {};
263-
void SetPosition(const CVector& vecPosition){};
264-
265-
private:
266262
std::shared_ptr<CIFPAnimations> m_pIFPAnimations;
267263
SString m_strBlockName;
268264
unsigned int m_u32Hashkey;

Client/mods/deathmatch/logic/CClientPed.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ void CClientPed::Init(CClientManager* pManager, unsigned long ulModelID, bool bI
9494
m_bisCurrentAnimationCustom = false;
9595
m_strCustomIFPBlockName = "Default";
9696
m_strCustomIFPAnimationName = "Default";
97+
m_u32CustomBlockNameHash = 0;
98+
m_u32CustomAnimationNameHash = 0;
9799
m_iVehicleInOutState = VEHICLE_INOUT_NONE;
98100
m_pPlayerPed = NULL;
99101
m_pTaskManager = NULL;

Client/mods/deathmatch/logic/CElementDeleter.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ void CElementDeleter::DeleteIFP(CClientEntity* pElement)
9292
{
9393
CClientIFP& IFP = static_cast<CClientIFP&>(*pElement);
9494
const unsigned int u32BlockNameHash = IFP.GetBlockNameHash();
95-
for (auto it = m_vecIFPElements.begin(); it != m_vecIFPElements.end(); ++it)
95+
96+
auto it = std::find_if(m_vecIFPElements.begin(), m_vecIFPElements.end(),
97+
[&u32BlockNameHash](std::shared_ptr<CClientIFP> const& pIFP)
9698
{
97-
if ((*it)->GetBlockNameHash() == u32BlockNameHash)
98-
{
99-
m_vecIFPElements.erase(it);
100-
break;
101-
}
99+
return pIFP->GetBlockNameHash() == u32BlockNameHash;
100+
});
101+
if(it != m_vecIFPElements.end())
102+
{
103+
m_vecIFPElements.erase(it);
102104
}
103105
}
104106

@@ -151,7 +153,7 @@ void CElementDeleter::OnClientIFPElementDestroy(CClientEntity* pElement)
151153

152154
// keep a reference to shared_ptr CClientIFP in list, so it does not get
153155
// destroyed after exiting this function
154-
m_vecIFPElements.push_back(pIFP);
156+
m_vecIFPElements.emplace_back(pIFP);
155157
}
156158
}
157159

0 commit comments

Comments
 (0)