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>
|
2024-01-10 23:46:50 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2023-10-17 13:45:12 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
|
|
|
#include <hex/api/achievement_manager.hpp>
|
2024-01-28 22:14:59 +01:00
|
|
|
#include <hex/api/workspace_manager.hpp>
|
2023-10-17 13:45:12 +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 <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-12-26 00:22:47 +01:00
|
|
|
#include <toasts/toast_notification.hpp>
|
2023-12-23 21:09:41 +01:00
|
|
|
#include <popups/popup_notification.hpp>
|
|
|
|
#include <popups/popup_question.hpp>
|
2023-04-08 00:58:53 +02:00
|
|
|
#include <content/popups/popup_tasks_waiting.hpp>
|
2023-11-07 15:31:54 +01:00
|
|
|
#include <content/popups/popup_unsaved_changes.hpp>
|
2024-03-01 18:21:15 +01:00
|
|
|
#include <content/popups/popup_crash_recovered.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) {
|
2024-02-26 21:43:28 +01:00
|
|
|
if (path.extension() == ".hexproj") {
|
|
|
|
if (!ProjectFile::load(path)) {
|
|
|
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang, wolv::util::toUTF8String(path)));
|
2024-02-26 21:48:56 +01:00
|
|
|
} else {
|
|
|
|
return;
|
2024-02-26 21:43:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()) {
|
2023-12-26 00:22:47 +01:00
|
|
|
ui::ToastError::open(hex::format("hex.builtin.provider.error.open"_lang, provider->getErrorMessage()));
|
2023-07-15 00:12:09 +02:00
|
|
|
TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); });
|
|
|
|
} else {
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::post(fileProvider);
|
2024-03-15 21:07:45 +01:00
|
|
|
AchievementManager::unlockAchievement("hex.builtin.achievement.starting_out", "hex.builtin.achievement.starting_out.open_file.name");
|
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;
|
2024-03-01 18:21:15 +01:00
|
|
|
EventCrashRecovered::subscribe([](const std::exception &e) {
|
|
|
|
PopupCrashRecovered::open(e);
|
|
|
|
});
|
|
|
|
|
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-12-23 21:09:41 +01:00
|
|
|
ui::PopupQuestion::open("hex.builtin.popup.exit_application.desc"_lang,
|
2023-11-07 15:31:54 +01:00
|
|
|
[] {
|
|
|
|
imhexClosing = true;
|
2024-02-10 23:31:05 +01:00
|
|
|
for (const auto &provider : ImHexApi::Provider::getProviders())
|
2023-11-07 15:31:54 +01:00
|
|
|
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) {
|
2024-02-23 17:59:37 +01:00
|
|
|
if (provider != nullptr && ImHexApi::Provider::get() == provider) {
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestUpdateWindowTitle::post();
|
2024-02-23 17:59:37 +01:00
|
|
|
}
|
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-12-26 00:22:47 +01:00
|
|
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang, wolv::util::toUTF8String(path)));
|
2024-02-26 21:48:56 +01:00
|
|
|
} else {
|
|
|
|
return;
|
2023-05-11 12:38:22 +02:00
|
|
|
}
|
2024-02-26 21:48:56 +01:00
|
|
|
}
|
2023-05-20 11:52:24 +02:00
|
|
|
|
2024-02-26 21:48:56 +01:00
|
|
|
auto newProvider = static_cast<FileProvider*>(
|
|
|
|
ImHexApi::Provider::createProvider("hex.builtin.provider.file", true)
|
|
|
|
);
|
2023-05-20 11:52:24 +02:00
|
|
|
|
2024-02-26 21:48:56 +01:00
|
|
|
if (newProvider == nullptr)
|
|
|
|
return;
|
2023-10-17 13:45:12 +02:00
|
|
|
|
2024-02-26 21:48:56 +01:00
|
|
|
newProvider->setPath(path);
|
|
|
|
if (!newProvider->open()) {
|
|
|
|
hex::ImHexApi::Provider::remove(newProvider);
|
|
|
|
} else {
|
|
|
|
EventProviderOpened::post(newProvider);
|
|
|
|
AchievementManager::unlockAchievement("hex.builtin.achievement.starting_out", "hex.builtin.achievement.starting_out.open_file.name");
|
2023-05-11 12:38:22 +02:00
|
|
|
}
|
2024-02-23 18:03:37 +01:00
|
|
|
}, {}, true);
|
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-12-26 00:22:47 +01:00
|
|
|
ui::ToastError::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-12-26 00:22:47 +01:00
|
|
|
ui::ToastError::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-12-26 00:22:47 +01:00
|
|
|
ui::ToastError::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);
|
|
|
|
});
|
|
|
|
|
2024-01-10 23:46:50 +01:00
|
|
|
EventFileDropped::subscribe([](const std::fs::path &path) {
|
|
|
|
// Check if a custom file handler can handle the file
|
|
|
|
bool handled = false;
|
|
|
|
for (const auto &[extensions, handler] : ContentRegistry::FileHandler::impl::getEntries()) {
|
|
|
|
for (const auto &extension : extensions) {
|
|
|
|
if (path.extension() == extension) {
|
|
|
|
// Pass the file to the handler and check if it was successful
|
|
|
|
if (!handler(path)) {
|
|
|
|
log::error("Handler for extensions '{}' failed to process file!", extension);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no custom handler was found, just open the file regularly
|
|
|
|
if (!handled)
|
|
|
|
RequestOpenFile::post(path);
|
|
|
|
});
|
|
|
|
|
2024-01-21 18:39:51 +01:00
|
|
|
EventWindowInitialized::subscribe([] {
|
2024-02-03 12:16:36 +01:00
|
|
|
if (ContentRegistry::Settings::read<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.prev_launch_version", "") == "") {
|
2024-01-21 18:39:51 +01:00
|
|
|
EventFirstLaunch::post();
|
|
|
|
}
|
|
|
|
|
2024-02-03 12:16:36 +01:00
|
|
|
ContentRegistry::Settings::write<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.prev_launch_version", ImHexApi::System::getImHexVersion());
|
2024-01-21 18:39:51 +01:00
|
|
|
});
|
|
|
|
|
2024-01-28 22:14:59 +01:00
|
|
|
EventWindowDeinitializing::subscribe([](GLFWwindow *window) {
|
|
|
|
WorkspaceManager::exportToFile();
|
|
|
|
if (auto workspace = WorkspaceManager::getCurrentWorkspace(); workspace != WorkspaceManager::getWorkspaces().end())
|
2024-02-03 12:16:36 +01:00
|
|
|
ContentRegistry::Settings::write<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.curr_workspace", workspace->first);
|
2024-01-28 22:14:59 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
int x = 0, y = 0, width = 0, height = 0, maximized = 0;
|
|
|
|
glfwGetWindowPos(window, &x, &y);
|
|
|
|
glfwGetWindowSize(window, &width, &height);
|
|
|
|
maximized = glfwGetWindowAttrib(window, GLFW_MAXIMIZED);
|
|
|
|
|
2024-02-03 12:16:36 +01:00
|
|
|
ContentRegistry::Settings::write<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.x", x);
|
|
|
|
ContentRegistry::Settings::write<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.y", y);
|
|
|
|
ContentRegistry::Settings::write<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.width", width);
|
|
|
|
ContentRegistry::Settings::write<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.height", height);
|
|
|
|
ContentRegistry::Settings::write<int>("hex.builtin.setting.interface", "hex.builtin.setting.interface.window.maximized", maximized);
|
2024-01-28 22:14:59 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-01 10:58:45 +01:00
|
|
|
EventImHexStartupFinished::subscribe([] {
|
|
|
|
const auto &initArgs = ImHexApi::System::getInitArguments();
|
|
|
|
if (auto it = initArgs.find("language"); it != initArgs.end())
|
|
|
|
LocalizationManager::loadLanguage(it->second);
|
|
|
|
});
|
|
|
|
|
2024-02-03 23:56:08 +01:00
|
|
|
EventWindowFocused::subscribe([](bool focused) {
|
|
|
|
const auto ctx = ImGui::GetCurrentContext();
|
|
|
|
if (ctx == nullptr)
|
|
|
|
return;
|
|
|
|
|
2024-02-24 18:59:01 +01:00
|
|
|
if (ImGui::IsPopupOpen("", ImGuiPopupFlags_AnyPopup))
|
|
|
|
return;
|
2024-02-28 23:52:07 +01:00
|
|
|
if (ImGui::IsAnyItemHovered())
|
|
|
|
return;
|
2024-02-24 18:59:01 +01:00
|
|
|
|
2024-02-23 17:48:37 +01:00
|
|
|
static ImGuiWindow *lastFocusedWindow = nullptr;
|
2024-02-03 23:56:08 +01:00
|
|
|
|
|
|
|
if (focused) {
|
|
|
|
// If the main window gains focus again, restore the last focused window
|
2024-02-23 17:48:37 +01:00
|
|
|
ImGui::FocusWindow(lastFocusedWindow);
|
2024-02-03 23:56:08 +01:00
|
|
|
ImGui::FocusWindow(lastFocusedWindow, ImGuiFocusRequestFlags_RestoreFocusedChild);
|
2024-02-23 17:48:37 +01:00
|
|
|
|
|
|
|
if (lastFocusedWindow != nullptr)
|
|
|
|
log::debug("Restoring focus on window '{}'", lastFocusedWindow->Name ? lastFocusedWindow->Name : "Unknown Window");
|
2024-02-03 23:56:08 +01:00
|
|
|
} else {
|
|
|
|
// If the main window loses focus, store the currently focused window
|
|
|
|
// and remove focus from it so it doesn't look like it's focused and
|
|
|
|
// cursor blink animations don't play
|
2024-02-23 17:48:37 +01:00
|
|
|
lastFocusedWindow = ctx->NavWindow;
|
2024-02-03 23:56:08 +01:00
|
|
|
ImGui::FocusWindow(nullptr);
|
2024-02-23 17:48:37 +01:00
|
|
|
|
|
|
|
if (lastFocusedWindow != nullptr)
|
|
|
|
log::debug("Removing focus from window '{}'", lastFocusedWindow->Name ? lastFocusedWindow->Name : "Unknown Window");
|
2024-02-03 23:56:08 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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-12-23 21:09:41 +01:00
|
|
|
ui::PopupError::open(hex::format("hex.builtin.popup.error.file_dialog.portal"_lang, errMsg));
|
2022-09-20 15:47:59 +02:00
|
|
|
#else
|
2023-12-23 21:09:41 +01:00
|
|
|
ui::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
|
|
|
}
|