From 069dbce5c57d2f17ff3aae66edbf2fc0e6429ee1 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 17 Sep 2016 12:16:11 +0200 Subject: [PATCH] Add more functions to paths.c --- paths.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ paths.h | 4 ++ retroarch.c | 161 +------------------------------------------------- 3 files changed, 173 insertions(+), 159 deletions(-) diff --git a/paths.c b/paths.c index 17023cfb2d..4ae36c20c1 100644 --- a/paths.c +++ b/paths.c @@ -15,6 +15,8 @@ #include #include +#include +#include #ifdef HAVE_CONFIG_H #include "config.h" @@ -22,7 +24,160 @@ #include "paths.h" +#include "configuration.h" +#include "content.h" +#include "file_path_special.h" + +#include "core.h" +#include "msg_hash.h" #include "runloop.h" +#include "verbosity.h" + +#define MENU_VALUE_NO_CORE 0x7d5472cbU + +static char current_savefile_dir[PATH_MAX_LENGTH] = {0}; + +void path_set_redirect(void) +{ + char current_savestate_dir[PATH_MAX_LENGTH] = {0}; + uint32_t global_library_name_hash = 0; + bool check_global_library_name_hash = false; + global_t *global = global_get_ptr(); + settings_t *settings = config_get_ptr(); + rarch_system_info_t *info = NULL; + + runloop_ctl(RUNLOOP_CTL_SYSTEM_INFO_GET, &info); + + if (!global) + return; + + if (info->info.library_name && + !string_is_empty(info->info.library_name)) + global_library_name_hash = + msg_hash_calculate(info->info.library_name); + + /* Initialize current save directories + * with the values from the config. */ + strlcpy(current_savefile_dir, + global->dir.savefile, + sizeof(current_savefile_dir)); + strlcpy(current_savestate_dir, + global->dir.savestate, + sizeof(current_savestate_dir)); + + check_global_library_name_hash = (global_library_name_hash != 0); +#ifdef HAVE_MENU + check_global_library_name_hash = check_global_library_name_hash && + (global_library_name_hash != MENU_VALUE_NO_CORE); +#endif + + if (check_global_library_name_hash) + { + /* per-core saves: append the library_name to the save location */ + if (settings->sort_savefiles_enable + && !string_is_empty(global->dir.savefile)) + { + fill_pathname_join( + current_savefile_dir, + global->dir.savefile, + info->info.library_name, + sizeof(global->dir.savefile)); + + /* If path doesn't exist, try to create it, + * if everything fails revert to the original path. */ + if(!path_is_directory(current_savefile_dir) + && !string_is_empty(current_savefile_dir)) + { + path_mkdir(current_savefile_dir); + if(!path_is_directory(current_savefile_dir)) + { + RARCH_LOG("%s %s\n", + msg_hash_to_str(MSG_REVERTING_SAVEFILE_DIRECTORY_TO), + global->dir.savefile); + + strlcpy(current_savefile_dir, + global->dir.savefile, + sizeof(current_savefile_dir)); + } + } + } + + /* per-core states: append the library_name to the save location */ + if (settings->sort_savestates_enable + && !string_is_empty(global->dir.savestate)) + { + fill_pathname_join( + current_savestate_dir, + global->dir.savestate, + info->info.library_name, + sizeof(global->dir.savestate)); + + /* If path doesn't exist, try to create it. + * If everything fails, revert to the original path. */ + if(!path_is_directory(current_savestate_dir) && + !string_is_empty(current_savestate_dir)) + { + path_mkdir(current_savestate_dir); + if(!path_is_directory(current_savestate_dir)) + { + RARCH_LOG("%s %s\n", + msg_hash_to_str(MSG_REVERTING_SAVESTATE_DIRECTORY_TO), + global->dir.savestate); + strlcpy(current_savestate_dir, + global->dir.savestate, + sizeof(current_savestate_dir)); + } + } + } + } + + /* Set savefile directory if empty based on content directory */ + if (string_is_empty(current_savefile_dir)) + { + global_t *global = global_get_ptr(); + strlcpy(current_savefile_dir, global->name.base, + sizeof(current_savefile_dir)); + path_basedir(current_savefile_dir); + } + + if(path_is_directory(current_savefile_dir)) + strlcpy(global->name.savefile, current_savefile_dir, + sizeof(global->name.savefile)); + + if(path_is_directory(current_savestate_dir)) + strlcpy(global->name.savestate, current_savestate_dir, + sizeof(global->name.savestate)); + + if (path_is_directory(global->name.savefile)) + { + fill_pathname_dir(global->name.savefile, global->name.base, + file_path_str(FILE_PATH_SRM_EXTENSION), + sizeof(global->name.savefile)); + RARCH_LOG("%s \"%s\".\n", + msg_hash_to_str(MSG_REDIRECTING_SAVEFILE_TO), + global->name.savefile); + } + + if (path_is_directory(global->name.savestate)) + { + fill_pathname_dir(global->name.savestate, global->name.base, + file_path_str(FILE_PATH_STATE_EXTENSION), + sizeof(global->name.savestate)); + RARCH_LOG("%s \"%s\".\n", + msg_hash_to_str(MSG_REDIRECTING_SAVESTATE_TO), + global->name.savestate); + } + + if (path_is_directory(global->name.cheatfile)) + { + fill_pathname_dir(global->name.cheatfile, global->name.base, + file_path_str(FILE_PATH_STATE_EXTENSION), + sizeof(global->name.cheatfile)); + RARCH_LOG("%s \"%s\".\n", + msg_hash_to_str(MSG_REDIRECTING_CHEATFILE_TO), + global->name.cheatfile); + } +} void path_set_basename(const char *path) { @@ -59,3 +214,15 @@ void path_set_basename(const char *path) if ((dst = strrchr(global->name.base, '.'))) *dst = '\0'; } + +const char *path_get_current_savefile_dir(void) +{ + char *ret = current_savefile_dir; + + /* try to infer the path in case it's still empty by calling + path_set_redirect */ + if (string_is_empty(ret) && !content_does_not_need_content()) + path_set_redirect(); + + return ret; +} diff --git a/paths.h b/paths.h index 414f03cb8d..ce8bc5f8d5 100644 --- a/paths.h +++ b/paths.h @@ -21,8 +21,12 @@ RETRO_BEGIN_DECLS +void path_set_redirect(void); + void path_set_basename(const char *path); +const char *path_get_current_savefile_dir(void); + RETRO_END_DECLS #endif diff --git a/retroarch.c b/retroarch.c index 03a1668ab7..d26dcf5dd9 100644 --- a/retroarch.c +++ b/retroarch.c @@ -113,7 +113,6 @@ enum static bool current_core_explicitly_set = false; static enum rarch_core_type current_core_type = CORE_TYPE_PLAIN; static enum rarch_core_type explicit_current_core_type = CORE_TYPE_PLAIN; -static char current_savefile_dir[PATH_MAX_LENGTH] = {0}; static char error_string[PATH_MAX_LENGTH] = {0}; static jmp_buf error_sjlj_context; @@ -375,162 +374,6 @@ static void retroarch_set_special_paths(char **argv, unsigned num_content) } } -#define MENU_VALUE_NO_CORE 0x7d5472cbU - -static void retroarch_set_paths_redirect(void) -{ - char current_savestate_dir[PATH_MAX_LENGTH] = {0}; - uint32_t global_library_name_hash = 0; - bool check_global_library_name_hash = false; - global_t *global = global_get_ptr(); - settings_t *settings = config_get_ptr(); - rarch_system_info_t *info = NULL; - - runloop_ctl(RUNLOOP_CTL_SYSTEM_INFO_GET, &info); - - if (!global) - return; - - if (info->info.library_name && - !string_is_empty(info->info.library_name)) - global_library_name_hash = - msg_hash_calculate(info->info.library_name); - - /* Initialize current save directories - * with the values from the config. */ - strlcpy(current_savefile_dir, - global->dir.savefile, - sizeof(current_savefile_dir)); - strlcpy(current_savestate_dir, - global->dir.savestate, - sizeof(current_savestate_dir)); - - check_global_library_name_hash = (global_library_name_hash != 0); -#ifdef HAVE_MENU - check_global_library_name_hash = check_global_library_name_hash && - (global_library_name_hash != MENU_VALUE_NO_CORE); -#endif - - if (check_global_library_name_hash) - { - /* per-core saves: append the library_name to the save location */ - if (settings->sort_savefiles_enable - && !string_is_empty(global->dir.savefile)) - { - fill_pathname_join( - current_savefile_dir, - global->dir.savefile, - info->info.library_name, - sizeof(global->dir.savefile)); - - /* If path doesn't exist, try to create it, - * if everything fails revert to the original path. */ - if(!path_is_directory(current_savefile_dir) - && !string_is_empty(current_savefile_dir)) - { - path_mkdir(current_savefile_dir); - if(!path_is_directory(current_savefile_dir)) - { - RARCH_LOG("%s %s\n", - msg_hash_to_str(MSG_REVERTING_SAVEFILE_DIRECTORY_TO), - global->dir.savefile); - - strlcpy(current_savefile_dir, - global->dir.savefile, - sizeof(current_savefile_dir)); - } - } - } - - /* per-core states: append the library_name to the save location */ - if (settings->sort_savestates_enable - && !string_is_empty(global->dir.savestate)) - { - fill_pathname_join( - current_savestate_dir, - global->dir.savestate, - info->info.library_name, - sizeof(global->dir.savestate)); - - /* If path doesn't exist, try to create it. - * If everything fails, revert to the original path. */ - if(!path_is_directory(current_savestate_dir) && - !string_is_empty(current_savestate_dir)) - { - path_mkdir(current_savestate_dir); - if(!path_is_directory(current_savestate_dir)) - { - RARCH_LOG("%s %s\n", - msg_hash_to_str(MSG_REVERTING_SAVESTATE_DIRECTORY_TO), - global->dir.savestate); - strlcpy(current_savestate_dir, - global->dir.savestate, - sizeof(current_savestate_dir)); - } - } - } - } - - /* Set savefile directory if empty based on content directory */ - if (string_is_empty(current_savefile_dir)) - { - global_t *global = global_get_ptr(); - strlcpy(current_savefile_dir, global->name.base, - sizeof(current_savefile_dir)); - path_basedir(current_savefile_dir); - } - - if(path_is_directory(current_savefile_dir)) - strlcpy(global->name.savefile, current_savefile_dir, - sizeof(global->name.savefile)); - - if(path_is_directory(current_savestate_dir)) - strlcpy(global->name.savestate, current_savestate_dir, - sizeof(global->name.savestate)); - - if (path_is_directory(global->name.savefile)) - { - fill_pathname_dir(global->name.savefile, global->name.base, - file_path_str(FILE_PATH_SRM_EXTENSION), - sizeof(global->name.savefile)); - RARCH_LOG("%s \"%s\".\n", - msg_hash_to_str(MSG_REDIRECTING_SAVEFILE_TO), - global->name.savefile); - } - - if (path_is_directory(global->name.savestate)) - { - fill_pathname_dir(global->name.savestate, global->name.base, - file_path_str(FILE_PATH_STATE_EXTENSION), - sizeof(global->name.savestate)); - RARCH_LOG("%s \"%s\".\n", - msg_hash_to_str(MSG_REDIRECTING_SAVESTATE_TO), - global->name.savestate); - } - - if (path_is_directory(global->name.cheatfile)) - { - fill_pathname_dir(global->name.cheatfile, global->name.base, - file_path_str(FILE_PATH_STATE_EXTENSION), - sizeof(global->name.cheatfile)); - RARCH_LOG("%s \"%s\".\n", - msg_hash_to_str(MSG_REDIRECTING_CHEATFILE_TO), - global->name.cheatfile); - } -} - -const char *retroarch_get_current_savefile_dir(void) -{ - char *ret = current_savefile_dir; - - /* try to infer the path in case it's still empty by calling - set_paths_redirect */ - if (string_is_empty(ret) && !content_does_not_need_content()) - retroarch_set_paths_redirect(); - - return ret; -} - enum rarch_content_type retroarch_path_is_media_type(const char *path) { char ext_lower[PATH_MAX_LENGTH] = {0}; @@ -1502,7 +1345,7 @@ bool rarch_ctl(enum rarch_ctl_state state, void *data) case RARCH_CTL_SET_PATHS_REDIRECT: if (content_does_not_need_content()) return false; - retroarch_set_paths_redirect(); + path_set_redirect(); break; case RARCH_CTL_SET_SRAM_ENABLE: { @@ -1581,7 +1424,7 @@ void retroarch_set_pathnames(const char *path) fill_pathname_noext(global->name.cheatfile, global->name.base, file_path_str(FILE_PATH_CHT_EXTENSION), sizeof(global->name.cheatfile)); - retroarch_set_paths_redirect(); + path_set_redirect(); } void retroarch_fill_pathnames(void)