Skip to content

Commit a9a8066

Browse files
committed
[FIX] unloadIFP function works good now.
1 parent 76f2d02 commit a9a8066

File tree

4 files changed

+70
-66
lines changed

4 files changed

+70
-66
lines changed

Client/mods/deathmatch/logic/CClientIFP.cpp

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <StdInc.h>
22

33
h_CAnimBlendHierarchy_SetName OLD__CAnimBlendHierarchy_SetName = (h_CAnimBlendHierarchy_SetName)0x4CF2D0;
4+
hCAnimBlendHierarchy_RemoveAnimSequences OLD_CAnimBlendHierarchy_RemoveAnimSequences = (hCAnimBlendHierarchy_RemoveAnimSequences)0x4CF8E0;
5+
hCAnimBlendHierarchy_RemoveFromUncompressedCache OLD_CAnimBlendHierarchy_RemoveFromUncompressedCache = (hCAnimBlendHierarchy_RemoveFromUncompressedCache)0x004D42A0;
46

57
hCMemoryMgr_Malloc OLD_CMemoryMgr_Malloc = (hCMemoryMgr_Malloc)0x0072F420;
8+
hCMemoryMgr_Free OLD_CMemoryMgr_Free = (hCMemoryMgr_Free)0x0072F430;
69
h_CAnimBlendSequence_SetName OLD__CAnimBlendSequence_SetName = (h_CAnimBlendSequence_SetName)0x4D0C50;
710
h_CAnimBlendSequence_SetBoneTag OLD__CAnimBlendSequence_SetBoneTag = (h_CAnimBlendSequence_SetBoneTag)0x4D0C70;
811
h_CAnimBlendSequence_SetNumFrames OLD__CAnimBlendSequence_SetNumFrames = (h_CAnimBlendSequence_SetNumFrames)0x4D0CD0;
@@ -18,6 +21,7 @@ CClientIFP::CClientIFP ( class CClientManager* pManager, ElementID ID ) : CClien
1821
// Init
1922
m_pManager = pManager;
2023
SetTypeName ( "IFP" );
24+
m_bisIFPLoaded = false;
2125
}
2226

2327
CClientIFP::~CClientIFP ( void )
@@ -27,47 +31,59 @@ CClientIFP::~CClientIFP ( void )
2731

2832
bool CClientIFP::LoadIFP ( const char* szFilePath, SString strBlockName )
2933
{
30-
//printf ("\nCClientIFP::LoadIFP: szFilePath %s\n szBlockName: %s\n\n", szFilePath, szBlockName);
34+
printf ("\nCClientIFP::LoadIFP: szFilePath %s\n szBlockName: %s\n\n", szFilePath, strBlockName.c_str());
3135

3236
m_strBlockName = strBlockName;
3337

34-
return LoadIFPFile ( szFilePath );
38+
if (LoadIFPFile(szFilePath))
39+
{
40+
m_bisIFPLoaded = true;
41+
return true;
42+
}
43+
return false;
3544
}
3645

37-
// Temporary method to avoid memory leaks, we'll need to rewrite the entire thing ;)
46+
3847
void CClientIFP::UnloadIFP ( void )
3948
{
40-
printf ("CClientIFP::UnloadIFP ( ) called, but IFP is not unloaded, PLEASE FIX THIS LATER!\n");
41-
/*
42-
printf ("CClientIFP::UnloadIFP ( ) called!\n");
49+
if ( m_bisIFPLoaded )
50+
{
51+
printf ("CClientIFP::UnloadIFP ( ) called!\n");
4352

44-
for ( size_t i = 0; i < m_Animations.size(); i++ )
45-
{
46-
IFP_Animation * ifpAnimation = &m_Animations[i];
47-
if ( isVersion1 )
48-
{
53+
54+
for ( size_t i = 0; i < m_Animations.size(); i++ )
55+
{
56+
IFP_Animation * ifpAnimation = &m_Animations[i];
57+
58+
OLD_CAnimBlendHierarchy_RemoveFromUncompressedCache ( (int)&ifpAnimation->Hierarchy );
59+
4960
for (unsigned short SequenceIndex = 0; SequenceIndex < ifpAnimation->Hierarchy.m_nSeqCount; SequenceIndex++)
5061
{
5162
_CAnimBlendSequence * pSequence = (_CAnimBlendSequence*)((BYTE*)ifpAnimation->Hierarchy.m_pSequences + (sizeof(_CAnimBlendSequence) * SequenceIndex));
52-
free ( pSequence->m_pFrames );
63+
64+
if ( !( (pSequence->m_nFlags >> 3) & 1 ) ) // If ( !OneBigChunkForAllSequences )
65+
{
66+
OLD_CMemoryMgr_Free ( pSequence->m_pFrames ); //*(void **)(pThis + 8)); //pSequence->m_pFrames );
67+
}
68+
else
69+
{
70+
if ( SequenceIndex == 0 )
71+
{
72+
// All frames of all sequences are allocated on one memory block, so free that one
73+
// and break the loop
74+
OLD_CMemoryMgr_Free ( pSequence->m_pFrames );
75+
break;
76+
}
77+
}
78+
5379
}
54-
}
55-
else
56-
{
57-
free ( ifpAnimation->pFramesMemoryVersion2 );
80+
delete ifpAnimation->pSequencesMemory;
5881
}
5982

60-
delete ifpAnimation->pSequencesMemory;
61-
}
83+
g_pClientGame->RemoveIFPPointerFromMap ( m_strBlockName );
6284

63-
for (size_t DummySequenceIndex = 0; DummySequenceIndex < m_DummySequencesKeyFrames.size(); DummySequenceIndex++)
64-
{
65-
unsigned char * pKeyFrames = m_DummySequencesKeyFrames [ DummySequenceIndex ];
66-
free ( pKeyFrames );
67-
}*/
68-
69-
g_pClientGame->RemoveIFPPointerFromMap ( m_strBlockName );
70-
//printf ("IFP unloaded sucessfully, removed from map as well.\n");
85+
printf ("IFP unloaded sucessfully, removed from map as well.\n");
86+
}
7187
}
7288

7389
bool CClientIFP::LoadIFPFile(const char * FilePath)
@@ -136,17 +152,12 @@ void CClientIFP::ReadIFPVersion2( bool anp3)
136152

137153
printf("Animation Name: %s | Index: %d \n", AnimationNode.Name, i);
138154

139-
unsigned char * pKeyFrames = nullptr;
140155
if (anp3)
141156
{
142157
readBuffer < int32_t >(&AnimationNode.FrameSize);
143158
readBuffer < int32_t >(&AnimationNode.isCompressed);
144159

145-
pAnimHierarchy->m_bRunningCompressed = AnimationNode.isCompressed & 1;
146-
147-
pKeyFrames = (unsigned char*) OLD_CMemoryMgr_Malloc(AnimationNode.FrameSize); //malloc(AnimationNode.FrameSize);
148-
149-
ifpAnimation.pFramesMemoryVersion2 = pKeyFrames;
160+
pAnimHierarchy->m_bRunningCompressed = AnimationNode.isCompressed & 1;
150161
}
151162

152163
OLD__CAnimBlendHierarchy_SetName(pAnimHierarchy, AnimationNode.Name);
@@ -156,12 +167,9 @@ void CClientIFP::ReadIFPVersion2( bool anp3)
156167
pAnimHierarchy->field_B = 0;
157168

158169
const unsigned short TotalSequences = IFP_TOTAL_SEQUENCES + pAnimHierarchy->m_nSeqCount;
159-
char * pNewSequencesMemory = ( char * ) operator new ( 12 * TotalSequences + 4 ); // Allocate memory for sequences ( 12 * seq_count + 4 )
170+
ifpAnimation.pSequencesMemory = ( char * ) operator new ( 12 * TotalSequences + 4 ); // Allocate memory for sequences ( 12 * seq_count + 4 )
160171

161-
// Okay, we have assigned the memory. We can free it when we want to
162-
ifpAnimation.pSequencesMemory = pNewSequencesMemory;
163-
164-
pAnimHierarchy->m_pSequences = ( _CAnimBlendSequence * )( pNewSequencesMemory + 4 );
172+
pAnimHierarchy->m_pSequences = ( _CAnimBlendSequence * )( ifpAnimation.pSequencesMemory + 4 );
165173

166174
std::map < std::string, _CAnimBlendSequence > MapOfSequences;
167175

@@ -265,6 +273,8 @@ void CClientIFP::ReadIFPVersion2( bool anp3)
265273
}
266274
if (!bInvalidType)
267275
{
276+
unsigned char * pKeyFrames = (unsigned char*) OLD_CMemoryMgr_Malloc ( data_size );
277+
268278
if ( bUnknownSequence )
269279
{
270280
OLD__CAnimBlendSequence_SetNumFrames ( pUnkownSequence, ObjectNode.TotalFrames, bIsRoot, bIsCompressed, pKeyFrames );
@@ -274,13 +284,7 @@ void CClientIFP::ReadIFPVersion2( bool anp3)
274284
OLD__CAnimBlendSequence_SetNumFrames ( &Sequence, ObjectNode.TotalFrames, bIsRoot, bIsCompressed, pKeyFrames );
275285
}
276286

277-
readBytes ( Sequence.m_pFrames, data_size );
278-
279-
if (anp3)
280-
{
281-
pKeyFrames += data_size;
282-
Sequence.m_nFlags |= 8u;
283-
}
287+
readBytes ( pKeyFrames, data_size );
284288

285289
if ( !bUnknownSequence )
286290
{
@@ -304,11 +308,11 @@ void CClientIFP::ReadIFPVersion2( bool anp3)
304308
}
305309
else
306310
{
307-
insertAnimDummySequence(anp3, pAnimHierarchy, SequenceIndex);
311+
insertAnimDummySequence ( pAnimHierarchy, SequenceIndex );
308312
}
309313
}
310-
311-
*(DWORD *)pNewSequencesMemory = IFP_TOTAL_SEQUENCES + TotalUnknownSequences;
314+
315+
*(DWORD *)ifpAnimation.pSequencesMemory = IFP_TOTAL_SEQUENCES + TotalUnknownSequences;
312316

313317
// This order is very important. As we need support for all 32 bones, we must change the total sequences count
314318
pAnimHierarchy->m_nSeqCount = IFP_TOTAL_SEQUENCES + TotalUnknownSequences;
@@ -383,12 +387,9 @@ void CClientIFP::ReadIFPVersion1 ( )
383387
pAnimHierarchy->field_B = 0;
384388

385389
const unsigned short TotalSequences = IFP_TOTAL_SEQUENCES + pAnimHierarchy->m_nSeqCount;
386-
char * pNewSequencesMemory = ( char * ) operator new ( 12 * TotalSequences + 4 ); // Allocate memory for sequences ( 12 * seq_count + 4 )
387-
388-
// Okay, we have assigned the memory. We can free it when we want to
389-
ifpAnimation.pSequencesMemory = pNewSequencesMemory;
390+
ifpAnimation.pSequencesMemory = ( char * ) operator new ( 12 * TotalSequences + 4 ); // Allocate memory for sequences ( 12 * seq_count + 4 )
390391

391-
pAnimHierarchy->m_pSequences = ( _CAnimBlendSequence * )( pNewSequencesMemory + 4 );
392+
pAnimHierarchy->m_pSequences = ( _CAnimBlendSequence * )( ifpAnimation.pSequencesMemory+ 4 );
392393

393394
std::map < std::string, _CAnimBlendSequence > MapOfSequences;
394395

@@ -496,7 +497,7 @@ void CClientIFP::ReadIFPVersion1 ( )
496497
else if (FrameType == IFP_FrameType::KR00)
497498
{
498499
//ofs << " | FrameType: KR00" << std::endl;
499-
ReadKr00FramesAsCompressed ( pKeyFrames, Anim.Frames, BoneID );
500+
ReadKr00FramesAsCompressed ( pKeyFrames, Anim.Frames );
500501
}
501502

502503
if (!bUnknownSequence)
@@ -523,11 +524,11 @@ void CClientIFP::ReadIFPVersion1 ( )
523524
}
524525
else
525526
{
526-
insertAnimDummySequence ( false, pAnimHierarchy, SequenceIndex );
527+
insertAnimDummySequence ( pAnimHierarchy, SequenceIndex );
527528
}
528529
}
529530

530-
*(DWORD *)pNewSequencesMemory = IFP_TOTAL_SEQUENCES + TotalUnknownSequences;
531+
*(DWORD *)ifpAnimation.pSequencesMemory = IFP_TOTAL_SEQUENCES + TotalUnknownSequences;
531532

532533
// This order is very important. As we need support for all 32 bones, we must change the total sequences count
533534
pAnimHierarchy->m_nSeqCount = IFP_TOTAL_SEQUENCES + TotalUnknownSequences;
@@ -588,7 +589,7 @@ void CClientIFP::ReadKrt0FramesAsCompressed ( BYTE * pKeyFrames, int32_t TotalF
588589
}
589590
}
590591

591-
void CClientIFP::ReadKr00FramesAsCompressed ( BYTE * pKeyFrames, int32_t TotalFrames, int32_t BoneID )
592+
void CClientIFP::ReadKr00FramesAsCompressed ( BYTE * pKeyFrames, int32_t TotalFrames )
592593
{
593594
for (int32_t FrameIndex = 0; FrameIndex < TotalFrames; FrameIndex++)
594595
{
@@ -654,7 +655,7 @@ IFP_FrameType CClientIFP::getFrameTypeFromFourCC ( char * FourCC )
654655
}
655656

656657

657-
void CClientIFP::insertAnimDummySequence ( bool anp3, _CAnimBlendHierarchy * pAnimHierarchy, size_t SequenceIndex)
658+
void CClientIFP::insertAnimDummySequence ( _CAnimBlendHierarchy * pAnimHierarchy, size_t SequenceIndex)
658659
{
659660
const char * BoneName = BoneNames [SequenceIndex ];
660661
DWORD BoneID = BoneIds [SequenceIndex];
@@ -927,11 +928,6 @@ void CClientIFP::insertAnimDummySequence ( bool anp3, _CAnimBlendHierarchy * pAn
927928
//ofs << " ERROR: BoneID is not being handled!" << std::endl;
928929
}
929930
}
930-
931-
if (anp3)
932-
{
933-
pSequence->m_nFlags |= 8u; //EXTERNAL_KEYFRAMES_MEM;
934-
}
935931
}
936932

937933

Client/mods/deathmatch/logic/CClientIFP.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ class CClientIFP: public CClientEntity, FileLoader
3030
void ReadKrt0FramesAsCompressed ( BYTE * pKeyFrames, int32_t TotalFrames );
3131
void ReadKr00FramesAsCompressed ( BYTE * pKeyFrames, int32_t TotalFrames );
3232

33-
// Remove this ugly function and use ReadKr00FramesAsCompressed instead
34-
void ReadKr00FramesAsCompressed ( BYTE * pKeyFrames, int32_t TotalFrames, int32_t BoneID );
35-
36-
void insertAnimDummySequence(bool anp3, _CAnimBlendHierarchy * pAnimHierarchy, size_t SequenceIndex);
33+
void insertAnimDummySequence ( _CAnimBlendHierarchy * pAnimHierarchy, size_t SequenceIndex);
3734
int32_t getBoneIDFromName(std::string const& BoneName);
3835
std::string getCorrectBoneNameFromName(std::string const& BoneName);
3936
std::string getCorrectBoneNameFromID(int32_t & BoneID);
@@ -53,6 +50,7 @@ class CClientIFP: public CClientEntity, FileLoader
5350
std::vector < unsigned char * > m_DummySequencesKeyFrames;
5451
bool isVersion1;
5552
IFPHeaderV2 HeaderV2;
53+
bool m_bisIFPLoaded;
5654

5755
};
5856

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ typedef void *(__cdecl* hCMemoryMgr_Malloc)
209209
);
210210

211211

212+
typedef void *(__cdecl* hCMemoryMgr_Free)
213+
(
214+
void *a1
215+
);
216+
212217
void _CAnimBlendHierarchy_Constructor(_CAnimBlendHierarchy * pAnimHierarchy);
213218
void _CAnimBlendSequence_Constructor(_CAnimBlendSequence * pSequence);
214219

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ typedef void (__cdecl * hUncompressAnimation)
249249
_CAnimBlendHierarchy * pAnimBlendHierarchy
250250
);
251251

252+
typedef int (__cdecl * hCAnimBlendHierarchy_RemoveFromUncompressedCache)
253+
(int a1);
254+
252255
typedef CAnimBlendAssoc * (__thiscall * hCAnimBlendAssoc_Constructor_staticAssocRef)
253256
(
254257
CAnimBlendAssoc * pThis,
@@ -345,6 +348,8 @@ void __cdecl NEW_UncompressAnimation
345348
_CAnimBlendHierarchy * pAnimBlendHierarchy
346349
);
347350

351+
typedef void (__thiscall* hCAnimBlendHierarchy_RemoveAnimSequences)
352+
( DWORD * pThis);
348353

349354
const char * GetNameFromHash(DWORD Hash);
350355
int GetNumAnimations(void);

0 commit comments

Comments
 (0)