From 0442aa5a31ee382517384ce289487f4a31e42192 Mon Sep 17 00:00:00 2001 From: Sven <40953353+RetroSven@users.noreply.github.com> Date: Sun, 29 Jul 2018 10:48:41 -0400 Subject: [PATCH] save/load from cheat_dir/core_name/game_name.cht ; only save to cheat file when there are >0 cheats --- managers/cheat_manager.c | 63 ++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/managers/cheat_manager.c b/managers/cheat_manager.c index 57c30c0b01..bfe544e5dc 100644 --- a/managers/cheat_manager.c +++ b/managers/cheat_manager.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -48,6 +49,7 @@ #include "../core.h" #include "../verbosity.h" #include "../input/input_driver.h" +#include "../configuration.h" unsigned cheat_manager_get_buf_size(void) @@ -124,6 +126,9 @@ bool cheat_manager_save(const char *path, const char *cheat_database, bool overw buf[0] = cheats_file[0] = '\0'; + if ( (!cheat_manager_state.cheats) || cheat_manager_state.size==0 ) + return false ; + if ( cheat_database == NULL ) { strncpy(cheats_file, path, PATH_MAX_LENGTH) ; @@ -149,12 +154,6 @@ bool cheat_manager_save(const char *path, const char *cheat_database, bool overw if (!conf) return false; - if (!cheat_manager_state.cheats) - { - config_file_free(conf); - return false; - } - config_set_int(conf, "cheats", cheat_manager_state.size); for (i = 0; i < cheat_manager_state.size; i++) @@ -546,24 +545,56 @@ bool cheat_manager_get_code_state(unsigned i) return cheat_manager_state.cheats[i].state; } +bool cheat_manager_get_game_specific_filename(char * cheat_filename, size_t max_length) +{ + settings_t *settings = config_get_ptr(); + global_t *global = global_get_ptr(); + const char *core_name = NULL; + const char *game_name = NULL; + struct retro_system_info system_info; + + if ( settings == NULL || global == NULL || cheat_filename == NULL) + return false ; + + if ( !core_get_system_info(&system_info) ) + return false ; + + + core_name = system_info.library_name; + game_name = path_basename(global->name.cheatfile); + + if ( string_is_empty(settings->paths.path_cheat_database) || + string_is_empty(core_name) || string_is_empty(game_name) ) + return false ; + + cheat_filename[0] = '\0'; + strncat(cheat_filename, settings->paths.path_cheat_database, max_length) ; + fill_pathname_slash(cheat_filename, max_length) ; + strncat(cheat_filename, core_name, max_length-strlen(cheat_filename)) ; + fill_pathname_slash(cheat_filename, max_length) ; + + if (!filestream_exists(cheat_filename)) + path_mkdir(cheat_filename); + + strncat(cheat_filename, game_name, max_length-strlen(cheat_filename)) ; + + return true ; + +} void cheat_manager_load_game_specific_cheats() { - global_t *global = global_get_ptr(); + char cheat_file[PATH_MAX_LENGTH] ; - if (global ) - { - cheat_manager_load(global->name.cheatfile,true) ; - } + if (cheat_manager_get_game_specific_filename(cheat_file, PATH_MAX_LENGTH) ) + cheat_manager_load(cheat_file,true) ; } void cheat_manager_save_game_specific_cheats() { - global_t *global = global_get_ptr(); + char cheat_file[PATH_MAX_LENGTH] ; - if (global ) - { - cheat_manager_save(global->name.cheatfile, NULL, true) ; - } + if (cheat_manager_get_game_specific_filename(cheat_file, PATH_MAX_LENGTH) ) + cheat_manager_save(cheat_file, NULL, true) ; }