From 4012958df6a95809044d026948876bbbedf22b0a Mon Sep 17 00:00:00 2001 From: AltoXorg <56553686+Alto1772@users.noreply.github.com> Date: Thu, 29 Jun 2023 09:23:57 +0800 Subject: [PATCH] utilize the user configuration directory for configs and savefiles (#307) * add LocateFileAcrossAppDirs and hardcode SHIP_BIN_DIR * clang-format * remove "soh" reference * dont use GetInstance() while uninitialized * wut remove dat * if not init yet use second param for short name * add NON_PORTABLE * use program path * little bit change * refactor "appname" and "last_slash" * also also --- CMakeLists.txt | 2 ++ src/CMakeLists.txt | 5 ++- src/Context.cpp | 62 ++++++++++++++++++++++++++++++++------ src/Context.h | 9 +++--- src/install_config.h.in | 2 ++ src/public/libultra/os.cpp | 6 ++-- 6 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 src/install_config.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 91c6e8a..e0b6894 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.16.0) +option(NON_PORTABLE "Build a non-portable version" OFF) + project(libultraship LANGUAGES C CXX) if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") enable_language(OBJCXX) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 64e000e..79496a1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,9 +9,12 @@ endif() #=================== Top-Level =================== +configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/install_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/install_config.h @ONLY) + set(Source_Files__TopLevel ${CMAKE_CURRENT_SOURCE_DIR}/Context.h ${CMAKE_CURRENT_SOURCE_DIR}/Context.cpp + ${CMAKE_CURRENT_BINARY_DIR}/install_config.h ) source_group("" FILES ${Source_Files__TopLevel}) @@ -351,7 +354,7 @@ endif() #=================== Packages & Includes =================== target_include_directories(libultraship - PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../extern + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../extern ${CMAKE_CURRENT_BINARY_DIR} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../extern/spdlog/include ${CMAKE_CURRENT_SOURCE_DIR}/../extern/stb ) diff --git a/src/Context.cpp b/src/Context.cpp index cddeaf3..97bfdba 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -4,6 +4,7 @@ #include #include #include +#include "install_config.h" #ifdef __APPLE__ #include "utils/OSXFolderManager.h" @@ -253,23 +254,36 @@ std::string Context::GetShortName() { return mShortName; } -std::string Context::GetAppBundlePath() { +std::string Context::GetAppInstallationPath() { +#ifdef NON_PORTABLE + return CMAKE_INSTALL_PREFIX; +#else #ifdef __APPLE__ FolderManager folderManager; return folderManager.getMainBundlePath(); #endif #ifdef __linux__ - char* fpath = std::getenv("SHIP_BIN_DIR"); - if (fpath != NULL) { - return std::string(fpath); + std::string progpath(PATH_MAX, '\0'); + int len = readlink("/proc/self/exe", &progpath[0], progpath.size() - 1); + if (len != -1) { + progpath.resize(len); + + // Find the last '/' and remove everything after it + int lastSlash = progpath.find_last_of("/"); + if (lastSlash != std::string::npos) { + progpath.erase(lastSlash); + } + + return progpath; } #endif return "."; +#endif } -std::string Context::GetAppDirectoryPath() { +std::string Context::GetAppDirectoryPath(std::string appName) { #if defined(__linux__) || defined(__APPLE__) char* fpath = std::getenv("SHIP_HOME"); if (fpath != NULL) { @@ -277,14 +291,44 @@ std::string Context::GetAppDirectoryPath() { } #endif +#ifdef NON_PORTABLE + if (appName.empty()) { + appName = GetInstance()->mShortName; + } + char* prefpath = SDL_GetPrefPath(NULL, appName.c_str()); + if (prefpath != NULL) { + std::string ret(prefpath); + SDL_free(prefpath); + return ret; + } +#endif + return "."; } -std::string Context::GetPathRelativeToAppBundle(const std::string path) { - return GetAppBundlePath() + "/" + path; +std::string Context::GetPathRelativeToAppInstallation(const std::string path) { + return GetAppInstallationPath() + "/" + path; } -std::string Context::GetPathRelativeToAppDirectory(const std::string path) { - return GetAppDirectoryPath() + "/" + path; +std::string Context::GetPathRelativeToAppDirectory(const std::string path, std::string appName) { + return GetAppDirectoryPath(appName) + "/" + path; } + +std::string Context::LocateFileAcrossAppDirs(const std::string path, std::string appName) { + std::string fpath; + + // app configuration dir + fpath = GetPathRelativeToAppDirectory(path, appName); + if (std::filesystem::exists(fpath)) { + return fpath; + } + // app install dir + fpath = GetPathRelativeToAppInstallation(path); + if (std::filesystem::exists(fpath)) { + return fpath; + } + // current dir + return "./" + std::string(path); +} + } // namespace LUS diff --git a/src/Context.h b/src/Context.h index 4ba7468..7604ce9 100644 --- a/src/Context.h +++ b/src/Context.h @@ -25,10 +25,11 @@ class Context { const std::unordered_set& validHashes = {}, uint32_t reservedThreadCount = 1); - static std::string GetAppBundlePath(); - static std::string GetAppDirectoryPath(); - static std::string GetPathRelativeToAppDirectory(const std::string path); - static std::string GetPathRelativeToAppBundle(const std::string path); + static std::string GetAppInstallationPath(); + static std::string GetAppDirectoryPath(std::string appName = ""); + static std::string GetPathRelativeToAppDirectory(const std::string path, std::string appName = ""); + static std::string GetPathRelativeToAppInstallation(const std::string path); + static std::string LocateFileAcrossAppDirs(const std::string path, std::string appName = ""); Context(std::string name, std::string shortName, std::string configFilePath); ~Context(); diff --git a/src/install_config.h.in b/src/install_config.h.in new file mode 100644 index 0000000..543be0a --- /dev/null +++ b/src/install_config.h.in @@ -0,0 +1,2 @@ +#define CMAKE_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@" +#cmakedefine NON_PORTABLE \ No newline at end of file diff --git a/src/public/libultra/os.cpp b/src/public/libultra/os.cpp index 2025557..3c6b912 100644 --- a/src/public/libultra/os.cpp +++ b/src/public/libultra/os.cpp @@ -14,8 +14,8 @@ int32_t osContInit(OSMesgQueue* mq, uint8_t* controllerBits, OSContStatus* statu } #ifndef __SWITCH__ - const char* controllerDb = "gamecontrollerdb.txt"; - int mappingsAdded = SDL_GameControllerAddMappingsFromFile(controllerDb); + std::string controllerDb = LUS::Context::LocateFileAcrossAppDirs("gamecontrollerdb.txt"); + int mappingsAdded = SDL_GameControllerAddMappingsFromFile(controllerDb.c_str()); if (mappingsAdded >= 0) { SPDLOG_INFO("Added SDL game controllers from \"{}\" ({})", controllerDb, mappingsAdded); } else { @@ -78,4 +78,4 @@ int32_t osRecvMesg(OSMesgQueue* mq, OSMesg* msg, int32_t flag) { mq->validCount--; return 0; } -} \ No newline at end of file +}