Skip to content

Commit 3b68f4c

Browse files
samr46saml1er
authored andcommitted
Raw data loading option for engineLoadIFP (#247)
* Modified function: engineLoadIFP New syntax: engineLoadIFP ( string ifp_file / string raw_data, string CustomBlockName ) Added option to use raw data instead of a file name. * Variables rename
1 parent 46bd677 commit 3b68f4c

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

Client/mods/deathmatch/logic/CClientIFP.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ CClientIFP::CClientIFP(class CClientManager* pManager, ElementID ID) : CClientEn
2424
m_u32Hashkey = 0;
2525
}
2626

27-
bool CClientIFP::LoadIFP(const SString& strFilePath, const SString& strBlockName)
27+
bool CClientIFP::LoadIFP(const SString& strFile, const bool isRawData, const SString& strBlockName)
2828
{
2929
m_strBlockName = strBlockName;
3030
m_pVecAnimations = &m_pIFPAnimations->vecAnimations;
3131

32-
if (LoadIFPFile(strFilePath))
32+
if (LoadIFPFile(strFile, isRawData))
3333
{
3434
m_u32Hashkey = HashString(strBlockName.ToLower());
3535
return true;
3636
}
3737
return false;
3838
}
3939

40-
bool CClientIFP::LoadIFPFile(const SString& strFilePath)
40+
bool CClientIFP::LoadIFPFile(const SString& strFile, const bool isRawData)
4141
{
42-
if (LoadFileToMemory(strFilePath))
42+
if (isRawData ? LoadDataBufferToMemory(strFile) : LoadFileToMemory(strFile))
4343
{
4444
if (ReadIFPByVersion())
4545
{

Client/mods/deathmatch/logic/CClientIFP.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class CClientIFP : public CClientEntity, CFileReader
205205
void MarkAsUnloading(void) { m_bUnloading = true; }
206206
bool IsUnloading(void) { return m_bUnloading; }
207207

208-
bool LoadIFP(const SString& strFilePath, const SString& strBlockName);
208+
bool LoadIFP(const SString& strFile, const bool isRawData, const SString& strBlockName);
209209

210210
const SString& GetBlockName(void) { return m_strBlockName; }
211211
const unsigned int& GetBlockNameHash(void) { return m_u32Hashkey; }
@@ -219,7 +219,7 @@ class CClientIFP : public CClientEntity, CFileReader
219219
void SetPosition(const CVector& vecPosition){};
220220

221221
private:
222-
bool LoadIFPFile(const SString& strFilePath);
222+
bool LoadIFPFile(const SString& strFile, const bool isRawData);
223223
bool ReadIFPByVersion(void);
224224
void ReadIFPVersion1(void);
225225
void ReadIFPVersion2(bool bAnp3);

Client/mods/deathmatch/logic/CFileReader.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,31 @@ void CFileReader::SkipBytes(const std::uint32_t u32BytesToSkip)
4545
bool CFileReader::LoadFileToMemory(const SString& strFilePath)
4646
{
4747
std::ifstream fileStream(FromUTF8(strFilePath), std::ios::binary | std::ios::ate);
48-
std::streamsize m_iFileSize = fileStream.tellg();
49-
if (m_iFileSize == eIFSTREAM::SIZE_ERROR)
48+
std::streamsize iFileSize = fileStream.tellg();
49+
if (iFileSize == eIFSTREAM::SIZE_ERROR)
5050
{
5151
return false;
5252
}
5353

5454
fileStream.seekg(0, std::ios::beg);
55-
m_vecFileDataBuffer.reserve(static_cast<size_t>(m_iFileSize));
56-
if (fileStream.read(m_vecFileDataBuffer.data(), m_iFileSize))
55+
m_vecFileDataBuffer.reserve(static_cast<size_t>(iFileSize));
56+
if (fileStream.read(m_vecFileDataBuffer.data(), iFileSize))
57+
{
58+
return true;
59+
}
60+
return false;
61+
}
62+
63+
bool CFileReader::LoadDataBufferToMemory(const SString& buffer)
64+
{
65+
std::streamsize iBufferSize = buffer.size();
66+
if (iBufferSize == eIFSTREAM::SIZE_ERROR)
67+
{
68+
return false;
69+
}
70+
71+
m_vecFileDataBuffer.reserve(static_cast<size_t>(iBufferSize));
72+
if (std::copy(buffer.begin(), buffer.end(), m_vecFileDataBuffer.data()))
5773
{
5874
return true;
5975
}

Client/mods/deathmatch/logic/CFileReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CFileReader
2424

2525
CFileReader(void);
2626
bool LoadFileToMemory(const SString& strFilePath);
27+
bool LoadDataBufferToMemory(const SString& buffer);
2728
// Do not call any file reader functions after calling this function
2829
void FreeFileReaderMemory(void);
2930

Client/mods/deathmatch/logic/CIFPEngine.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <StdInc.h>
1212

13-
std::shared_ptr<CClientIFP> CIFPEngine::EngineLoadIFP(CResource* pResource, CClientManager* pManager, const SString& strPath, const SString& strBlockName)
13+
std::shared_ptr<CClientIFP> CIFPEngine::EngineLoadIFP(CResource* pResource, CClientManager* pManager, const SString& strFile, bool bIsRawData, const SString& strBlockName)
1414
{
1515
// Grab the resource root entity
1616
CClientEntity* pRoot = pResource->GetResourceIFPRoot();
@@ -23,7 +23,7 @@ std::shared_ptr<CClientIFP> CIFPEngine::EngineLoadIFP(CResource* pResource, CCli
2323
std::shared_ptr<CClientIFP> pIFP(new CClientIFP(pManager, INVALID_ELEMENT_ID));
2424

2525
// Try to load the IFP file
26-
if (pIFP->LoadIFP(strPath, strBlockName))
26+
if (pIFP->LoadIFP(strFile, bIsRawData, strBlockName))
2727
{
2828
// We can use the map to retrieve correct IFP by block name later
2929
g_pClientGame->InsertIFPPointerToMap(u32BlockNameHash, pIFP);
@@ -99,3 +99,9 @@ bool CIFPEngine::EngineRestoreAnimation(CClientEntity* pEntity, const SString& s
9999
}
100100
return false;
101101
}
102+
103+
// Return true if data looks like IFP file contents
104+
bool CIFPEngine::IsIFPData(const SString& strData)
105+
{
106+
return strData.length() > 32 && memcmp(strData, "\x41\x4E\x50", 3) == 0;
107+
}

Client/mods/deathmatch/logic/CIFPEngine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ class CIFPEngine
2525
ALL
2626
};
2727

28-
static std::shared_ptr<CClientIFP> EngineLoadIFP(CResource* pResource, CClientManager* pManager, const SString& strPath, const SString& strBlockName);
28+
static std::shared_ptr<CClientIFP> EngineLoadIFP(CResource* pResource, CClientManager* pManager, const SString& strFile, bool bIsRawData, const SString& strBlockName);
2929
static bool EngineReplaceAnimation(CClientEntity* pEntity, const SString& strInternalBlockName, const SString& strInternalAnimName,
3030
const SString& strCustomBlockName, const SString& strCustomAnimName);
3131
static bool EngineRestoreAnimation(CClientEntity* pEntity, const SString& strInternalBlockName, const SString& strInternalAnimName,
3232
const eRestoreAnimation& eRestoreType);
33+
static bool IsIFPData(const SString& strData);
3334
};
3435

3536
#endif

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,23 @@ int CLuaEngineDefs::EngineLoadIFP(lua_State* luaVM)
297297
CResource* pResource = pLuaMain->GetResource();
298298
if (pResource)
299299
{
300+
bool bIsRawData = CIFPEngine::IsIFPData(strFile);
300301
SString strPath;
301302
// Is this a legal filepath?
302-
if (CResourceManager::ParseResourcePathInput(strFile, pResource, &strPath))
303+
if (bIsRawData || CResourceManager::ParseResourcePathInput(strFile, pResource, &strPath))
303304
{
304-
std::shared_ptr<CClientIFP> pIFP = CIFPEngine::EngineLoadIFP(pResource, m_pManager, strPath, strBlockName);
305+
std::shared_ptr<CClientIFP> pIFP = CIFPEngine::EngineLoadIFP(pResource, m_pManager, bIsRawData ? strFile : strPath, bIsRawData, strBlockName);
305306
if (pIFP != nullptr)
306307
{
307308
// Return the IFP element
308309
lua_pushelement(luaVM, pIFP.get());
309310
return 1;
310311
}
311312
else
312-
argStream.SetCustomError(strFile, "Error loading IFP");
313+
argStream.SetCustomError(bIsRawData ? "raw data" : strFile, "Error loading IFP");
313314
}
314315
else
315-
argStream.SetCustomError(strFile, "Bad file path");
316+
argStream.SetCustomError(bIsRawData ? "raw data" : strFile, "Bad file path");
316317
}
317318
}
318319
}

0 commit comments

Comments
 (0)