Introduce App Directory Path (#572)

* Introduce app directory path concept

* macos: Remove hacky way of using applicaiton directory

* Update the new SaveManager

* Address stack user after return

* Remove unecessary property

* Use std::string for filepath

* Improve clang specific detections

* Use new path system for imgui files

* Improve helper for getting relative paths
This commit is contained in:
David Chavez 2022-07-13 06:19:07 +02:00 committed by GitHub
commit 7b04f67884
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 186 additions and 70 deletions

View file

@ -120,6 +120,7 @@ ifeq ($(UNAME), Darwin) #APPLE
LDLIBS += \
$(addprefix -framework , \
OpenGL \
Foundation \
) \
$(shell sdl2-config --libs) $(shell pkg-config --libs glew)
endif
@ -224,9 +225,6 @@ appbundle: macosx/$(APPNAME).icns
cp macosx/PkgInfo $(APPBUNDLECONTENTS)/
cp macosx/$(APPNAME).icns $(APPBUNDLEICON)/
cp $(TARGET) $(APPBUNDLEEXE)/soh
cp macosx/launcher.sh $(APPBUNDLEEXE)/launcher.sh
clang -ObjC macosx/appsupport.m -arch arm64 -arch x86_64 -framework Foundation -o macosx/appsupport
cp macosx/appsupport $(APPBUNDLEEXE)/appsupport
otool -l $(TARGET) | grep -A 2 LC_RPATH | tail -n 1 | awk '{print $2}' | dylibbundler -od -b -x $(APPBUNDLEEXE)/soh -d $(APPBUNDLECONTENTS)/libs
macosx/$(APPNAME).icns: macosx/$(APPNAME)Icon.png
@ -244,7 +242,3 @@ macosx/$(APPNAME).icns: macosx/$(APPNAME)Icon.png
cp macosx/$(APPNAME)Icon.png macosx/$(APPNAME).iconset/icon_512x512@2x.png
iconutil -c icns -o macosx/$(APPNAME).icns macosx/$(APPNAME).iconset
rm -r macosx/$(APPNAME).iconset
filledappbundle: appbundle
cp ./oot.otr $(APPBUNDLEEXE)/oot.otr

View file

@ -7,7 +7,7 @@
<key>CFBundleName</key>
<string>Ship of Harkinian</string>
<key>CFBundleExecutable</key>
<string>launcher.sh</string>
<string>soh</string>
<key>CFBundleGetInfoString</key>
<string>2.0.0</string>
<key>CFBundleIconFile</key>

View file

@ -1,26 +0,0 @@
#import <Foundation/Foundation.h>
int main(void) {
NSString *appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject];
//If there isn't an App Support Directory yet ...
if (![[NSFileManager defaultManager] fileExistsAtPath:appSupportDir isDirectory:NULL]) {
NSError *error = nil;
//Create one
if (![[NSFileManager defaultManager] createDirectoryAtPath:appSupportDir withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"%@", error.localizedDescription);
}
else {
// *** OPTIONAL *** Mark the directory as excluded from iCloud backups
NSURL *url = [NSURL fileURLWithPath:appSupportDir];
if (![url setResourceValue:@YES
forKey:NSURLIsExcludedFromBackupKey
error:&error])
{
NSLog(@"Error excluding %@ from backup %@", url.lastPathComponent, error.localizedDescription);
}
else {
NSLog(@"Yay");
}
}
}
printf("%s\n", [appSupportDir UTF8String]);
}

View file

@ -1,9 +0,0 @@
#!/bin/bash
APPPATH="${0%/*}"
cd "${APPPATH}"
APPPATH=$(pwd)
APPSUPPORT=$(./appsupport)
mkdir -p "${APPSUPPORT}/com.shipofharkinian.soh"
cd "${APPSUPPORT}/com.shipofharkinian.soh"
cp "${APPPATH}/oot.otr" .
${APPPATH}/soh

View file

@ -500,8 +500,9 @@ template <typename Numeric> bool is_number(const std::string& s) {
void DebugConsole_LoadCVars()
{
if (File::Exists("cvars.cfg")) {
const auto lines = File::ReadAllLines("cvars.cfg");
auto cvarsConfig = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("cvars.cfg");
if (File::Exists(cvarsConfig)) {
const auto lines = File::ReadAllLines(cvarsConfig);
for (const std::string& line : lines) {
std::vector<std::string> cfg = StringHelper::Split(line, " = ");
@ -535,5 +536,7 @@ void DebugConsole_SaveCVars()
output += StringHelper::Sprintf("%s = %f\n", cvar.first.c_str(), cvar.second->value.valueFloat);
}
File::WriteAllText("cvars.cfg", output);
auto cvarsConfig = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("cvars.cfg");
File::WriteAllText(cvarsConfig, output);
}

View file

@ -57,7 +57,6 @@ OTRGlobals* OTRGlobals::Instance;
SaveManager* SaveManager::Instance;
OTRGlobals::OTRGlobals() {
context = Ship::GlobalCtx2::CreateInstance("Ship of Harkinian");
gSaveStateMgr = std::make_shared<SaveStateMgr>();
gRandomizer = std::make_shared<Randomizer>();
@ -1158,7 +1157,7 @@ std::filesystem::path GetSaveFile(Ship::ConfigFile& Conf) {
std::string fileName = Conf.get("SAVE").get("Save Filename");
if (fileName.empty()) {
Conf["SAVE"]["Save Filename"] = "oot_save.sav";
Conf["SAVE"]["Save Filename"] = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("oot_save.sav");
Conf.Save();
}
std::filesystem::path saveFile = std::filesystem::absolute(fileName);

View file

@ -1,4 +1,5 @@
#include "SaveManager.h"
#include "OTRGlobals.h"
#include "z64.h"
#include "functions.h"
@ -14,11 +15,9 @@
extern "C" SaveContext gSaveContext;
static const std::filesystem::path sSavePath("Save"); // TODO maybe let this be user-configurable?
static const std::filesystem::path sGlobalPath = sSavePath / "global.sav";
std::filesystem::path SaveManager::GetFileName(int fileNum) {
return sSavePath / (std::string("file") + std::to_string(fileNum + 1) + ".sav");
const std::filesystem::path sSavePath(Ship::GlobalCtx2::GetPathRelativeToAppDirectory("Save"));
return sSavePath / ("file" + std::to_string(fileNum + 1) + ".sav");
}
SaveManager::SaveManager() {
@ -128,15 +127,20 @@ void SaveManager::SaveRandomizer() {
}
void SaveManager::Init() {
const std::filesystem::path sSavePath(Ship::GlobalCtx2::GetPathRelativeToAppDirectory("Save"));
const std::filesystem::path sGlobalPath = sSavePath / std::string("global.sav");
auto sOldSavePath = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("oot_save.sav");
auto sOldBackupSavePath = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("oot_save.bak");
// If the save directory does not exist, create it
if (!std::filesystem::exists(sSavePath)) {
std::filesystem::create_directory(sSavePath);
}
// If there is a lingering unversioned save, convert it
if (std::filesystem::exists("oot_save.sav")) {
if (std::filesystem::exists(sOldSavePath)) {
ConvertFromUnversioned();
std::filesystem::rename("oot_save.sav", "oot_save.bak");
std::filesystem::rename(sOldSavePath, sOldBackupSavePath);
}
// If the global save file exist, load it. Otherwise, create it.