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

@ -17,7 +17,9 @@ struct EmulatorConfig {
// Default to 3% battery to make users suffer // Default to 3% battery to make users suffer
int batteryPercentage = 3; int batteryPercentage = 3;
std::filesystem::path filePath;
EmulatorConfig(const std::filesystem::path& path); EmulatorConfig(const std::filesystem::path& path);
void load(const std::filesystem::path& path); void load();
void save(const std::filesystem::path& path); void save();
}; };

View file

@ -122,4 +122,7 @@ class Emulator {
RendererType getRendererType() const { return config.rendererType; } RendererType getRendererType() const { return config.rendererType; }
Renderer* getRenderer() { return gpu.getRenderer(); } Renderer* getRenderer() { return gpu.getRenderer(); }
u64 getTicks() { return cpu.getTicks(); } u64 getTicks() { return cpu.getTicks(); }
std::filesystem::path getConfigPath();
std::filesystem::path getAndroidAppPath();
}; };

View file

@ -90,6 +90,13 @@ namespace Helpers {
return false; return false;
} }
static constexpr bool isAndroid() {
#ifdef __ANDROID__
return true;
#endif
return false;
}
static void debug_printf(const char* fmt, ...) { static void debug_printf(const char* fmt, ...) {
if constexpr (buildingInDebugMode()) { if constexpr (buildingInDebugMode()) {
std::va_list args; std::va_list args;

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 // 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. // 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 // If the configuration file does not exist, create it and return
std::error_code error; std::error_code error;
if (!std::filesystem::exists(path, error)) { if (!std::filesystem::exists(path, error)) {
save(path); save();
return; 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; toml::basic_value<toml::preserve_comments> data;
const std::filesystem::path& path = filePath;
std::error_code error; std::error_code error;
if (std::filesystem::exists(path, error)) { if (std::filesystem::exists(path, error)) {

View file

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