Skip to content

Commit 7710d91

Browse files
dcvzbriaguya0
authored andcommitted
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
1 parent d44b172 commit 7710d91

File tree

16 files changed

+186
-70
lines changed

16 files changed

+186
-70
lines changed

BUILDING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ make setup -j8 DEBUG=0
6363
# Compile the code (watch the -j parameter as above)
6464
make -j8 DEBUG=0
6565
# Create macOS app bundle
66-
make filledappbundle
66+
make appbundle
6767
```
68-
9. Launch soh app in the soh folder!
68+
9. Copy your OTR file to ~/Library/Application\ Support/com.shipofharkinian.soh
69+
10. Launch soh app in the soh folder!
6970

7071
# Compatible Roms
7172
```

ZAPDTR/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ LDFLAGS := -lm -ldl \
4949
-L../StormLib/build -L../libultraship -lbz2 -pthread -lultraship -lstorm
5050

5151
ifeq ($(UNAME), Darwin)
52-
LDFLAGS += $(shell pkg-config --libs glew libpng zlib) $(shell sdl2-config --libs) -framework OpenGL
52+
LDFLAGS += $(shell pkg-config --libs glew libpng zlib) $(shell sdl2-config --libs) -framework OpenGL -framework Foundation
5353
INC += $(shell pkg-config --cflags libpng)
5454
else
5555
LDFLAGS += -lpng -lGL -lGLEW -lX11 -lz -lSDL2 -lpulse

libultraship/Makefile

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
CXX ?= g++
44
CC ?= gcc
5-
AR := ar
5+
AR := ar
66
FORMAT := clang-format-11
77
UNAME := $(shell uname)
88

@@ -11,6 +11,15 @@ DEBUG ?= 1
1111
OPTFLAGS ?= -O0
1212
LTO ?= 0
1313

14+
# flag to save whether the compiler being used is clang or gcc by checking CXX --version
15+
CXX_IS_CLANG ?= $(shell $(CXX) --version | grep -c clang)
16+
ifeq ($(CXX_IS_CLANG),1)
17+
MXX := $(CXX)
18+
else
19+
MXX ?= clang++
20+
endif
21+
22+
1423
WARN := -Wall -Wextra -Werror \
1524
-Wno-unused-variable \
1625
-Wno-unused-parameter \
@@ -29,8 +38,7 @@ WARN := -Wall -Wextra -Werror \
2938
CWARN :=
3039
CXXWARN := -Wno-deprecated-enum-enum-conversion -Wno-deprecated-copy
3140

32-
COMPILER_VERSION := $(shell $(CXX) --version)
33-
ifneq '' '$(findstring g++,$(COMPILER_VERSION))'
41+
ifneq ($(CXX_IS_CLANG),1)
3442
WARN += -Wno-error=stringop-overflow
3543
CXXWARN += -Wno-error=maybe-uninitialized
3644
endif
@@ -39,12 +47,17 @@ CXXFLAGS := $(WARN) $(CXXWARN) -std=c++20 -D_GNU_SOURCE -DENABLE_OPENGL -DSPDLOG
3947
CFLAGS := $(WARN) $(CWARN) -std=c99 -D_GNU_SOURCE -DENABLE_OPENGL -DSPDLOG_ACTIVE_LEVEL=0
4048
CPPFLAGS := -MMD
4149

42-
ifeq ($(UNAME), Darwin) #APPLE
43-
CPPFLAGS += $(shell pkg-config --cflags sdl2 glew) -framework OpenGL
50+
MMFLAGS := -Wno-deprecated-declarations -ObjC++ -fobjc-weak -fobjc-arc
51+
52+
# if not using clang, ask clang to use gcc standard library
53+
ifneq ($(CXX_IS_CLANG),1)
54+
STD_ISYSTEM=$(shell ${CXX} -xc++ -E -v - < /dev/null 2>&1 | grep "> search starts here" -A2 | tail -n 2 | head -n 1)
55+
CXX_ISYSTEM=$(shell ${CXX} -xc++ -E -v - < /dev/null 2>&1 | grep "> search starts here" -A2 | tail -n 2 | tail -n 1)
56+
MMFLAGS += -stdlib++-isystem ${STD_ISYSTEM} -cxx-isystem ${CXX_ISYSTEM}
4457
endif
4558

46-
ifeq ($(UNAME), Linux)
47-
WARN += -Wno-error=stringop-overflow # This is required with clang on Linux.
59+
ifeq ($(UNAME), Darwin) #APPLE
60+
CPPFLAGS += $(shell pkg-config --cflags sdl2 glew) -framework OpenGL -framework Foundation
4861
endif
4962

5063
ifneq ($(DEBUG),0)
@@ -78,12 +91,19 @@ C_FILES := \
7891
libultraship/mixer.c \
7992
libultraship/Lib/stb/stb_impl.c
8093

94+
MM_FILES := \
95+
libultraship/OSXFolderManager.mm
96+
8197
FMT_FILES := $(shell find libultraship/ -type f \( -name "*.cpp" -o -name "*.h" \) -a -not -path "libultraship/Lib/*")
8298

8399
O_FILES := \
84100
$(CXX_FILES:%.cpp=build/%.o) \
85101
$(C_FILES:%.c=build/%.o)
86102

103+
ifeq ($(UNAME), Darwin) #APPLE
104+
O_FILES += $(MM_FILES:%.mm=build/%.o)
105+
endif
106+
87107
D_FILES := $(O_FILES:%.o=%.d)
88108

89109
LIB := libultraship.a
@@ -117,6 +137,9 @@ build/%.o: %.cpp
117137
build/%.o: %.c
118138
$(CC) $(CFLAGS) $(CPPFLAGS) $(OPTFLAGS) $(INC_DIRS) -c $< -o $@
119139

140+
build/%.o: %.mm
141+
$(MXX) $(MMFLAGS) $(CXXFLAGS) $(OPTFLAGS) $(INC_DIRS) -c $< -o $@
142+
120143
$(LIB): $(O_FILES)
121144
$(AR) rcs $@ $^
122145

libultraship/libultraship/ConfigFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ namespace Ship {
5656
}
5757

5858
bool ConfigFile::CreateDefaultConfig() {
59-
(*this)["ARCHIVE"]["Main Archive"] = "oot.otr";
59+
(*this)["ARCHIVE"]["Main Archive"] = "";
6060
(*this)["ARCHIVE"]["Patches Directory"] = "";
6161

62-
(*this)["SAVE"]["Save Filename"] = "oot_save.sav";
62+
(*this)["SAVE"]["Save Filename"] = "";
6363

6464
(*this)["CONTROLLERS"]["CONTROLLER 1"] = "Auto";
6565
(*this)["CONTROLLERS"]["CONTROLLER 2"] = "Unplugged";

libultraship/libultraship/GlobalCtx2.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
#include "spdlog/sinks/stdout_color_sinks.h"
99
#include "spdlog/sinks/sohconsole_sink.h"
1010
#include "ModManager.h"
11+
#ifdef __APPLE__
12+
#include "OSXFolderManager.h"
13+
#endif
1114

1215
namespace Ship {
1316
std::weak_ptr<GlobalCtx2> GlobalCtx2::Context;
1417
ModManager* INSTANCE;
18+
1519
std::shared_ptr<GlobalCtx2> GlobalCtx2::GetInstance() {
1620
return Context.lock();
1721
}
@@ -29,6 +33,22 @@ namespace Ship {
2933
return GetInstance();
3034
}
3135

36+
std::string GlobalCtx2::GetAppDirectoryPath() {
37+
#ifdef __APPLE__
38+
FolderManager folderManager;
39+
std::string fpath = std::string(folderManager.pathForDirectory(NSApplicationSupportDirectory, NSUserDomainMask));
40+
fpath.append("/com.shipofharkinian.soh");
41+
return fpath;
42+
#endif
43+
44+
return ".";
45+
46+
}
47+
48+
std::string GlobalCtx2::GetPathRelativeToAppDirectory(const char* path) {
49+
return GlobalCtx2::GetAppDirectoryPath() + "/" + path;
50+
}
51+
3252
GlobalCtx2::GlobalCtx2(const std::string& Name) : Name(Name), MainPath(""), PatchesPath("") {
3353

3454
}
@@ -40,14 +60,14 @@ namespace Ship {
4060

4161
void GlobalCtx2::InitWindow() {
4262
InitLogging();
43-
Config = std::make_shared<ConfigFile>(GlobalCtx2::GetInstance(), "shipofharkinian.ini");
63+
Config = std::make_shared<ConfigFile>(GlobalCtx2::GetInstance(), GetPathRelativeToAppDirectory("shipofharkinian.ini"));
4464
MainPath = (*Config)["ARCHIVE"]["Main Archive"];
4565
if (MainPath.empty()) {
46-
MainPath = "oot.otr";
66+
MainPath = GetPathRelativeToAppDirectory("oot.otr");
4767
}
4868
PatchesPath = (*Config)["ARCHIVE"]["Patches Directory"];
4969
if (PatchesPath.empty()) {
50-
PatchesPath = "./";
70+
PatchesPath = GetAppDirectoryPath() + "/";
5171
}
5272
ResMan = std::make_shared<ResourceMgr>(GlobalCtx2::GetInstance(), MainPath, PatchesPath);
5373
Win = std::make_shared<Window>(GlobalCtx2::GetInstance());
@@ -67,11 +87,13 @@ namespace Ship {
6787

6888
void GlobalCtx2::InitLogging() {
6989
try {
90+
auto logPath = GetPathRelativeToAppDirectory(("logs/" + GetName() + ".log").c_str());
91+
7092
// Setup Logging
7193
spdlog::init_thread_pool(8192, 1);
7294
auto SohConsoleSink = std::make_shared<spdlog::sinks::soh_sink_mt>();
7395
auto ConsoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
74-
auto FileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("logs/" + GetName() + ".log", 1024 * 1024 * 10, 10);
96+
auto FileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logPath, 1024 * 1024 * 10, 10);
7597
SohConsoleSink->set_level(spdlog::level::trace);
7698
ConsoleSink->set_level(spdlog::level::trace);
7799
FileSink->set_level(spdlog::level::trace);

libultraship/libultraship/GlobalCtx2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ namespace Ship {
2424
std::shared_ptr<spdlog::logger> GetLogger() { return Logger; }
2525
std::shared_ptr<ConfigFile> GetConfig() { return Config; }
2626

27+
static std::string GetAppDirectoryPath();
28+
static std::string GetPathRelativeToAppDirectory(const char* path);
29+
2730
void WriteSaveFile(std::filesystem::path savePath, uintptr_t addr, void* dramAddr, size_t size);
2831
void ReadSaveFile(std::filesystem::path savePath, uintptr_t addr, void* dramAddr, size_t size);
2932

libultraship/libultraship/ImGuiImpl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ namespace SohImGui {
304304
SohImGui::overlay->TextDrawNotification(30.0f, true, "Press F1 to access enhancements menu");
305305
}
306306

307+
auto imguiIniPath = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("imgui.ini");
308+
auto imguiLogPath = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("imgui_log.txt");
309+
io->IniFilename = strcpy(new char[imguiIniPath.length() + 1], imguiIniPath.c_str());
310+
io->LogFilename = strcpy(new char[imguiLogPath.length() + 1], imguiLogPath.c_str());
311+
307312
if (UseViewports()) {
308313
io->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
309314
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// OSXFolderManager.h
3+
// libultraship
4+
//
5+
// Created by David Chavez on 28.06.22.
6+
//
7+
8+
#ifndef OSXFolderManager_h
9+
#define OSXFolderManager_h
10+
11+
#include <stdio.h>
12+
namespace Ship {
13+
enum {
14+
NSApplicationDirectory = 1,
15+
NSDemoApplicationDirectory,
16+
NSDeveloperApplicationDirectory,
17+
NSAdminApplicationDirectory,
18+
NSLibraryDirectory,
19+
NSDeveloperDirectory,
20+
NSUserDirectory,
21+
NSDocumentationDirectory,
22+
NSDocumentDirectory,
23+
NSCoreServiceDirectory,
24+
NSAutosavedInformationDirectory = 11,
25+
NSDesktopDirectory = 12,
26+
NSCachesDirectory = 13,
27+
NSApplicationSupportDirectory = 14,
28+
NSDownloadsDirectory = 15,
29+
NSInputMethodsDirectory = 16,
30+
NSMoviesDirectory = 17,
31+
NSMusicDirectory = 18,
32+
NSPicturesDirectory = 19,
33+
NSPrinterDescriptionDirectory = 20,
34+
NSSharedPublicDirectory = 21,
35+
NSPreferencePanesDirectory = 22,
36+
NSApplicationScriptsDirectory = 23,
37+
NSItemReplacementDirectory = 99,
38+
NSAllApplicationsDirectory = 100,
39+
NSAllLibrariesDirectory = 101,
40+
NSTrashDirectory = 102
41+
};
42+
typedef unsigned long SearchPathDirectory;
43+
44+
enum {
45+
NSUserDomainMask = 1, // user's home directory --- place to install user's personal items (~)
46+
NSLocalDomainMask = 2, // local to the current machine --- place to install items available to everyone on this machine (/Library)
47+
NSNetworkDomainMask = 4, // publically available location in the local area network --- place to install items available on the network (/Network)
48+
NSSystemDomainMask = 8, // provided by Apple, unmodifiable (/System)
49+
NSAllDomainsMask = 0x0ffff // all domains: all of the above and future items
50+
};
51+
typedef unsigned long SearchPathDomainMask;
52+
53+
class FolderManager {
54+
public:
55+
const char *pathForDirectory(SearchPathDirectory directory, SearchPathDomainMask domainMask);
56+
const char *pathForDirectoryAppropriateForItemAtPath(SearchPathDirectory directory, SearchPathDomainMask domainMask, const char *itemPath, bool create = false);
57+
};
58+
};
59+
60+
#endif /* OSXFolderManager_h */
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// OSXFolderManager.m
3+
// libultraship
4+
//
5+
// Created by David Chavez on 28.06.22.
6+
//
7+
8+
#include "OSXFolderManager.h"
9+
#import <Foundation/Foundation.h>
10+
11+
using namespace Ship;
12+
13+
const char * FolderManager::pathForDirectory(SearchPathDirectory directory, SearchPathDomainMask domainMask) {
14+
NSFileManager *fileManager = [NSFileManager defaultManager];
15+
NSArray *URLs = [fileManager URLsForDirectory:(NSSearchPathDirectory)directory inDomains:domainMask];
16+
if (URLs.count == 0) return NULL;
17+
18+
NSURL *URL = [URLs objectAtIndex:0];
19+
NSString *path = URL.path;
20+
21+
// `fileSystemRepresentation` on an `NSString` gives a path suitable for POSIX APIs
22+
return path.fileSystemRepresentation;
23+
}
24+
25+
const char * FolderManager::pathForDirectoryAppropriateForItemAtPath(SearchPathDirectory directory,
26+
SearchPathDomainMask domainMask, const char *itemPath, bool create) {
27+
28+
NSFileManager *fileManager = [NSFileManager defaultManager];
29+
NSString *nsPath = [fileManager stringWithFileSystemRepresentation:itemPath length:strlen(itemPath)];
30+
NSURL *itemURL = (nsPath ? [NSURL fileURLWithPath:nsPath] : nil);
31+
32+
NSURL *URL = [fileManager URLForDirectory:(NSSearchPathDirectory)directory
33+
inDomain:domainMask
34+
appropriateForURL:itemURL
35+
create:create error:NULL];
36+
return URL.path.fileSystemRepresentation;
37+
}

soh/Makefile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ ifeq ($(UNAME), Darwin) #APPLE
120120
LDLIBS += \
121121
$(addprefix -framework , \
122122
OpenGL \
123+
Foundation \
123124
) \
124125
$(shell sdl2-config --libs) $(shell pkg-config --libs glew)
125126
endif
@@ -224,9 +225,6 @@ appbundle: macosx/$(APPNAME).icns
224225
cp macosx/PkgInfo $(APPBUNDLECONTENTS)/
225226
cp macosx/$(APPNAME).icns $(APPBUNDLEICON)/
226227
cp $(TARGET) $(APPBUNDLEEXE)/soh
227-
cp macosx/launcher.sh $(APPBUNDLEEXE)/launcher.sh
228-
clang -ObjC macosx/appsupport.m -arch arm64 -arch x86_64 -framework Foundation -o macosx/appsupport
229-
cp macosx/appsupport $(APPBUNDLEEXE)/appsupport
230228
otool -l $(TARGET) | grep -A 2 LC_RPATH | tail -n 1 | awk '{print $2}' | dylibbundler -od -b -x $(APPBUNDLEEXE)/soh -d $(APPBUNDLECONTENTS)/libs
231229

232230
macosx/$(APPNAME).icns: macosx/$(APPNAME)Icon.png
@@ -244,7 +242,3 @@ macosx/$(APPNAME).icns: macosx/$(APPNAME)Icon.png
244242
cp macosx/$(APPNAME)Icon.png macosx/$(APPNAME).iconset/[email protected]
245243
iconutil -c icns -o macosx/$(APPNAME).icns macosx/$(APPNAME).iconset
246244
rm -r macosx/$(APPNAME).iconset
247-
248-
filledappbundle: appbundle
249-
cp ./oot.otr $(APPBUNDLEEXE)/oot.otr
250-

0 commit comments

Comments
 (0)