-
Notifications
You must be signed in to change notification settings - Fork 3
/
scrlog_VC.cpp
324 lines (278 loc) · 8.21 KB
/
scrlog_VC.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
/*
* scrlog_VC.cpp:
* This file contains the hookers for GTA Vice City (all versions);
*
* Issues:
* *No CollectGlobalVariableIndex or CollectVariablePointer (game fault)
* *If using CLEO, this must be loaded after CLEO.asi (not really a issue, just a advice),
* hopefully, this may happen always since scrlog.asi is after CLEO.asi in alphabetic order
* *CLEO hooks all commands with label as parameter, so... yeah, we cant show label params if CLEO is installed
* *Many of the Vice's script engine stuff is inlined, and when inlined we wont have access to the values since
* no func is called.
*
* LICENSE:
* (c) 2020 - lazyuselessman, Junior_Djjr
* (c) 2013 - LINK/2012 - <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this plugin and source code, to copy, modify, merge, publish and/or distribute
* as long as you leave the original credits (above copyright notice)
* together with your derived work. Please not you are NOT permited to sell or get money from it
*
* THE SOFTWARE AND SOURCE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "GameInfo.h"
#include "Injector.h"
#include "scrlog.h"
// Some macros to help in saving important registers being used
#define push_regs(a, b) _asm push a _asm push b
#define pop_regs(a, b) _asm pop b _asm pop a
static void PatchALL(bool bSteam, bool bJapanese);
void VC_Patch(GameInfo& info)
{
PatchALL(info.IsSteam(), info.IsJapanese());
}
static void* CRunningScript__ProcessOneCommand;
static void HOOK_RegisterCommand();
static void HOOK_AfterScripts();
static void HOOK_RegisterScript();
static void HOOK_CalledCollectParameters();
static void HOOK_RegisterCollectedDatatype();
static void HOOK_RegisterCollectedParam();
static void HOOK_CalledStoreParameters();
static void HOOK_CalledCollectVariablePointer();
static void HOOK_RegisterCollectVariablePointer();
static uint32_t phAfterScriptsOriginal;
static uint32_t phAfterScriptsRet;
extern uint32_t pCheatString;
extern uint32_t pScriptsProcessed;
// Structure to store addresses for each version, used in CommonPatch() function
struct CommonPatchInfo
{
uint32_t pCheatString;
uint32_t pScriptsProcessed;
uint32_t phAfterScripts;
uint32_t phRegisterScript;
uint32_t phRegisterCommand;
uint32_t phCollectParameters;
uint32_t phCollectedDatatype;
uint32_t phCollectedValue;
uint32_t phStoreParameters;
uint32_t phUpdateCompareFlag;
uint32_t CollectiveArray;
uint32_t ScriptsUpdated;
uint32_t ScriptSpace;
uint32_t MissionSpace;
size_t ScriptSpaceSize;
size_t MissionSpaceSize;
};
// Patches based on the sent CommonPatchInfo structure
static void CommonPatch(CommonPatchInfo& c)
{
uint32_t ptr;
c.ScriptSpaceSize = 225512;
c.MissionSpaceSize = 35000;
// Get pointers for scrlog
{
SCRLog::ScriptSpace = ReadMemory<char*>(c.ScriptSpace, true);
SCRLog::MissionSpace = SCRLog::ScriptSpace + c.ScriptSpaceSize;
SCRLog::CollectiveArray = ReadMemory<ScriptVar*>(c.CollectiveArray, true);
SCRLog::ScriptsUpdated = ReadMemory<short*>(c.ScriptsUpdated, true);
SCRLog::ScriptSpaceEnd = SCRLog::ScriptSpace + c.ScriptSpaceSize;
SCRLog::MissionSpaceEnd = SCRLog::MissionSpace + c.MissionSpaceSize;
}
// RegisterScript
if(SCRLog::bHookRegisterScript)
{
ptr = c.phRegisterScript;
MakeCALL(ptr, &HOOK_RegisterScript);
MakeNOP(ptr+5, 1);
}
if (SCRLog::bHookAfterScripts) {
ptr = c.phAfterScripts;
if (ptr) {
//phAfterScriptsRet = ptr + 5;
//phAfterScriptsOriginal = GetAbsoluteOffset(ReadMemory<uintptr_t>(ptr + 1, true), phAfterScriptsRet);
MakeCALL(ptr, &HOOK_AfterScripts);
}
}
pCheatString = c.pCheatString;
pScriptsProcessed = c.pScriptsProcessed;
if (!SCRLog::bHookOnlyRegisterScript)
{
// RegisterCommand
if (SCRLog::bHookRegisterCommand)
{
ptr = c.phRegisterCommand;
CRunningScript__ProcessOneCommand = (void*)GetAbsoluteOffset(ReadMemory<int>(ptr + 1, true), ptr + 5);
MakeCALL(ptr, &HOOK_RegisterCommand);
}
// Called CollectParameters
if (SCRLog::bHookCollectParam)
{
ptr = c.phCollectParameters;
MakeCALL(ptr, &HOOK_CalledCollectParameters);
}
// Register collected datatype
if (SCRLog::bHookFindDatatype)
{
ptr = c.phCollectedDatatype;
MakeCALL(ptr, &HOOK_RegisterCollectedDatatype);
}
// Register collected value
if (SCRLog::bHookCollectParam)
{
ptr = c.phCollectedValue;
MakeCALL(ptr, &HOOK_RegisterCollectedParam);
MakeNOP(ptr + 5, 2);
}
// Register call to store parameters
if (SCRLog::bHookStoreParam)
{
ptr = c.phStoreParameters;
MakeCALL(ptr, &HOOK_CalledStoreParameters);
}
// Replace UpdateCompareFlag
if (SCRLog::bHookUpdateCompareFlag)
{
ptr = c.phUpdateCompareFlag;
MakeJMP(ptr, SCRLog::New_CRunningScript__UpdateCompareFlag);
MakeNOP(ptr + 5, 2);
}
}
}
// Patch all Vice City's versions
// 1.0 and 1.1 have similar addresses (at least in .text segment)
// Steam has a difference of -240 in the addresses compared to retail version
// Japanese has a difference of 880 in the addresses compared to retail version
static void PatchALL(bool bSteam, bool bJapanese)
{
CommonPatchInfo c;
unsigned int off = bSteam? -240 : bJapanese ? 880 : 0;
if (!bSteam && !bJapanese) { // not tested on non-1.0
c.phAfterScripts = 0x44FE27;
c.pCheatString = 0xA10942;
c.pScriptsProcessed = 0xA10988;
}
else {
c.phAfterScripts = 0;
c.pCheatString = 0;
c.pScriptsProcessed = 0;
}
c.phRegisterScript = 0x44FD71 + off;
c.phRegisterCommand = 0x44FDB5 + off;
c.phCollectParameters = 0x451025 + off;
c.phCollectedDatatype = 0x451030 + off;
c.phCollectedValue = 0x4510F0 + off;
c.phStoreParameters = 0x450E57 + off;
c.phUpdateCompareFlag = !bSteam? 0x463F00 + off : 0x463DE0; // this one differ
c.ScriptsUpdated = 0x450239 + off;
c.CollectiveArray = c.phCollectParameters + 1;
c.ScriptSpace = c.phCollectedDatatype + 1;
c.MissionSpace = 0;
CommonPatch(c);
}
void __declspec(naked) HOOK_RegisterScript()
{
_asm
{
mov ebx, ecx // Replaced code
push ecx
call SCRLog::RegisterScript
// Run replaced code and go back to normal flow
mov ecx, ebx
cmp byte ptr [ecx+0x7A], 0
retn
}
}
void __declspec(naked) HOOK_AfterScripts()
{
_asm
{
call SCRLog::AfterScripts
xor al, al
pop ebx
retn
}
}
void __declspec(naked) HOOK_RegisterCommand()
{
_asm
{
call SCRLog::RegisterCommand
// Back
mov ecx, ebx
call CRunningScript__ProcessOneCommand
retn
}
}
void __declspec(naked) HOOK_CalledCollectParameters()
{
_asm
{
push edx
push edi
call SCRLog::RegisterCallToCollectParameters
pop edx
// Run replaced code and go back to normal flow
mov ecx, SCRLog::CollectiveArray
retn
}
}
void __declspec(naked) HOOK_RegisterCollectedDatatype()
{
_asm
{
xor eax, eax
mov ebx, dword ptr [edx] // CRunningScript.ip
add ebx, dword ptr [SCRLog::ScriptSpace]
mov al, byte ptr [ebx]
mov SCRLog::Datatype, eax
// Run replaced code and go back to normal flow
mov eax, SCRLog::ScriptSpace
retn
}
}
void __declspec(naked) HOOK_RegisterCollectedParam()
{
_asm
{
push_regs(ecx, edx)
mov eax, ecx // ecx is current value in iteration over CollectiveArray
sub eax, SCRLog::CollectiveArray
shr eax, 2 // (eax >>= 2) = (eax /= 4)
inc eax // the index starts from 1
push eax
call SCRLog::RegisterCollectionAtIndex
pop_regs(ecx, edx)
// Jump back
add ecx, 4
sub di, 1
retn
}
}
void __declspec(naked) HOOK_CalledStoreParameters()
{
_asm
{
push ecx
push edi
mov SCRLog::Datatype, 1
call SCRLog::RegisterCallToStoreParameters
pop ecx
// Run replaced code and go back to normal flow
xor esi, esi
test di, di
retn
}
}
void VCCHudSetHelpMessage(wchar_t const *message, bool quickMessage, bool permanent) {
((void(__cdecl *)(wchar_t const *, bool, bool))0x55BFC0)(message, quickMessage, permanent);
}