Skip to content

Commit

Permalink
Merge branch 'master' into TheNormalnij/new_txd_allocating
Browse files Browse the repository at this point in the history
  • Loading branch information
Pirulax authored Aug 30, 2023
2 parents dc7a378 + 3e9a373 commit 58579b0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Client/game_sa/CGameSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ void CGameSA::Reset()

// Restore vehicle model wheel sizes
CModelInfoSA::ResetAllVehiclesWheelSizes();

// Restore changed TXD IDs
CModelInfoSA::StaticResetTextureDictionaries();
}
}

Expand Down
35 changes: 33 additions & 2 deletions Client/game_sa/CModelInfoSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ std::unordered_map<std::uint32_t, std::map<eVehicleDummies, CVector>> CModelInfo
std::map<CTimeInfoSAInterface*, CTimeInfoSAInterface*> CModelInfoSA::ms_ModelDefaultModelTimeInfo;
std::unordered_map<DWORD, unsigned short> CModelInfoSA::ms_OriginalObjectPropertiesGroups;
std::unordered_map<DWORD, std::pair<float, float>> CModelInfoSA::ms_VehicleModelDefaultWheelSizes;
std::map<unsigned short, int> CModelInfoSA::ms_DefaultTxdIDMap;

union tIdeFlags
{
Expand Down Expand Up @@ -784,8 +785,38 @@ unsigned short CModelInfoSA::GetTextureDictionaryID()
void CModelInfoSA::SetTextureDictionaryID(unsigned short usID)
{
m_pInterface = ppModelInfo[m_dwModelID];
if (m_pInterface)
m_pInterface->usTextureDictionary = usID;
if (!m_pInterface)
return;

// Remove ref from the old TXD
CTxdStore_RemoveRef(m_pInterface->usTextureDictionary);

// Store vanilla TXD ID
if (!MapContains(ms_DefaultTxdIDMap, m_dwModelID))
ms_DefaultTxdIDMap[m_dwModelID] = m_pInterface->usTextureDictionary;

// Set new TXD and increase ref of it
m_pInterface->usTextureDictionary = usID;
CTxdStore_AddRef(usID);
}

void CModelInfoSA::ResetTextureDictionaryID()
{
const auto it = ms_DefaultTxdIDMap.find(m_dwModelID);
if (it == ms_DefaultTxdIDMap.end()) {
return;
}
SetTextureDictionaryID(it->second);
ms_DefaultTxdIDMap.erase(it); // Only erase after calling the function above [otherwise gets reinserted]
}

void CModelInfoSA::StaticResetTextureDictionaries()
{
while (!ms_DefaultTxdIDMap.empty()) {
const auto mi = pGame->GetModelInfo(ms_DefaultTxdIDMap.begin()->first);
assert(mi);
mi->ResetTextureDictionaryID();
}
}

float CModelInfoSA::GetLODDistance()
Expand Down
3 changes: 3 additions & 0 deletions Client/game_sa/CModelInfoSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ class CModelInfoSA : public CModelInfo
static std::map<CTimeInfoSAInterface*, CTimeInfoSAInterface*> ms_ModelDefaultModelTimeInfo;
static std::unordered_map<DWORD, unsigned short> ms_OriginalObjectPropertiesGroups;
static std::unordered_map<DWORD, std::pair<float, float>> ms_VehicleModelDefaultWheelSizes;
static std::map<unsigned short, int> ms_DefaultTxdIDMap;
SVehicleSupportedUpgrades m_ModelSupportedUpgrades;

public:
Expand Down Expand Up @@ -376,6 +377,8 @@ class CModelInfoSA : public CModelInfo
float GetDistanceFromCentreOfMassToBaseOfModel();
unsigned short GetTextureDictionaryID();
void SetTextureDictionaryID(unsigned short usID);
void ResetTextureDictionaryID();
static void StaticResetTextureDictionaries();
float GetLODDistance();
float GetOriginalLODDistance();
void SetLODDistance(float fDistance, bool bOverrideMaxDistance = false);
Expand Down
35 changes: 34 additions & 1 deletion Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ void CLuaEngineDefs::LoadFunctions()
{"engineImageGetFiles", ArgumentParser<EngineImageGetFileList>},
{"engineImageGetFile", ArgumentParser<EngineImageGetFile>},
{"engineGetModelTXDID", ArgumentParser<EngineGetModelTXDID>},
{"engineSetModelTXDID", ArgumentParser<EngineSetModelTXDID>},
{"engineResetModelTXDID", ArgumentParser<EngineResetModelTXDID>},
{"engineStreamingFreeUpMemory", ArgumentParser<EngineStreamingFreeUpMemory>},
{"engineStreamingGetUsedMemory", ArgumentParser<EngineStreamingGetUsedMemory>},
{"engineStreamingSetMemorySize", ArgumentParser<EngineStreamingSetMemorySize>},
Expand Down Expand Up @@ -177,6 +179,10 @@ void CLuaEngineDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "resetModelFlags", "engineResetModelFlags");
lua_classfunction(luaVM, "getModelFlags", "engineGetModelFlags");

lua_classfunction(luaVM, "getModelTXDID", "engineGetModelTXDID");
lua_classfunction(luaVM, "setModelTXDID", "engineSetModelTXDID");
lua_classfunction(luaVM, "resetModelTXDID", "engineResetModelTXDID");

lua_registerstaticclass(luaVM, "Engine");

// `EngineStreaming` class
Expand Down Expand Up @@ -1270,7 +1276,34 @@ int CLuaEngineDefs::EngineRemoveShaderFromWorldTexture(lua_State* luaVM)

uint CLuaEngineDefs::EngineGetModelTXDID(uint uiModelID)
{
return g_pGame->GetRenderWare()->GetTXDIDForModelID(uiModelID);
CModelInfo* pModelInfo = g_pGame->GetModelInfo(uiModelID);

if (uiModelID >= g_pGame->GetBaseIDforTXD() || !pModelInfo)
throw std::invalid_argument("Expected a valid model ID at argument 1");

return pModelInfo->GetTextureDictionaryID();
}

bool CLuaEngineDefs::EngineSetModelTXDID(uint uiModelID, unsigned short usTxdId)
{
CModelInfo* pModelInfo = g_pGame->GetModelInfo(uiModelID);

if (uiModelID >= g_pGame->GetBaseIDforTXD() || !pModelInfo)
throw std::invalid_argument("Expected a valid model ID at argument 1");

pModelInfo->SetTextureDictionaryID(usTxdId);
return true;
}

bool CLuaEngineDefs::EngineResetModelTXDID(uint uiModelID)
{
CModelInfo* pModelInfo = g_pGame->GetModelInfo(uiModelID);

if (uiModelID >= g_pGame->GetBaseIDforTXD() || !pModelInfo)
throw std::invalid_argument("Expected a valid model ID at argument 1");

pModelInfo->ResetTextureDictionaryID();
return true;
}

int CLuaEngineDefs::EngineGetModelNameFromID(lua_State* luaVM)
Expand Down
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class CLuaEngineDefs : public CLuaDefs
static bool EngineSetModelFlag(uint uiModelID, eModelIdeFlag eFlag, bool state);
static bool EngineResetModelFlags(uint uiModelID);
static uint EngineGetModelTXDID(uint uiDffModelID);
static bool EngineSetModelTXDID(uint uiDffModelID, unsigned short uiTxdId);
static bool EngineResetModelTXDID(uint uiDffModelID);
static CClientIMG* EngineLoadIMG(lua_State* const luaVM, std::string strFilePath);
static bool EngineAddImage(CClientIMG* pImg);
static bool EngineRemoveImage(CClientIMG* pImg);
Expand Down
3 changes: 2 additions & 1 deletion Client/sdk/game/CModelInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ class CModelInfo
virtual bool IsValid() = 0;
virtual bool IsAllocatedInArchive() = 0;
virtual unsigned short GetTextureDictionaryID() = 0;
virtual void SetTextureDictionaryID(unsigned short usSlotID) = 0;
virtual void SetTextureDictionaryID(unsigned short usTxdId) = 0;
virtual void ResetTextureDictionaryID() = 0;
virtual float GetLODDistance() = 0;
virtual float GetOriginalLODDistance() = 0;
virtual void SetLODDistance(float fDistance, bool bOverrideMaxDistance = false) = 0;
Expand Down

0 comments on commit 58579b0

Please sign in to comment.