forked from root670/CheatDevicePS2
/
cheats.c
387 lines (324 loc) · 7.54 KB
/
cheats.c
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
#include "cheats.h"
#include "database.h"
#include "menus.h"
#include "graphics.h"
#include <debug.h>
#include <kernel.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <erl.h>
static cheatsGame_t *gamesHead = NULL;
static cheatsGame_t *activeGame = NULL;
static cheatDatabaseType_t dbType;
static int numGames = 0;
static int numCheats = 0;
static int numEnabledCheats = 0;
static int initialized = 0;
extern unsigned char _engine_erl_start[];
int initCheatMan()
{
if(!initialized)
{
printf("\n ** Initializing Cheat Manager **\n");
initialized = 1;
return 1;
}
else
return 0;
}
int killCheatMan()
{
if(initialized)
{
int first = 1;
printf("\n ** Killing Cheat Manager **\n");
dbCloseDatabase();
cheatsGame_t *game = gamesHead;
while(game)
{
cheatsGame_t *next = game->next;
printf("Freeing %s\n", game->title);
if(game->cheats != NULL) {
if(first && dbType == BINARY) // Binay DB has allocates cheats structs and code lines once.
{
free(game->cheats);
free(game->cheats->codeLines);
first = 0;
}
free(game->cheats->codeLines);
free(game->cheats);
}
game = next;
}
free(gamesHead);
}
return 1;
}
// CheatDB --> Game --> Cheat --> Code
int cheatsOpenDatabase(const char* path, cheatDatabaseType_t t)
{
if(initialized)
{
dbType = t;
switch(dbType)
{
case TEXT:
break;
case BINARY:
numGames = dbOpenDatabase(path);
gamesHead = dbGetCheatStruct();
dbCloseDatabase();
break;
default:
break;
}
}
printf("loaded %d games\n", numGames);
return numGames;
}
int cheatsSaveDatabase(const char* path, cheatDatabaseType_t t);
int cheatsLoadGameMenu()
{
if(initialized && gamesHead!=NULL)
{
cheatsGame_t *node = gamesHead;
menuItem_t *items = calloc(numGames, sizeof(menuItem_t));
menuItem_t *item = items;
while(node)
{
item->type = NORMAL;
item->text = calloc(1, strlen(node->title) + 1);
strcpy(item->text, node->title);
item->extra = node;
menuAppendItem(item);
node = node->next;
item++;
}
return 1;
}
return 0;
}
cheatsGame_t* cheatsLoadCheatMenu(const cheatsGame_t* game)
{
if(initialized && gamesHead!=NULL && game)
{
cheatsGame_t *node = gamesHead;
printf("Loading cheat menu for %s\n", game->title);
/* Find the game */
while(node)
{
if(node == game)
{
/* Build the menu */
cheatsCheat_t *cheat = node->cheats;
menuItem_t *items = calloc(node->numCheats, sizeof(menuItem_t));
menuItem_t *item = items;
while(cheat != NULL)
{
if(cheat->type == CHEATNORMAL)
item->type = NORMAL;
else
item->type = HEADER;
item->text = calloc(1, strlen(cheat->title) + 1);
strcpy(item->text, cheat->title);
item->extra = (void *)cheat;
menuAppendItem(item);
cheat = cheat->next;
item++;
}
numCheats = node->numCheats;
return node;
}
node = node->next;
}
}
return NULL;
}
int cheatsLoadCodeMenu(const char* game, const char* cheat);
int cheatsAddGame(const char* title)
{
cheatsGame_t *node = gamesHead;
cheatsGame_t *newGame = calloc(1, sizeof(cheatsGame_t));
if(!newGame)
return 0;
while(node++->next);
node->next = newGame;
strncpy(newGame->title, title, 80);
return 1;
}
int cheatsAddCheat(const char *title, cheatsCheat_t *cheat);
int cheatsToggleCheat(cheatsCheat_t *cheat)
{
if(cheat && cheat->type != CHEATHEADER)
{
if(cheat == activeGame->cheats) // first cheat is the enable code, which will be enabled/disabled automatically.
return 0;
if(!cheat->enabled)
{
cheat->enabled = 1;
numEnabledCheats++;
if(numEnabledCheats == 1) // first cheat was enabled
{
activeGame->cheats->enabled = 1; // toggle enable code on
numEnabledCheats++;
}
}
else
{
cheat->enabled = 0;
numEnabledCheats--;
if(numEnabledCheats == 1) // last cheat was disabled
{
activeGame->cheats->enabled = 0; // toggle enable code off
numEnabledCheats--;
}
}
}
else
return 0;
return 1;
}
void cheatsDrawStats()
{
if(activeGame && activeGame->title)
{
if(numEnabledCheats > 0)
{
char active_cheats[32];
if(numEnabledCheats == 1)
snprintf(active_cheats, 32, "%i active cheat", numEnabledCheats);
else
snprintf(active_cheats, 32, "%i active cheats", numEnabledCheats);
graphicsDrawText(482, 25, active_cheats, WHITE);
}
}
static menuID_t activeMenu = -1;
static int x = 0;
menuID_t oldMenu = activeMenu;
activeMenu = menuGetActive();
if(activeMenu != oldMenu)
x = 0;
if(activeMenu == GAMEMENU)
{
if (x < 3000)
x+= 2;
else
x = 0;
char msg[150];
snprintf(msg, 150, "%d games available. "
"Press X to view a game's cheats. "
"Press CIRCLE for main menu. "
"Press SELECT to view the credits screen."
, numGames);
graphicsDrawText(640 - x, 405, msg, WHITE);
}
if(activeMenu == CHEATMENU)
{
if (x < 1700)
x+= 2;
else
x = 0;
char msg[150];
snprintf(msg, 150, "%d cheats available. "
"Press X to enable or disable a cheat. "
"Press CIRCLE to return to the game list."
, numCheats);
graphicsDrawText(640 - x, 405, msg, WHITE);
}
}
int cheatsIsActiveGame(const cheatsGame_t *game)
{
return game == activeGame;
}
int cheatsSetActiveGame(cheatsGame_t *game)
{
/* Disable all active cheats if a new game was selected */
if(activeGame != NULL && game != activeGame)
{
cheatsCheat_t *cheat = activeGame->cheats;
while(cheat)
{
cheat->enabled = 0;
cheat = cheat->next;
}
numEnabledCheats = 0;
}
if(game)
{
activeGame = game;
}
return 1;
}
void SetupERL()
{
struct erl_record_t *erl;
erl_add_global_symbol("GetSyscallHandler", (u32)GetSyscallHandler);
erl_add_global_symbol("SetSyscall", (u32)SetSyscall);
/* Install cheat engine ERL */
erl = load_erl_from_mem_to_addr(_engine_erl_start, 0x00080000, 0, NULL);
if(!erl)
{
printf("Error loading cheat engine ERL!\n");
SleepThread();
}
erl->flags |= ERL_FLAG_CLEAR;
FlushCache(0);
printf("Installed cheat engine ERL. Getting symbols...\n");
struct symbol_t *sym;
#define GET_SYMBOL(var, name) \
sym = erl_find_local_symbol(name, erl); \
if (sym == NULL) { \
printf("%s: could not find symbol '%s'\n", __FUNCTION__, name); \
SleepThread(); \
} \
printf("%08x %s\n", (u32)sym->address, name); \
var = (typeof(var))sym->address
GET_SYMBOL(get_max_hooks, "get_max_hooks");
GET_SYMBOL(get_num_hooks, "get_num_hooks");
GET_SYMBOL(add_hook, "add_hook");
GET_SYMBOL(clear_hooks, "clear_hooks");
GET_SYMBOL(get_max_codes, "get_max_codes");
GET_SYMBOL(set_max_codes, "set_max_codes");
GET_SYMBOL(get_num_codes, "get_num_codes");
GET_SYMBOL(add_code, "add_code");
GET_SYMBOL(clear_codes, "clear_codes");
printf("Symbols loaded.\n");
}
void cheatsInstallCodesForEngine()
{
if(activeGame != NULL)
{
int i;
u32 addr, val;
int nextCodeCanBeHook = 1;
SetupERL();
cheatsCheat_t *cheat = activeGame->cheats;
while(cheat)
{
if(cheat->enabled)
{
for(i = 0; i < cheat->numCodeLines; ++i)
{
addr = (u32)*((u32 *)cheat->codeLines + 2*i);
val = (u32)*((u32 *)cheat->codeLines + 2*i + 1);
if(((addr & 0xfe000000) == 0x90000000) && nextCodeCanBeHook == 1)
{
printf("hook: %08X %08X\n", addr, val);
add_hook(addr, val);
}
else
{
printf("code: %08X %08X\n", addr, val);
add_code(addr, val);
}
if ((addr & 0xf0000000) == 0x40000000 || (addr & 0xf0000000) == 0x30000000)
nextCodeCanBeHook = 0;
else
nextCodeCanBeHook = 1;
}
}
cheat = cheat->next;
}
}
}