common: decouple getting and setting user paths

paths -> g_paths
This commit is contained in:
BreadFish64 2019-01-24 19:37:50 -06:00
parent de1128c60d
commit d3d53bae83
2 changed files with 36 additions and 45 deletions

View file

@ -672,14 +672,19 @@ std::string GetSysDirectory() {
return sysDir; return sysDir;
} }
// Returns a string with a Citra data dir or file in the user's home namespace {
// directory. To be used in "multi-user" mode (that is, installed). std::unordered_map<UserPath, std::string> g_paths;
const std::string& GetUserPath(UserPath path, const std::string& new_path) { }
static std::unordered_map<UserPath, std::string> paths;
auto& user_path = paths[UserPath::UserDir];
// Set up all paths and files on the first run void SetUserPath(const std::string& path) {
if (user_path.empty()) { std::string& user_path = g_paths[UserPath::UserDir];
if (!path.empty() && CreateFullPath(path)) {
LOG_INFO(Common_Filesystem, "Using {} as the user directory", path);
user_path = path;
g_paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP);
g_paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP);
} else {
#ifdef _WIN32 #ifdef _WIN32
user_path = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; user_path = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
if (!FileUtil::IsDirectory(user_path)) { if (!FileUtil::IsDirectory(user_path)) {
@ -688,56 +693,40 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path) {
LOG_INFO(Common_Filesystem, "Using the local user directory"); LOG_INFO(Common_Filesystem, "Using the local user directory");
} }
paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); g_paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP);
paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); g_paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP);
#else #else
if (FileUtil::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) { if (FileUtil::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) {
user_path = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; user_path = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP;
paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); g_paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP);
paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); g_paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP);
} else { } else {
std::string data_dir = GetUserDirectory("XDG_DATA_HOME"); std::string data_dir = GetUserDirectory("XDG_DATA_HOME");
std::string config_dir = GetUserDirectory("XDG_CONFIG_HOME"); std::string config_dir = GetUserDirectory("XDG_CONFIG_HOME");
std::string cache_dir = GetUserDirectory("XDG_CACHE_HOME"); std::string cache_dir = GetUserDirectory("XDG_CACHE_HOME");
user_path = data_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; user_path = data_dir + DIR_SEP EMU_DATA_DIR DIR_SEP;
paths.emplace(UserPath::ConfigDir, config_dir + DIR_SEP EMU_DATA_DIR DIR_SEP); g_paths.emplace(UserPath::ConfigDir, config_dir + DIR_SEP EMU_DATA_DIR DIR_SEP);
paths.emplace(UserPath::CacheDir, cache_dir + DIR_SEP EMU_DATA_DIR DIR_SEP); g_paths.emplace(UserPath::CacheDir, cache_dir + DIR_SEP EMU_DATA_DIR DIR_SEP);
} }
#endif #endif
paths.emplace(UserPath::SDMCDir, user_path + SDMC_DIR DIR_SEP); }
paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP); g_paths.emplace(UserPath::SDMCDir, user_path + SDMC_DIR DIR_SEP);
paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP); g_paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP);
g_paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP);
// TODO: Put the logs in a better location for each OS // TODO: Put the logs in a better location for each OS
paths.emplace(UserPath::LogDir, user_path + LOG_DIR DIR_SEP); g_paths.emplace(UserPath::LogDir, user_path + LOG_DIR DIR_SEP);
paths.emplace(UserPath::CheatsDir, user_path + CHEATS_DIR DIR_SEP); g_paths.emplace(UserPath::CheatsDir, user_path + CHEATS_DIR DIR_SEP);
paths.emplace(UserPath::DLLDir, user_path + DLL_DIR DIR_SEP); g_paths.emplace(UserPath::DLLDir, user_path + DLL_DIR DIR_SEP);
} }
if (!new_path.empty()) { // Returns a string with a Citra data dir or file in the user's home
if (!FileUtil::IsDirectory(new_path)) { // directory. To be used in "multi-user" mode (that is, installed).
LOG_ERROR(Common_Filesystem, "Invalid path specified {}", new_path); const std::string& GetUserPath(UserPath path) {
return paths[path]; // Set up all paths and files on the first run
} else { if (g_paths.empty())
paths[path] = new_path; SetUserPath();
} return g_paths[path];
switch (path) {
case UserPath::RootDir:
user_path = paths[UserPath::RootDir] + DIR_SEP;
break;
case UserPath::UserDir:
user_path = paths[UserPath::RootDir] + DIR_SEP;
paths[UserPath::ConfigDir] = user_path + CONFIG_DIR DIR_SEP;
paths[UserPath::CacheDir] = user_path + CACHE_DIR DIR_SEP;
paths[UserPath::SDMCDir] = user_path + SDMC_DIR DIR_SEP;
paths[UserPath::NANDDir] = user_path + NAND_DIR DIR_SEP;
break;
}
}
return paths[path];
} }
size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) { size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) {

View file

@ -123,9 +123,11 @@ void CopyDir(const std::string& source_path, const std::string& dest_path);
// Set the current directory to given directory // Set the current directory to given directory
bool SetCurrentDir(const std::string& directory); bool SetCurrentDir(const std::string& directory);
void SetUserPath(const std::string& path = "");
// Returns a pointer to a string with a Citra data dir in the user's home // Returns a pointer to a string with a Citra data dir in the user's home
// directory. To be used in "multi-user" mode (that is, installed). // directory. To be used in "multi-user" mode (that is, installed).
const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); const std::string& GetUserPath(UserPath path);
// Returns the path to where the sys file are // Returns the path to where the sys file are
std::string GetSysDirectory(); std::string GetSysDirectory();