diff --git a/flatpak/FlatpakLaunch.sh b/flatpak/FlatpakLaunch.sh index ea42414..8fe4e97 100644 --- a/flatpak/FlatpakLaunch.sh +++ b/flatpak/FlatpakLaunch.sh @@ -1,2 +1,2 @@ #!/bin/sh -JMINOS_DATA="/app/share" /app/bin/graph \ No newline at end of file +JMINOS_DATA="/app/share" JMINOS_CONFIG=$XDG_CONFIG_HOME /app/bin/graph \ No newline at end of file diff --git a/src/GraphicalUI/Keybinds.cpp b/src/GraphicalUI/Keybinds.cpp index 5435982..bb70708 100644 --- a/src/GraphicalUI/Keybinds.cpp +++ b/src/GraphicalUI/Keybinds.cpp @@ -21,7 +21,7 @@ Keybinds::Keybinds(int layoutNumber) : } void Keybinds::loadKeybindsFromFile() { - std::ifstream layoutFile(AssetManager::getResourcePath("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin"), std::ios::binary); + std::ifstream layoutFile(AssetManager::getConfigPath("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin"), std::ios::binary); for (Action action : ACTION_LIST_IN_ORDER) { this->keybinds.at(action).clear(); @@ -48,7 +48,7 @@ void Keybinds::loadKeybindsFromFile() { void Keybinds::saveKeybindsToFile() const { if (!this->modifiable) return; - std::ofstream layoutFile(AssetManager::getResourcePath("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin"), std::ios::trunc | std::ios::binary); + std::ofstream layoutFile(AssetManager::getConfigPath("data/config/keybinds/layout" + std::to_string(this->layoutNumber) + ".bin"), std::ios::trunc | std::ios::binary); char byte; for (Action action : ACTION_LIST_IN_ORDER) { diff --git a/src/GraphicalUI/Settings.cpp b/src/GraphicalUI/Settings.cpp index 1a2f2df..13a2185 100644 --- a/src/GraphicalUI/Settings.cpp +++ b/src/GraphicalUI/Settings.cpp @@ -50,7 +50,7 @@ void Settings::loadPieces(int loadablePiecesSizeRequest) { } void Settings::loadSettingsFromFile(bool loadPieces, std::optional loadablePiecesSizeRequest) { - std::ifstream settingsFile(AssetManager::getResourcePath("data/config/settings.bin"), std::ios::binary); + std::ifstream settingsFile(AssetManager::getConfigPath("data/config/settings.bin"), std::ios::binary); char byte; // file format version @@ -159,7 +159,7 @@ void Settings::saveSettingsToFile() const { this->keybinds.at(CUSTOMIZABLE_KEYBINDS).saveKeybindsToFile(); - std::ofstream settingsFile(AssetManager::getResourcePath("data/config/settings.bin"), std::ios::trunc | std::ios::binary); + std::ofstream settingsFile(AssetManager::getConfigPath("data/config/settings.bin"), std::ios::trunc | std::ios::binary); char byte; // file format version diff --git a/src/GraphicalUI/main.cpp b/src/GraphicalUI/main.cpp index 71899fb..e04d72d 100644 --- a/src/GraphicalUI/main.cpp +++ b/src/GraphicalUI/main.cpp @@ -59,19 +59,19 @@ int main() { // CHECK CONFIG FILES - if (!std::filesystem::exists(AssetManager::getResourcePath("data/config/settings.bin"))) { + if (!std::filesystem::exists(AssetManager::getConfigPath("data/config/settings.bin"))) { std::cout << "INFO: Settings file not found, generating..." << std::endl; everythingIsOK &= resetSettingsFile(); for (int i = 0; i < NUMBER_OF_KEYBINDS; i++) { - if (!std::filesystem::exists(AssetManager::getResourcePath("data/config/keybinds/layout" + std::to_string(i) + ".bin"))) { + if (!std::filesystem::exists(AssetManager::getConfigPath("data/config/keybinds/layout" + std::to_string(i) + ".bin"))) { std::cout << "INFO: Keybind file number " << (i + 1) << "/" << NUMBER_OF_KEYBINDS << " not found, generating..." << std::endl; everythingIsOK &= resetKeybindFile(i); } } } else { - std::ifstream settingsFile(AssetManager::getResourcePath("data/config/settings.bin"), std::ios::binary); + std::ifstream settingsFile(AssetManager::getConfigPath("data/config/settings.bin"), std::ios::binary); char byte; settingsFile.get(byte); @@ -101,11 +101,11 @@ int main() { bool resetSettingsFile() { - if (!std::filesystem::exists(AssetManager::getResourcePath("data/config"))) { - std::filesystem::create_directories(AssetManager::getResourcePath("data/config")); + if (!std::filesystem::exists(AssetManager::getConfigPath("data/config"))) { + std::filesystem::create_directories(AssetManager::getConfigPath("data/config")); } - auto filePath = AssetManager::getResourcePath("data/config/settings.bin"); + auto filePath = AssetManager::getConfigPath("data/config/settings.bin"); std::ofstream settingsFile(filePath, std::ios::trunc | std::ios::binary); if (!settingsFile.good()) { std::cerr << "ERROR: Could not open file " << filePath << std::endl; @@ -182,11 +182,11 @@ bool resetKeybindFile(int layout) { return false; } - if (!std::filesystem::exists(AssetManager::getResourcePath("data/config/keybinds"))) { - std::filesystem::create_directories(AssetManager::getResourcePath("data/config/keybinds")); + if (!std::filesystem::exists(AssetManager::getConfigPath("data/config/keybinds"))) { + std::filesystem::create_directories(AssetManager::getConfigPath("data/config/keybinds")); } - auto filePath = AssetManager::getResourcePath("data/config/keybinds/layout" + std::to_string(layout) + ".bin"); + auto filePath = AssetManager::getConfigPath("data/config/keybinds/layout" + std::to_string(layout) + ".bin"); std::ofstream layoutFile(filePath, std::ios::trunc | std::ios::binary); if (!layoutFile.good()) { std::cerr << "ERROR: Could not open file " << filePath << std::endl; diff --git a/src/Utils/AssetManager.cpp b/src/Utils/AssetManager.cpp index 61a1126..0b12e94 100644 --- a/src/Utils/AssetManager.cpp +++ b/src/Utils/AssetManager.cpp @@ -2,11 +2,15 @@ namespace fs = std::filesystem; -std::string AssetManager::getResourcePrefix() { - char* env = std::getenv("JMINOS_DATA"); - return env ? env : std::string{}; +static std::string getEnv(const std::string& var) { + char* env = std::getenv(var.c_str()); + return env ? env : ""; } fs::path AssetManager::getResourcePath(const std::string& resource) { - return fs::path{getResourcePrefix()} / resource; + return fs::path{getEnv("JMINOS_DATA")} / resource; +} + +fs::path AssetManager::getConfigPath(const std::string& resource) { + return fs::path{getEnv("JMINOS_CONFIG")} / resource; } \ No newline at end of file diff --git a/src/Utils/AssetManager.h b/src/Utils/AssetManager.h index 15cfd88..ab36e94 100644 --- a/src/Utils/AssetManager.h +++ b/src/Utils/AssetManager.h @@ -6,6 +6,13 @@ class AssetManager { public: - static std::string getResourcePrefix(); + /** + * @brief Used to load things (might be read-only) + */ static std::filesystem::path getResourcePath(const std::string& resource); + + /** + * @brief Used to save things + */ + static std::filesystem::path getConfigPath(const std::string& resource); }; \ No newline at end of file