Start adding cheat_manager_load function

This commit is contained in:
twinaphex 2014-12-15 22:40:23 +01:00
parent ecb5b08432
commit cf66692b21
3 changed files with 56 additions and 4 deletions

View File

@ -16,6 +16,8 @@
#include "cheats.h"
#include "general.h"
#include "dynamic.h"
#include <file/config_file.h>
#include <file/config_file_macros.h>
#include <compat/strl.h>
#include <compat/posix_string.h>
@ -40,7 +42,55 @@ void cheat_manager_apply_cheats(cheat_manager_t *handle)
}
}
cheat_manager_t *cheat_manager_new(void)
cheat_manager_t *cheat_manager_load(const char *path)
{
unsigned cheats = 0, i;
cheat_manager_t *cheat = NULL;
config_file_t *conf = config_file_new(path);
if (!conf)
return NULL;
config_get_uint(conf, "cheats", &cheats);
if (cheats == 0)
return NULL;
cheat = cheat_manager_new(cheats);
if (!cheat)
return NULL;
for (i = 0; i < cheats; i++)
{
char key[64];
char desc_key[256];
char code_key[256];
char enable_key[256];
char *tmp = NULL;
bool tmp_bool;
snprintf(key, sizeof(key), "cheat%u", i);
snprintf(desc_key, sizeof(desc_key), "cheat%u_desc", i);
snprintf(code_key, sizeof(code_key), "cheat%u_code", i);
snprintf(enable_key, sizeof(enable_key), "cheat%u_enable", i);
if (config_get_string(conf, desc_key, &tmp))
cheat->cheats[i].desc = strdup(tmp);
if (config_get_string(conf, code_key, &tmp))
cheat->cheats[i].code = strdup(tmp);
if (config_get_bool(conf, enable_key, &tmp_bool))
cheat->cheats[i].state = tmp_bool;
}
config_file_free(conf);
return cheat;
}
cheat_manager_t *cheat_manager_new(unsigned size)
{
unsigned i;
cheat_manager_t *handle = NULL;
@ -48,7 +98,7 @@ cheat_manager_t *cheat_manager_new(void)
if (!handle)
return NULL;
handle->buf_size = handle->size = 0;
handle->buf_size = handle->size = size;
handle->cheats = (struct item_cheat*)
calloc(handle->buf_size, sizeof(struct item_cheat));

View File

@ -39,7 +39,9 @@ struct cheat_manager
typedef struct cheat_manager cheat_manager_t;
cheat_manager_t *cheat_manager_new(void);
cheat_manager_t *cheat_manager_new(unsigned size);
cheat_manager_t *cheat_manager_load(const char *path);
bool cheat_manager_realloc(cheat_manager_t *handle, unsigned new_size);

View File

@ -166,7 +166,7 @@ void *menu_init(const void *data)
#ifdef HAVE_SHADER_MANAGER
menu->shader = (struct gfx_shader*)calloc(1, sizeof(struct gfx_shader));
#endif
menu->cheats = cheat_manager_new();
menu->cheats = cheat_manager_new(0);
menu->push_start_screen = g_settings.menu_show_start_screen;
g_settings.menu_show_start_screen = false;