Fix configuration file path on Android

This commit is contained in:
wheremyfoodat 2023-11-28 12:08:25 +02:00
parent 7a78b2cf20
commit 070d5b1c6d
5 changed files with 41 additions and 13 deletions

View file

@ -11,13 +11,15 @@
// We are legally allowed, as per the author's wish, to use the above code without any licensing restrictions
// However we still want to follow the license as closely as possible and offer the proper attributions.
EmulatorConfig::EmulatorConfig(const std::filesystem::path& path) { load(path); }
EmulatorConfig::EmulatorConfig(const std::filesystem::path& path) : filePath(path) { load(); }
void EmulatorConfig::load() {
const std::filesystem::path& path = filePath;
void EmulatorConfig::load(const std::filesystem::path& path) {
// If the configuration file does not exist, create it and return
std::error_code error;
if (!std::filesystem::exists(path, error)) {
save(path);
save();
return;
}
@ -84,8 +86,9 @@ void EmulatorConfig::load(const std::filesystem::path& path) {
}
}
void EmulatorConfig::save(const std::filesystem::path& path) {
void EmulatorConfig::save() {
toml::basic_value<toml::preserve_comments> data;
const std::filesystem::path& path = filePath;
std::error_code error;
if (std::filesystem::exists(path, error)) {

View file

@ -15,7 +15,7 @@ __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 1;
#endif
Emulator::Emulator()
: config(std::filesystem::current_path() / "config.toml"), kernel(cpu, memory, gpu, config), cpu(memory, kernel), gpu(memory, config),
: config(getConfigPath()), kernel(cpu, memory, gpu, config), cpu(memory, kernel), gpu(memory, config),
memory(cpu.getTicksRef(), config), cheats(memory, kernel.getServiceManager().getHID()), lua(memory), running(false), programRunning(false)
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
, httpServer(this)
@ -31,7 +31,7 @@ Emulator::Emulator()
}
Emulator::~Emulator() {
config.save(std::filesystem::current_path() / "config.toml");
config.save();
lua.close();
#ifdef PANDA3DS_ENABLE_DISCORD_RPC
@ -68,6 +68,23 @@ void Emulator::reset(ReloadOption reload) {
}
}
std::filesystem::path Emulator::getAndroidAppPath() {
// SDL_GetPrefPath fails to get the path due to no JNI environment
std::ifstream cmdline("/proc/self/cmdline");
std::string applicationName;
std::getline(cmdline, applicationName, '\0');
return std::filesystem::path("/data") / "data" / applicationName / "files";
}
std::filesystem::path Emulator::getConfigPath() {
if constexpr (Helpers::isAndroid()) {
return getAndroidAppPath() / "config.toml";
} else {
return std::filesystem::current_path() / "config.toml";
}
}
void Emulator::step() {}
void Emulator::render() {}
@ -115,11 +132,7 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
std::filesystem::path appDataPath;
#ifdef __ANDROID__
// SDL_GetPrefPath fails to get the path due to no JNI environment
std::ifstream cmdline("/proc/self/cmdline");
std::string applicationName;
std::getline(cmdline, applicationName, '\0');
appDataPath = std::filesystem::path("/data") / "data" / applicationName / "files";
appDataPath = getAndroidAppPath();
#else
char* appData;
if (!config.usePortableBuild) {