Repository navigation
Expand file tree
/
Copy pathpatch_engine.c
More file actions
330 lines (310 loc) · 12.9 KB
/
Copy pathpatch_engine.c
File metadata and controls
330 lines (310 loc) · 12.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
#include "patch_engine.h"
#include "features.h"
#include "features/autodrive_notice.h"
#include "patches_generated.h"
#include "support.h"
#define CALLBACK_PATCH_COUNT 18
#define ALL_PATCH_COUNT (FIXED_PATCH_COUNT + CALLBACK_PATCH_COUNT)
#define MAX_PATCH_PAGES 64
typedef struct {
unsigned char *game_image;
const ModOptions *options;
SIZE_T page_size;
uintptr_t pages[MAX_PATCH_PAGES];
DWORD original_protection[MAX_PATCH_PAGES];
unsigned page_count;
unsigned writable_page_count;
unsigned changed_count;
} PatchTransaction;
static unsigned char original_bytes[ALL_PATCH_COUNT][MAX_PATCH_SIZE];
static const PatchDefinition *const callback_patches[CALLBACK_PATCH_COUNT] = {
&garage_hook_patch, &language_hook_patch, &road_hook_patch,
&gamerpic_hook_patch, &treasure_map_hook_patch, &autodrive_hook_patches[0],
&autodrive_hook_patches[1], &autodrive_hook_patches[2], &autodrive_hook_patches[3],
&autodrive_hook_patches[4], &autodrive_hook_patches[5], &autodrive_hook_patches[6],
&autodrive_notice_hook_patch, &autodrive_hook_patches[7], &autodrive_hook_patches[8],
&autodrive_hook_patches[9], &autodrive_hook_patches[10], &autodrive_hook_patches[11]};
#ifdef FH2_TWEAKS_TEST
static int protect_failure_at = -1;
static int protect_call_count;
#endif
static const PatchDefinition *patch_at(unsigned index) {
if (index < FIXED_PATCH_COUNT) {
return &fixed_patches[index];
}
return callback_patches[index - FIXED_PATCH_COUNT];
}
static const unsigned char *patch_target(const PatchDefinition *patch, const ModOptions *options) {
int enabled = 0;
switch (patch->group) {
case GROUP_SUN_SHADOWS:
enabled = options->smooth_sun_shadows;
break;
case GROUP_AUTODRIVE:
enabled = options->autodrive;
break;
case GROUP_TREASURE_MAP:
enabled = options->unlock_treasure_map;
break;
case GROUP_PHOTO_CAMERA:
enabled = options->free_photo_camera;
break;
case GROUP_GAMERPIC:
enabled = options->custom_gamerpic;
break;
case GROUP_PARENTAL:
enabled = options->disable_parental_locks;
break;
case GROUP_ISLAND_CARS:
enabled = options->unlock_storm_island_cars;
break;
case GROUP_ROAD:
enabled = options->garage_from_pause && options->snap_car_to_road;
break;
case GROUP_TEXT_RELOAD:
enabled = options->text_language >= 0;
break;
case GROUP_TEXT_INIT:
return options->text_language < 0 ? patch->original
: language_text_init_targets[options->text_language];
case GROUP_TEXT_LOCK:
enabled = options->text_language >= 0;
break;
case GROUP_AUDIO_LANGUAGE:
return options->audio_language < 0
? patch->original
: language_audio_init_targets[options->audio_language];
case GROUP_DLC:
enabled = options->unlock_dlc;
break;
case GROUP_VIP:
enabled = options->offline_vip;
break;
case GROUP_INTRO:
enabled = options->skip_intro;
break;
case GROUP_MARKETPLACE:
enabled = options->enable_marketplace;
break;
case GROUP_LIVERY:
enabled = options->enable_4k_liveries;
break;
case GROUP_MSAA:
enabled = options->enable_4k_liveries && options->livery_msaa == 4;
break;
case GROUP_PAUSE_GARAGE:
enabled = options->garage_from_pause;
break;
}
if (!enabled) {
return patch->original;
}
return patch->group == GROUP_LIVERY && options->livery_supersampling ? patch->supersampled
: patch->standard;
}
static int supported_image(const unsigned char *game_image) {
const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER *)game_image;
if (!game_image || dos->e_magic != IMAGE_DOS_SIGNATURE || dos->e_lfanew < 64 ||
dos->e_lfanew > 4096) {
return 0;
}
const IMAGE_NT_HEADERS64 *headers = (const IMAGE_NT_HEADERS64 *)(game_image + dos->e_lfanew);
return headers->Signature == IMAGE_NT_SIGNATURE &&
headers->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64 &&
headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC &&
headers->FileHeader.TimeDateStamp == GAME_TIMESTAMP &&
headers->OptionalHeader.SizeOfImage == GAME_IMAGE_SIZE;
}
static int validate_patch_sites(PatchTransaction *transaction) {
for (unsigned index = 0; index < ALL_PATCH_COUNT; ++index) {
const PatchDefinition *patch = patch_at(index);
const unsigned char *loaded_bytes = transaction->game_image + patch->rva;
int recognized = 0;
for (unsigned variant = 0; variant < patch->accepted_count; ++variant) {
if (memory_equal(loaded_bytes, patch->accepted[variant], patch->size)) {
recognized = 1;
break;
}
}
if (!recognized) {
status_set("REFUSED: unexpected loaded bytes at ");
status_append(patch->name);
status_append(". No patches applied.");
return 0;
}
memory_copy(original_bytes[index], loaded_bytes, patch->size);
if (!memory_equal(loaded_bytes, patch_target(patch, transaction->options), patch->size)) {
++transaction->changed_count;
}
}
return 1;
}
static int pin_callback_module(const ModOptions *options) {
if (!options->garage_from_pause && options->text_language < 0 && !options->custom_gamerpic &&
!options->unlock_treasure_map && !options->autodrive) {
return 1;
}
HMODULE pinned_module;
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCWSTR)(uintptr_t)garage_callback_address(), &pinned_module)) {
return 1;
}
status_set("REFUSED: cannot keep the callback DLL resident. No patches applied.");
return 0;
}
static int collect_patch_pages(PatchTransaction *transaction) {
for (unsigned index = 0; index < ALL_PATCH_COUNT; ++index) {
const PatchDefinition *patch = patch_at(index);
unsigned char *address = transaction->game_image + patch->rva;
if (memory_equal(address, patch_target(patch, transaction->options), patch->size)) {
continue;
}
uintptr_t first_page = (uintptr_t)address / transaction->page_size * transaction->page_size;
uintptr_t last_page = (uintptr_t)(address + patch->size - 1) / transaction->page_size *
transaction->page_size;
for (uintptr_t page = first_page; page <= last_page; page += transaction->page_size) {
unsigned existing = 0;
while (existing < transaction->page_count && transaction->pages[existing] != page) {
++existing;
}
if (existing != transaction->page_count) {
continue;
}
if (transaction->page_count == MAX_PATCH_PAGES) {
status_set("REFUSED: too many patch pages.");
return 0;
}
transaction->pages[transaction->page_count++] = page;
}
}
return 1;
}
static BOOL make_writable(void *address, SIZE_T size, DWORD *previous_protection) {
#ifdef FH2_TWEAKS_TEST
if (protect_call_count++ == protect_failure_at) {
SetLastError(ERROR_ACCESS_DENIED);
return FALSE;
}
#endif
return VirtualProtect(address, size, PAGE_EXECUTE_READWRITE, previous_protection);
}
static int restore_page_protection(const PatchTransaction *transaction) {
int restored = 1;
for (unsigned index = 0; index < transaction->writable_page_count; ++index) {
DWORD ignored;
if (!VirtualProtect((void *)transaction->pages[index], transaction->page_size,
transaction->original_protection[index], &ignored)) {
restored = 0;
}
}
return restored;
}
static int make_patch_pages_writable(PatchTransaction *transaction) {
while (transaction->writable_page_count < transaction->page_count) {
unsigned index = transaction->writable_page_count;
if (!make_writable((void *)transaction->pages[index], transaction->page_size,
&transaction->original_protection[index])) {
restore_page_protection(transaction);
status_set("REFUSED: cannot make every patch page writable. No bytes changed.");
return 0;
}
++transaction->writable_page_count;
}
return 1;
}
static void write_patch_sites(const PatchTransaction *transaction, int rollback) {
for (unsigned index = 0; index < ALL_PATCH_COUNT; ++index) {
const PatchDefinition *patch = patch_at(index);
const unsigned char *target = patch_target(patch, transaction->options);
if (memory_equal(original_bytes[index], target, patch->size)) {
continue;
}
const unsigned char *bytes = rollback ? original_bytes[index] : target;
unsigned char *address = transaction->game_image + patch->rva;
if (index >= FIXED_PATCH_COUNT) {
uintptr_t callback;
memory_copy(&callback, bytes, sizeof(callback));
*(volatile uintptr_t *)address = callback;
} else {
memory_copy(address, bytes, patch->size);
}
}
}
static int commit_transaction(const PatchTransaction *transaction) {
write_patch_sites(transaction, 0);
language_write_changes(0);
int flushed =
FlushInstructionCache(GetCurrentProcess(), transaction->game_image, GAME_IMAGE_SIZE) != 0;
if (flushed) {
language_commit_options(transaction->options);
status_set("OK: requested tweaks applied in memory. The EXE file was not changed.");
} else {
language_write_changes(1);
write_patch_sites(transaction, 1);
FlushInstructionCache(GetCurrentProcess(), transaction->game_image, GAME_IMAGE_SIZE);
status_set("REFUSED: instruction-cache flush failed. Patch bytes rolled back.");
}
if (!restore_page_protection(transaction)) {
status_append(" WARNING: page protection restoration failed.");
return 0;
}
return flushed;
}
int patch_engine_apply(unsigned char *game_image, const ModOptions *options) {
if (!supported_image(game_image)) {
status_set("REFUSED: unsupported loaded executable headers. No patches applied.");
return 0;
}
if (!options->enabled) {
status_set("DISABLED: no memory changes made. Existing EXE patches remain active.");
return 1;
}
if (options->text_language < LANGUAGE_AUTO || options->text_language >= LANGUAGE_COUNT ||
options->text_language == 1 ||
(options->audio_language != LANGUAGE_AUTO &&
!language_supports_audio(options->audio_language))) {
status_set("REFUSED: invalid language selection. No patches applied.");
return 0;
}
garage_prepare_hook(game_image);
language_prepare_hook(game_image, options);
road_prepare_hook(game_image);
gamerpic_prepare_hook(game_image);
treasure_map_prepare_hook(game_image);
autodrive_prepare_hooks(game_image, options);
PatchTransaction transaction;
transaction.game_image = game_image;
transaction.options = options;
transaction.page_count = 0;
transaction.writable_page_count = 0;
transaction.changed_count = 0;
if (!validate_patch_sites(&transaction) ||
!language_prepare_existing_objects(game_image, options)) {
return 0;
}
transaction.changed_count += language_count_changes();
if (!pin_callback_module(options)) {
return 0;
}
if (!transaction.changed_count) {
language_commit_options(options);
status_set("OK: all requested tweaks already match the loaded executable.");
return 1;
}
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
transaction.page_size = system_info.dwPageSize;
if (!collect_patch_pages(&transaction) || !make_patch_pages_writable(&transaction)) {
return 0;
}
return commit_transaction(&transaction);
}
#ifdef FH2_TWEAKS_TEST
__declspec(dllexport) int Fh2TestApply(void *base, const ModOptions *options) {
return patch_engine_apply(base, options);
}
__declspec(dllexport) void Fh2TestProtectFailure(int index) {
protect_failure_at = index;
protect_call_count = 0;
}
#endif