-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
cheats.c
264 lines (241 loc) · 8.27 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
/**
*
* Gecko OS/WiiRD cheat engine (kenobigc)
*
* Adapted to Swiss by emu_kidid 2012-2015
*
*/
#include <stdio.h>
#include <gccore.h> /*** Wrapper to include common libogc headers ***/
#include <ogcsys.h> /*** Needed for console support ***/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <malloc.h>
#include <ctype.h>
#include "swiss.h"
#include "main.h"
#include "cheats.h"
#include "patcher.h"
#include "deviceHandler.h"
#include "FrameBufferMagic.h"
static CheatEntries _cheats;
void printCheats(void) {
int i = 0, j = 0;
print_gecko("There are %i cheats\r\n", _cheats.num_cheats);
for(i = 0; i < _cheats.num_cheats; i++) {
CheatEntry *cheat = &_cheats.cheat[i];
print_gecko("Cheat: (%i codes) %s\r\n", cheat->num_codes, cheat->name);
for(j = 0; j < cheat->num_codes; j++) {
print_gecko("%08X %08X\r\n", cheat->codes[j][0], cheat->codes[j][1]);
}
}
}
int getEnabledCheatsSize(void) {
int i = 0, size = 0;
for(i = 0; i < _cheats.num_cheats; i++) {
CheatEntry *cheat = &_cheats.cheat[i];
if(cheat->enabled) {
size += ((cheat->num_codes*2)*4);
}
}
//print_gecko("Size of cheats: %i\r\n", size);
return size;
}
static char *validCodeValues = "0123456789AaBbCcDdEeFf";
// Checks that the line contains a valid code in the format of "01234567 89ABCDEF"
int isValidCode(char *code) {
int i, j;
if(strlen(code) < 17) return 0; // Not in format
for(i = 0; i < 16; i++) {
if(i == 8 && code[i] != ' ')
return 0; // No space separating the two values
if(i == 8)
continue;
int found = 0;
for(j = 0; j < strlen(validCodeValues); j++) {
if(code[i] == validCodeValues[j]) {
found = 1;
break;
}
}
if(!found) return 0; // Wasn't a valid hexadecimal value
}
return 1;
}
int containsXX(char *line) {
return (strlen(line)>=16 && (tolower((int)line[15]) == 'x'||tolower((int)line[15]) == 'y'));
}
/**
Given a char array with the contents of a .txt,
this method will allocate and return a populated Parameters struct
*/
void parseCheats(char *filecontents) {
char *line = NULL, *prevLine = NULL, *linectx = NULL;
int numCheats = 0;
line = strtok_r( filecontents, "\n", &linectx );
memset(&_cheats, 0, sizeof(CheatEntries));
CheatEntry *curCheat = NULL; // The current one we're parsing
while( line != NULL ) {
//print_gecko("Line [%s]\r\n", line);
if(isValidCode(line) && (prevLine != NULL && !containsXX(prevLine))) { // The line looks like a valid code
if(prevLine != NULL) {
curCheat = &_cheats.cheat[numCheats];
strncpy(curCheat->name, prevLine, strlen(prevLine) > CHEATS_NAME_LEN ? CHEATS_NAME_LEN-1:strlen(prevLine));
if(curCheat->name[strlen(curCheat->name)-1] == '\r')
curCheat->name[strlen(curCheat->name)-1] = 0;
//print_gecko("Cheat Name: [%s]\r\n", prevLine);
}
int numCodes = 0, unsupported = 0;
// Add this valid code as the first code for this cheat
curCheat->codes[numCodes][0] = (u32)strtoul(line, NULL, 16);
curCheat->codes[numCodes][1] = (u32)strtoul(line+8, NULL, 16);
numCodes++;
line = strtok_r( NULL, "\n", &linectx);
// Keep going until we're out of codes for this cheat
while( line != NULL ) {
// If a code contains "XX" in it, it is unsupported, discard it entirely
if(containsXX(line)) {
unsupported = 1;
break;
}
if(isValidCode(line)) {
// Add this valid code
curCheat->codes[numCodes][0] = (u32)strtoul(line, NULL, 16);
curCheat->codes[numCodes][1] = (u32)strtoul(line+8, NULL, 16);
numCodes++;
}
else {
break;
}
line = strtok_r( NULL, "\n", &linectx);
}
curCheat->num_codes = numCodes;
if(unsupported) {
memset(curCheat, 0, sizeof(CheatEntry));
while(line != NULL && strlen(line) > 0) {
line = strtok_r( NULL, "\n", &linectx); // finish this unsupported cheat.
}
}
else
numCheats++;
}
prevLine = line;
// And round we go again
line = strtok_r( NULL, "\n", &linectx);
}
_cheats.num_cheats = numCheats;
//printCheats();
}
CheatEntries* getCheats() {
return &_cheats;
}
// Installs the GeckoOS (kenobiGC) cheats engine and sets up variables/copies cheats
void kenobi_install_engine() {
int isDebug = swissSettings.wiirdDebug;
// If high memory is in use, we'll use low, otherwise high.
u8 *ptr = isDebug ? kenobigc_dbg_bin : kenobigc_bin;
u32 size = isDebug ? kenobigc_dbg_bin_size : kenobigc_bin_size;
print_gecko("Copying kenobi%s to %08X\r\n", (isDebug?"_dbg":""),(u32)CHEATS_ENGINE);
memcpy(CHEATS_ENGINE, ptr, size);
memcpy(CHEATS_GAMEID, (void*)0x80000000, CHEATS_GAMEID_LEN);
if(!isDebug) {
CHEATS_ENABLE_CHEATS = CHEATS_TRUE;
}
CHEATS_START_PAUSED = isDebug ? CHEATS_TRUE : CHEATS_FALSE;
memset(CHEATS_LOCATION(size), 0, kenobi_get_maxsize());
print_gecko("Copying %i bytes of cheats to %08X\r\n", getEnabledCheatsSize(),(u32)CHEATS_LOCATION(size));
u32 *cheatsLocation = (u32*)CHEATS_LOCATION(size);
cheatsLocation[0] = 0x00D0C0DE;
cheatsLocation[1] = 0x00D0C0DE;
cheatsLocation+=2;
int i = 0, j = 0;
for(i = 0; i < _cheats.num_cheats; i++) {
CheatEntry *cheat = &_cheats.cheat[i];
if(cheat->enabled) {
for(j = 0; j < cheat->num_codes; j++) {
// Copy & fix cheats that want to jump to the old cheat engine location 0x800018A8 -> CHEATS_ENGINE+0xA8
cheatsLocation[0] = cheat->codes[j][0];
cheatsLocation[1] = cheat->codes[j][1] == 0x800018A8 ? (u32)(CHEATS_ENGINE+0xA8) : cheat->codes[j][1];
cheatsLocation+=2;
}
}
}
cheatsLocation[0] = 0xFF000000;
DCFlushRange((void*)CHEATS_ENGINE, WIIRD_ENGINE_SPACE);
ICInvalidateRange((void*)CHEATS_ENGINE, WIIRD_ENGINE_SPACE);
}
int kenobi_get_maxsize() {
return CHEATS_MAX_SIZE((swissSettings.wiirdDebug ? kenobigc_dbg_bin_size : kenobigc_bin_size));
}
int findCheats(bool silent) {
char trimmedGameId[8];
memset(trimmedGameId, 0, 8);
memcpy(trimmedGameId, (char*)&GCMDisk, 6);
file_handle *cheatsFile = memalign(32,sizeof(file_handle));
memcpy(cheatsFile, devices[DEVICE_CUR]->initial, sizeof(file_handle));
sprintf(cheatsFile->name, "%s/cheats/%s.txt", devices[DEVICE_CUR]->initial->name, trimmedGameId);
print_gecko("Looking for cheats file @ %s\r\n", cheatsFile->name);
cheatsFile->size = -1;
devices[DEVICE_TEMP] = devices[DEVICE_CUR];
// Check SD in both slots if we're not already running from SD, or if we fail from current device
if(devices[DEVICE_TEMP]->readFile(cheatsFile, &trimmedGameId, 8) != 8) {
// Try SD slots now
devices[DEVICE_TEMP] = &__device_sd_a;
file_handle *slotFile = devices[DEVICE_TEMP]->initial;
memcpy(cheatsFile, slotFile, sizeof(file_handle));
sprintf(cheatsFile->name, "%s/cheats/%s.txt", slotFile->name, trimmedGameId);
print_gecko("Looking for cheats file @ %s\r\n", cheatsFile->name);
cheatsFile->size = -1;
devices[DEVICE_TEMP]->init(cheatsFile);
if(devices[DEVICE_TEMP]->readFile(cheatsFile, &trimmedGameId, 8) != 8) {
devices[DEVICE_TEMP] = NULL;
}
devices[DEVICE_TEMP] = &__device_sd_b;
slotFile = devices[DEVICE_TEMP]->initial;
memcpy(cheatsFile, slotFile, sizeof(file_handle));
sprintf(cheatsFile->name, "%s/cheats/%s.txt", slotFile->name, trimmedGameId);
print_gecko("Looking for cheats file @ %s\r\n", cheatsFile->name);
cheatsFile->size = -1;
devices[DEVICE_TEMP]->init(cheatsFile);
if(devices[DEVICE_TEMP]->readFile(cheatsFile, &trimmedGameId, 8) != 8) {
devices[DEVICE_TEMP] = NULL;
}
// Still fail?
if(devices[DEVICE_TEMP] == NULL) {
if(!silent) {
while(PAD_ButtonsHeld(0) & PAD_BUTTON_Y);
DrawFrameStart();
DrawMessageBox(D_INFO,"No cheats file found.\nPress A to continue.");
DrawFrameFinish();
while(!(PAD_ButtonsHeld(0) & PAD_BUTTON_A));
while(PAD_ButtonsHeld(0) & PAD_BUTTON_A);
}
free(cheatsFile);
return 0;
}
}
print_gecko("Cheats file found with size %i\r\n", cheatsFile->size);
char *cheats_buffer = memalign(32, cheatsFile->size);
if(cheats_buffer) {
devices[DEVICE_TEMP]->seekFile(cheatsFile, 0, DEVICE_HANDLER_SEEK_SET);
devices[DEVICE_TEMP]->readFile(cheatsFile, cheats_buffer, cheatsFile->size);
parseCheats(cheats_buffer);
free(cheats_buffer);
free(cheatsFile);
}
return _cheats.num_cheats;
}
int applyAllCheats() {
int i = 0, j = 0;
for(i = 0; i < _cheats.num_cheats; i++) {
CheatEntry *cheat = &_cheats.cheat[i];
cheat->enabled = 1;
if(getEnabledCheatsSize() > kenobi_get_maxsize())
cheat->enabled = 0;
j++;
}
return j;
}