Skip to content

Commit

Permalink
Better checks
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Aug 27, 2024
1 parent 58247b8 commit a4d7aac
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
22 changes: 17 additions & 5 deletions Client/game_sa/CGameSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,17 +1051,29 @@ bool CGameSA::SetBuildingPoolSize(size_t size)

void CGameSA::UnloadUnusedModels()
{
for (std::size_t id = 0; id < GetBaseIDforCOL(); id++)
// Unload DFF's
// CJ should not be unloaded
const std::size_t baseIdForTxd = GetBaseIDforTXD();
for (std::size_t id = 1; id < baseIdForTxd; id++)
{
CStreamingInfo* streamingInfo = m_pStreaming->GetStreamingInfo(id);
if (streamingInfo->loadState != eModelLoadState::LOADSTATE_NOT_LOADED && streamingInfo->sizeInBlocks > 0)
{
if (ModelInfo[id].GetRefCount() == 0)
{
m_pStreaming->RemoveModel(id);
}
CModelInfoSA& model = ModelInfo[id];
if (model.GetRefCount() == 0)
model.UnloadUnused();
};
}
// Unload TXD
for (std::size_t id = baseIdForTxd; id < GetBaseIDforCOL(); id++)
{
CStreamingInfo* streamingInfo = m_pStreaming->GetStreamingInfo(id);
std::size_t refsCount = GetPools()->GetTxdPool().GetRefsCount(id - baseIdForTxd);
if (streamingInfo->loadState != eModelLoadState::LOADSTATE_NOT_LOADED && streamingInfo->sizeInBlocks > 0 && refsCount == 0)
{
GetStreaming()->RemoveModel(id);
}
}
}

// Ensure models have the default lod distances
Expand Down
2 changes: 2 additions & 0 deletions Client/game_sa/CModelInfoSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ void CModelInfoSA::Remove()

bool CModelInfoSA::UnloadUnused()
{
m_pInterface = ppModelInfo[m_dwModelID];

if (m_pInterface->usNumberOfRefs == 0 && !m_pCustomClump && !m_pCustomColModel)
{
pGame->GetStreaming()->RemoveModel(m_dwModelID);
Expand Down
2 changes: 1 addition & 1 deletion Client/game_sa/CPoolSAInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class CPoolSAInterface
return !IsEmpty(index);
}

B* GetObject(std::int32_t objectIndex) { return &m_pObjects[objectIndex]; }
B* GetObject(std::int32_t objectIndex) const { return &m_pObjects[objectIndex]; }

uint GetObjectIndex(B* pObject) { return ((DWORD)pObject - (DWORD)m_pObjects) / sizeof(B); }

Expand Down
9 changes: 9 additions & 0 deletions Client/game_sa/CTxdPoolSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ std::uint16_t CTxdPoolSA::GetFreeTextureDictonarySlot()
{
return (*m_ppTxdPoolInterface)->GetFreeSlot();
}

std::uint16_t CTxdPoolSA::GetRefsCount(std::uint16_t slot) const
{
CTextureDictonarySAInterface* pTxd = (*m_ppTxdPoolInterface)->GetObject(slot);
if (!pTxd)
return -1;

return pTxd->usUsagesCount;
}
1 change: 1 addition & 0 deletions Client/game_sa/CTxdPoolSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CTxdPoolSA final : public CTxdPool
bool IsFreeTextureDictonarySlot(std::uint32_t uiTxdId);

std::uint16_t GetFreeTextureDictonarySlot();
std::uint16_t GetRefsCount(std::uint16_t slot) const;

private:
CPoolSAInterface<CTextureDictonarySAInterface>** m_ppTxdPoolInterface;
Expand Down
1 change: 1 addition & 0 deletions Client/sdk/game/CTxdPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class CTxdPool
virtual bool IsFreeTextureDictonarySlot(std::uint32_t uiTxdID) = 0;

virtual std::uint16_t GetFreeTextureDictonarySlot() = 0;
virtual std::uint16_t GetRefsCount(std::uint16_t slot) const = 0;
};

0 comments on commit a4d7aac

Please sign in to comment.