feat: Display detailed error message when loading of project fails (#1135)

In order to do this I add to make some other additions :
- Add a warning popup (TODO, maybe add some icons to differentiate
error/warning popups in a future PR ?)
- create showError() and showWarning() functions, as helpers to show a
message both to the logs and as a popup
This commit is contained in:
iTrooz
2023-06-21 20:07:36 +02:00
committed by GitHub
parent 3fe6cd057b
commit b7d8e46288
13 changed files with 363 additions and 182 deletions

View File

@@ -12,15 +12,15 @@
#include <hex/helpers/concepts.hpp>
#include <hex/helpers/tar.hpp>
/**
* @brief Project file manager
*
* The project file manager is used to load and store project files. It is used by all features of ImHex
* that want to store any data to a Project File.
*
*/
namespace hex {
/**
* @brief Project file manager
*
* The project file manager is used to load and store project files. It is used by all features of ImHex
* that want to store any data to a Project File.
*
*/
class ProjectFile {
public:
struct Handler {
@@ -39,6 +39,17 @@ namespace hex {
Function load, store; //< Functions to load and store data
};
/**
* @brief Set implementations for loading and restoring a project
*
* @param loadFun function to use to load a project in ImHex
* @param storeFun function to use to store a project to disk
*/
static void setProjectFunctions(
const std::function<bool(const std::fs::path&)> &loadFun,
const std::function<bool(std::optional<std::fs::path>)> &storeFun
);
/**
* @brief Load a project file
*
@@ -119,6 +130,9 @@ namespace hex {
private:
ProjectFile() = default;
static std::function<bool(const std::fs::path&)> s_loadProjectFunction;
static std::function<bool(std::optional<std::fs::path>)> s_storeProjectFunction;
static std::fs::path s_currProjectPath;
static std::vector<Handler> s_handlers;
static std::vector<ProviderHandler> s_providerHandlers;

View File

@@ -26,6 +26,12 @@ namespace hex {
void close();
/**
* @brief get the error string explaining the error that occured when opening the file.
* This error is a combination of the tar error and the native file open error
*/
std::string getOpenErrorString();
[[nodiscard]] std::vector<u8> readVector(const std::fs::path &path);
[[nodiscard]] std::string readString(const std::fs::path &path);
@@ -45,6 +51,10 @@ namespace hex {
std::fs::path m_path;
bool m_valid = false;
// these will be updated when the constructor is called
int m_tarOpenErrno = MTAR_ESUCCESS;
int m_fileOpenErrno = 0;
};
}

View File

@@ -11,138 +11,28 @@
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;
std::function<bool(const std::fs::path&)> ProjectFile::s_loadProjectFunction;
std::function<bool(std::optional<std::fs::path>)> ProjectFile::s_storeProjectFunction;
void ProjectFile::setProjectFunctions(
const std::function<bool(const std::fs::path&)> &loadFun,
const std::function<bool(std::optional<std::fs::path>)> &storeFun
) {
ProjectFile::s_loadProjectFunction = loadFun;
ProjectFile::s_storeProjectFunction = storeFun;
}
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 (!wolv::io::fs::exists(filePath) || !wolv::io::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.readVector(MetadataPath);
if (!std::string(metadataContent.begin(), metadataContent.end()).starts_with(MetadataHeaderMagic))
return false;
}
auto providers = auto(ImHexApi::Provider::getProviders());
for (const auto &provider : providers) {
ImHexApi::Provider::remove(provider);
}
bool result = true;
for (const auto &handler : ProjectFile::getHandlers()) {
try {
if (!handler.load(handler.basePath, tar)) {
log::warn("Project file handler for {} failed to load {}", filePath.string(), handler.basePath.string());
result = false;
}
} catch (std::exception &e) {
log::warn("Project file handler for {} failed to load {}: {}", filePath.string(), handler.basePath.string(), e.what());
result = false;
}
if (!result && handler.required) {
return 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;
}
if (!result && handler.required) {
return false;
}
}
}
resetPath.release();
EventManager::post<EventProjectOpened>();
EventManager::post<RequestUpdateWindowTitle>();
return true;
return s_loadProjectFunction(filePath);
}
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;
bool result = true;
for (const auto &handler : ProjectFile::getHandlers()) {
try {
if (!handler.store(handler.basePath, tar) && handler.required)
result = false;
} catch (std::exception &e) {
log::info("{}", e.what());
if (handler.required)
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) && handler.required)
result = false;
} catch (std::exception &e) {
log::info("{}", e.what());
if (handler.required)
result = false;
}
}
}
{
const auto metadataContent = hex::format("{}\n{}", MetadataHeaderMagic, IMHEX_VERSION);
tar.writeString(MetadataPath, metadataContent);
}
ImHexApi::Provider::resetDirty();
resetPath.release();
return result;
return s_storeProjectFunction(filePath);
}
bool ProjectFile::hasPath() {

View File

@@ -2,6 +2,7 @@
#include <hex/helpers/literals.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/fmt.hpp>
#include <wolv/io/file.hpp>
@@ -10,7 +11,7 @@ namespace hex {
using namespace hex::literals;
Tar::Tar(const std::fs::path &path, Mode mode) {
int error = MTAR_ESUCCESS;
int tar_error = MTAR_ESUCCESS;
// Explicitly create file so a short path gets generated
if (mode == Mode::Create) {
@@ -20,16 +21,23 @@ namespace hex {
auto shortPath = wolv::io::fs::toShortPath(path);
if (mode == Tar::Mode::Read)
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "r");
tar_error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "r");
else if (mode == Tar::Mode::Write)
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "a");
tar_error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "a");
else if (mode == Tar::Mode::Create)
error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "w");
tar_error = mtar_open(&this->m_ctx, shortPath.string().c_str(), "w");
else
error = MTAR_EFAILURE;
tar_error = MTAR_EFAILURE;
this->m_path = path;
this->m_valid = (error == MTAR_ESUCCESS);
this->m_valid = (tar_error == MTAR_ESUCCESS);
if (!this->m_valid) {
this->m_tarOpenErrno = tar_error;
// hopefully this errno corresponds to the file open call in mtar_open
this->m_fileOpenErrno = errno;
}
}
Tar::~Tar() {
@@ -40,6 +48,8 @@ namespace hex {
this->m_ctx = other.m_ctx;
this->m_path = other.m_path;
this->m_valid = other.m_valid;
this->m_tarOpenErrno = other.m_tarOpenErrno;
this->m_fileOpenErrno = other.m_fileOpenErrno;
other.m_ctx = { };
other.m_valid = false;
@@ -54,6 +64,8 @@ namespace hex {
this->m_valid = other.m_valid;
other.m_valid = false;
this->m_tarOpenErrno = other.m_tarOpenErrno;
this->m_fileOpenErrno = other.m_fileOpenErrno;
return *this;
}
@@ -79,6 +91,10 @@ namespace hex {
return mtar_find(&this->m_ctx, path.string().c_str(), &header) == MTAR_ESUCCESS;
}
std::string Tar::getOpenErrorString(){
return hex::format("{}: {}", mtar_strerror(this->m_tarOpenErrno), std::strerror(this->m_fileOpenErrno));
}
void Tar::close() {
if (this->m_valid) {
mtar_finalize(&this->m_ctx);