forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cheats.c
403 lines (332 loc) · 8.76 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "cheats.h"
#include "hash.h"
#include "dynamic.h"
#include "general.h"
#include "compat/strl.h"
#include "compat/posix_string.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "conf/config_file.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#ifdef HAVE_LIBXML2
#include <libxml/parser.h>
#include <libxml/tree.h>
#else
#define RXML_LIBXML2_COMPAT
#include "compat/rxml/rxml.h"
#endif
struct cheat
{
char *desc;
bool state;
char *code;
};
struct cheat_manager
{
struct cheat *cheats;
unsigned ptr;
unsigned size;
unsigned buf_size;
};
static char *strcat_alloc(char *dest, const char *input)
{
size_t dest_len = dest ? strlen(dest) : 0;
size_t input_len = strlen(input);
size_t required_len = dest_len + input_len + 1;
char *output = (char*)realloc(dest, required_len);
if (!output)
return NULL;
if (dest)
strlcat(output, input, required_len);
else
strlcpy(output, input, required_len);
return output;
}
static bool xml_grab_cheat(struct cheat *cht, xmlNodePtr ptr)
{
bool first;
if (!ptr)
return false;
memset(cht, 0, sizeof(struct cheat));
first = true;
for (; ptr; ptr = ptr->next)
{
if (strcmp((const char*)ptr->name, "description") == 0)
cht->desc = (char*)xmlNodeGetContent(ptr);
else if (strcmp((const char*)ptr->name, "code") == 0)
{
xmlChar *code;
if (!first)
{
cht->code = strcat_alloc(cht->code, "+");
if (!cht->code)
return false;
}
code = (xmlChar*)xmlNodeGetContent(ptr);
if (!code)
return false;
cht->code = strcat_alloc(cht->code, (const char*)code);
xmlFree(code);
if (!cht->code)
return false;
first = false;
}
}
return true;
}
static bool xml_grab_cheats(cheat_manager_t *handle, xmlNodePtr ptr)
{
for (; ptr; ptr = ptr->next)
{
if (strcmp((const char*)ptr->name, "name") == 0)
{
xmlChar *name = (xmlChar*)xmlNodeGetContent(ptr);
if (name)
{
RARCH_LOG("Found cheat for game: \"%s\"\n", name);
xmlFree(name);
}
}
else if (strcmp((const char*)ptr->name, "cheat") == 0)
{
if (handle->size == handle->buf_size)
{
handle->buf_size *= 2;
handle->cheats = (struct cheat*)
realloc(handle->cheats, handle->buf_size * sizeof(struct cheat));
if (!handle->cheats)
return false;
}
if (xml_grab_cheat(&handle->cheats[handle->size], ptr->children))
handle->size++;
}
}
return true;
}
static void cheat_manager_apply_cheats(cheat_manager_t *handle)
{
unsigned i, index;
index = 0;
pretro_cheat_reset();
for (i = 0; i < handle->size; i++)
{
if (handle->cheats[i].state)
pretro_cheat_set(index++, true, handle->cheats[i].code);
}
}
static void cheat_manager_load_config(cheat_manager_t *handle,
const char *path, const char *sha256)
{
const char *num;
char *str, *save;
config_file_t *conf;
if (!(*path))
return;
conf = (config_file_t*)config_file_new(path);
if (!conf)
return;
str = NULL;
if (!config_get_string(conf, sha256, &str))
{
config_file_free(conf);
return;
}
save = NULL;
num = (const char*)strtok_r(str, ";", &save);
while (num)
{
unsigned index = strtoul(num, NULL, 0);
if (index < handle->size)
handle->cheats[index].state = true;
num = strtok_r(NULL, ";", &save);
}
free(str);
config_file_free(conf);
cheat_manager_apply_cheats(handle);
}
static void cheat_manager_save_config(cheat_manager_t *handle,
const char *path, const char *sha256)
{
unsigned i;
char conf_str[512] = {0};
char tmp[32] = {0};
if (!(*path))
return;
config_file_t *conf = config_file_new(path);
if (!conf)
conf = config_file_new(NULL);
if (!conf)
{
RARCH_ERR("Cannot save XML cheat settings.\n");
return;
}
for (i = 0; i < handle->size; i++)
{
if (handle->cheats[i].state)
{
snprintf(tmp, sizeof(tmp), "%u;", i);
strlcat(conf_str, tmp, sizeof(conf_str));
}
}
if (*conf_str)
conf_str[strlen(conf_str) - 1] = '\0'; /* Remove the trailing ';' */
config_set_string(conf, sha256, conf_str);
if (!config_file_write(conf, path))
RARCH_ERR("Failed to write XML cheat settings to \"%s\". Check permissions.\n", path);
config_file_free(conf);
}
cheat_manager_t *cheat_manager_new(const char *path)
{
xmlParserCtxtPtr ctx;
xmlNodePtr head, cur;
xmlDocPtr doc;
cheat_manager_t *handle;
LIBXML_TEST_VERSION;
pretro_cheat_reset();
ctx = NULL;
doc = NULL;
handle = (cheat_manager_t*)calloc(1, sizeof(struct cheat_manager));
if (!handle)
return NULL;
head = NULL;
cur = NULL;
handle->buf_size = 1;
handle->cheats = (struct cheat*)
calloc(handle->buf_size, sizeof(struct cheat));
if (!handle->cheats)
{
handle->buf_size = 0;
goto error;
}
ctx = xmlNewParserCtxt();
if (!ctx)
goto error;
doc = xmlCtxtReadFile(ctx, path, NULL, 0);
if (!doc)
{
RARCH_ERR("Failed to parse XML file: %s\n", path);
goto error;
}
#ifdef HAVE_LIBXML2
if (ctx->valid == 0)
{
RARCH_ERR("Cannot validate XML file: %s\n", path);
goto error;
}
#endif
head = xmlDocGetRootElement(doc);
for (cur = head; cur; cur = cur->next)
{
if (cur->type == XML_ELEMENT_NODE
&& strcmp((const char*)cur->name, "database") == 0)
break;
}
if (!cur)
goto error;
for (cur = cur->children; cur; cur = cur->next)
{
if (cur->type != XML_ELEMENT_NODE)
continue;
if (strcmp((const char*)cur->name, "cartridge") == 0)
{
xmlChar *sha256 = xmlGetProp(cur, (const xmlChar*)"sha256");
if (!sha256)
continue;
if (*g_extern.sha256 && strcmp((const char*)sha256,
g_extern.sha256) == 0)
{
xmlFree(sha256);
break;
}
xmlFree(sha256);
}
}
if (!cur)
goto error;
if (!xml_grab_cheats(handle, cur->children))
{
RARCH_ERR("Failed to grab cheats. This should not happen.\n");
goto error;
}
if (handle->size == 0)
{
RARCH_ERR("Did not find any cheats in XML file: %s\n", path);
goto error;
}
cheat_manager_load_config(handle,
g_settings.cheat_settings_path, g_extern.sha256);
xmlFreeDoc(doc);
xmlFreeParserCtxt(ctx);
return handle;
error:
cheat_manager_free(handle);
if (doc)
xmlFreeDoc(doc);
if (ctx)
xmlFreeParserCtxt(ctx);
return NULL;
}
void cheat_manager_free(cheat_manager_t *handle)
{
unsigned i;
if (!handle)
return;
if (handle->cheats)
{
cheat_manager_save_config(handle,
g_settings.cheat_settings_path, g_extern.sha256);
for (i = 0; i < handle->size; i++)
{
xmlFree(handle->cheats[i].desc);
free(handle->cheats[i].code);
}
free(handle->cheats);
}
free(handle);
}
static void cheat_manager_update(cheat_manager_t *handle)
{
msg_queue_clear(g_extern.msg_queue);
char msg[256];
snprintf(msg, sizeof(msg), "Cheat: #%u [%s]: %s",
handle->ptr, handle->cheats[handle->ptr].state ? "ON" : "OFF",
handle->cheats[handle->ptr].desc);
msg_queue_push(g_extern.msg_queue, msg, 1, 180);
RARCH_LOG("%s\n", msg);
}
void cheat_manager_toggle(cheat_manager_t *handle)
{
handle->cheats[handle->ptr].state ^= true;
cheat_manager_apply_cheats(handle);
cheat_manager_update(handle);
}
void cheat_manager_index_next(cheat_manager_t *handle)
{
handle->ptr = (handle->ptr + 1) % handle->size;
cheat_manager_update(handle);
}
void cheat_manager_index_prev(cheat_manager_t *handle)
{
if (handle->ptr == 0)
handle->ptr = handle->size - 1;
else
handle->ptr--;
cheat_manager_update(handle);
}