Skip to content

Commit

Permalink
Fix invalid references counter to TXD after engineSetTXDID
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Feb 20, 2025
1 parent 7763da4 commit a020269
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 14 additions & 3 deletions Client/game_sa/CModelInfoSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,16 +801,27 @@ void CModelInfoSA::SetTextureDictionaryID(unsigned short usID)
if (!m_pInterface)
return;

// Remove ref from the old TXD
CTxdStore_RemoveRef(m_pInterface->usTextureDictionary);
// CBaseModelInfo::AddRef adds references to model and TXD
// We need transfer added references from old TXD to new TXD
size_t referencesCount = m_pInterface->usNumberOfRefs;

// +1 reference for active rwObject
// TODO: pRwObject can have to unloaded TXD
if (m_pInterface->pRwObject)
referencesCount++;

for (size_t i = 0; i < referencesCount; i++)
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);

for (size_t i = 0; i < referencesCount; i++)
CTxdStore_AddRef(usID);
}

void CModelInfoSA::ResetTextureDictionaryID()
Expand Down
4 changes: 3 additions & 1 deletion Client/game_sa/CRenderWareSA.TextureReplacing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,10 @@ void CRenderWareSA::ModelInfoTXDRemoveTextures(SReplacementTextures* pReplacemen
ListRemove(currentTextures, pOriginalTexture);
}
assert(currentTextures.empty());
#endif

int32_t refsCount = CTxdStore_GetNumRefs(pInfo->usTxdId);
assert(refsCount > 0, "Should have at least one TXD reference here");
#endif
// Remove info
CTxdStore_RemoveRef(pInfo->usTxdId);
MapRemove(ms_ModelTexturesInfoMap, usTxdId);
Expand Down

0 comments on commit a020269

Please sign in to comment.