Fix user data path on macOS.

This commit is contained in:
squidbus 2024-07-20 03:03:09 -07:00 committed by TheTurtle
parent 225ca3ac5b
commit 43c0f313f2
2 changed files with 38 additions and 10 deletions

View File

@ -4,6 +4,12 @@
#include <unordered_map> #include <unordered_map>
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/path_util.h" #include "common/path_util.h"
#include "common/scope_exit.h"
#ifdef __APPLE__
#include <CoreFoundation/CFBundle.h>
#include <sys/param.h>
#endif
#ifndef MAX_PATH #ifndef MAX_PATH
#ifdef _WIN32 #ifdef _WIN32
@ -19,7 +25,36 @@ namespace Common::FS {
namespace fs = std::filesystem; namespace fs = std::filesystem;
#ifdef __APPLE__
static std::filesystem::path GetBundleParentDirectory() {
if (CFBundleRef bundle_ref = CFBundleGetMainBundle()) {
if (CFURLRef bundle_url_ref = CFBundleCopyBundleURL(bundle_ref)) {
SCOPE_EXIT {
CFRelease(bundle_url_ref);
};
if (CFStringRef bundle_path_ref =
CFURLCopyFileSystemPath(bundle_url_ref, kCFURLPOSIXPathStyle)) {
SCOPE_EXIT {
CFRelease(bundle_path_ref);
};
char app_bundle_path[MAXPATHLEN];
if (CFStringGetFileSystemRepresentation(bundle_path_ref, app_bundle_path,
sizeof(app_bundle_path))) {
std::filesystem::path bundle_path{app_bundle_path};
return bundle_path.parent_path();
}
}
}
}
return std::filesystem::current_path();
}
#endif
static auto UserPaths = [] { static auto UserPaths = [] {
#ifdef __APPLE__
std::filesystem::current_path(GetBundleParentDirectory());
#endif
std::unordered_map<PathType, fs::path> paths; std::unordered_map<PathType, fs::path> paths;
const auto user_dir = std::filesystem::current_path() / PORTABLE_DIR; const auto user_dir = std::filesystem::current_path() / PORTABLE_DIR;

View File

@ -18,16 +18,9 @@ int main(int argc, char* argv[]) {
QApplication a(argc, argv); QApplication a(argc, argv);
// Load configurations and initialize Qt application // Load configurations and initialize Qt application
const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
Config::load(config_dir / "config.toml"); Config::load(user_dir / "config.toml");
QString gameDataPath = QDir::currentPath() + "/user/game_data/"; std::filesystem::create_directory(user_dir / "game_data");
std::string stdStr = gameDataPath.toStdString();
std::filesystem::path path(stdStr);
#ifdef _WIN64
std::wstring wstdStr = gameDataPath.toStdWString();
path = std::filesystem::path(wstdStr);
#endif
std::filesystem::create_directory(path);
// Check if the game install directory is set // Check if the game install directory is set
if (Config::getGameInstallDir() == "") { if (Config::getGameInstallDir() == "") {