From 38066b6ec705c56f29b710179f94ccdaa51eedd7 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 19 Jan 2015 19:00:26 +0100 Subject: [PATCH] Add Cheat File Save As --- cheats.c | 61 ++++++++++++++++++++++++++++++++++++++++- cheats.h | 10 +++++++ menu/menu_entries_cbs.c | 15 ++++++++++ menu/menu_input.c | 3 ++ 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/cheats.c b/cheats.c index d50946b627..ab5da5ecfe 100644 --- a/cheats.c +++ b/cheats.c @@ -18,7 +18,7 @@ #include "general.h" #include "dynamic.h" #include -#include +#include #include #include @@ -43,6 +43,65 @@ void cheat_manager_apply_cheats(cheat_manager_t *handle) } } +/** + * cheat_manager_save: + * @path : Path to cheats file (relative path). + * + * Saves cheats to file on disk. + * + * Returns: true (1) if successful, otherwise false (0). + **/ +bool cheat_manager_save(cheat_manager_t *handle, const char *path) +{ + bool ret; + unsigned i; + char buf[PATH_MAX_LENGTH]; + char cheats_file[PATH_MAX_LENGTH]; + config_file_t *conf = NULL; + + fill_pathname_join(buf, g_settings.cheat_database, + path, sizeof(buf)); + + fill_pathname_noext(cheats_file, buf, ".cht", sizeof(cheats_file)); + + conf = config_file_new(cheats_file); + + if (!conf) + conf = config_file_new(NULL); + + if (!conf) + return false; + + if (!handle) + return false; + + config_set_int(conf, "cheats", handle->size); + + for (i = 0; i < handle->size; i++) + { + char key[64], desc_key[256], code_key[256], enable_key[256]; + char *tmp = NULL; + bool tmp_bool = false; + + 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 (handle->cheats[i].desc) + config_set_string(conf, desc_key, handle->cheats[i].desc); + else + config_set_string(conf, desc_key, handle->cheats[i].code); + config_set_string(conf, code_key, handle->cheats[i].code); + config_set_bool(conf, enable_key, handle->cheats[i].state); + } + + ret = config_file_write(conf, cheats_file); + config_file_free(conf); + + return ret; +} + cheat_manager_t *cheat_manager_load(const char *path) { unsigned cheats = 0, i; diff --git a/cheats.h b/cheats.h index da4a2409aa..51ae737174 100644 --- a/cheats.h +++ b/cheats.h @@ -44,6 +44,16 @@ cheat_manager_t *cheat_manager_new(unsigned size); cheat_manager_t *cheat_manager_load(const char *path); +/** + * cheat_manager_save: + * @path : Path to cheats file (absolute path). + * + * Saves cheats to file on disk. + * + * Returns: true (1) if successful, otherwise false (0). + **/ +bool cheat_manager_save(cheat_manager_t *handle, const char *path); + bool cheat_manager_realloc(cheat_manager_t *handle, unsigned new_size); void cheat_manager_free(cheat_manager_t *handle); diff --git a/menu/menu_entries_cbs.c b/menu/menu_entries_cbs.c index c1f5805e5b..5a87d235f6 100644 --- a/menu/menu_entries_cbs.c +++ b/menu/menu_entries_cbs.c @@ -434,6 +434,17 @@ static int action_ok_shader_preset_save_as(const char *path, return 0; } +static int action_ok_cheat_file_save_as(const char *path, + const char *label, unsigned type, size_t idx) +{ + if (!driver.menu) + return -1; + + menu_input_key_start_line(driver.menu, "Cheat Filename", + label, type, idx, menu_input_st_string_callback); + return 0; +} + static int action_ok_remap_file_save_as(const char *path, const char *label, unsigned type, size_t idx) { @@ -2064,6 +2075,8 @@ static int deferred_push_core_cheat_options(void *data, void *userdata, menu_list_clear(list); menu_list_push(list, "Cheat File Load", "cheat_file_load", MENU_SETTING_ACTION, 0); + menu_list_push(list, "Cheat File Save As", + "cheat_file_save_as", MENU_SETTING_ACTION, 0); menu_list_push(list, "Cheat Passes", "cheat_num_passes", 0, 0); menu_list_push(list, "Apply Cheat Changes", "cheat_apply_changes", @@ -2673,6 +2686,8 @@ static void menu_entries_cbs_init_bind_ok(menu_file_list_cbs_t *cbs, cbs->action_ok = action_ok_cheat_apply_changes; else if (!strcmp(label, "video_shader_preset_save_as")) cbs->action_ok = action_ok_shader_preset_save_as; + else if (!strcmp(label, "cheat_file_save_as")) + cbs->action_ok = action_ok_cheat_file_save_as; else if (!strcmp(label, "remap_file_save_as")) cbs->action_ok = action_ok_remap_file_save_as; else if (!strcmp(label, "core_list")) diff --git a/menu/menu_input.c b/menu/menu_input.c index 68b5397410..4f760317b2 100644 --- a/menu/menu_input.c +++ b/menu/menu_input.c @@ -26,6 +26,7 @@ #include "menu.h" #include "menu_action.h" #include "menu_shader.h" +#include "../cheats.h" #include "../performance.h" #include "../settings_data.h" #include "../input/input_joypad.h" @@ -107,6 +108,8 @@ void menu_input_st_string_callback(void *userdata, const char *str) menu_shader_manager_save_preset(str, false); else if (!strcmp(menu->keyboard.label_setting, "remap_file_save_as")) input_remapping_save_file(str); + else if (!strcmp(menu->keyboard.label_setting, "cheat_file_save_as")) + cheat_manager_save(g_extern.cheat, str); } }