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
This commit is contained in:
AltoXorg
2023-06-28 21:23:57 -04:00
committed by GitHub
parent 282d879c20
commit 4012958df6
6 changed files with 69 additions and 17 deletions
+2
View File
@@ -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)
+4 -1
View File
@@ -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
)
+53 -9
View File
@@ -4,6 +4,7 @@
#include <spdlog/async.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#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
+5 -4
View File
@@ -25,10 +25,11 @@ class Context {
const std::unordered_set<uint32_t>& 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();
+2
View File
@@ -0,0 +1,2 @@
#define CMAKE_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@"
#cmakedefine NON_PORTABLE
+3 -3
View File
@@ -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;
}
}
}