Skip to content

Commit 015fd39

Browse files
committed
Added CIFPEngine which contains most logic for Lua IFP functions
1 parent 8bbd104 commit 015fd39

File tree

4 files changed

+124
-94
lines changed

4 files changed

+124
-94
lines changed

Client/mods/deathmatch/StdInc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
#include <CLogger.h>
9595
#include <CMapEventManager.h>
9696
#include <CModelNames.h>
97+
#include <CIFPEngine.h>
9798
#include <CScriptFile.h>
9899
#include <CWeaponNames.h>
99100
#include <CVehicleNames.h>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <StdInc.h>
2+
3+
std::shared_ptr < CClientIFP > CIFPEngine::EngineLoadIFP ( CResource * pResource, CClientManager * pManager, const SString & strPath, const SString & strBlockName )
4+
{
5+
// Grab the resource root entity
6+
CClientEntity* pRoot = pResource->GetResourceIFPRoot ( );
7+
8+
// Check whether the IFP blockname exists or not
9+
if ( g_pClientGame->GetIFPPointerFromMap ( strBlockName ) == nullptr )
10+
{
11+
// Create a IFP element
12+
std::shared_ptr < CClientIFP > pIFP ( new CClientIFP ( pManager, INVALID_ELEMENT_ID ) );
13+
14+
// Try to load the IFP file
15+
if ( pIFP->LoadIFP ( strPath, strBlockName ) )
16+
{
17+
// We can use the map to retrieve correct IFP by block name later
18+
g_pClientGame->InsertIFPPointerToMap ( strBlockName, pIFP );
19+
20+
// Success loading the file. Set parent to IFP root
21+
pIFP->SetParent ( pRoot );
22+
return pIFP;
23+
}
24+
}
25+
return nullptr;
26+
}
27+
28+
bool CIFPEngine::EngineReplaceAnimation ( CClientEntity * pEntity, const SString & strInternalBlockName, const SString & strInternalAnimName, const SString & strCustomBlockName, const SString & strCustomAnimName )
29+
{
30+
if ( IS_PED ( pEntity ) )
31+
{
32+
CClientPed& Ped = static_cast < CClientPed& > ( *pEntity );
33+
34+
CAnimBlock * pInternalBlock = g_pGame->GetAnimManager ()->GetAnimationBlock ( strInternalBlockName );
35+
std::shared_ptr < CClientIFP > pCustomIFP = g_pClientGame->GetIFPPointerFromMap ( strCustomBlockName );
36+
if ( pInternalBlock && pCustomIFP )
37+
{
38+
CAnimBlendHierarchy * pInternalAnimHierarchy = g_pGame->GetAnimManager ()->GetAnimation ( strInternalAnimName, pInternalBlock );
39+
CAnimBlendHierarchySAInterface * pCustomAnimHierarchyInterface = pCustomIFP->GetAnimationHierarchy ( strCustomAnimName );
40+
if ( pInternalAnimHierarchy && pCustomAnimHierarchyInterface )
41+
{
42+
Ped.ReplaceAnimation ( pInternalAnimHierarchy, pCustomIFP, pCustomAnimHierarchyInterface );
43+
return true;
44+
}
45+
}
46+
}
47+
return false;
48+
}
49+
50+
bool CIFPEngine::EngineRestoreAnimation ( CClientEntity * pEntity, const SString & strInternalBlockName, const SString & strInternalAnimName, eRestoreAnimation eRestoreType )
51+
{
52+
if ( IS_PED ( pEntity ) )
53+
{
54+
CClientPed& Ped = static_cast < CClientPed& > ( *pEntity );
55+
56+
if ( eRestoreType == eRestoreAnimation::ALL )
57+
{
58+
Ped.RestoreAllAnimations ( );
59+
return true;
60+
}
61+
else
62+
{
63+
CAnimBlock * pInternalBlock = g_pGame->GetAnimManager ()->GetAnimationBlock ( strInternalBlockName );
64+
if ( pInternalBlock )
65+
{
66+
if ( eRestoreType == eRestoreAnimation::BLOCK )
67+
{
68+
Ped.RestoreAnimations ( *pInternalBlock );
69+
return true;
70+
}
71+
else
72+
{
73+
CAnimBlendHierarchy * pInternalAnimHierarchy = g_pGame->GetAnimManager ()->GetAnimation ( strInternalAnimName, pInternalBlock );
74+
if ( pInternalAnimHierarchy )
75+
{
76+
Ped.RestoreAnimation ( pInternalAnimHierarchy );
77+
return true;
78+
}
79+
}
80+
}
81+
}
82+
}
83+
return false;
84+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma
2+
#ifndef __CIFPENGINE_H
3+
#define __CIFPENGINE_H
4+
5+
class CIFPEngine
6+
{
7+
public:
8+
enum eRestoreAnimation
9+
{
10+
SINGLE,
11+
BLOCK,
12+
ALL
13+
};
14+
15+
static std::shared_ptr < CClientIFP > EngineLoadIFP ( CResource * pResource, CClientManager * pManager, const SString & strPath, const SString & strBlockName );
16+
static bool EngineReplaceAnimation ( CClientEntity * pEntity, const SString & strInternalBlockName, const SString & strInternalAnimName, const SString & strCustomBlockName, const SString & strCustomAnimName );
17+
static bool EngineRestoreAnimation ( CClientEntity * pEntity, const SString & strInternalBlockName, const SString & strInternalAnimName, eRestoreAnimation RestoreType );
18+
19+
};
20+
21+
#endif

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

Lines changed: 18 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -305,39 +305,15 @@ int CLuaEngineDefs::EngineLoadIFP ( lua_State* luaVM )
305305
// Is this a legal filepath?
306306
if ( CResourceManager::ParseResourcePathInput( strFile, pResource, &strPath ) )
307307
{
308-
// Grab the resource root entity
309-
CClientEntity* pRoot = pResource->GetResourceIFPRoot ();
310-
311-
// Check whether the IFP blockname exists or not
312-
if ( g_pClientGame->GetIFPPointerFromMap ( strBlockName ) == nullptr )
313-
{
314-
// Create a IFP element
315-
std::shared_ptr < CClientIFP > pIFP ( new CClientIFP ( m_pManager, INVALID_ELEMENT_ID ) );
316-
317-
// Try to load the IFP file
318-
if ( pIFP->LoadIFP ( strPath, strBlockName ) )
319-
{
320-
// We can use the map to retrieve correct IFP by block name later
321-
g_pClientGame->InsertIFPPointerToMap ( strBlockName, pIFP );
322-
323-
// Success loading the file. Set parent to IFP root
324-
pIFP->SetParent ( pRoot );
325-
326-
// Return the IFP element
327-
lua_pushelement ( luaVM, pIFP.get ( ) );
328-
return 1;
329-
}
330-
else
331-
{
332-
// Delete it again
333-
//delete pIFP;
334-
argStream.SetCustomError ( strFile, "Error loading IFP" );
335-
}
336-
}
337-
else
308+
std::shared_ptr < CClientIFP > pIFP = CIFPEngine::EngineLoadIFP ( pResource, m_pManager, strPath, strBlockName );
309+
if ( pIFP != nullptr )
338310
{
339-
argStream.SetCustomError ( strFile, "Block name already exists" );
311+
// Return the IFP element
312+
lua_pushelement ( luaVM, pIFP.get ( ) );
313+
return 1;
340314
}
315+
else
316+
argStream.SetCustomError ( strFile, "Error loading IFP" );
341317
}
342318
else
343319
argStream.SetCustomError ( strFile, "Bad file path" );
@@ -526,27 +502,10 @@ int CLuaEngineDefs::EngineReplaceAnimation ( lua_State* luaVM )
526502

527503
if ( !argStream.HasErrors () )
528504
{
529-
if ( IS_PED ( pEntity ) )
530-
{
531-
CClientPed& Ped = static_cast < CClientPed& > ( *pEntity );
532-
533-
CAnimBlock * pInternalBlock = g_pGame->GetAnimManager ()->GetAnimationBlock ( strInternalBlockName );
534-
std::shared_ptr < CClientIFP > pCustomIFP = g_pClientGame->GetIFPPointerFromMap ( strCustomBlockName );
535-
if ( pInternalBlock && pCustomIFP )
536-
{
537-
CAnimBlendHierarchy * pInternalAnimHierarchy = g_pGame->GetAnimManager ()->GetAnimation ( strInternalAnimName, pInternalBlock );
538-
CAnimBlendHierarchySAInterface * pCustomAnimHierarchyInterface = pCustomIFP->GetAnimationHierarchy ( strCustomAnimName );
539-
if ( pInternalAnimHierarchy && pCustomAnimHierarchyInterface )
540-
{
541-
Ped.ReplaceAnimation ( pInternalAnimHierarchy, pCustomIFP, pCustomAnimHierarchyInterface );
542-
lua_pushboolean ( luaVM, true );
543-
return 1;
544-
}
545-
else
546-
argStream.SetCustomError ( "Incorrect Animation name" );
547-
}
548-
else
549-
argStream.SetCustomError ( "Incorrect Block name" );
505+
if ( CIFPEngine::EngineReplaceAnimation ( pEntity, strInternalBlockName, strInternalAnimName, strCustomBlockName, strCustomAnimName ) )
506+
{
507+
lua_pushboolean ( luaVM, true );
508+
return 1;
550509
}
551510
}
552511
if ( argStream.HasErrors () )
@@ -560,23 +519,22 @@ int CLuaEngineDefs::EngineReplaceAnimation ( lua_State* luaVM )
560519
int CLuaEngineDefs::EngineRestoreAnimation ( lua_State* luaVM )
561520
{
562521
CClientEntity * pEntity = nullptr;
563-
bool bRestoreAllAnimations = false;
564-
bool bRestoreBlockAnimations = false;
522+
CIFPEngine::eRestoreAnimation eRestoreType = CIFPEngine::eRestoreAnimation::SINGLE;
565523
SString strInternalBlockName = "";
566524
SString strInternalAnimName = "";
567525

568526
CScriptArgReader argStream ( luaVM );
569527
argStream.ReadUserData ( pEntity );
570528
if ( argStream.NextIsNil ( ) || argStream.NextIsNone ( ) )
571529
{
572-
bRestoreAllAnimations = true;
530+
eRestoreType = CIFPEngine::eRestoreAnimation::ALL;
573531
}
574532
else
575533
{
576534
argStream.ReadString ( strInternalBlockName );
577535
if ( argStream.NextIsNil ( ) || argStream.NextIsNone ( ) )
578536
{
579-
bRestoreBlockAnimations = true;
537+
eRestoreType = CIFPEngine::eRestoreAnimation::BLOCK;
580538
}
581539
else
582540
{
@@ -586,44 +544,10 @@ int CLuaEngineDefs::EngineRestoreAnimation ( lua_State* luaVM )
586544

587545
if ( !argStream.HasErrors () )
588546
{
589-
if ( IS_PED ( pEntity ) )
590-
{
591-
CClientEntity & Entity = *pEntity;
592-
CClientPed& Ped = static_cast < CClientPed& > ( Entity );
593-
594-
if ( bRestoreAllAnimations )
595-
{
596-
Ped.RestoreAllAnimations ( );
597-
lua_pushboolean ( luaVM, true );
598-
return 1;
599-
}
600-
else
601-
{
602-
CAnimBlock * pInternalBlock = g_pGame->GetAnimManager ()->GetAnimationBlock ( strInternalBlockName );
603-
if ( pInternalBlock )
604-
{
605-
if ( bRestoreBlockAnimations )
606-
{
607-
Ped.RestoreAnimations ( *pInternalBlock );
608-
lua_pushboolean ( luaVM, true );
609-
return 1;
610-
}
611-
else
612-
{
613-
CAnimBlendHierarchy * pInternalAnimHierarchy = g_pGame->GetAnimManager ()->GetAnimation ( strInternalAnimName, pInternalBlock );
614-
if ( pInternalAnimHierarchy )
615-
{
616-
Ped.RestoreAnimation ( pInternalAnimHierarchy );
617-
lua_pushboolean ( luaVM, true );
618-
return 1;
619-
}
620-
else
621-
argStream.SetCustomError ( "Incorrect Animation name" );
622-
}
623-
}
624-
else
625-
argStream.SetCustomError ( "Incorrect Block name" );
626-
}
547+
if ( CIFPEngine::EngineRestoreAnimation ( pEntity, strInternalBlockName, strInternalAnimName, eRestoreType ) )
548+
{
549+
lua_pushboolean ( luaVM, true );
550+
return 1;
627551
}
628552
}
629553
if ( argStream.HasErrors () )

0 commit comments

Comments
 (0)