#include #include #include #include #include #include #include #include #include #include #include #include namespace hex::plugin::builtin { static void openFile(const std::fs::path &path) { auto provider = ImHexApi::Provider::createProvider("hex.builtin.provider.file", true); if (auto *fileProvider = dynamic_cast(provider); fileProvider != nullptr) { fileProvider->setPath(path); if (fileProvider->open()) EventManager::post(fileProvider); } } void registerEventHandlers() { EventManager::subscribe([](GLFWwindow *window) { if (ImHexApi::Provider::isDirty()) { glfwSetWindowShouldClose(window, GLFW_FALSE); TaskManager::doLater([] { ImGui::OpenPopup("hex.builtin.popup.exit_application.title"_lang); }); } }); EventManager::subscribe([](hex::prv::Provider *provider, bool *shouldClose) { if (provider->isDirty()) { *shouldClose = false; TaskManager::doLater([] { ImGui::OpenPopup("hex.builtin.popup.close_provider.title"_lang); }); } }); EventManager::subscribe([](hex::prv::Provider *oldProvider, hex::prv::Provider *newProvider) { hex::unused(oldProvider); hex::unused(newProvider); EventManager::post(); }); EventManager::subscribe([](hex::prv::Provider *provider) { if (provider != nullptr && ImHexApi::Provider::get() == provider) EventManager::post(); }); EventManager::subscribe(openFile); EventManager::subscribe([](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(path); }); } else if (name == "Open File") { ImHexApi::Provider::createProvider("hex.builtin.provider.file"); } else if (name == "Open Project") { fs::openFileBrowser(fs::DialogMode::Open, { {"Project File", "hexproj"} }, [](const auto &path) { if (!ProjectFile::load(path)) { View::showErrorPopup("hex.builtin.popup.error.project.load"_lang); } }); } }); EventManager::subscribe([](auto, auto) { EventManager::post(); }); EventManager::subscribe([](hex::prv::Provider *provider) { if (provider->shouldSkipLoadInterface()) return; if (provider->hasFilePicker()) { if (!provider->handleFilePicker()) { TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); }); return; } if (!provider->open()) { View::showErrorPopup("hex.builtin.popup.error.open"_lang); TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); }); return; } EventManager::post(provider); } else if (provider->hasLoadInterface()) EventManager::post(View::toWindowName("hex.builtin.view.provider_settings.load_popup")); else { if (!provider->open() || !provider->isAvailable()) { View::showErrorPopup("hex.builtin.popup.error.open"_lang); TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); }); return; } EventManager::post(provider); } }); EventManager::subscribe([](hex::prv::Provider *provider) { ProviderExtraData::erase(provider); }); EventManager::subscribe([](const ImHexApi::HexEditor::ProviderRegion ®ion) { ImHexApi::HexEditor::impl::setCurrentSelection(region); }); 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 }); } }