2022-08-08 21:23:52 +02:00
|
|
|
#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>
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/utils/guards.hpp>
|
|
|
|
#include <wolv/io/fs.hpp>
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
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) {
|
2023-01-11 23:31:25 +01:00
|
|
|
auto originalPath = ProjectFile::s_currProjectPath;
|
|
|
|
|
|
|
|
ProjectFile::s_currProjectPath = filePath;
|
|
|
|
auto resetPath = SCOPE_GUARD {
|
|
|
|
ProjectFile::s_currProjectPath = originalPath;
|
|
|
|
};
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
if (!wolv::io::fs::exists(filePath) || !wolv::io::fs::isRegularFile(filePath))
|
2022-08-08 21:23:52 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
{
|
2023-03-23 11:23:07 +01:00
|
|
|
const auto metadataContent = tar.readVector(MetadataPath);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
if (!std::string(metadataContent.begin(), metadataContent.end()).starts_with(MetadataHeaderMagic))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-07 22:43:44 +01:00
|
|
|
auto providers = auto(ImHexApi::Provider::getProviders());
|
|
|
|
for (const auto &provider : providers) {
|
2022-09-22 09:05:09 +02:00
|
|
|
ImHexApi::Provider::remove(provider);
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
bool result = true;
|
|
|
|
for (const auto &handler : ProjectFile::getHandlers()) {
|
|
|
|
try {
|
2023-01-19 11:09:24 +01:00
|
|
|
if (!handler.load(handler.basePath, tar)) {
|
|
|
|
log::warn("Project file handler for {} failed to load {}", filePath.string(), handler.basePath.string());
|
2022-08-08 21:23:52 +02:00
|
|
|
result = false;
|
2023-01-19 11:09:24 +01:00
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
} catch (std::exception &e) {
|
2023-01-19 11:09:24 +01:00
|
|
|
log::warn("Project file handler for {} failed to load {}: {}", filePath.string(), handler.basePath.string(), e.what());
|
2022-08-08 21:23:52 +02:00
|
|
|
result = false;
|
|
|
|
}
|
2022-09-20 15:33:36 +02:00
|
|
|
|
|
|
|
if (!result && handler.required) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-09-20 15:33:36 +02:00
|
|
|
|
|
|
|
if (!result && handler.required) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 23:31:25 +01:00
|
|
|
resetPath.release();
|
2023-01-07 17:31:22 +01:00
|
|
|
EventManager::post<RequestUpdateWindowTitle>();
|
2023-01-11 23:31:25 +01:00
|
|
|
|
2022-09-20 15:33:36 +02:00
|
|
|
return true;
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProjectFile::store(std::optional<std::fs::path> filePath) {
|
2023-01-11 23:31:25 +01:00
|
|
|
auto originalPath = ProjectFile::s_currProjectPath;
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
if (!filePath.has_value())
|
|
|
|
filePath = ProjectFile::s_currProjectPath;
|
|
|
|
|
2023-01-11 23:31:25 +01:00
|
|
|
ProjectFile::s_currProjectPath = filePath.value();
|
|
|
|
auto resetPath = SCOPE_GUARD {
|
|
|
|
ProjectFile::s_currProjectPath = originalPath;
|
|
|
|
};
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
Tar tar(*filePath, Tar::Mode::Create);
|
|
|
|
if (!tar.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
for (const auto &handler : ProjectFile::getHandlers()) {
|
|
|
|
try {
|
2023-05-04 23:23:44 +02:00
|
|
|
if (!handler.store(handler.basePath, tar) && handler.required)
|
2022-08-08 21:23:52 +02:00
|
|
|
result = false;
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
log::info("{}", e.what());
|
2023-05-04 23:23:44 +02:00
|
|
|
|
|
|
|
if (handler.required)
|
|
|
|
result = false;
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2023-05-04 23:23:44 +02:00
|
|
|
if (!handler.store(provider, basePath / handler.basePath, tar) && handler.required)
|
2022-08-08 21:23:52 +02:00
|
|
|
result = false;
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
log::info("{}", e.what());
|
2023-05-04 23:23:44 +02:00
|
|
|
|
|
|
|
if (handler.required)
|
|
|
|
result = false;
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto metadataContent = hex::format("{}\n{}", MetadataHeaderMagic, IMHEX_VERSION);
|
2023-03-23 11:23:07 +01:00
|
|
|
tar.writeString(MetadataPath, metadataContent);
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 13:15:17 +02:00
|
|
|
ImHexApi::Provider::resetDirty();
|
2023-01-11 23:31:25 +01:00
|
|
|
resetPath.release();
|
2022-08-29 13:15:17 +02:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-25 10:47:11 +01:00
|
|
|
bool ProjectFile::hasPath() {
|
|
|
|
return !ProjectFile::s_currProjectPath.empty();
|
|
|
|
}
|
|
|
|
|
2023-01-07 17:31:22 +01:00
|
|
|
void ProjectFile::clearPath() {
|
|
|
|
ProjectFile::s_currProjectPath.clear();
|
|
|
|
}
|
2023-02-12 17:52:09 +01:00
|
|
|
|
2023-01-07 17:31:22 +01:00
|
|
|
std::fs::path ProjectFile::getPath() {
|
|
|
|
return ProjectFile::s_currProjectPath;
|
|
|
|
}
|
|
|
|
|
2023-02-12 17:52:09 +01:00
|
|
|
void ProjectFile::setPath(const std::fs::path &path) {
|
|
|
|
ProjectFile::s_currProjectPath = path;
|
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|