2022-05-27 20:42:07 +02:00
|
|
|
#include <hex/api/event.hpp>
|
2022-07-02 16:22:38 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2022-07-02 17:53:13 +02:00
|
|
|
#include <hex/providers/provider.hpp>
|
2022-05-27 20:42:07 +02:00
|
|
|
#include <hex/ui/view.hpp>
|
|
|
|
#include <hex/api/localization.hpp>
|
|
|
|
#include <hex/helpers/file.hpp>
|
2022-07-02 16:22:38 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2022-09-18 16:22:08 +02:00
|
|
|
#include <content/helpers/provider_extra_data.hpp>
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2022-09-18 16:22:08 +02:00
|
|
|
#include <content/providers/file_provider.hpp>
|
2022-08-14 10:07:45 +02:00
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
static void openFile(const std::fs::path &path) {
|
2022-08-14 10:07:45 +02:00
|
|
|
auto provider = ImHexApi::Provider::createProvider("hex.builtin.provider.file", true);
|
2022-11-07 00:04:47 +01:00
|
|
|
if (auto *fileProvider = dynamic_cast<FileProvider*>(provider); fileProvider != nullptr) {
|
2022-08-14 10:07:45 +02:00
|
|
|
fileProvider->setPath(path);
|
2022-09-04 11:16:20 +02:00
|
|
|
if (fileProvider->open())
|
|
|
|
EventManager::post<EventProviderOpened>(fileProvider);
|
2022-08-14 10:07:45 +02:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerEventHandlers() {
|
|
|
|
|
|
|
|
EventManager::subscribe<EventWindowClosing>([](GLFWwindow *window) {
|
2022-08-08 21:23:52 +02:00
|
|
|
if (ImHexApi::Provider::isDirty()) {
|
2022-05-27 20:42:07 +02:00
|
|
|
glfwSetWindowShouldClose(window, GLFW_FALSE);
|
2022-08-17 16:15:36 +02:00
|
|
|
TaskManager::doLater([] { ImGui::OpenPopup("hex.builtin.popup.exit_application.title"_lang); });
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
EventManager::subscribe<EventProviderClosing>([](hex::prv::Provider *provider, bool *shouldClose) {
|
|
|
|
if (provider->isDirty()) {
|
|
|
|
*shouldClose = false;
|
2022-08-17 16:15:36 +02:00
|
|
|
TaskManager::doLater([] { ImGui::OpenPopup("hex.builtin.popup.close_provider.title"_lang); });
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-10 00:11:16 +02:00
|
|
|
EventManager::subscribe<EventProviderChanged>([](hex::prv::Provider *oldProvider, hex::prv::Provider *newProvider) {
|
|
|
|
hex::unused(oldProvider);
|
2023-01-07 17:31:22 +01:00
|
|
|
hex::unused(newProvider);
|
2022-08-10 00:11:16 +02:00
|
|
|
|
2023-01-07 17:31:22 +01:00
|
|
|
EventManager::post<RequestUpdateWindowTitle>();
|
2022-08-10 00:11:16 +02:00
|
|
|
});
|
|
|
|
|
2022-08-22 11:00:31 +02:00
|
|
|
EventManager::subscribe<EventProviderOpened>([](hex::prv::Provider *provider) {
|
|
|
|
if (provider != nullptr && ImHexApi::Provider::get() == provider)
|
2023-01-07 17:31:22 +01:00
|
|
|
EventManager::post<RequestUpdateWindowTitle>();
|
2022-08-22 11:00:31 +02:00
|
|
|
});
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
EventManager::subscribe<RequestOpenFile>(openFile);
|
|
|
|
|
|
|
|
EventManager::subscribe<RequestOpenWindow>([](const std::string &name) {
|
|
|
|
if (name == "Create File") {
|
|
|
|
fs::openFileBrowser(fs::DialogMode::Save, {}, [](const auto &path) {
|
|
|
|
fs::File file(path, fs::File::Mode::Create);
|
|
|
|
|
|
|
|
if (!file.isValid()) {
|
|
|
|
View::showErrorPopup("hex.builtin.popup.error.create"_lang);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.setSize(1);
|
|
|
|
|
|
|
|
EventManager::post<RequestOpenFile>(path);
|
|
|
|
});
|
|
|
|
} else if (name == "Open File") {
|
2022-08-12 15:11:27 +02:00
|
|
|
ImHexApi::Provider::createProvider("hex.builtin.provider.file");
|
2022-05-27 20:42:07 +02:00
|
|
|
} else if (name == "Open Project") {
|
|
|
|
fs::openFileBrowser(fs::DialogMode::Open, { {"Project File", "hexproj"} },
|
|
|
|
[](const auto &path) {
|
2022-09-20 15:33:36 +02:00
|
|
|
if (!ProjectFile::load(path)) {
|
|
|
|
View::showErrorPopup("hex.builtin.popup.error.project.load"_lang);
|
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
EventManager::subscribe<EventProviderChanged>([](auto, auto) {
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
|
|
|
});
|
2022-07-02 17:53:13 +02:00
|
|
|
|
|
|
|
EventManager::subscribe<EventProviderCreated>([](hex::prv::Provider *provider) {
|
2022-08-14 10:07:45 +02:00
|
|
|
if (provider->shouldSkipLoadInterface())
|
|
|
|
return;
|
|
|
|
|
2022-08-12 15:11:27 +02:00
|
|
|
if (provider->hasFilePicker()) {
|
|
|
|
if (!provider->handleFilePicker()) {
|
2022-08-17 16:15:36 +02:00
|
|
|
TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); });
|
2022-08-12 15:11:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!provider->open()) {
|
|
|
|
View::showErrorPopup("hex.builtin.popup.error.open"_lang);
|
2022-08-17 16:15:36 +02:00
|
|
|
TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); });
|
2022-08-12 15:11:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-09-04 11:16:20 +02:00
|
|
|
|
|
|
|
EventManager::post<EventProviderOpened>(provider);
|
2022-08-12 15:11:27 +02:00
|
|
|
}
|
|
|
|
else if (provider->hasLoadInterface())
|
2022-07-02 17:53:13 +02:00
|
|
|
EventManager::post<RequestOpenPopup>(View::toWindowName("hex.builtin.view.provider_settings.load_popup"));
|
2022-08-12 15:11:27 +02:00
|
|
|
else {
|
|
|
|
if (!provider->open() || !provider->isAvailable()) {
|
|
|
|
View::showErrorPopup("hex.builtin.popup.error.open"_lang);
|
2022-08-17 16:15:36 +02:00
|
|
|
TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); });
|
2022-09-04 11:16:20 +02:00
|
|
|
return;
|
2022-08-12 15:11:27 +02:00
|
|
|
}
|
2022-09-04 11:16:20 +02:00
|
|
|
|
|
|
|
EventManager::post<EventProviderOpened>(provider);
|
2022-08-12 15:11:27 +02:00
|
|
|
}
|
2022-07-02 17:53:13 +02:00
|
|
|
});
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
EventManager::subscribe<EventProviderDeleted>([](hex::prv::Provider *provider) {
|
|
|
|
ProviderExtraData::erase(provider);
|
|
|
|
});
|
|
|
|
|
2022-11-09 18:49:07 +01:00
|
|
|
EventManager::subscribe<EventRegionSelected>([](const ImHexApi::HexEditor::ProviderRegion ®ion) {
|
|
|
|
ImHexApi::HexEditor::impl::setCurrentSelection(region);
|
|
|
|
});
|
|
|
|
|
2022-09-20 15:47:59 +02:00
|
|
|
fs::setFileBrowserErrorCallback([]{
|
|
|
|
#if defined(NFD_PORTAL)
|
|
|
|
View::showErrorPopup("hex.builtin.popup.error.file_dialog.portal"_lang);
|
|
|
|
#else
|
|
|
|
View::showErrorPopup("hex.builtin.popup.error.file_dialog.common"_lang);
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|