2022-05-27 20:42:07 +02:00
|
|
|
#include <hex/api/event.hpp>
|
|
|
|
|
2023-10-17 13:45:12 +02:00
|
|
|
#include <hex/api/localization.hpp>
|
|
|
|
#include <hex/api/project_file_manager.hpp>
|
|
|
|
#include <hex/api/achievement_manager.hpp>
|
|
|
|
|
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>
|
2022-07-02 16:22:38 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
|
2022-09-18 16:22:08 +02:00
|
|
|
#include <content/providers/file_provider.hpp>
|
2022-08-14 10:07:45 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/fs.hpp>
|
2023-07-28 00:01:49 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
2023-03-12 18:27:29 +01:00
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
#include <content/popups/popup_notification.hpp>
|
|
|
|
#include <content/popups/popup_question.hpp>
|
|
|
|
#include <content/popups/popup_tasks_waiting.hpp>
|
2023-11-07 15:31:54 +01:00
|
|
|
#include <content/popups/popup_unsaved_changes.hpp>
|
2023-04-08 00:58:53 +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);
|
2023-07-15 00:12:09 +02:00
|
|
|
if (!provider->open() || !provider->isAvailable()) {
|
|
|
|
PopupError::open(hex::format("hex.builtin.provider.error.open"_lang, provider->getErrorMessage()));
|
|
|
|
TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); });
|
|
|
|
} else {
|
2022-09-04 11:16:20 +02:00
|
|
|
EventManager::post<EventProviderOpened>(fileProvider);
|
2023-07-15 00:12:09 +02:00
|
|
|
}
|
2022-08-14 10:07:45 +02:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerEventHandlers() {
|
|
|
|
|
2023-11-07 15:31:54 +01:00
|
|
|
static bool imhexClosing = false;
|
2022-05-27 20:42:07 +02:00
|
|
|
EventManager::subscribe<EventWindowClosing>([](GLFWwindow *window) {
|
2023-11-07 15:31:54 +01:00
|
|
|
if (ImHexApi::Provider::isDirty() && !imhexClosing) {
|
2022-05-27 20:42:07 +02:00
|
|
|
glfwSetWindowShouldClose(window, GLFW_FALSE);
|
2023-04-08 00:58:53 +02:00
|
|
|
PopupQuestion::open("hex.builtin.popup.exit_application.desc"_lang,
|
2023-11-07 15:31:54 +01:00
|
|
|
[] {
|
|
|
|
imhexClosing = true;
|
|
|
|
for (const auto &provider : auto(ImHexApi::Provider::getProviders()))
|
|
|
|
ImHexApi::Provider::remove(provider);
|
2023-04-08 00:58:53 +02:00
|
|
|
},
|
2023-04-16 21:34:29 +02:00
|
|
|
[] { }
|
2023-04-08 00:58:53 +02:00
|
|
|
);
|
2023-01-28 12:16:41 +01:00
|
|
|
} else if (TaskManager::getRunningTaskCount() > 0 || TaskManager::getRunningBackgroundTaskCount() > 0) {
|
|
|
|
glfwSetWindowShouldClose(window, GLFW_FALSE);
|
|
|
|
TaskManager::doLater([] {
|
|
|
|
for (auto &task : TaskManager::getRunningTasks())
|
|
|
|
task->interrupt();
|
2023-04-08 00:58:53 +02:00
|
|
|
PopupTasksWaiting::open();
|
2023-01-28 12:16:41 +01:00
|
|
|
});
|
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;
|
2023-11-07 15:31:54 +01:00
|
|
|
PopupUnsavedChanges::open("hex.builtin.popup.close_provider.desc"_lang,
|
2023-04-08 00:58:53 +02:00
|
|
|
[]{
|
2023-11-07 15:31:54 +01:00
|
|
|
for (auto &provider : ImHexApi::Provider::impl::getClosingProviders())
|
|
|
|
ImHexApi::Provider::remove(provider, true);
|
2023-04-08 00:58:53 +02:00
|
|
|
},
|
2023-11-07 15:31:54 +01:00
|
|
|
[] {
|
|
|
|
ImHexApi::Provider::impl::resetClosingProvider();
|
|
|
|
imhexClosing = false;
|
|
|
|
}
|
2023-04-08 00:58:53 +02:00
|
|
|
);
|
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>();
|
2023-04-11 15:26:18 +02:00
|
|
|
EventManager::post<EventProviderChanged>(nullptr, provider);
|
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") {
|
2023-03-07 16:05:03 +01:00
|
|
|
auto newProvider = hex::ImHexApi::Provider::createProvider("hex.builtin.provider.mem_file", true);
|
|
|
|
if (newProvider != nullptr && !newProvider->open())
|
|
|
|
hex::ImHexApi::Provider::remove(newProvider);
|
|
|
|
else
|
|
|
|
EventManager::post<EventProviderOpened>(newProvider);
|
2022-05-27 20:42:07 +02:00
|
|
|
} else if (name == "Open File") {
|
2023-05-11 12:38:22 +02:00
|
|
|
fs::openFileBrowser(fs::DialogMode::Open, { }, [](const auto &path) {
|
2023-05-20 11:52:24 +02:00
|
|
|
if (path.extension() == ".hexproj") {
|
2023-05-11 12:38:22 +02:00
|
|
|
if (!ProjectFile::load(path)) {
|
2023-07-28 00:01:49 +02:00
|
|
|
PopupError::open(hex::format("hex.builtin.popup.error.project.load"_lang, wolv::util::toUTF8String(path)));
|
2023-05-11 12:38:22 +02:00
|
|
|
}
|
2023-05-20 11:52:24 +02:00
|
|
|
} else {
|
2023-05-11 12:38:22 +02:00
|
|
|
FileProvider* newProvider = static_cast<FileProvider*>(
|
|
|
|
ImHexApi::Provider::createProvider("hex.builtin.provider.file", true)
|
|
|
|
);
|
2023-05-20 11:52:24 +02:00
|
|
|
|
|
|
|
if (newProvider == nullptr)
|
|
|
|
return;
|
|
|
|
|
2023-05-11 12:38:22 +02:00
|
|
|
newProvider->setPath(path);
|
2023-05-20 11:52:24 +02:00
|
|
|
if (!newProvider->open())
|
2023-05-11 12:38:22 +02:00
|
|
|
hex::ImHexApi::Provider::remove(newProvider);
|
2023-10-17 13:45:12 +02:00
|
|
|
else {
|
2023-05-11 12:38:22 +02:00
|
|
|
EventManager::post<EventProviderOpened>(newProvider);
|
2023-10-17 13:45:12 +02:00
|
|
|
AchievementManager::unlockAchievement("hex.builtin.achievement.starting_out", "hex.builtin.achievement.starting_out.open_file.name");
|
|
|
|
}
|
|
|
|
|
2023-05-11 12:38:22 +02:00
|
|
|
}
|
2023-05-20 11:52:24 +02:00
|
|
|
});
|
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)) {
|
2023-07-28 00:01:49 +02:00
|
|
|
PopupError::open(hex::format("hex.builtin.popup.error.project.load"_lang, wolv::util::toUTF8String(path)));
|
2022-09-20 15:33:36 +02:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
EventManager::subscribe<EventProviderChanged>([](auto, auto) {
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
|
|
|
});
|
2022-07-02 17:53:13 +02:00
|
|
|
|
2023-05-05 22:02:18 +02:00
|
|
|
// Handles the provider initialization, and calls EventProviderOpened if successful
|
|
|
|
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()) {
|
2023-04-30 18:37:22 +02:00
|
|
|
PopupError::open(hex::format("hex.builtin.provider.error.open"_lang, provider->getErrorMessage()));
|
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()) {
|
2023-04-30 18:37:22 +02:00
|
|
|
PopupError::open(hex::format("hex.builtin.provider.error.open"_lang, provider->getErrorMessage()));
|
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
|
|
|
|
2022-11-09 18:49:07 +01:00
|
|
|
EventManager::subscribe<EventRegionSelected>([](const ImHexApi::HexEditor::ProviderRegion ®ion) {
|
|
|
|
ImHexApi::HexEditor::impl::setCurrentSelection(region);
|
|
|
|
});
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
EventManager::subscribe<RequestOpenInfoPopup>([](const std::string &message) {
|
|
|
|
PopupInfo::open(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
EventManager::subscribe<RequestOpenErrorPopup>([](const std::string &message) {
|
|
|
|
PopupError::open(message);
|
|
|
|
});
|
|
|
|
|
|
|
|
EventManager::subscribe<RequestOpenFatalPopup>([](const std::string &message) {
|
|
|
|
PopupFatal::open(message);
|
|
|
|
});
|
|
|
|
|
2023-03-26 11:02:51 +02:00
|
|
|
fs::setFileBrowserErrorCallback([](const std::string& errMsg){
|
2022-09-20 15:47:59 +02:00
|
|
|
#if defined(NFD_PORTAL)
|
2023-04-08 00:58:53 +02:00
|
|
|
PopupError::open(hex::format("hex.builtin.popup.error.file_dialog.portal"_lang, errMsg));
|
2022-09-20 15:47:59 +02:00
|
|
|
#else
|
2023-04-08 00:58:53 +02:00
|
|
|
PopupError::open(hex::format("hex.builtin.popup.error.file_dialog.common"_lang, errMsg));
|
2022-09-20 15:47:59 +02:00
|
|
|
#endif
|
|
|
|
});
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 16:05:03 +01:00
|
|
|
}
|