From 03d216f116b4892195fcaffdd096cc7b2e5ded47 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 11 Jan 2023 23:31:25 +0100 Subject: [PATCH] feat: Allow most resources to be loaded relative to the current project --- .../source/api/project_file_manager.cpp | 20 ++++++++++++++++--- lib/libimhex/source/helpers/fs.cpp | 7 ++++++- .../content/providers/file_provider.cpp | 16 ++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/libimhex/source/api/project_file_manager.cpp b/lib/libimhex/source/api/project_file_manager.cpp index 7f375637e..8670443f3 100644 --- a/lib/libimhex/source/api/project_file_manager.cpp +++ b/lib/libimhex/source/api/project_file_manager.cpp @@ -17,6 +17,13 @@ namespace hex { std::fs::path ProjectFile::s_currProjectPath; bool ProjectFile::load(const std::fs::path &filePath) { + auto originalPath = ProjectFile::s_currProjectPath; + + ProjectFile::s_currProjectPath = filePath; + auto resetPath = SCOPE_GUARD { + ProjectFile::s_currProjectPath = originalPath; + }; + if (!fs::exists(filePath) || !fs::isRegularFile(filePath)) return false; if (filePath.extension() != ".hexproj") @@ -73,15 +80,23 @@ namespace hex { } } - ProjectFile::s_currProjectPath = filePath; + resetPath.release(); EventManager::post(); + return true; } bool ProjectFile::store(std::optional filePath) { + auto originalPath = ProjectFile::s_currProjectPath; + if (!filePath.has_value()) filePath = ProjectFile::s_currProjectPath; + ProjectFile::s_currProjectPath = filePath.value(); + auto resetPath = SCOPE_GUARD { + ProjectFile::s_currProjectPath = originalPath; + }; + Tar tar(*filePath, Tar::Mode::Create); if (!tar.isValid()) return false; @@ -114,9 +129,8 @@ namespace hex { tar.write(MetadataPath, metadataContent); } - ProjectFile::s_currProjectPath = filePath.value(); - ImHexApi::Provider::resetDirty(); + resetPath.release(); return result; } diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index bf8b8f0b6..90137e860 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -1,10 +1,11 @@ #include #include +#include + #include #include #include -#include #include #include @@ -166,6 +167,10 @@ namespace hex::fs { auto additionalDirs = ImHexApi::System::getAdditionalFolderPaths(); std::copy(additionalDirs.begin(), additionalDirs.end(), std::back_inserter(paths)); + if (ProjectFile::hasPath()) { + paths.push_back(ProjectFile::getPath().parent_path()); + } + return paths; } diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index 15896a329..758d603d6 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -4,6 +4,8 @@ #include #include +#include + #include #include #include @@ -285,12 +287,20 @@ namespace hex::plugin::builtin { void FileProvider::loadSettings(const nlohmann::json &settings) { Provider::loadSettings(settings); - auto path = settings["path"].get(); - this->setPath(std::u8string(path.begin(), path.end())); + auto pathString = settings["path"].get(); + std::fs::path path = std::u8string(pathString.begin(), pathString.end()); + + if (auto projectPath = ProjectFile::getPath(); !projectPath.empty()) + this->setPath(std::filesystem::weakly_canonical(projectPath.parent_path() / path)); + else + this->setPath(path); } nlohmann::json FileProvider::storeSettings(nlohmann::json settings) const { - settings["path"] = hex::toUTF8String(this->m_path); + if (auto projectPath = ProjectFile::getPath(); !projectPath.empty()) + settings["path"] = hex::toUTF8String(std::fs::relative(this->m_path, projectPath.parent_path())); + else + settings["path"] = hex::toUTF8String(this->m_path); return Provider::storeSettings(settings); }