2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/event_manager.hpp>
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
#include <hex/api/localization_manager.hpp>
|
2023-10-17 13:45:12 +02:00
|
|
|
#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>
|
|
|
|
|
|
|
|
#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 {
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::post(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;
|
2023-12-08 10:29:44 +01:00
|
|
|
EventWindowClosing::subscribe([](GLFWwindow *window) {
|
2023-11-24 11:42:27 +01:00
|
|
|
imhexClosing = false;
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderClosing::subscribe([](const prv::Provider *provider, bool *shouldClose) {
|
2022-08-08 21:23:52 +02:00
|
|
|
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-10 20:47:08 +01:00
|
|
|
for (const auto &provider : ImHexApi::Provider::impl::getClosingProviders())
|
2023-11-07 15:31:54 +01:00
|
|
|
ImHexApi::Provider::remove(provider, true);
|
2023-11-24 11:42:27 +01:00
|
|
|
|
|
|
|
if (imhexClosing)
|
|
|
|
ImHexApi::System::closeImHex(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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderChanged::subscribe([](hex::prv::Provider *oldProvider, hex::prv::Provider *newProvider) {
|
2022-08-10 00:11:16 +02:00
|
|
|
hex::unused(oldProvider);
|
2023-01-07 17:31:22 +01:00
|
|
|
hex::unused(newProvider);
|
2022-08-10 00:11:16 +02:00
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestUpdateWindowTitle::post();
|
2022-08-10 00:11:16 +02:00
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::subscribe([](hex::prv::Provider *provider) {
|
2022-08-22 11:00:31 +02:00
|
|
|
if (provider != nullptr && ImHexApi::Provider::get() == provider)
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestUpdateWindowTitle::post();
|
|
|
|
EventProviderChanged::post(nullptr, provider);
|
2022-08-22 11:00:31 +02:00
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenFile::subscribe(openFile);
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenWindow::subscribe([](const std::string &name) {
|
2022-05-27 20:42:07 +02:00
|
|
|
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
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::post(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-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::post(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
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderChanged::subscribe([](auto, auto) {
|
|
|
|
EventHighlightingChanged::post();
|
2022-05-27 20:42:07 +02:00
|
|
|
});
|
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
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderCreated::subscribe([](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
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::post(provider);
|
2022-08-12 15:11:27 +02:00
|
|
|
}
|
2023-11-27 15:34:05 +01:00
|
|
|
else if (!provider->hasLoadInterface()) {
|
2022-08-12 15:11:27 +02:00
|
|
|
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
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::post(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
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventRegionSelected::subscribe([](const ImHexApi::HexEditor::ProviderRegion ®ion) {
|
2022-11-09 18:49:07 +01:00
|
|
|
ImHexApi::HexEditor::impl::setCurrentSelection(region);
|
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenInfoPopup::subscribe([](const std::string &message) {
|
2023-04-08 00:58:53 +02:00
|
|
|
PopupInfo::open(message);
|
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenErrorPopup::subscribe([](const std::string &message) {
|
2023-04-08 00:58:53 +02:00
|
|
|
PopupError::open(message);
|
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenFatalPopup::subscribe([](const std::string &message) {
|
2023-04-08 00:58:53 +02:00
|
|
|
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
|
|
|
}
|