diff options
author | StapleButter <thetotalworm@gmail.com> | 2018-02-19 13:08:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-19 13:08:34 +0100 |
commit | aab0030137d70a7ef06c2d2e859f195dd06e0079 (patch) | |
tree | aa30c02d16c31744dc735254f5e1c5afeb95468c /src/Config.cpp | |
parent | 12e33ae872795518aba882c57857dfefbc0c0eda (diff) | |
parent | 52df6be5f8a912aeef050fa4c563cca3ef80ad5a (diff) |
Merge pull request #224 from hcorion/alternate-loading
Load bios, firmware and melonDS.ini from exe, ~/.config/melonds or AppData
Diffstat (limited to 'src/Config.cpp')
-rw-r--r-- | src/Config.cpp | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/src/Config.cpp b/src/Config.cpp index dfa4416..660ff8c 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -20,6 +20,10 @@ #include <string.h> #include <stdlib.h> #include "Config.h" +#include <string> +#ifndef _WIN32 +#include <glib.h> +#endif namespace Config @@ -100,6 +104,46 @@ ConfigEntry ConfigFile[] = {"", -1, NULL, 0, NULL, 0} }; +FILE* GetConfigFile(const char* fileName, const char* permissions) +{ + // Locations are application directory, and XDG_CONFIG_HOME/melonds or AppData/MelonDS on windows + + FILE* f; + + // First check application directory + f = fopen(fileName, permissions); + if (f) return f; +#ifdef _WIN32 + // Now check AppData + PWSTR appDataPath = NULL; + SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath); + if (!appDataPath) + return NULL; + std::string path = std::string(appDataPath) + "\\melonds\\" + fileName; + f = fopen(path, permissions); + if (f) return f; +#else + // Now check XDG_CONFIG_HOME + std::string path = std::string(g_get_user_config_dir()) + "/melonds/" + fileName; + f = fopen(path.c_str(), permissions); + if (f) return f; +#endif + + return NULL; + +} + +bool HasConfigFile(const char* fileName) +{ + FILE* f = GetConfigFile(fileName, "rb"); + if (f) + { + fclose(f); + return true; + } + else + return false; +} void Load() { @@ -116,7 +160,7 @@ void Load() entry++; } - FILE* f = fopen("melonDS.ini", "r"); + FILE* f = Config::GetConfigFile("melonDS.ini", "r"); if (!f) return; char linebuf[1024]; @@ -152,7 +196,7 @@ void Load() void Save() { - FILE* f = fopen("melonDS.ini", "w"); + FILE* f = Config::GetConfigFile("melonDS.ini", "w"); if (!f) return; ConfigEntry* entry = &ConfigFile[0]; |