/
CRenderWareSA.cpp
840 lines (718 loc) · 27.8 KB
/
CRenderWareSA.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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CRenderWareSA.cpp
* PURPOSE: RenderWare mapping to Grand Theft Auto: San Andreas
* and miscellaneous rendering functions
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
* RenderWare is © Criterion Software
*
*****************************************************************************/
#include "StdInc.h"
#define RWFUNC_IMPLEMENT
#include "gamesa_renderware.h"
#include "gamesa_renderware.hpp"
#include "CRenderWareSA.ShaderMatching.h"
extern CGameSA* pGame;
// RwFrameForAllObjects struct and callback used to replace dynamic vehicle parts
struct SReplaceParts
{
const char* szName; // name of the part you want to replace (e.g. 'door_lf' or 'door_rf')
unsigned char ucIndex; // index counter for internal usage (0 is the 'ok' part model, 1 is the 'dam' part model)
RpAtomicContainer* pReplacements; // replacement atomics
unsigned int uiReplacements; // number of replacements
};
static RwObject* ReplacePartsCB(RwObject* object, SReplaceParts* data)
{
RpAtomic* Atomic = (RpAtomic*)object;
char szAtomicName[16] = {0};
// iterate through our loaded atomics
for (unsigned int i = 0; i < data->uiReplacements; i++)
{
// get the correct atomic name based on the object # in our parent frame
if (data->ucIndex == 0)
snprintf(&szAtomicName[0], 16, "%s_ok", data->szName);
else
snprintf(&szAtomicName[0], 16, "%s_dam", data->szName);
// see if we have such an atomic in our replacement atomics array
if (strncmp(&data->pReplacements[i].szName[0], &szAtomicName[0], 16) == 0)
{
// if so, override the geometry
RpAtomicSetGeometry(Atomic, data->pReplacements[i].atomic->geometry, 0);
// and copy the matrices
RwFrameCopyMatrix(RpGetFrame(Atomic), RpGetFrame(data->pReplacements[i].atomic));
object = (RwObject*)data->pReplacements[i].atomic;
data->ucIndex++;
}
}
return object;
}
// RpClumpForAllAtomics callback used to add atomics to a vehicle
static bool AddAllAtomicsCB(RpAtomic* atomic, void* pClump)
{
RpClump* data = reinterpret_cast<RpClump*>(pClump);
RwFrame* pFrame = RpGetFrame(data);
// add the atomic to the frame
RpAtomicSetFrame(atomic, pFrame);
RpClumpAddAtomic(data, atomic);
return true;
}
// RpClumpForAllAtomics struct and callback used to replace all wheels with a given wheel model
struct SReplaceWheels
{
const char* szName; // name of the new wheel model
RpClump* pClump; // the vehicle's clump
RpAtomicContainer* pReplacements; // replacement atomics
unsigned int uiReplacements; // number of replacements
};
static bool ReplaceWheelsCB(RpAtomic* atomic, void* pData)
{
SReplaceWheels* data = reinterpret_cast<SReplaceWheels*>(pData);
RwFrame* Frame = RpGetFrame(atomic);
// find our wheel atomics
if (strncmp(&Frame->szName[0], "wheel_lf_dummy", 16) == 0 || strncmp(&Frame->szName[0], "wheel_rf_dummy", 16) == 0 ||
strncmp(&Frame->szName[0], "wheel_lm_dummy", 16) == 0 || strncmp(&Frame->szName[0], "wheel_rm_dummy", 16) == 0 ||
strncmp(&Frame->szName[0], "wheel_lb_dummy", 16) == 0 || strncmp(&Frame->szName[0], "wheel_rb_dummy", 16) == 0)
{
// find a replacement atomic
for (unsigned int i = 0; i < data->uiReplacements; i++)
{
// see if it's name is equal to the specified wheel
if (strncmp(&data->pReplacements[i].szName[0], data->szName, 16) == 0)
{
// clone our wheel atomic
RpAtomic* WheelAtomic = RpAtomicClone(data->pReplacements[i].atomic);
RpAtomicSetFrame(WheelAtomic, Frame);
// add it to the clump
RpClumpAddAtomic(data->pClump, WheelAtomic);
// delete the current atomic
RpClumpRemoveAtomic(data->pClump, atomic);
}
}
}
return true;
}
// RpClumpForAllAtomics struct and callback used to replace all atomics for a vehicle
struct SReplaceAll
{
RpClump* pClump; // the vehicle's clump
RpAtomicContainer* pReplacements; // replacement atomics
unsigned int uiReplacements; // number of replacements
};
static bool ReplaceAllCB(RpAtomic* atomic, void* pData)
{
SReplaceAll* data = reinterpret_cast<SReplaceAll*>(pData);
RwFrame* Frame = RpGetFrame(atomic);
if (Frame == NULL)
return true;
// find a replacement atomic
for (unsigned int i = 0; i < data->uiReplacements; i++)
{
// see if we can find an atomic with the same name
if (strncmp(&data->pReplacements[i].szName[0], &Frame->szName[0], 16) == 0)
{
// copy the matrices
RwFrameCopyMatrix(RpGetFrame(atomic), RpGetFrame(data->pReplacements[i].atomic));
// set the new atomic's frame to the current one
RpAtomicSetFrame(data->pReplacements[i].atomic, Frame);
// override all engine and material related callbacks and pointers
data->pReplacements[i].atomic->renderCallback = atomic->renderCallback;
data->pReplacements[i].atomic->frame = atomic->frame;
data->pReplacements[i].atomic->render = atomic->render;
data->pReplacements[i].atomic->interpolation = atomic->interpolation;
data->pReplacements[i].atomic->info = atomic->info;
// add the new atomic to the vehicle clump
RpClumpAddAtomic(data->pClump, data->pReplacements[i].atomic);
// remove the current atomic
RpClumpRemoveAtomic(data->pClump, atomic);
}
}
return true;
}
// RpClumpForAllAtomics struct and callback used to load the atomics from a specific clump into a container
struct SLoadAtomics
{
RpAtomicContainer* pReplacements; // replacement atomics
unsigned int uiReplacements; // number of replacements
};
static bool LoadAtomicsCB(RpAtomic* atomic, void* pData)
{
SLoadAtomics* data = reinterpret_cast<SLoadAtomics*>(pData);
RwFrame* Frame = RpGetFrame(atomic);
// add the atomic to the container
data->pReplacements[data->uiReplacements].atomic = atomic;
strncpy(&data->pReplacements[data->uiReplacements].szName[0], &Frame->szName[0], 16);
// and increment the counter
data->uiReplacements++;
return true;
}
/////////////////////////////////////////////////////////////////////////////
//
// CRenderWareSA
//
//
/////////////////////////////////////////////////////////////////////////////
CRenderWareSA::CRenderWareSA(eGameVersion version)
{
InitRwFunctions(version);
InitTextureWatchHooks();
m_pMatchChannelManager = new CMatchChannelManager();
m_iRenderingEntityType = TYPE_MASK_WORLD;
m_GTAVertexShadersDisabledTimer.SetMaxIncrement(1000, true);
m_bGTAVertexShadersEnabled = true;
}
CRenderWareSA::~CRenderWareSA()
{
SAFE_DELETE(m_pMatchChannelManager);
}
// Reads and parses a TXD file specified by a path (szTXD)
RwTexDictionary* CRenderWareSA::ReadTXD(const SString& strFilename, const CBuffer& fileData)
{
// open the stream
RwStream* streamTexture;
RwBuffer buffer;
if (!fileData.IsEmpty())
{
buffer.ptr = (void*)fileData.GetData();
buffer.size = fileData.GetSize();
streamTexture = RwStreamOpen(STREAM_TYPE_BUFFER, STREAM_MODE_READ, &buffer);
}
else
streamTexture = RwStreamOpen(STREAM_TYPE_FILENAME, STREAM_MODE_READ, *strFilename);
// check for errors
if (streamTexture == NULL)
return NULL;
// TXD header id: 0x16
// find our txd chunk (dff loads textures, so correct loading order is: txd, dff)
if (RwStreamFindChunk(streamTexture, 0x16, NULL, NULL) == false)
{
RwStreamClose(streamTexture, NULL);
return NULL;
}
// read the texture dictionary from our model (txd)
RwTexDictionary* pTex = RwTexDictionaryGtaStreamRead(streamTexture);
// close the stream
RwStreamClose(streamTexture, NULL);
ScriptAddedTxd(pTex);
return pTex;
}
// Reads and parses a DFF file specified by a path (szDFF) into a CModelInfo identified by the object id (usModelID)
// bLoadEmbeddedCollisions should be true for vehicles
// Any custom TXD should be imported before this call
RpClump* CRenderWareSA::ReadDFF(const SString& strFilename, const SString& buffer, unsigned short usModelID, bool bLoadEmbeddedCollisions)
{
// Set correct TXD as materials are processed at the same time
if (usModelID != 0)
{
unsigned short usTxdId = ((CBaseModelInfoSAInterface**)ARRAY_ModelInfo)[usModelID]->usTextureDictionary;
SetTextureDict(usTxdId);
}
// open the stream
RwStream* streamModel;
RwBuffer streamBuffer;
if (!buffer.empty())
{
streamBuffer.ptr = (void*)buffer.data();
streamBuffer.size = buffer.size();
streamModel = RwStreamOpen(STREAM_TYPE_BUFFER, STREAM_MODE_READ, &streamBuffer);
}
else
streamModel = RwStreamOpen(STREAM_TYPE_FILENAME, STREAM_MODE_READ, *strFilename);
// get the modelinfo array
DWORD* pPool = (DWORD*)ARRAY_ModelInfo;
// check for errors
if (streamModel == NULL)
return NULL;
// DFF header id: 0x10
// find our dff chunk
if (RwStreamFindChunk(streamModel, 0x10, NULL, NULL) == false)
{
RwStreamClose(streamModel, NULL);
return NULL;
}
// rockstar's collision hack: set the global particle emitter to the modelinfo pointer of this model
if (bLoadEmbeddedCollisions)
RpPrtStdGlobalDataSetStreamEmbedded((void*)pPool[usModelID]);
// read the clump with all its extensions
RpClump* pClump = RpClumpStreamRead(streamModel);
// reset collision hack
if (bLoadEmbeddedCollisions)
RpPrtStdGlobalDataSetStreamEmbedded(NULL);
// close the stream
RwStreamClose(streamModel, NULL);
return pClump;
}
//
// Returns list of atomics inside a clump
//
void CRenderWareSA::GetClumpAtomicList(RpClump* pClump, std::vector<RpAtomic*>& outAtomicList)
{
RpClumpForAllAtomics(pClump,
[](RpAtomic* pAtomic, void* pData) {
reinterpret_cast<std::vector<RpAtomic*>*>(pData)->push_back(pAtomic);
return true;
},
&outAtomicList);
}
//
// Returns true if the clump geometry sort of matches
//
// ClumpA vs ClumpB(or)AtomicB
//
bool CRenderWareSA::DoContainTheSameGeometry(RpClump* pClumpA, RpClump* pClumpB, RpAtomic* pAtomicB)
{
// Fast check if comparing one atomic
if (pAtomicB)
{
RpGeometry* pGeometryA = ((RpAtomic*)((pClumpA->atomics.root.next) - 0x8))->geometry;
RpGeometry* pGeometryB = pAtomicB->geometry;
return pGeometryA == pGeometryB;
}
// Get atomic list from both sides
std::vector<RpAtomic*> atomicListA;
std::vector<RpAtomic*> atomicListB;
GetClumpAtomicList(pClumpA, atomicListA);
if (pClumpB)
GetClumpAtomicList(pClumpB, atomicListB);
if (pAtomicB)
atomicListB.push_back(pAtomicB);
// Count geometries that exist in both sides
std::set<RpGeometry*> geometryListA;
for (uint i = 0; i < atomicListA.size(); i++)
MapInsert(geometryListA, atomicListA[i]->geometry);
uint uiInBoth = 0;
for (uint i = 0; i < atomicListB.size(); i++)
if (MapContains(geometryListA, atomicListB[i]->geometry))
uiInBoth++;
// If less than 50% match then assume it is not the same
if (uiInBoth * 2 < atomicListB.size() || atomicListB.size() == 0)
return false;
return true;
}
// Replaces a vehicle/weapon/ped model
void CRenderWareSA::ReplaceModel(RpClump* pNew, unsigned short usModelID, DWORD dwFunc)
{
CModelInfo* pModelInfo = pGame->GetModelInfo(usModelID);
if (pModelInfo)
{
RpClump* pOldClump = (RpClump*)pModelInfo->GetRwObject();
if (!DoContainTheSameGeometry(pNew, pOldClump, NULL))
{
if (pModelInfo->IsVehicle())
{
// Reset our valid upgrade list
pModelInfo->ResetSupportedUpgrades();
}
// Make new clump container for the model geometry
// Clone twice as the geometry render order seems to be reversed each time it is cloned.
RpClump* pTemp = RpClumpClone(pNew);
RpClump* pNewClone = RpClumpClone(pTemp);
RpClumpDestroy(pTemp);
// Calling CVehicleModelInfo::SetClump() allocates a new CVehicleStructure.
// So let's delete the old one first to avoid CPool<CVehicleStructure> depletion.
if (dwFunc == FUNC_LoadVehicleModel)
{
CVehicleModelInfoSAInterface* pVehicleModelInfoInterface = (CVehicleModelInfoSAInterface*)pModelInfo->GetInterface();
if (pVehicleModelInfoInterface->pVisualInfo)
{
DWORD dwDeleteFunc = FUNC_CVehicleStructure_delete;
CVehicleModelVisualInfoSAInterface* info = pVehicleModelInfoInterface->pVisualInfo;
_asm
{
mov eax, info
push eax
call dwDeleteFunc
add esp, 4
}
pVehicleModelInfoInterface->pVisualInfo = nullptr;
}
}
// ModelInfo::SetClump
CBaseModelInfoSAInterface* pModelInfoInterface = pModelInfo->GetInterface();
_asm
{
mov ecx, pModelInfoInterface
push pNewClone
call dwFunc
}
// Destroy old clump container
RpClumpDestroy(pOldClump);
}
}
}
// Replaces a vehicle model
void CRenderWareSA::ReplaceVehicleModel(RpClump* pNew, unsigned short usModelID)
{
ReplaceModel(pNew, usModelID, FUNC_LoadVehicleModel);
}
// Replaces a weapon model
void CRenderWareSA::ReplaceWeaponModel(RpClump* pNew, unsigned short usModelID)
{
ReplaceModel(pNew, usModelID, FUNC_LoadWeaponModel);
}
// Replaces a ped model
void CRenderWareSA::ReplacePedModel(RpClump* pNew, unsigned short usModelID)
{
ReplaceModel(pNew, usModelID, FUNC_LoadPedModel);
}
// Reads and parses a COL3 file
CColModel* CRenderWareSA::ReadCOL(const CBuffer& fileData)
{
if (fileData.GetSize() < sizeof(ColModelFileHeader) + 16)
return NULL;
const ColModelFileHeader& header = *(ColModelFileHeader*)fileData.GetData();
// Load the col model
if (header.version[0] == 'C' && header.version[1] == 'O' && header.version[2] == 'L')
{
unsigned char* pModelData = (unsigned char*)fileData.GetData() + sizeof(ColModelFileHeader);
// Create a new CColModel
CColModelSA* pColModel = new CColModelSA();
if (header.version[3] == 'L')
{
LoadCollisionModel(pModelData, pColModel->GetInterface(), NULL);
}
else if (header.version[3] == '2')
{
LoadCollisionModelVer2(pModelData, header.size - 0x18, pColModel->GetInterface(), NULL);
}
else if (header.version[3] == '3')
{
LoadCollisionModelVer3(pModelData, header.size - 0x18, pColModel->GetInterface(), NULL);
}
// Return the collision model
return pColModel;
}
return NULL;
}
// Loads all atomics from a clump into a container struct and returns the number of atomics it loaded
unsigned int CRenderWareSA::LoadAtomics(RpClump* pClump, RpAtomicContainer* pAtomics)
{
// iterate through all atomics in the clump
SLoadAtomics data = {0};
data.pReplacements = pAtomics;
RpClumpForAllAtomics(pClump, LoadAtomicsCB, &data);
// return the number of atomics that were added to the container
return data.uiReplacements;
}
// Replaces all atomics for a specific model
typedef struct
{
unsigned short usTxdID;
RpClump* pClump;
} SAtomicsReplacer;
bool AtomicsReplacer(RpAtomic* pAtomic, void* data)
{
SAtomicsReplacer* pData = reinterpret_cast<SAtomicsReplacer*>(data);
((void (*)(RpAtomic*, void*))FUNC_AtomicsReplacer)(pAtomic, pData->pClump);
// The above function adds a reference to the model's TXD. Remove it again.
CTxdStore_RemoveRef(pData->usTxdID);
return true;
}
void CRenderWareSA::ReplaceAllAtomicsInModel(RpClump* pNew, unsigned short usModelID)
{
CModelInfo* pModelInfo = pGame->GetModelInfo(usModelID);
if (pModelInfo)
{
RpAtomic* pOldAtomic = (RpAtomic*)pModelInfo->GetRwObject();
if (!DoContainTheSameGeometry(pNew, NULL, pOldAtomic))
{
// Clone the clump that's to be replaced (FUNC_AtomicsReplacer removes the atomics from the source clump)
RpClump* pCopy = RpClumpClone(pNew);
// Replace the atomics
SAtomicsReplacer data;
data.usTxdID = ((CBaseModelInfoSAInterface**)ARRAY_ModelInfo)[usModelID]->usTextureDictionary;
data.pClump = pCopy;
MemPutFast<DWORD>((DWORD*)DWORD_AtomicsReplacerModelID, usModelID);
RpClumpForAllAtomics(pCopy, AtomicsReplacer, &data);
// Get rid of the now empty copied clump
RpClumpDestroy(pCopy);
}
}
}
// Replaces all atomics in a vehicle
void CRenderWareSA::ReplaceAllAtomicsInClump(RpClump* pDst, RpAtomicContainer* pAtomics, unsigned int uiAtomics)
{
SReplaceAll data;
data.pClump = pDst;
data.pReplacements = pAtomics;
data.uiReplacements = uiAtomics;
RpClumpForAllAtomics(pDst, ReplaceAllCB, &data);
}
// Replaces the wheels in a vehicle
void CRenderWareSA::ReplaceWheels(RpClump* pClump, RpAtomicContainer* pAtomics, unsigned int uiAtomics, const char* szWheel)
{
// get the clump's frame
RwFrame* pFrame = RpGetFrame(pClump);
SReplaceWheels data;
data.pClump = pClump;
data.pReplacements = pAtomics;
data.uiReplacements = uiAtomics;
data.szName = szWheel;
RpClumpForAllAtomics(pClump, ReplaceWheelsCB, &data);
}
// Repositions an atomic
void CRenderWareSA::RepositionAtomic(RpClump* pDst, RpClump* pSrc, const char* szName)
{
RwFrame* pDstFrame = RpGetFrame(pDst);
RwFrame* pSrcFrame = RpGetFrame(pSrc);
RwFrameCopyMatrix(RwFrameFindFrame(pDstFrame, szName), RwFrameFindFrame(pSrcFrame, szName));
}
// Adds the atomics from a source clump (pSrc) to a destination clump (pDst)
void CRenderWareSA::AddAllAtomics(RpClump* pDst, RpClump* pSrc)
{
RpClumpForAllAtomics(pSrc, AddAllAtomicsCB, pDst);
}
// Replaces dynamic parts of the vehicle (models that have two different versions: 'ok' and 'dam'), such as doors
// szName should be without the part suffix (e.g. 'door_lf' or 'door_rf', and not 'door_lf_dummy')
bool CRenderWareSA::ReplacePartModels(RpClump* pClump, RpAtomicContainer* pAtomics, unsigned int uiAtomics, const char* szName)
{
// get the part's dummy name
char szDummyName[16] = {0};
snprintf(&szDummyName[0], 16, "%s_dummy", szName);
// get the clump's frame
RwFrame* pFrame = RpGetFrame(pClump);
// get the part's dummy frame
RwFrame* pPart = RwFrameFindFrame(pFrame, &szDummyName[0]);
if (pPart == NULL)
return false;
// now replace all the objects in the frame
SReplaceParts data = {0};
data.pReplacements = pAtomics;
data.uiReplacements = uiAtomics;
data.szName = szName;
RwFrameForAllObjects(pPart, ReplacePartsCB, &data);
return true;
}
// Replaces a CColModel for a specific object identified by the object id (usModelID)
void CRenderWareSA::ReplaceCollisions(CColModel* pCol, unsigned short usModelID)
{
DWORD* pPool = (DWORD*)ARRAY_ModelInfo;
CColModelSA* pColModel = (CColModelSA*)pCol;
CModelInfoSA* pModelInfoSA = (CModelInfoSA*)(pGame->GetModelInfo(usModelID));
// Apply some low-level hacks (copies the old col area and sets a flag)
DWORD pColModelInterface = (DWORD)pColModel->GetInterface();
DWORD pOldColModelInterface = *((DWORD*)pPool[usModelID] + 20);
MemOrFast<BYTE>(pPool[usModelID] + 0x13, 8);
MemPutFast<BYTE>(pColModelInterface + 40, *((BYTE*)(pOldColModelInterface + 40)));
// TODO: It seems that on entering the game, when this function is executed, the modelinfo array for this
// model is still zero, leading to a crash!
pModelInfoSA->IsLoaded();
}
// Destroys a DFF instance
void CRenderWareSA::DestroyDFF(RpClump* pClump)
{
if (pClump)
RpClumpDestroy(pClump);
}
// Destroys a TXD instance
void CRenderWareSA::DestroyTXD(RwTexDictionary* pTXD)
{
if (pTXD)
RwTexDictionaryDestroy(pTXD);
}
// Destroys a texture instance
void CRenderWareSA::DestroyTexture(RwTexture* pTex)
{
if (pTex)
{
ScriptRemovedTexture(pTex);
RwTextureDestroy(pTex);
}
}
void CRenderWareSA::RwTexDictionaryRemoveTexture(RwTexDictionary* pTXD, RwTexture* pTex)
{
if (pTex->txd != pTXD)
return;
pTex->TXDList.next->prev = pTex->TXDList.prev;
pTex->TXDList.prev->next = pTex->TXDList.next;
pTex->txd = NULL;
}
short CRenderWareSA::CTxdStore_GetTxdRefcount(unsigned short usTxdID)
{
return *(short*)(*(*(DWORD**)0xC8800C) + 0xC * usTxdID + 4);
}
bool CRenderWareSA::RwTexDictionaryContainsTexture(RwTexDictionary* pTXD, RwTexture* pTex)
{
return pTex->txd == pTXD;
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::TxdForceUnload
//
// 1. Unload txd from memory
// 2. Cross fingers
//
// Why do we do this?
// Player model adds (seemingly) unnecessary refs
// (Will crash if anything is actually using the txd)
//
// No idea what will happen if there is a custom txd replacement
//
////////////////////////////////////////////////////////////////
void CRenderWareSA::TxdForceUnload(ushort usTxdId, bool bDestroyTextures)
{
RwTexDictionary* pTxd = CTxdStore_GetTxd(usTxdId);
if (!pTxd)
return;
// We can abandon the textures instead of destroy. It might be safer, but will cause a memory leak
if (bDestroyTextures)
{
// Unref the textures
std::vector<RwTexture*> textureList;
pGame->GetRenderWareSA()->GetTxdTextures(textureList, pTxd);
for (std::vector<RwTexture*>::iterator iter = textureList.begin(); iter != textureList.end(); iter++)
{
RwTexture* pTexture = *iter;
while (pTexture->refs > 1)
RwTextureDestroy(pTexture);
RwTextureDestroy(pTexture);
}
}
// Need to have at least one ref for RemoveRef to work correctly
if (CTxdStore_GetNumRefs(usTxdId) == 0)
{
CTxdStore_AddRef(usTxdId);
}
while (CTxdStore_GetNumRefs(usTxdId) > 0)
{
CTxdStore_RemoveRef(usTxdId);
}
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::GetTXDIDForModelID
//
// Get a TXD ID associated with the model ID
//
////////////////////////////////////////////////////////////////
ushort CRenderWareSA::GetTXDIDForModelID(ushort usModelID)
{
if (usModelID >= 20000 && usModelID < 25000)
{
// Get global TXD ID instead
return usModelID - 20000;
}
else
{
// Get the CModelInfo's TXD ID
// Ensure valid
if (usModelID >= 20000 || !((CBaseModelInfoSAInterface**)ARRAY_ModelInfo)[usModelID])
return 0;
return ((CBaseModelInfoSAInterface**)ARRAY_ModelInfo)[usModelID]->usTextureDictionary;
}
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::GetModelTextureNames
//
// Get list of texture names associated with the model
// Will try to load the model if needed
//
////////////////////////////////////////////////////////////////
void CRenderWareSA::GetModelTextureNames(std::vector<SString>& outNameList, ushort usModelId)
{
outNameList.clear();
// Special case for CJ
if (usModelId == 0)
{
outNameList.push_back("CJ");
return;
}
ushort usTxdId = GetTXDIDForModelID(usModelId);
if (usTxdId == 0)
return;
// Get the TXD corresponding to this ID
RwTexDictionary* pTXD = CTxdStore_GetTxd(usTxdId);
bool bLoadedModel = false;
if (!pTXD)
{
// Try model load
bLoadedModel = true;
pGame->GetModelInfo(usModelId)->Request(BLOCKING, "CRenderWareSA::GetModelTextureNames");
pTXD = CTxdStore_GetTxd(usTxdId);
}
std::vector<RwTexture*> textureList;
GetTxdTextures(textureList, pTXD);
for (std::vector<RwTexture*>::iterator iter = textureList.begin(); iter != textureList.end(); iter++)
{
outNameList.push_back((*iter)->name);
}
if (bLoadedModel)
((void(__cdecl*)(unsigned short))FUNC_RemoveModel)(usModelId);
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::GetTxdTextures
//
// If TXD must already be loaded
//
////////////////////////////////////////////////////////////////
void CRenderWareSA::GetTxdTextures(std::vector<RwTexture*>& outTextureList, ushort usTxdId)
{
if (usTxdId == 0)
return;
// Get the TXD corresponding to this ID
RwTexDictionary* pTXD = CTxdStore_GetTxd(usTxdId);
if (!pTXD)
return;
GetTxdTextures(outTextureList, pTXD);
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::GetTxdTextures
//
// Get textures from a TXD
//
////////////////////////////////////////////////////////////////
void CRenderWareSA::GetTxdTextures(std::vector<RwTexture*>& outTextureList, RwTexDictionary* pTXD)
{
if (pTXD)
{
RwTexDictionaryForAllTextures(pTXD, StaticGetTextureCB, &outTextureList);
}
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::StaticGetTextureCB
//
// Callback used in GetTxdTextures
//
////////////////////////////////////////////////////////////////
bool CRenderWareSA::StaticGetTextureCB(RwTexture* texture, std::vector<RwTexture*>* pTextureList)
{
pTextureList->push_back(texture);
return true;
}
////////////////////////////////////////////////////////////////
//
// CRenderWareSA::GetTextureName
//
// Only called by CRenderItemManager::GetVisibleTextureNames ?
//
////////////////////////////////////////////////////////////////
const char* CRenderWareSA::GetTextureName(CD3DDUMMY* pD3DData)
{
STexInfo** ppTexInfo = MapFind(m_D3DDataTexInfoMap, pD3DData);
if (ppTexInfo)
return (*ppTexInfo)->strTextureName;
if (!pD3DData)
return FAKE_NAME_NO_TEXTURE;
return "";
}
//
// CFastHashMap functions
//
CD3DDUMMY* GetEmptyMapKey(CD3DDUMMY**)
{
return FAKE_D3DTEXTURE_EMPTY_KEY;
}
CD3DDUMMY* GetDeletedMapKey(CD3DDUMMY**)
{
return FAKE_D3DTEXTURE_DELETED_KEY;
}
RwFrame* CRenderWareSA::GetFrameFromName(RpClump* pRoot, SString strName)
{
return RwFrameFindFrame(RpGetFrame(pRoot), strName);
}