-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathstreaming.cpp
More file actions
525 lines (456 loc) · 16.9 KB
/
Copy pathstreaming.cpp
File metadata and controls
525 lines (456 loc) · 16.9 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* Standard Streamer Plugin for Mod Loader
* Copyright (C) 2014 LINK/2012 <dma_2012@hotmail.com>
* Licensed under the MIT License, see LICENSE at top level directory.
*
*/
#include <stdinc.hpp>
#include "streaming.hpp"
#include "cdstreamsync.inl"
using namespace modloader;
CAbstractStreaming* streaming;
/*
* Constructs the abstract streaming object
*/
CAbstractStreaming::CAbstractStreaming()
{
InitializeCriticalSection(&cs);
InitializeCriticalSectionAndSpinCount(&cdStreamSyncLock, 10);
cdStreamSyncFuncs = CdStreamSyncFix::InitializeSyncFuncs();
}
CAbstractStreaming::~CAbstractStreaming()
{
DeleteCriticalSection(&cdStreamSyncLock);
DeleteCriticalSection(&cs);
Fastman92LimitAdjusterDestroy(this->f92la);
}
void CAbstractStreaming::InitialiseStructAbstraction()
{
if(this->bIsFirstLaunched)
return; // We already init this once, no need to do again
this->sizeof_CStreamingInfo = CStreamingInfo::GetSizeof();
this->f92la = Fastman92LimitAdjusterCreate();
if(this->f92la.hLib)
{
plugin_ptr->Log("Using fastman92limitadjuster (%p).", f92la.hLib);
InfoForModel = &CAbstractStreaming::InfoForModelInternal_FLA;
}
else if (gvm.IsSA())
{
InfoForModel = &CAbstractStreaming::InfoForModelInternal_SA;
}
else
{
InfoForModel = &CAbstractStreaming::InfoForModelInternal_VC;
}
}
const LibF92LA& CAbstractStreaming::GetF92LA()
{
return this->f92la;
}
const LibF92LA* CStreamingInfo::GetF92LA()
{
auto& f92la = streaming->GetF92LA();
return f92la.hLib? &f92la : nullptr;
}
int32_t CStreamingInfo::AsF92LA()
{
return streaming->InfoForModelIndex(*this);
}
/*
* CAbstractStreaming::InfoForModel
* Returns the streaming info pointer for the specified resource id
* San Andreas version of the function
*/
CStreamingInfo* CAbstractStreaming::InfoForModelInternal_SA(id_t id) const
{
// Note: sizeof(CStreamingInfo) isn't the actual size, so we must do the indexing manually with sizeof_CStreamingInfo!!
static const uint32_t max_NumResources = (lazy_object<0x5B8AFC, char*>::get() - reinterpret_cast<char*>(ms_aInfoForModel)) / sizeof_CStreamingInfo;
if (id < max_NumResources)
{
return (CStreamingInfo*)((uint8_t*)(ms_aInfoForModel) + (id * sizeof_CStreamingInfo));
}
return nullptr;
}
/*
* CAbstractStreaming::InfoForModel
* Returns the streaming info pointer for the specified resource id
* III/Vice City version of the function
*/
CStreamingInfo* CAbstractStreaming::InfoForModelInternal_VC(id_t id) const
{
// Note: sizeof(CStreamingInfo) isn't the actual size, so we must do the indexing manually with sizeof_CStreamingInfo!!
static const uint32_t max_NumResources = lazy_object<xVc(0x4102B2), uint32_t>::get();
if (id < max_NumResources)
{
return (CStreamingInfo*)((uint8_t*)(ms_aInfoForModel) + (id * sizeof_CStreamingInfo));
}
return nullptr;
}
/*
* CAbstractStreaming::InfoForModel
* Returns the streaming info pointer for the specified resource id
* FLA version of the function
*/
CStreamingInfo* CAbstractStreaming::InfoForModelInternal_FLA(id_t id) const
{
// Note: sizeof(CStreamingInfo) isn't the actual size, so we must do the indexing manually with sizeof_CStreamingInfo!!
static const uint32_t max_NumResources = f92la.GetNumberOfFileIDs();
if (id < max_NumResources)
{
return (CStreamingInfo*)((uint8_t*)(ms_aInfoForModel) + (id * sizeof_CStreamingInfo));
}
return nullptr;
}
// Returns the resource index from it's model info structure
auto CAbstractStreaming::InfoForModelIndex(const CStreamingInfo& model) -> id_t
{
// Note: sizeof(CStreamingInfo) isn't the actual size, so we must do the substraction manually with sizeof_CStreamingInfo!!
return (id_t)( ((uint8_t*)(&model) - (uint8_t*)(ms_aInfoForModel)) / sizeof_CStreamingInfo );
}
/*
* CAbstractStreaming::IsModelOnStreaming
* Checks if the specified model is on the streaming, either on the bus to get loaded or already loaded.
*/
bool CAbstractStreaming::IsModelOnStreaming(id_t id)
{
return std::invoke(InfoForModel, this, id)->GetLoadStatus() != 0;
}
/*
* CAbstractStreaming::IsModelAvailable
* Checks if the specified model is available to use (i.e. already loaded).
*/
bool CAbstractStreaming::IsModelAvailable(id_t id)
{
return std::invoke(InfoForModel, this, id)->GetLoadStatus() == 1;
}
/*
* CAbstractStreaming::RequestModel
* Requests a resource into the streaming, essentially loading it
* Notice the model will not be available right after the call, it's necessary to wait for it.
* Flags are unknown... still researching about them.
*/
void CAbstractStreaming::RequestModel(id_t id, uint32_t flags)
{
injector::cstd<void(int, int)>::call<0x4087E0>(id, flags); // CStreaming::RequestModel
}
/*
* CAbstractStreaming::RemoveModel
* Removes a resource from the streaming, essentially unloading it
* Flags are unknown (and optional) so leave it as default.
*/
void CAbstractStreaming::RemoveModel(id_t id)
{
injector::cstd<void(int)>::call<0x4089A0>(id); // CStreaming::RemoveModel
}
/*
* CAbstractStreaming::LoadAllRequestedModels
* Free ups the streaming bus by loading everything previosly requested
*/
void CAbstractStreaming::LoadAllRequestedModels()
{
plugin_ptr->Log("Loading requested resources...");
injector::cstd<void()>::call<0x5619D0>(); // CTimer::Suspend
injector::cstd<void(int)>::call<0x40EA10>(0); // CStreaming::LoadAllRequestedModels
injector::cstd<void(int)>::call<0x40EA10>(0); // CStreaming::LoadAllRequestedModels
injector::cstd<void()>::call<0x561A00>(); // CTimer::Resume
}
/*
* CAbstractStreaming::FlushChannels
* Flushes the streaming channels loading anything on the bus
*/
void CAbstractStreaming::FlushChannels()
{
plugin_ptr->Log("Flushing streaming channels...");
injector::cstd<void()>::call<0x5619D0>(); // CTimer::Suspend
injector::cstd<void(int)>::call<0x40E460>(0); // CStreaming::FlushChannels
injector::cstd<void()>::call<0x561A00>(); // CTimer::Resume
}
/*
* CAbstractStreaming::RemoveUnusedResources
* Free ups the streaming by removing any unused resource
*/
void CAbstractStreaming::RemoveUnusedResources()
{
plugin_ptr->Log("Removing unused resources...");
injector::cstd<void()>::call<0x5619D0>(); // CTimer::Suspend
injector::cstd<void()>::call<0x40CF80>(); // CStreaming::RemoveAllUnusedModels
injector::cstd<char(uint32_t)>::call<0x40CFD0>(0x20); // CStreaming::RemoveLeastUsedModel
injector::cstd<void()>::call<0x561A00>(); // CTimer::Resume
}
/*
* CAbstractStreaming::RegisterModelIndex
* Registers the existence of an index assigned to a specific filename.
*/
void CAbstractStreaming::RegisterModelIndex(const char* filename, id_t index)
{
this->indices[mhash(filename)] = index;
}
/*
* CAbstractStreaming::RegisterClothingItem
* Registers the existence of a clothing item
*/
void CAbstractStreaming::RegisterClothingItem(const char* filename, int index)
{
this->clothes_map[mhash(filename)] = index;
}
/*
* CAbstractStreaming::RegisterStockEntry
* Registers the stock/default/original cd directory data of an index, important so it can be restored later when necessary.
*/
void CAbstractStreaming::RegisterStockEntry(const char* filename, DirectoryInfo& entry, id_t index, int img_id)
{
// Please note entry here is incomplete because the game null terminated the string before the extension '.', so use filename
this->cd_dir[index] = CdDirectoryItem(filename, entry, img_id);
}
/*
* CAbstractStreaming::InstallFile
* Installs a model, clothing or any streamable file, refreshing them after the process
*/
bool CAbstractStreaming::InstallFile(const modloader::file& file)
{
// If the streaming hasn't initialized we cannot assume much things about the streaming
// One thing to keep in mind is that in principle stuff should load in alpha order (for streamed scenes etc)
if(!this->bHasInitializedStreaming)
{
// Just push it to this list and it will get loaded when the streaming initializes
// At this point we don't know if this is a clothing item or an model, for that reason "raw"
// The initializer will take care of filtering clothes and models from the list
this->raw_models.insert({ file.filename(), &file });
return true;
}
else
{
// We cannot do much at this point, too many calls may come, repeated calls, uninstalls, well, many things will still happen
// so we'll delay the actual install to the next frame, put everything on an import list
this->BeginUpdate();
if(IsNonStreamed(&file))
return false;
else if(!IsClothes(&file))
{
this->to_import[file.hash] = &file;
return true;
}
else
{
auto size_blocks = GetSizeInBlocks(file.size);
this->newcloth_blocks = size_blocks > newcloth_blocks? size_blocks : newcloth_blocks;
this->to_rebuild_player = true;
return this->ImportCloth(&file);
}
}
return false;
}
/*
* CAbstractStreaming::UninstallFile
* Uninstalls a specific file
*/
bool CAbstractStreaming::UninstallFile(const modloader::file& file)
{
// Ahhh, see the comments at InstallFile.....
if(!this->bHasInitializedStreaming)
{
if(IsNonStreamed(&file))
return false;
// Streaming hasn't initialized, just remove it from our raw list
const auto range = this->raw_models.equal_range(file.filename());
const auto it = std::find_if(range.first, range.second, [&file](const auto& p) {
return p.second == &file;
});
if(it != range.second)
this->raw_models.erase(it);
return true;
}
else
{
this->BeginUpdate();
if(IsNonStreamed(&file))
return false;
else if(!IsClothes(&file))
{
// Remove special model (if it is a special model)
erase_from_map(special, &file);
// Mark the specified file [hash] to be vanished
this->to_import[file.hash] = nullptr;
return true;
}
else
{
this->to_rebuild_player = true;
return this->UnimportCloth(&file);
}
}
return false;
}
/*
* CAbstractStreaming::ReinstallFile
* Does the same as InstallFile
*/
bool CAbstractStreaming::ReinstallFile(const modloader::file& file)
{
// Reinstalling works the same way as installing
// Do success even if InstallFile fails, the failure might be related to already having a file installed
InstallFile(file);
return true;
}
/*
* CAbstractStreaming::Update
* Updates the abstract streaming after a serie of install/uninstall
*/
void CAbstractStreaming::Update()
{
if(this->IsUpdating())
{
// Refresh necessary files
this->ProcessRefreshes();
this->EndUpdate();
}
}
/*
* CAbstractStreaming::ImportModels
* Imports the files in the list into the abstract streaming
*/
void CAbstractStreaming::ImportModels(ref_list<const modloader::file*> files)
{
LoadAbstractCdDirectory(files);
}
/*
* CAbstractStreaming::UnimportModel
* Removes the imported index from the abstract streaming
*/
void CAbstractStreaming::UnimportModel(id_t index)
{
// Remove special model related to this index if possible
auto it = imports.find(index);
if(it != imports.end()) erase_from_map(special, it->second.file);
// Restore model information to stock and erase it from the import list
this->RestoreInfoForModel(index);
this->QuickUnimport(index);
}
/*
* CAbstractStreaming::QuickImport
* Quickly import into the specified index the specified file with the specified flags
* This is a raw import technique and should be used ONLY when necessary
* It does not perform any kind of checking such as if the streaming buffer size is enought
*/
void CAbstractStreaming::QuickImport(id_t index, const modloader::file* file, bool isSpecialModel, bool isCloth)
{
plugin_ptr->Log("Importing model file for index %d at \"%s\"", index, file->filepath());
// Add import into the import table
auto& imp = imports[index];
imp.file = file;
imp.isFallingBack = false;
imp.isSpecialModel = isSpecialModel;
imp.isClothes = isCloth;
// Register the existence of such a model and setup info for it
if(!isCloth) this->RegisterModelIndex(file->filename(), index);
this->SetInfoForModel(index, 0, GetSizeInBlocks(file->size));
}
/*
* CAbstractStreaming::QuickUnimport
* Quickly unimport the specified index
* Analogue to QuickImport. This version of unimporting do not mess with the info for model structure.
*/
void CAbstractStreaming::QuickUnimport(id_t index)
{
plugin_ptr->Log("Removing imported model file at index %d", index);
this->imports.erase(index);
}
/*
* CAbstractStreaming::IsClothes
* Checks if a specific file is a clothing item
*/
bool CAbstractStreaming::IsClothes(const modloader::file* file)
{
if(gvm.IsSA()) // Only San Andreas has clothing items
{
if(file->behaviour & is_fcloth_mask)
return true; // Forced clothes
else if(!this->indices.count(file->hash) && this->clothes_map.count(file->hash))
return true; // Not present in standard indices but in clothes map
else if(!strcmp(file->filename(), "coach.dff"))
{
// Coach is both a clothing item and a vehicle, what is up with this file?
// If the number of Clump sections on the RwStream is greater than 1, it's a clothing item.
if(auto f = fopen(file->fullpath().c_str(), "rb"))
{
struct {
uint32_t id, size, version;
} section;
int nclumps = 0;
while(fread(§ion, sizeof(section), 1, f) == 1)
{
if(section.id == 0x0010) // Clump
{
++nclumps;
if(!!fseek(f, section.size, SEEK_CUR))
break;
}
else break;
}
fclose(f);
// If more than one clump in the RwStream, it's a clothing item
bool is_cloth = (nclumps > 1);
if(is_cloth) plugin_ptr->Log("Warning: Clothing coach item outside a player.img directory: %s", file->filepath());
return is_cloth;
}
}
}
return false;
}
/*
* CAbstractStreaming::ImportCloth
* Imports the specified clothing item
* !!!!! Notice that importing a clothing item doesn't contribute to the streaming buffer, please use
* 'StreamingBufferUpdater' manually to do so !!!!
*/
bool CAbstractStreaming::ImportCloth(const modloader::file* file)
{
if(gvm.IsSA())
{
auto clothesDirectory = (CDirectorySA*)(::clothesDirectory);
if(auto entry = FindClothEntry(file->hash))
{
// Every cloth import should go through this code path....
// The else condition calls this function again so it goes by this path
// Allow import only if we don't have any clothing like that installed
return this->clothes.emplace(entry->m_dwFileOffset, file).second;
}
else if(true)
{
// We need to add a new entry into the clothes directory...
DirectoryInfo entry;
plugin_ptr->Log("Adding new item to clothing directory \"%s\"", file->filename());
this->RegisterClothingItem(file->filename(), clothesDirectory->m_dwCount); // Tell us about it
FillDirectoryEntry(entry, file->filename(), TakeClothSparseOffset(), file->size);
injector::thiscall<void(CDirectorySA*, DirectoryInfo*)>::call<0x532310>(clothesDirectory, &entry); // CDirectory::AddItem
sparse_dir_entries.emplace_back(entry);
// Try again, now it'll work
return this->ImportCloth(file);
}
}
return false;
}
/*
* CAbstractStreaming::UnimportCloth
* Unimports the specified clothing item
*/
bool CAbstractStreaming::UnimportCloth(const modloader::file* file)
{
// Right, since the index is a file offset we need to iterate on the map until we find this file
for(auto it = this->clothes.begin(); it != clothes.end(); ++it)
{
if(it->second == file)
{
// We cannot unimport added clothing items during runtime!!!
if(this->IsClothSparseOffset(it->first) == false)
{
// Fine, let's unimport
this->clothes.erase(it);
return true;
}
return false;
}
}
return false;
}