1
0
mirror of synced 2025-01-19 01:24:15 +01:00

feat: Allow most resources to be loaded relative to the current project

This commit is contained in:
WerWolv 2023-01-11 23:31:25 +01:00
parent b1cab5ccd2
commit 03d216f116
3 changed files with 36 additions and 7 deletions

View File

@ -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<RequestUpdateWindowTitle>();
return true;
}
bool ProjectFile::store(std::optional<std::fs::path> 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;
}

View File

@ -1,10 +1,11 @@
#include <hex/helpers/fs.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/api/project_file_manager.hpp>
#include <hex/helpers/fs_macos.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/intrinsics.hpp>
#include <hex/helpers/net.hpp>
#include <hex/helpers/utils.hpp>
#include <xdg.hpp>
@ -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;
}

View File

@ -4,6 +4,8 @@
#include <hex/api/imhex_api.hpp>
#include <hex/api/localization.hpp>
#include <hex/api/project_file_manager.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/fmt.hpp>
@ -285,12 +287,20 @@ namespace hex::plugin::builtin {
void FileProvider::loadSettings(const nlohmann::json &settings) {
Provider::loadSettings(settings);
auto path = settings["path"].get<std::string>();
this->setPath(std::u8string(path.begin(), path.end()));
auto pathString = settings["path"].get<std::string>();
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);
}