2023-06-21 20:07:36 +02:00
|
|
|
#include <filesystem>
|
|
|
|
|
2023-11-30 11:23:12 +01:00
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
#include <wolv/utils/guards.hpp>
|
|
|
|
#include <wolv/utils/string.hpp>
|
|
|
|
|
2023-11-30 11:23:12 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2023-06-21 20:07:36 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2023-11-21 14:38:01 +01:00
|
|
|
#include <hex/api/localization_manager.hpp>
|
2023-08-06 21:33:15 +02:00
|
|
|
#include <hex/api/achievement_manager.hpp>
|
2024-06-22 10:44:55 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
impr: Refactor and restructure Event Manager (#2082)
### Problem description
This PR addresses issue #2013 that described a cluttered Event Manager.
This is a DX issue and should not impact the users whatsoever.
### Implementation description
The changes revolve around three main points:
1. the Event Manager (`event_manager.hpp`) was split into four
categories: GUI, Interaction, Lifecycle, and Provider, and two types:
Events, and Requests. This results in the following files:
- `events_gui.hpp`
- `events_interaction.hpp`
- `events_lifecycle.hpp`
- `events_provider.hpp`
- `requests_gui.hpp`
- `requests_interaction.hpp`
- `requests_lifecycle.hpp`
- `requests_provider.hpp`
2. Every event and request now has its own piece of documentation, with
a `@brief`, accompanied by a longer comment if needed, and gets its
`@param`s described.
3. The old `event_manager.hpp` import was removed and replaced by the
correct imports wherever needed, as to reduce spread of those files only
to where they are truly useful.
### Additional things
The commits have been split into (chrono-)logical steps:
- `feat`: split the Event Manager, and replace the imports
- `refactor`, `chore`: make various small changes to match the required
structure
- `docs`: add documentation for events and requests
Hopefully, this will help to review the PR.
*Note: Beware of very long rebuild times in between the commits, use
them sparingly! The Actions will ensure this PR builds anyways*
Closes #2013
---------
Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com>
Co-authored-by: Nik <werwolv98@gmail.com>
2025-01-25 16:32:07 +01:00
|
|
|
#include <hex/api/events/events_lifecycle.hpp>
|
|
|
|
#include <hex/api/events/requests_gui.hpp>
|
2023-08-06 21:33:15 +02:00
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
#include <hex/helpers/fmt.hpp>
|
2023-11-30 11:23:12 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2024-02-26 20:51:08 +01:00
|
|
|
#include <toasts/toast_notification.hpp>
|
2023-06-21 20:07:36 +02:00
|
|
|
|
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
constexpr static auto MetadataHeaderMagic = "HEX";
|
|
|
|
constexpr static auto MetadataPath = "IMHEX_METADATA";
|
2023-06-21 20:07:36 +02:00
|
|
|
|
2023-07-04 23:30:37 +02:00
|
|
|
bool load(const std::fs::path &filePath) {
|
2023-06-21 20:07:36 +02:00
|
|
|
if (!wolv::io::fs::exists(filePath) || !wolv::io::fs::isRegularFile(filePath)) {
|
2024-02-26 20:51:08 +01:00
|
|
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
|
2023-06-21 20:07:36 +02:00
|
|
|
hex::format("hex.builtin.popup.error.project.load.file_not_found"_lang,
|
|
|
|
wolv::util::toUTF8String(filePath)
|
|
|
|
)));
|
2024-02-26 20:51:08 +01:00
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tar tar(filePath, Tar::Mode::Read);
|
|
|
|
if (!tar.isValid()) {
|
2024-02-26 20:51:08 +01:00
|
|
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
|
2023-06-21 20:07:36 +02:00
|
|
|
hex::format("hex.builtin.popup.error.project.load.invalid_tar"_lang,
|
|
|
|
tar.getOpenErrorString()
|
|
|
|
)));
|
2024-02-26 20:51:08 +01:00
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tar.contains(MetadataPath)) {
|
2024-02-26 20:51:08 +01:00
|
|
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
|
2023-06-21 20:07:36 +02:00
|
|
|
hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang)
|
|
|
|
));
|
2024-02-26 20:51:08 +01:00
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto metadataContent = tar.readVector(MetadataPath);
|
|
|
|
|
|
|
|
if (!std::string(metadataContent.begin(), metadataContent.end()).starts_with(MetadataHeaderMagic)) {
|
2024-02-26 20:51:08 +01:00
|
|
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang,
|
2023-06-21 20:07:36 +02:00
|
|
|
hex::format("hex.builtin.popup.error.project.load.invalid_magic"_lang)
|
|
|
|
));
|
2024-02-26 20:51:08 +01:00
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-10 23:31:05 +01:00
|
|
|
for (const auto &provider : ImHexApi::Provider::getProviders()) {
|
2023-06-21 20:07:36 +02:00
|
|
|
ImHexApi::Provider::remove(provider);
|
|
|
|
}
|
|
|
|
|
2023-07-04 23:30:37 +02:00
|
|
|
auto originalPath = ProjectFile::getPath();
|
|
|
|
ProjectFile::setPath(filePath);
|
|
|
|
auto resetPath = SCOPE_GUARD {
|
|
|
|
ProjectFile::setPath(originalPath);
|
|
|
|
};
|
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
for (const auto &handler : ProjectFile::getHandlers()) {
|
|
|
|
bool result = true;
|
2023-10-04 12:00:32 +02:00
|
|
|
// Handlers are supposed to show the error/warning popup to the user themselves, so we don't show one here
|
2023-06-21 20:07:36 +02:00
|
|
|
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()) {
|
|
|
|
bool result = true;
|
|
|
|
// Handlers are supposed to show the error/warning popup to the user themselves, so we don't show one here
|
|
|
|
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();
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProjectOpened::post();
|
|
|
|
RequestUpdateWindowTitle::post();
|
2023-06-21 20:07:36 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-01 12:32:28 +02:00
|
|
|
bool store(std::optional<std::fs::path> filePath = std::nullopt, bool updateLocation = true) {
|
2023-06-21 20:07:36 +02:00
|
|
|
auto originalPath = ProjectFile::getPath();
|
|
|
|
|
|
|
|
if (!filePath.has_value())
|
|
|
|
filePath = originalPath;
|
|
|
|
|
|
|
|
ProjectFile::setPath(filePath.value());
|
|
|
|
auto resetPath = SCOPE_GUARD {
|
|
|
|
ProjectFile::setPath(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-12-26 14:00:50 +01:00
|
|
|
const auto metadataContent = hex::format("{}\n{}", MetadataHeaderMagic, ImHexApi::System::getImHexVersion().get(false));
|
2023-06-21 20:07:36 +02:00
|
|
|
tar.writeString(MetadataPath, metadataContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImHexApi::Provider::resetDirty();
|
2023-07-01 12:32:28 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// If saveLocation is false, reset the project path (do not release the lock)
|
2023-07-01 12:32:28 +02:00
|
|
|
if (updateLocation) {
|
|
|
|
resetPath.release();
|
2023-12-11 11:42:33 +01:00
|
|
|
|
|
|
|
// Request, as this puts us into a project state
|
|
|
|
RequestUpdateWindowTitle::post();
|
2023-07-01 12:32:28 +02:00
|
|
|
}
|
2023-06-21 20:07:36 +02:00
|
|
|
|
2023-08-06 21:33:15 +02:00
|
|
|
AchievementManager::unlockAchievement("hex.builtin.achievement.starting_out", "hex.builtin.achievement.starting_out.save_project.name");
|
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerProjectHandlers() {
|
2024-06-22 10:44:55 +02:00
|
|
|
hex::ProjectFile::setProjectFunctions(load, store);
|
2023-06-21 20:07:36 +02:00
|
|
|
}
|
2024-02-26 20:51:08 +01:00
|
|
|
}
|