From 070d5b1c6dad1878dd95fbc4c33e24425e6e73d5 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:08:25 +0200 Subject: [PATCH] Fix configuration file path on Android --- include/config.hpp | 6 ++++-- include/emulator.hpp | 3 +++ include/helpers.hpp | 7 +++++++ src/config.cpp | 11 +++++++---- src/emulator.cpp | 27 ++++++++++++++++++++------- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/config.hpp b/include/config.hpp index 326ab203..424d0741 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -17,7 +17,9 @@ struct EmulatorConfig { // Default to 3% battery to make users suffer int batteryPercentage = 3; + std::filesystem::path filePath; + EmulatorConfig(const std::filesystem::path& path); - void load(const std::filesystem::path& path); - void save(const std::filesystem::path& path); + void load(); + void save(); }; \ No newline at end of file diff --git a/include/emulator.hpp b/include/emulator.hpp index 2da76847..d992b331 100644 --- a/include/emulator.hpp +++ b/include/emulator.hpp @@ -122,4 +122,7 @@ class Emulator { RendererType getRendererType() const { return config.rendererType; } Renderer* getRenderer() { return gpu.getRenderer(); } u64 getTicks() { return cpu.getTicks(); } + + std::filesystem::path getConfigPath(); + std::filesystem::path getAndroidAppPath(); }; diff --git a/include/helpers.hpp b/include/helpers.hpp index 9082cc54..037c8976 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -90,6 +90,13 @@ namespace Helpers { return false; } + static constexpr bool isAndroid() { +#ifdef __ANDROID__ + return true; +#endif + return false; + } + static void debug_printf(const char* fmt, ...) { if constexpr (buildingInDebugMode()) { std::va_list args; diff --git a/src/config.cpp b/src/config.cpp index 59ee78f6..28946d52 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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 data; + const std::filesystem::path& path = filePath; std::error_code error; if (std::filesystem::exists(path, error)) { diff --git a/src/emulator.cpp b/src/emulator.cpp index 81d3ac5c..bcd74966 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -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) {