/
CClientIMG.cpp
229 lines (179 loc) · 5.47 KB
/
CClientIMG.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* (Shared logic for modifications)
* LICENSE: See LICENSE in the top level directory
* FILE: mods/shared_logic/CClientIMG.cpp
* PURPOSE: IMG container class
*
*****************************************************************************/
#include <StdInc.h>
#include "game/CStreaming.h"
#define INVALID_ARCHIVE_ID 0xFF
struct tImgHeader
{
char szMagic[4];
unsigned int uiFilesCount;
};
CClientIMG::CClientIMG(class CClientManager* pManager, ElementID ID)
: ClassInit(this), CClientEntity(ID), m_pImgManager(pManager->GetIMGManager()), m_ucArchiveID(INVALID_ARCHIVE_ID), m_usRequiredBufferSize(0)
{
m_pManager = pManager;
SetTypeName("img");
m_pImgManager->AddToList(this);
}
CClientIMG::~CClientIMG()
{
m_pImgManager->RemoveFromList(this);
if (IsStreamed())
StreamDisable();
if (m_ifs.is_open())
Unload();
}
bool CClientIMG::Load(fs::path filePath)
{
if (!m_fileInfos.empty())
return false;
if (m_ifs.is_open())
return false;
if (filePath.empty())
return false;
if (!fs::exists(filePath))
return false;
m_filePath = filePath.string();
m_ifs = std::ifstream(filePath, std::ios::binary);
// Open the file
if (m_ifs.fail())
{
m_ifs.close();
return false;
}
tImgHeader fileHeader;
// Read header
m_ifs.read(reinterpret_cast<char*>(&fileHeader), sizeof(tImgHeader));
if (m_ifs.fail() || m_ifs.eof() || memcmp(&fileHeader.szMagic, "VER2", 4) != 0)
{
m_ifs.close();
return false;
}
// Read content info
try
{
m_fileInfos.resize(fileHeader.uiFilesCount);
}
catch (const std::bad_alloc&)
{
m_ifs.close();
return false;
}
m_ifs.read(reinterpret_cast<char*>(m_fileInfos.data()), sizeof(tImgFileInfo) * (std::streampos)fileHeader.uiFilesCount);
if (m_ifs.fail() || m_ifs.eof())
{
m_ifs.close();
return false;
}
return true;
}
void CClientIMG::Unload()
{
m_fileInfos.clear();
m_fileInfos.shrink_to_fit();
m_ifs.close();
}
bool CClientIMG::GetFile(size_t fileID, std::string& buffer)
{
const tImgFileInfo* pFileInfo = GetFileInfo(fileID);
if (!pFileInfo)
throw std::invalid_argument("Invalid file id");
const auto ulToReadSize = pFileInfo->usSize * 2048;
try
{
buffer.resize(ulToReadSize);
}
catch (const std::bad_alloc&)
{
throw std::invalid_argument("Out of memory");
}
m_ifs.seekg((std::streampos)pFileInfo->uiOffset * 2048);
m_ifs.read(buffer.data(), ulToReadSize);
return !m_ifs.fail() && !m_ifs.eof();
}
tImgFileInfo* CClientIMG::GetFileInfo(size_t fileID)
{
if (fileID >= m_fileInfos.size())
return nullptr;
return &m_fileInfos[fileID];
}
std::optional<size_t> CClientIMG::GetFileID(std::string_view filename)
{
const auto it =
std::find_if(m_fileInfos.begin(), m_fileInfos.end(), [filename](const auto& fileInfo) { return filename.compare(fileInfo.szFileName) == 0; });
if (it == m_fileInfos.end())
return std::nullopt;
return std::distance(m_fileInfos.begin(), it);
}
bool CClientIMG::IsStreamed()
{
return m_ucArchiveID != INVALID_ARCHIVE_ID;
}
bool CClientIMG::StreamEnable()
{
if (m_fileInfos.empty())
return false;
if (IsStreamed())
return false;
if (m_usRequiredBufferSize == 0)
{
for (const auto& fileInfo : m_fileInfos)
m_usRequiredBufferSize = Max(m_usRequiredBufferSize, fileInfo.usSize);
}
m_ucArchiveID = g_pGame->GetStreaming()->AddArchive(m_filePath.c_str());
if (IsStreamed())
{
m_pImgManager->UpdateStreamerBufferSize();
return true;
}
return false;
}
bool CClientIMG::StreamDisable()
{
if (!IsStreamed())
return false;
// Unlink all models
for (const auto& v : m_restoreInfo)
{
g_pGame->GetStreaming()->SetStreamingInfo(v.uiModelID, v.ucStreamID, v.uiOffset, v.usSize);
}
m_restoreInfo.clear();
m_restoreInfo.shrink_to_fit();
// Remove archive from streaming
g_pGame->GetStreaming()->RemoveArchive(m_ucArchiveID);
m_ucArchiveID = INVALID_ARCHIVE_ID;
m_pImgManager->UpdateStreamerBufferSize();
g_pClientGame->RestreamWorld(true);
return true;
}
bool CClientIMG::LinkModel(unsigned int uiModelID, size_t uiFileID)
{
if (!IsStreamed())
return false;
tImgFileInfo* pFileInfo = GetFileInfo(uiFileID);
if (!pFileInfo)
return false;
CStreamingInfo* pCurrInfo = g_pGame->GetStreaming()->GetStreamingInfo(uiModelID);
if (pCurrInfo->archiveId == m_ucArchiveID)
return true; // Already linked
m_restoreInfo.emplace_back(uiModelID, pCurrInfo->offsetInBlocks, pCurrInfo->sizeInBlocks, pCurrInfo->archiveId);
g_pGame->GetStreaming()->SetStreamingInfo(uiModelID, m_ucArchiveID, pFileInfo->uiOffset, pFileInfo->usSize);
return true;
}
bool CClientIMG::UnlinkModel(unsigned int uiModelID)
{
const auto it =
std::find_if(m_restoreInfo.begin(), m_restoreInfo.end(), [uiModelID](const auto& restoreInfo) { return restoreInfo.uiModelID == uiModelID; });
if (it == m_restoreInfo.end())
return false;
g_pGame->GetStreaming()->SetStreamingInfo(uiModelID, it->ucStreamID, it->uiOffset, it->usSize);
m_restoreInfo.erase(it);
return true;
}