mirror of
https://github.com/libretro/RetroArch
synced 2025-02-28 12:40:23 +00:00
Remove bsnes XML cheat code - rework this into something more generic
This commit is contained in:
parent
199cbda252
commit
46f8713165
289
cheats.c
289
cheats.c
@ -27,100 +27,6 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.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
|
|
||||||
|
|
||||||
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 item_cheat *cht, xmlNodePtr ptr)
|
|
||||||
{
|
|
||||||
bool first;
|
|
||||||
if (!ptr)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
memset(cht, 0, sizeof(struct item_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)
|
|
||||||
{
|
|
||||||
if (!cheat_manager_realloc(handle, handle->buf_size * 2))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (xml_grab_cheat(&handle->cheats[handle->size], ptr->children))
|
|
||||||
handle->size++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void cheat_manager_apply_cheats(cheat_manager_t *handle)
|
void cheat_manager_apply_cheats(cheat_manager_t *handle)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
@ -134,85 +40,6 @@ void cheat_manager_apply_cheats(cheat_manager_t *handle)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 idx = strtoul(num, NULL, 0);
|
|
||||||
if (idx < handle->size)
|
|
||||||
handle->cheats[idx].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(void)
|
cheat_manager_t *cheat_manager_new(void)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
@ -274,118 +101,6 @@ bool cheat_manager_realloc(cheat_manager_t *handle, unsigned new_size)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
cheat_manager_t *cheat_manager_new_from_xml(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 item_cheat*)
|
|
||||||
calloc(handle->buf_size, sizeof(struct item_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)
|
void cheat_manager_free(cheat_manager_t *handle)
|
||||||
{
|
{
|
||||||
unsigned i;
|
unsigned i;
|
||||||
@ -394,11 +109,9 @@ void cheat_manager_free(cheat_manager_t *handle)
|
|||||||
|
|
||||||
if (handle->cheats)
|
if (handle->cheats)
|
||||||
{
|
{
|
||||||
cheat_manager_save_config(handle,
|
|
||||||
g_settings.cheat_settings_path, g_extern.sha256);
|
|
||||||
for (i = 0; i < handle->size; i++)
|
for (i = 0; i < handle->size; i++)
|
||||||
{
|
{
|
||||||
xmlFree(handle->cheats[i].desc);
|
free(handle->cheats[i].desc);
|
||||||
free(handle->cheats[i].code);
|
free(handle->cheats[i].code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1116,11 +1116,6 @@ static void init_cheats(void)
|
|||||||
|
|
||||||
if (!allow_cheats)
|
if (!allow_cheats)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#if 0
|
|
||||||
if (*g_settings.cheat_database)
|
|
||||||
g_extern.cheat = cheat_manager_new_from_xml(g_settings.cheat_database);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_rewind(void)
|
static void init_rewind(void)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user