Skip to content

Commit

Permalink
Improve helper for getting relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Jul 12, 2022
1 parent 477f400 commit 867a6ec
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 27 deletions.
15 changes: 10 additions & 5 deletions libultraship/libultraship/GlobalCtx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Ship {
std::weak_ptr<GlobalCtx2> GlobalCtx2::Context;
ModManager* INSTANCE;

std::shared_ptr<GlobalCtx2> GlobalCtx2::GetInstance() {
return Context.lock();
}
Expand All @@ -32,7 +33,7 @@ namespace Ship {
return GetInstance();
}

std::string GlobalCtx2::GetAppDirectoryPath() {
std::string GlobalCtx2::AppDirectoryPath() {
#ifdef __APPLE__
FolderManager folderManager;
std::string fpath = std::string(folderManager.pathForDirectory(NSApplicationSupportDirectory, NSUserDomainMask));
Expand All @@ -44,6 +45,10 @@ namespace Ship {

}

std::string GlobalCtx2::PathRelativeToAppDirectory(const char* path) {
return GlobalCtx2::AppDirectoryPath() + "/" + path;
}

GlobalCtx2::GlobalCtx2(const std::string& Name) : Name(Name), MainPath(""), PatchesPath("") {

}
Expand All @@ -55,14 +60,14 @@ namespace Ship {

void GlobalCtx2::InitWindow() {
InitLogging();
Config = std::make_shared<ConfigFile>(GlobalCtx2::GetInstance(), GetAppDirectoryPath() + "/shipofharkinian.ini");
Config = std::make_shared<ConfigFile>(GlobalCtx2::GetInstance(), PathRelativeToAppDirectory("shipofharkinian.ini"));
MainPath = (*Config)["ARCHIVE"]["Main Archive"];
if (MainPath.empty()) {
MainPath = GetAppDirectoryPath() + "/oot.otr";
MainPath = PathRelativeToAppDirectory("oot.otr");
}
PatchesPath = (*Config)["ARCHIVE"]["Patches Directory"];
if (PatchesPath.empty()) {
PatchesPath = GetAppDirectoryPath() + "/";
PatchesPath = AppDirectoryPath() + "/";
}
ResMan = std::make_shared<ResourceMgr>(GlobalCtx2::GetInstance(), MainPath, PatchesPath);
Win = std::make_shared<Window>(GlobalCtx2::GetInstance());
Expand All @@ -82,7 +87,7 @@ namespace Ship {

void GlobalCtx2::InitLogging() {
try {
auto logPath = GetAppDirectoryPath() + "/logs/" + GetName() + ".log";
auto logPath = PathRelativeToAppDirectory(("logs/" + GetName() + ".log").c_str());

// Setup Logging
spdlog::init_thread_pool(8192, 1);
Expand Down
4 changes: 3 additions & 1 deletion libultraship/libultraship/GlobalCtx2.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ namespace Ship {
static std::shared_ptr<GlobalCtx2> CreateInstance(const std::string& Name);

std::string GetName() { return Name; }
std::string GetAppDirectoryPath();
std::shared_ptr<Window> GetWindow() { return Win; }
std::shared_ptr<ResourceMgr> GetResourceManager() { return ResMan; }
std::shared_ptr<spdlog::logger> GetLogger() { return Logger; }
std::shared_ptr<ConfigFile> GetConfig() { return Config; }

static std::string AppDirectoryPath();
static std::string PathRelativeToAppDirectory(const char* path);

void WriteSaveFile(std::filesystem::path savePath, uintptr_t addr, void* dramAddr, size_t size);
void ReadSaveFile(std::filesystem::path savePath, uintptr_t addr, void* dramAddr, size_t size);

Expand Down
5 changes: 5 additions & 0 deletions libultraship/libultraship/ImGuiImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ namespace SohImGui {
SohImGui::overlay->TextDrawNotification(30.0f, true, "Press F1 to access enhancements menu");
}

auto imguiIniPath = Ship::GlobalCtx2::PathRelativeToAppDirectory("imgui.ini");
auto imguiLogPath = Ship::GlobalCtx2::PathRelativeToAppDirectory("imgui_log.txt");
io->IniFilename = strcpy(new char[imguiIniPath.length() + 1], imguiIniPath.c_str());
io->LogFilename = strcpy(new char[imguiLogPath.length() + 1], imguiLogPath.c_str());

if (UseViewports()) {
io->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
}
Expand Down
8 changes: 2 additions & 6 deletions libultraship/libultraship/Lib/ImGui/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,6 @@ CODE
#include <TargetConditionals.h>
#endif

#include "GlobalCtx2.h"

// Visual Studio warnings
#ifdef _MSC_VER
#pragma warning (disable: 4127) // condition expression is constant
Expand Down Expand Up @@ -1146,16 +1144,14 @@ ImGuiIO::ImGuiIO()
memset(this, 0, sizeof(*this));
IM_STATIC_ASSERT(IM_ARRAYSIZE(ImGuiIO::MouseDown) == ImGuiMouseButton_COUNT && IM_ARRAYSIZE(ImGuiIO::MouseClicked) == ImGuiMouseButton_COUNT);

auto appDirectoryPath = Ship::GlobalCtx2::GetInstance()->GetAppDirectoryPath();

// Settings
ConfigFlags = ImGuiConfigFlags_None;
BackendFlags = ImGuiBackendFlags_None;
DisplaySize = ImVec2(-1.0f, -1.0f);
DeltaTime = 1.0f / 60.0f;
IniSavingRate = 5.0f;
IniFilename = (appDirectoryPath + "/imgui.ini").c_str(); // Important: "imgui.ini" is relative to current working dir, most apps will want to lock this to an absolute path (e.g. same path as executables).
LogFilename = (appDirectoryPath + "/imgui_log.txt").c_str();
IniFilename = "imgui.ini"; // Important: "imgui.ini" is relative to current working dir, most apps will want to lock this to an absolute path (e.g. same path as executables).
LogFilename = "imgui_log.txt";
MouseDoubleClickTime = 0.30f;
MouseDoubleClickMaxDist = 6.0f;
#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO
Expand Down
9 changes: 6 additions & 3 deletions soh/soh/Enhancements/debugconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::PathRelativeToAppDirectory("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, " = ");
Expand Down Expand Up @@ -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::PathRelativeToAppDirectory("cvars.cfg");
File::WriteAllText(cvarsConfig, output);
}
5 changes: 2 additions & 3 deletions soh/soh/OTRGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ OTRGlobals* OTRGlobals::Instance;
SaveManager* SaveManager::Instance;

OTRGlobals::OTRGlobals() {

context = Ship::GlobalCtx2::CreateInstance("Ship of Harkinian");
gSaveStateMgr = std::make_shared<SaveStateMgr>();
context->GetWindow()->Init();
Expand Down Expand Up @@ -102,7 +101,7 @@ extern "C" void OTRExtScanner() {

extern "C" void InitOTR() {
OTRGlobals::Instance = new OTRGlobals();
SaveManager::Instance = new SaveManager(OTRGlobals::Instance->context->GetAppDirectoryPath());
SaveManager::Instance = new SaveManager();
auto t = OTRGlobals::Instance->context->GetResourceManager()->LoadFile("version");

if (!t->bHasLoadError)
Expand Down Expand Up @@ -1149,7 +1148,7 @@ std::filesystem::path GetSaveFile(Ship::ConfigFile& Conf) {
std::string fileName = Conf.get("SAVE").get("Save Filename");

if (fileName.empty()) {
Conf["SAVE"]["Save Filename"] = OTRGlobals::Instance->context->GetAppDirectoryPath() + "/oot_save.sav";
Conf["SAVE"]["Save Filename"] = Ship::GlobalCtx2::PathRelativeToAppDirectory("oot_save.sav");
Conf.Save();
}
std::filesystem::path saveFile = std::filesystem::absolute(fileName);
Expand Down
12 changes: 5 additions & 7 deletions soh/soh/SaveManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
extern "C" SaveContext gSaveContext;

std::filesystem::path SaveManager::GetFileName(int fileNum) {
const std::filesystem::path sSavePath(appDirectoryPath + "/Save");
const std::filesystem::path sSavePath(Ship::GlobalCtx2::PathRelativeToAppDirectory("Save"));
return sSavePath / ("file" + std::to_string(fileNum + 1) + ".sav");
}

SaveManager::SaveManager(std::string appDirectoryPath) {
this->appDirectoryPath = appDirectoryPath;

SaveManager::SaveManager() {
AddLoadFunction("base", 1, LoadBaseVersion1);

AddSaveFunction("base", 1, SaveBase);
Expand All @@ -44,10 +42,10 @@ SaveManager::SaveManager(std::string appDirectoryPath) {
}

void SaveManager::Init() {
const std::filesystem::path sSavePath(appDirectoryPath + "/Save");
const std::filesystem::path sSavePath(Ship::GlobalCtx2::PathRelativeToAppDirectory("Save"));
const std::filesystem::path sGlobalPath = sSavePath / std::string("global.sav");
auto sOldSavePath = appDirectoryPath + "/oot_save.sav";
auto sOldBackupSavePath = appDirectoryPath + "/oot_save.bak";
auto sOldSavePath = Ship::GlobalCtx2::PathRelativeToAppDirectory("oot_save.sav");
auto sOldBackupSavePath = Ship::GlobalCtx2::PathRelativeToAppDirectory("oot_save.bak");

// If the save directory does not exist, create it
if (!std::filesystem::exists(sSavePath)) {
Expand Down
3 changes: 1 addition & 2 deletions soh/soh/SaveManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SaveManager {
using SaveFunc = void(*)();
using PostFunc = void(*)(int version);

SaveManager(std::string appDirectoryPath);
SaveManager();

void Init();
void InitFile(bool isDebug);
Expand Down Expand Up @@ -114,7 +114,6 @@ class SaveManager {
static void LoadBaseVersion1();
static void SaveBase();

std::string appDirectoryPath;
std::vector<InitFunc> initFuncs;

using SectionLoadHandler = std::map<int, LoadFunc>;
Expand Down

0 comments on commit 867a6ec

Please sign in to comment.