From 867a6ec7719b7a400fba18447bf23f71bf08a943 Mon Sep 17 00:00:00 2001 From: David Chavez Date: Tue, 12 Jul 2022 10:53:21 +0200 Subject: [PATCH] Improve helper for getting relative paths --- libultraship/libultraship/GlobalCtx2.cpp | 15 ++++++++++----- libultraship/libultraship/GlobalCtx2.h | 4 +++- libultraship/libultraship/ImGuiImpl.cpp | 5 +++++ libultraship/libultraship/Lib/ImGui/imgui.cpp | 8 ++------ soh/soh/Enhancements/debugconsole.cpp | 9 ++++++--- soh/soh/OTRGlobals.cpp | 5 ++--- soh/soh/SaveManager.cpp | 12 +++++------- soh/soh/SaveManager.h | 3 +-- 8 files changed, 34 insertions(+), 27 deletions(-) diff --git a/libultraship/libultraship/GlobalCtx2.cpp b/libultraship/libultraship/GlobalCtx2.cpp index fdc6f8a0df5..0468288e3e4 100644 --- a/libultraship/libultraship/GlobalCtx2.cpp +++ b/libultraship/libultraship/GlobalCtx2.cpp @@ -15,6 +15,7 @@ namespace Ship { std::weak_ptr GlobalCtx2::Context; ModManager* INSTANCE; + std::shared_ptr GlobalCtx2::GetInstance() { return Context.lock(); } @@ -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)); @@ -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("") { } @@ -55,14 +60,14 @@ namespace Ship { void GlobalCtx2::InitWindow() { InitLogging(); - Config = std::make_shared(GlobalCtx2::GetInstance(), GetAppDirectoryPath() + "/shipofharkinian.ini"); + Config = std::make_shared(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(GlobalCtx2::GetInstance(), MainPath, PatchesPath); Win = std::make_shared(GlobalCtx2::GetInstance()); @@ -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); diff --git a/libultraship/libultraship/GlobalCtx2.h b/libultraship/libultraship/GlobalCtx2.h index 48d51a567a1..df87d8bc8f9 100644 --- a/libultraship/libultraship/GlobalCtx2.h +++ b/libultraship/libultraship/GlobalCtx2.h @@ -19,12 +19,14 @@ namespace Ship { static std::shared_ptr CreateInstance(const std::string& Name); std::string GetName() { return Name; } - std::string GetAppDirectoryPath(); std::shared_ptr GetWindow() { return Win; } std::shared_ptr GetResourceManager() { return ResMan; } std::shared_ptr GetLogger() { return Logger; } std::shared_ptr 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); diff --git a/libultraship/libultraship/ImGuiImpl.cpp b/libultraship/libultraship/ImGuiImpl.cpp index 409675da2ae..36f315cdb83 100644 --- a/libultraship/libultraship/ImGuiImpl.cpp +++ b/libultraship/libultraship/ImGuiImpl.cpp @@ -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; } diff --git a/libultraship/libultraship/Lib/ImGui/imgui.cpp b/libultraship/libultraship/Lib/ImGui/imgui.cpp index 6bcceabe850..a67501c8c58 100644 --- a/libultraship/libultraship/Lib/ImGui/imgui.cpp +++ b/libultraship/libultraship/Lib/ImGui/imgui.cpp @@ -872,8 +872,6 @@ CODE #include #endif -#include "GlobalCtx2.h" - // Visual Studio warnings #ifdef _MSC_VER #pragma warning (disable: 4127) // condition expression is constant @@ -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 diff --git a/soh/soh/Enhancements/debugconsole.cpp b/soh/soh/Enhancements/debugconsole.cpp index 445134f2abd..295151eadc9 100644 --- a/soh/soh/Enhancements/debugconsole.cpp +++ b/soh/soh/Enhancements/debugconsole.cpp @@ -500,8 +500,9 @@ template 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 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::PathRelativeToAppDirectory("cvars.cfg"); + File::WriteAllText(cvarsConfig, output); } diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index 99f855e3494..d435c1c698e 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -54,7 +54,6 @@ OTRGlobals* OTRGlobals::Instance; SaveManager* SaveManager::Instance; OTRGlobals::OTRGlobals() { - context = Ship::GlobalCtx2::CreateInstance("Ship of Harkinian"); gSaveStateMgr = std::make_shared(); context->GetWindow()->Init(); @@ -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) @@ -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); diff --git a/soh/soh/SaveManager.cpp b/soh/soh/SaveManager.cpp index b3769b3424a..cd58f4268e6 100644 --- a/soh/soh/SaveManager.cpp +++ b/soh/soh/SaveManager.cpp @@ -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); @@ -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)) { diff --git a/soh/soh/SaveManager.h b/soh/soh/SaveManager.h index 7f7061ea35c..cc0bba42c7d 100644 --- a/soh/soh/SaveManager.h +++ b/soh/soh/SaveManager.h @@ -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); @@ -114,7 +114,6 @@ class SaveManager { static void LoadBaseVersion1(); static void SaveBase(); - std::string appDirectoryPath; std::vector initFuncs; using SectionLoadHandler = std::map;