Skip to content

Commit

Permalink
Fix configuration file path on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
wheremyfoodat committed Nov 28, 2023
1 parent 7a78b2c commit 070d5b1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
6 changes: 4 additions & 2 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
3 changes: 3 additions & 0 deletions include/emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
7 changes: 7 additions & 0 deletions include/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)) {
Expand Down
27 changes: 20 additions & 7 deletions src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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() {}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 070d5b1

Please sign in to comment.