-
-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathCFileLoaderSA.cpp
181 lines (152 loc) · 6.1 KB
/
CFileLoaderSA.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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CFileLoaderSA.cpp
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
#include "gamesa_renderware.h"
#include "CFileLoaderSA.h"
#include "CModelInfoSA.h"
#include "CRenderWareSA.h"
CFileLoaderSA::CFileLoaderSA()
{
}
CFileLoaderSA::~CFileLoaderSA()
{
}
void CFileLoaderSA::StaticSetHooks()
{
HookInstall(0x5371F0, (DWORD)CFileLoader_LoadAtomicFile, 5);
HookInstall(0x537150, (DWORD)CFileLoader_SetRelatedModelInfoCB, 5);
HookInstall(0x538690, (DWORD)CFileLoader_LoadObjectInstance, 5);
}
CEntitySAInterface* CFileLoaderSA::LoadObjectInstance(SFileObjectInstance* obj)
{
// Second argument is model name. It's unused in the function
return ((CEntitySAInterface * (__cdecl*)(SFileObjectInstance*, const char*))0x538090)(obj, nullptr);
}
class CAtomicModelInfo
{
public:
void CAtomicModelInfo::DeleteRwObject() { ((void(__thiscall*)(CAtomicModelInfo*))(*(void***)this)[8])(this); }
void CAtomicModelInfo::SetAtomic(RpAtomic* atomic) { ((void(__thiscall*)(CAtomicModelInfo*, RpAtomic*))(*(void***)this)[15])(this, atomic); }
};
class CDamagableModelInfo
{
public:
void CDamagableModelInfo::SetDamagedAtomic(RpAtomic* atomic) { ((void(__thiscall*)(CDamagableModelInfo*, RpAtomic*))0x4C48D0)(this, atomic); }
};
static void CVisibilityPlugins_SetAtomicRenderCallback(RpAtomic* pRpAtomic, RpAtomic* (*renderCB)(RpAtomic*))
{
return ((void(__cdecl*)(RpAtomic*, RpAtomic * (*renderCB)(RpAtomic*)))0x7328A0)(pRpAtomic, renderCB);
}
static void CVisibilityPlugins_SetAtomicId(RpAtomic* pRpAtomic, int id)
{
return ((void(__cdecl*)(RpAtomic*, int))0x732230)(pRpAtomic, id);
}
static void CVehicleModelInfo_UseCommonVehicleTexDicationary()
{
((void(__cdecl*)())0x4C75A0)();
}
static void CVehicleModelInfo_StopUsingCommonVehicleTexDicationary()
{
((void(__cdecl*)())0x4C75C0)();
}
static auto CModelInfo_ms_modelInfoPtrs = (CBaseModelInfoSAInterface**)ARRAY_ModelInfo;
static unsigned int& gAtomicModelId = *reinterpret_cast<unsigned int*>(DWORD_AtomicsReplacerModelID);
bool CFileLoader_LoadAtomicFile(RwStream* stream, unsigned int modelId)
{
CBaseModelInfoSAInterface* pBaseModelInfo = CModelInfo_ms_modelInfoPtrs[modelId];
auto pAtomicModelInfo = reinterpret_cast<CAtomicModelInfo*>(pBaseModelInfo);
bool bUseCommonVehicleTexDictionary = false;
if (pAtomicModelInfo && pBaseModelInfo->bWetRoadReflection)
{
bUseCommonVehicleTexDictionary = true;
CVehicleModelInfo_UseCommonVehicleTexDicationary();
}
const unsigned int rwID_CLUMP = 16;
if (RwStreamFindChunk(stream, rwID_CLUMP, nullptr, nullptr))
{
RpClump* pReadClump = RpClumpStreamRead(stream);
if (!pReadClump)
{
if (bUseCommonVehicleTexDictionary)
{
CVehicleModelInfo_StopUsingCommonVehicleTexDicationary();
}
return false;
}
gAtomicModelId = modelId;
SRelatedModelInfo relatedModelInfo = {0};
relatedModelInfo.pClump = pReadClump;
relatedModelInfo.bDeleteOldRwObject = false;
RpClumpForAllAtomics(pReadClump, reinterpret_cast<RpClumpForAllAtomicsCB_t>(CFileLoader_SetRelatedModelInfoCB), &relatedModelInfo);
RpClumpDestroy(pReadClump);
}
if (!pBaseModelInfo->pRwObject)
{
return false;
}
if (bUseCommonVehicleTexDictionary)
{
CVehicleModelInfo_StopUsingCommonVehicleTexDicationary();
}
return true;
}
RpAtomic* CFileLoader_SetRelatedModelInfoCB(RpAtomic* atomic, SRelatedModelInfo* pRelatedModelInfo)
{
char name[24];
CBaseModelInfoSAInterface* pBaseModelInfo = CModelInfo_ms_modelInfoPtrs[gAtomicModelId];
auto pAtomicModelInfo = reinterpret_cast<CAtomicModelInfo*>(pBaseModelInfo);
RwFrame* pOldFrame = reinterpret_cast<RwFrame*>(atomic->object.object.parent);
char* frameNodeName = CRenderWareSA::GetFrameNodeName(pOldFrame);
bool bDamage = false;
CRenderWareSA::GetNameAndDamage(frameNodeName, name, bDamage);
CVisibilityPlugins_SetAtomicRenderCallback(atomic, 0);
RpAtomic* pOldAtomic = reinterpret_cast<RpAtomic*>(pBaseModelInfo->pRwObject);
if (bDamage)
{
auto pDamagableModelInfo = reinterpret_cast<CDamagableModelInfo*>(pAtomicModelInfo);
pDamagableModelInfo->SetDamagedAtomic(atomic);
}
else
{
pAtomicModelInfo->SetAtomic(atomic);
}
RpClumpRemoveAtomic(pRelatedModelInfo->pClump, atomic);
RwFrame* newFrame = RwFrameCreate();
RpAtomicSetFrame(atomic, newFrame);
CVisibilityPlugins_SetAtomicId(atomic, gAtomicModelId);
// Fix #359: engineReplaceModel memory leak
if (!bDamage && pRelatedModelInfo->bDeleteOldRwObject)
{
if (pOldAtomic)
{
RpAtomicDestroy(pOldAtomic);
}
if (pOldFrame)
{
RwFrameDestroy(pOldFrame);
}
}
return atomic;
}
CEntitySAInterface* CFileLoader_LoadObjectInstance(const char* szLine)
{
char szModelName[24];
SFileObjectInstance inst;
sscanf(szLine, "%d %s %d %f %f %f %f %f %f %f %d", &inst.modelID, szModelName, &inst.interiorID, &inst.position.fX, &inst.position.fY, &inst.position.fZ,
&inst.rotation.fX, &inst.rotation.fY, &inst.rotation.fZ, &inst.rotation.fW, &inst.lod);
/*
A quaternion is must be normalized. GTA is relying on an internal R* exporter and everything is OK,
but custom exporters might not contain the normalization. And we must do it instead.
*/
const float fLenSq = inst.rotation.LengthSquared();
if (fLenSq > 0.0f && std::fabs(fLenSq - 1.0f) > std::numeric_limits<float>::epsilon())
inst.rotation /= std::sqrt(fLenSq);
return ((CEntitySAInterface * (__cdecl*)(SFileObjectInstance*))0x538090)(&inst);
}