Skip to content

Commit

Permalink
Config now lives in ~/Library/Preferences/com.tashcan.startrekpatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tashcan committed Mar 25, 2024
1 parent 3d4cf2f commit a8f2689
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
33 changes: 30 additions & 3 deletions mods/src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@
#include <string>
#include <string_view>

#if !_WIN32
#include "folder_manager.h"
#endif

static auto make_config_path(auto filename, bool create_dir = false)
{
#if !_WIN32
auto ApplicationSupportPath =
(char*)fm::FolderManager::pathForDirectory(fm::NSApplicationSupportDirectory, fm::NSUserDomainMask);
auto LibraryPath = (char*)fm::FolderManager::pathForDirectory(fm::NSLibraryDirectory, fm::NSUserDomainMask);

const auto config_dir = std::filesystem::path(LibraryPath) / "Preferences" / "com.tashcan.startrekpatch";

if (create_dir) {
std::error_code ec;
std::filesystem::create_directories(config_dir, ec);
}
std::filesystem::path config_path = config_dir / filename;
return config_path.u8string();
#else
return filename;
#endif
}

static const eastl::tuple<const char*, int> bannerTypes[] = {
{"Standard", ToastState::Standard},
{"FactionWarning", ToastState::FactionWarning},
Expand Down Expand Up @@ -47,7 +71,10 @@ Config::Config()
void Config::Save(toml::table config, std::string_view filename, bool apply_warning)
{
std::ofstream config_file;
config_file.open(filename);

auto config_path = make_config_path(filename, true);

config_file.open(config_path);
if (apply_warning) {
char buff[44];
snprintf(buff, 44, "%-44s", CONFIG_FILE_DEFAULT);
Expand Down Expand Up @@ -242,7 +269,7 @@ void Config::Load()
toml::table parsed;
bool write_config = false;
try {
config = std::move(toml::parse_file("community_patch_settings.toml"));
config = std::move(toml::parse_file(make_config_path(CONFIG_FILE_DEFAULT)));
write_config = true;
} catch (const toml::parse_error& e) {
spdlog::warn("Failed to load config file, falling back to default settings: {}", e.description());
Expand Down Expand Up @@ -455,7 +482,7 @@ void Config::Load()
parse_config_shortcut(config, parsed, "toggle_cargo_armada", GameFunction::ToggleCargoArmada, "ALT-5");
}

if (!std::filesystem::exists(CONFIG_FILE_DEFAULT)) {
if (!std::filesystem::exists(make_config_path(CONFIG_FILE_RUNTIME))) {
message.str("");
message << "Creating " << CONFIG_FILE_DEFAULT << " (default config file)";
spdlog::warn(message.str());
Expand Down
55 changes: 55 additions & 0 deletions mods/src/folder_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

namespace fm
{
enum {
NSApplicationDirectory = 1,
NSDemoApplicationDirectory,
NSDeveloperApplicationDirectory,
NSAdminApplicationDirectory,
NSLibraryDirectory,
NSDeveloperDirectory,
NSUserDirectory,
NSDocumentationDirectory,
NSDocumentDirectory,
NSCoreServiceDirectory,
NSAutosavedInformationDirectory = 11,
NSDesktopDirectory = 12,
NSCachesDirectory = 13,
NSApplicationSupportDirectory = 14,
NSDownloadsDirectory = 15,
NSInputMethodsDirectory = 16,
NSMoviesDirectory = 17,
NSMusicDirectory = 18,
NSPicturesDirectory = 19,
NSPrinterDescriptionDirectory = 20,
NSSharedPublicDirectory = 21,
NSPreferencePanesDirectory = 22,
NSApplicationScriptsDirectory = 23,
NSItemReplacementDirectory = 99,
NSAllApplicationsDirectory = 100,
NSAllLibrariesDirectory = 101,
NSTrashDirectory = 102
};
typedef unsigned long SearchPathDirectory;

enum {
NSUserDomainMask = 1, // user's home directory --- place to install user's personal items (~)
NSLocalDomainMask =
2, // local to the current machine --- place to install items available to everyone on this machine (/Library)
NSNetworkDomainMask = 4, // publically available location in the local area network --- place to install items
// available on the network (/Network)
NSSystemDomainMask = 8, // provided by Apple, unmodifiable (/System)
NSAllDomainsMask = 0x0ffff // all domains: all of the above and future items
};
typedef unsigned long SearchPathDomainMask;

class FolderManager
{
public:
static const char *pathForDirectory(SearchPathDirectory directory, SearchPathDomainMask domainMask);
static const char *pathForDirectoryAppropriateForItemAtPath(SearchPathDirectory directory,
SearchPathDomainMask domainMask, const char *itemPath,
bool create = false);
};
}; // namespace fm
31 changes: 31 additions & 0 deletions mods/src/folder_manager.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "folder_manager.h"

#import <Foundation/Foundation.h>

using namespace fm;

const char * FolderManager::pathForDirectory(SearchPathDirectory directory, SearchPathDomainMask domainMask) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *URLs = [fileManager URLsForDirectory:(NSSearchPathDirectory)directory inDomains:domainMask];
if (URLs.count == 0) return NULL;

NSURL *URL = [URLs objectAtIndex:0];
NSString *path = URL.path;

// `fileSystemRepresentation` on an `NSString` gives a path suitable for POSIX APIs
return path.fileSystemRepresentation;
}

const char * FolderManager::pathForDirectoryAppropriateForItemAtPath(SearchPathDirectory directory,
SearchPathDomainMask domainMask, const char *itemPath, bool create) {

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *nsPath = [fileManager stringWithFileSystemRepresentation:itemPath length:strlen(itemPath)];
NSURL *itemURL = (nsPath ? [NSURL fileURLWithPath:nsPath] : nil);

NSURL *URL = [fileManager URLForDirectory:(NSSearchPathDirectory)directory
inDomain:domainMask
appropriateForURL:itemURL
create:create error:NULL];
return URL.path.fileSystemRepresentation;
}
1 change: 1 addition & 0 deletions mods/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ do
end
if is_plat("macosx") then
add_cxflags("-fms-extensions")
add_files("src/*.mm")
end
set_policy("build.optimization.lto", true)
end

0 comments on commit a8f2689

Please sign in to comment.