#include #include #include #include #include #include #include "content/providers/file_provider.hpp" namespace hex::plugin::builtin { static void openFile(const std::fs::path &path) { hex::prv::Provider *provider = nullptr; EventManager::post("hex.builtin.provider.file", &provider); if (auto fileProvider = dynamic_cast(provider)) { fileProvider->setPath(path); if (!fileProvider->open()) { View::showErrorPopup("hex.builtin.popup.error.open"_lang); ImHexApi::Provider::remove(provider); return; } } if (!provider->isWritable()) { View::showErrorPopup("hex.builtin.popup.error.read_only"_lang); } if (!provider->isAvailable()) { View::showErrorPopup("hex.builtin.popup.error.open"_lang); ImHexApi::Provider::remove(provider); return; } ProjectFile::setFilePath(path); EventManager::post(path); EventManager::post(); EventManager::post(); } void registerEventHandlers() { EventManager::subscribe([]() { EventManager::post(ProjectFile::getFilePath()); }); EventManager::subscribe([](GLFWwindow *window) { if (ProjectFile::hasUnsavedChanges()) { glfwSetWindowShouldClose(window, GLFW_FALSE); ImHexApi::Tasks::doLater([] { ImGui::OpenPopup("hex.builtin.popup.exit_application.title"_lang); }); } }); 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") { fs::openFileBrowser(fs::DialogMode::Open, {}, [](const auto &path) { EventManager::post(path); }); } else if (name == "Open Project") { fs::openFileBrowser(fs::DialogMode::Open, { {"Project File", "hexproj"} }, [](const auto &path) { ProjectFile::load(path); }); } }); EventManager::subscribe([](auto, auto) { EventManager::post(); }); } }