forked from multitheftauto/mtasa-blue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRenderWareSA.ClothesReplacing.cpp
More file actions
338 lines (286 loc) · 9.72 KB
/
CRenderWareSA.ClothesReplacing.cpp
File metadata and controls
338 lines (286 loc) · 9.72 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CRenderWareSA.ClothesReplacing.cpp
*
*****************************************************************************/
#include "StdInc.h"
#include "CGameSA.h"
#include "CDirectorySA.h"
#include "gamesa_renderware.h"
extern CGameSA* pGame;
namespace
{
struct SImgGTAItemInfo
{
ushort usNext;
ushort usPrev;
ushort uiUnknown1; // Parent ?
uchar uiUnknown2; // 0x12 when loading, 0x02 when finished loading
uchar ucImgId;
int iBlockOffset;
int iBlockCount;
uint uiLoadflag; // 0-not loaded 2-requested 3-loaded 1-processed
};
std::unordered_map<ushort, char*> ms_ReplacementClothesFileDataMap;
std::unordered_map<ushort, std::uint16_t> ms_OriginalStreamingSizesMap;
std::unordered_map<std::string, char*> ms_ClothesFileDataMap;
bool bClothesReplacementChanged = false;
struct SPlayerImgItem
{
uint uiBlockOffset;
uint uiBlockCount;
const char szName[24];
};
struct SPlayerImgItemArray
{
SPlayerImgItem* pItems;
uint uiArrayMax;
uint uiArraySize;
};
DWORD FUNC_CStreamingConvertBufferToObject = 0x40C6B0;
auto g_clothesDirectory = reinterpret_cast<CDirectorySAInterface*>(0xBC12C0);
int iReturnFileId;
char* pReturnBuffer;
size_t GetSizeInBlocks(size_t size)
{
auto blockDiv = std::div(size, 2048);
return (blockDiv.quot + (blockDiv.rem ? 1 : 0));
}
} // namespace
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::ClothesAddReplacement
//
// Add replacement txd/dff for a clothing component
//
////////////////////////////////////////////////////////////////
void CRenderWareSA::ClothesAddReplacement(char* pFileData, size_t fileSize, ushort usFileId)
{
if (!pFileData)
return;
if (pFileData != MapFindRef(ms_ReplacementClothesFileDataMap, usFileId))
{
MapSet(ms_ReplacementClothesFileDataMap, usFileId, pFileData);
MapSet(ms_OriginalStreamingSizesMap, usFileId, g_clothesDirectory->GetModelStreamingSize(usFileId));
g_clothesDirectory->SetModelStreamingSize(usFileId, GetSizeInBlocks(fileSize));
bClothesReplacementChanged = true;
}
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::ClothesRemoveReplacement
//
// Remove replacement txd/dff for a clothing component
//
////////////////////////////////////////////////////////////////
void CRenderWareSA::ClothesRemoveReplacement(char* pFileData)
{
if (!pFileData)
return;
for (auto iter = ms_ReplacementClothesFileDataMap.begin(); iter != ms_ReplacementClothesFileDataMap.end();)
{
if (iter->second == pFileData)
{
auto it = ms_OriginalStreamingSizesMap.find(iter->first);
if (it != ms_OriginalStreamingSizesMap.end())
{
std::uint16_t originalStreamingSize = it->second;
g_clothesDirectory->SetModelStreamingSize(iter->first, originalStreamingSize);
}
iter = ms_ReplacementClothesFileDataMap.erase(iter);
bClothesReplacementChanged = true;
}
else
++iter;
}
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::HasClothesReplacementChanged
//
// Returns true (once) if clothes textures need regenerating
//
////////////////////////////////////////////////////////////////
bool CRenderWareSA::HasClothesReplacementChanged()
{
bool bResult = bClothesReplacementChanged;
bClothesReplacementChanged = false;
return bResult;
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::ClothesAddFile
//
// Add a file to the clothes directory
//
////////////////////////////////////////////////////////////////
bool CRenderWareSA::ClothesAddFile(const char* fileData, std::size_t fileSize, const char* fileName)
{
if (!fileData || !fileName)
return false;
if (MapFind(ms_ClothesFileDataMap, fileName))
return false;
DirectoryInfoSA entry{};
entry.m_streamingSize = GetSizeInBlocks(fileSize);
std::strncpy(entry.m_name, fileName, sizeof(entry.m_name));
if (!g_clothesDirectory->AddEntry(entry))
return false;
MapSet(ms_ClothesFileDataMap, fileName, (char*)fileData);
bClothesReplacementChanged = true;
return true;
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::ClothesRemoveFile
//
// Remove a file from the clothes directory
//
////////////////////////////////////////////////////////////////
bool CRenderWareSA::ClothesRemoveFile(char* fileData)
{
if (!fileData)
return false;
for (auto iter = ms_ClothesFileDataMap.begin(); iter != ms_ClothesFileDataMap.end();)
{
if (iter->second == fileData)
{
if (!g_clothesDirectory->RemoveEntry(iter->first.c_str()))
return false;
iter = ms_ClothesFileDataMap.erase(iter);
bClothesReplacementChanged = true;
}
else
++iter;
}
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::HasClothesFile
//
// Check if clothe file exits
//
////////////////////////////////////////////////////////////////
bool CRenderWareSA::HasClothesFile(const char* fileName)
{
if (!fileName || !MapFind(ms_ClothesFileDataMap, fileName))
{
return false;
}
return true;
}
////////////////////////////////////////////////////////////////
//
// CStreaming_RequestModel_Mid
//
// If request is for a file inside player.img (imgId 5)
// then maybe switch to replacement txd/dff file data
//
////////////////////////////////////////////////////////////////
__declspec(noinline) bool _cdecl OnCStreaming_RequestModel_Mid(int flags, SImgGTAItemInfo* pImgGTAInfo)
{
// Check is player.img
if (pImgGTAInfo->ucImgId != 5)
return false;
// Early out if no clothes textures to replace with
if (ms_ReplacementClothesFileDataMap.empty() && ms_ClothesFileDataMap.empty())
return false;
// Initialze lookup map if needed
static std::map<uint, int> blockOffsetToFileIdMap;
static std::map<uint, std::string> blockOffsetToFileNameMap;
if (blockOffsetToFileIdMap.empty())
{
// Check is player.img dir has been loaded by GTA
SPlayerImgItemArray* pItemArray = (SPlayerImgItemArray*)0x00BC12C0;
std::uint32_t maxArraySize = 542 + ms_ClothesFileDataMap.size();
if (!pItemArray->pItems || pItemArray->uiArraySize != maxArraySize)
return false;
for (uint i = 0; i < pItemArray->uiArraySize; i++)
{
SPlayerImgItem* pImgItem = &pItemArray->pItems[i];
MapSet(blockOffsetToFileIdMap, pImgItem->uiBlockOffset, i);
MapSet(blockOffsetToFileNameMap, pImgItem->uiBlockOffset, pImgItem->szName);
}
}
char* pReplacementFileData = nullptr;
int* piPlayerImgFileId = MapFind(blockOffsetToFileIdMap, pImgGTAInfo->iBlockOffset);
if (piPlayerImgFileId)
{
pReplacementFileData = MapFindRef(ms_ReplacementClothesFileDataMap, *piPlayerImgFileId);
}
if (!pReplacementFileData)
{
std::string* pFileName = MapFind(blockOffsetToFileNameMap, pImgGTAInfo->iBlockOffset);
if (pFileName)
{
pReplacementFileData = MapFindRef(ms_ClothesFileDataMap, *pFileName);
}
}
if (!pReplacementFileData)
return false;
// If bLoadingBigModel is set, try to get it unset
#define VAR_CStreaming_bLoadingBigModel 0x08E4A58
BYTE& bLoadingBigModel = *(BYTE*)VAR_CStreaming_bLoadingBigModel;
if (bLoadingBigModel)
{
pGame->GetStreaming()->LoadAllRequestedModels(true);
if (bLoadingBigModel)
pGame->GetStreaming()->LoadAllRequestedModels(false);
assert(!bLoadingBigModel);
}
// Set results
iReturnFileId = ((char*)pImgGTAInfo - (char*)CStreaming__ms_aInfoForModel) / 20;
pReturnBuffer = pReplacementFileData;
// Update flags
pImgGTAInfo->uiLoadflag = 3;
// Remove priorty flag, as not counted in ms_numPriorityRequests
pImgGTAInfo->uiUnknown2 &= ~0x10;
return true;
}
// Hook info
#define HOOKPOS_CStreaming_RequestModel_Mid 0x040895A
#define HOOKSIZE_CStreaming_RequestModel_Mid 5
DWORD RETURN_CStreaming_RequestModel_MidA = 0x0408960;
DWORD RETURN_CStreaming_RequestModel_MidB = 0x0408990;
void _declspec(naked) HOOK_CStreaming_RequestModel_Mid()
{
_asm
{
pushad
push esi
push ebx
call OnCStreaming_RequestModel_Mid
add esp, 4*2
cmp al, 0
jnz skip
// Continue with standard code
popad
mov eax, ds:0x08E4C58
push eax
jmp RETURN_CStreaming_RequestModel_MidA
// Handle load here
skip:
popad
pushad
mov eax, 0
push eax
mov eax, iReturnFileId
push eax
mov eax, pReturnBuffer
push eax
call FUNC_CStreamingConvertBufferToObject
add esp, 4*3
popad
jmp RETURN_CStreaming_RequestModel_MidB
}
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// Setup hooks
//
//////////////////////////////////////////////////////////////////////////////////////////
void CRenderWareSA::StaticSetClothesReplacingHooks()
{
EZHookInstall(CStreaming_RequestModel_Mid);
}