Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utilize the user configuration directory for configs and savefiles #307

Merged
merged 11 commits into from
Jun 29, 2023
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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
)

Expand Down
62 changes: 53 additions & 9 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -253,38 +254,81 @@ 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) {
return std::string(fpath);
}
#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, std::string appName) {
return GetAppDirectoryPath(appName) + "/" + path;
}

std::string Context::GetPathRelativeToAppDirectory(const std::string path) {
return GetAppDirectoryPath() + "/" + 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
9 changes: 5 additions & 4 deletions src/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/install_config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define CMAKE_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@"
#cmakedefine NON_PORTABLE
6 changes: 3 additions & 3 deletions src/public/libultra/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -78,4 +78,4 @@ int32_t osRecvMesg(OSMesgQueue* mq, OSMesg* msg, int32_t flag) {
mq->validCount--;
return 0;
}
}
}