1
0
mirror of synced 2024-12-11 07:26:00 +01:00
ImHex/lib/libimhex/source/api/project_file_manager.cpp
WerWolv 966f3b8597
sys: Replace existing bad project system with a much better one (#663)
* sys: Initial effort to replace existing project files with a better system

* sys: Added back marking provider as dirty

* sys: Remove git commit information from project files

* sys: Format data processor save file nicely

* fix: Automatic pattern loading not working correctly

* ui: Added warning popup when closing a provider with modifications

Closes #604

* sys: Fixed build issues

* tests: Removed useless debug logs

* patterns: Updated pattern language

* sys: Added log message when crashing with a signal

* sys: Make sure abnormal termination handlers are being called more reliably
2022-08-08 21:23:52 +02:00

107 lines
3.4 KiB
C++

#include <hex/api/project_file_manager.hpp>
#include <hex/helpers/tar.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/providers/provider.hpp>
namespace hex {
constexpr static auto MetadataHeaderMagic = "HEX";
constexpr static auto MetadataPath = "IMHEX_METADATA";
std::vector<ProjectFile::Handler> ProjectFile::s_handlers;
std::vector<ProjectFile::ProviderHandler> ProjectFile::s_providerHandlers;
std::fs::path ProjectFile::s_currProjectPath;
bool ProjectFile::load(const std::fs::path &filePath) {
if (!fs::exists(filePath) || !fs::isRegularFile(filePath))
return false;
if (filePath.extension() != ".hexproj")
return false;
Tar tar(filePath, Tar::Mode::Read);
if (!tar.isValid())
return false;
if (!tar.contains(MetadataPath))
return false;
{
const auto metadataContent = tar.read(MetadataPath);
if (!std::string(metadataContent.begin(), metadataContent.end()).starts_with(MetadataHeaderMagic))
return false;
}
bool result = true;
for (const auto &handler : ProjectFile::getHandlers()) {
try {
if (!handler.load(handler.basePath, tar))
result = false;
} catch (std::exception &e) {
log::info("{}", e.what());
result = false;
}
}
for (const auto &provider : ImHexApi::Provider::getProviders()) {
const auto basePath = std::fs::path(std::to_string(provider->getID()));
for (const auto &handler: ProjectFile::getProviderHandlers()) {
try {
if (!handler.load(provider, basePath / handler.basePath, tar))
result = false;
} catch (std::exception &e) {
log::info("{}", e.what());
result = false;
}
}
}
ProjectFile::s_currProjectPath = filePath;
return result;
}
bool ProjectFile::store(std::optional<std::fs::path> filePath) {
if (!filePath.has_value())
filePath = ProjectFile::s_currProjectPath;
Tar tar(*filePath, Tar::Mode::Create);
if (!tar.isValid())
return false;
bool result = true;
for (const auto &handler : ProjectFile::getHandlers()) {
try {
if (!handler.store(handler.basePath, tar))
result = false;
} catch (std::exception &e) {
log::info("{}", e.what());
result = false;
}
}
for (const auto &provider : ImHexApi::Provider::getProviders()) {
const auto basePath = std::fs::path(std::to_string(provider->getID()));
for (const auto &handler: ProjectFile::getProviderHandlers()) {
try {
if (!handler.store(provider, basePath / handler.basePath, tar))
result = false;
} catch (std::exception &e) {
log::info("{}", e.what());
result = false;
}
}
}
{
const auto metadataContent = hex::format("{}\n{}", MetadataHeaderMagic, IMHEX_VERSION);
tar.write(MetadataPath, metadataContent);
}
return result;
}
}