-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathCClientModelManager.cpp
142 lines (123 loc) · 4.09 KB
/
CClientModelManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* (Shared logic for modifications)
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/CClientModelManager.cpp
* PURPOSE: Model manager class
*
*****************************************************************************/
#include "StdInc.h"
#include "CLodModels.h"
CClientModelManager::CClientModelManager() : m_Models(std::make_unique<std::shared_ptr<CClientModel>[]>(g_pGame->GetBaseIDforCOL()))
{
const unsigned int uiMaxModelID = g_pGame->GetBaseIDforCOL();
for (unsigned int i = 0; i < uiMaxModelID; i++)
{
m_Models[i] = nullptr;
}
}
CClientModelManager::~CClientModelManager(void)
{
RemoveAll();
// Reset Level-Of-Detail system
CLodModels::ResetAllModelLOD();
}
void CClientModelManager::RemoveAll(void)
{
const unsigned int uiMaxModelID = g_pGame->GetBaseIDforCOL();
for (unsigned int i = 0; i < uiMaxModelID; i++)
{
m_Models[i] = nullptr;
}
m_modelCount = 0;
}
void CClientModelManager::Add(const std::shared_ptr<CClientModel>& pModel)
{
if (m_Models[pModel->GetModelID()] != nullptr)
{
dassert(m_Models[pModel->GetModelID()].get() == pModel.get());
return;
}
m_Models[pModel->GetModelID()] = pModel;
m_modelCount++;
}
bool CClientModelManager::Remove(const std::shared_ptr<CClientModel>& pModel)
{
int modelId = pModel->GetModelID();
if (m_Models[modelId] != nullptr)
{
CResource* parentResource = m_Models[modelId]->GetParentResource();
if (parentResource)
parentResource->GetResourceModelStreamer()->FullyReleaseModel(modelId);
m_Models[modelId]->RestoreEntitiesUsingThisModel();
m_Models[modelId] = nullptr;
m_modelCount--;
// Force reset the model in Level-Of-Detail system
CLodModels::ResetModelLODByHigh(modelId);
CLodModels::ResetModelLODByLow(modelId);
return true;
}
return false;
}
int CClientModelManager::GetFirstFreeModelID(void)
{
const unsigned int uiMaxModelID = g_pGame->GetBaseIDforCOL();
for (unsigned int i = 0; i < uiMaxModelID; i++)
{
CModelInfo* pModelInfo = g_pGame->GetModelInfo(i, true);
if (!pModelInfo->IsValid())
{
return i;
}
}
return INVALID_MODEL_ID;
}
int CClientModelManager::GetFreeTxdModelID()
{
std::uint16_t usTxdId = g_pGame->GetPools()->GetTxdPool().GetFreeTextureDictonarySlot();
if (usTxdId == -1)
return INVALID_MODEL_ID;
return MAX_MODEL_DFF_ID + usTxdId;
}
std::shared_ptr<CClientModel> CClientModelManager::FindModelByID(int iModelID)
{
int32_t iMaxModelId = g_pGame->GetBaseIDforCOL();
if (iModelID < iMaxModelId)
return m_Models[iModelID];
return nullptr;
}
std::shared_ptr<CClientModel> CClientModelManager::Request(CClientManager* pManager, int iModelID, eClientModelType eType)
{
std::shared_ptr<CClientModel> pModel = FindModelByID(iModelID);
if (pModel == nullptr)
{
pModel = std::make_shared<CClientModel>(pManager, iModelID, eType);
}
pModel->m_eModelType = eType;
return pModel;
}
std::vector<std::shared_ptr<CClientModel>> CClientModelManager::GetModelsByType(const eClientModelType type, const unsigned int minModelID)
{
std::vector<std::shared_ptr<CClientModel>> found;
found.reserve(m_modelCount);
const unsigned int uiMaxModelID = g_pGame->GetBaseIDforCOL();
for (unsigned int i = minModelID; i < uiMaxModelID; i++)
{
const std::shared_ptr<CClientModel>& model = m_Models[i];
if (model && model->GetModelType() == type)
{
found.push_back(model);
}
}
return found;
}
void CClientModelManager::DeallocateModelsAllocatedByResource(CResource* pResource)
{
const unsigned int uiMaxModelID = g_pGame->GetBaseIDforCOL();
for (unsigned int i = 0; i < uiMaxModelID; i++)
{
if (m_Models[i] != nullptr && m_Models[i]->GetParentResource() == pResource)
Remove(m_Models[i]);
}
}