From 4d677f430b6babc8321f4a29fc7d3c0ae24ae2e7 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 17 Aug 2021 13:41:44 +0200 Subject: [PATCH] sys: Save project backup if ImHex crashes --- plugins/libimhex/include/hex/api/event.hpp | 1 + .../libimhex/source/api/content_registry.cpp | 2 -- source/views/view_hexeditor.cpp | 7 ---- source/views/view_settings.cpp | 3 ++ source/window.cpp | 33 ++++++++++++++++--- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/plugins/libimhex/include/hex/api/event.hpp b/plugins/libimhex/include/hex/api/event.hpp index e8cae5fd5..92f5cb4e6 100644 --- a/plugins/libimhex/include/hex/api/event.hpp +++ b/plugins/libimhex/include/hex/api/event.hpp @@ -106,6 +106,7 @@ namespace hex { EVENT_DEF(EventProjectFileStore); EVENT_DEF(EventProjectFileLoad); EVENT_DEF(EventSettingsChanged); + EVENT_DEF(EventAbnormalTermination, int); EVENT_DEF(RequestOpenWindow, std::string); EVENT_DEF(RequestSelectionChange, Region); diff --git a/plugins/libimhex/source/api/content_registry.cpp b/plugins/libimhex/source/api/content_registry.cpp index 193a04068..b3de495b4 100644 --- a/plugins/libimhex/source/api/content_registry.cpp +++ b/plugins/libimhex/source/api/content_registry.cpp @@ -214,8 +214,6 @@ namespace hex { void ContentRegistry::Language::addLocalizations(std::string_view languageCode, const LanguageDefinition &definition) { getLanguageDefinitions()[languageCode.data()].push_back(definition); - - EventManager::post(); } std::map& ContentRegistry::Language::getLanguages() { diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index d36634e9a..c0272e495 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -198,10 +198,6 @@ namespace hex { } }); - EventManager::subscribe(this, [](std::string path) { - EventManager::post(std::filesystem::path(path).filename().string()); - }); - EventManager::subscribe(this, [this] { auto alpha = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.highlight_alpha"); @@ -217,7 +213,6 @@ namespace hex { EventManager::unsubscribe(this); EventManager::unsubscribe(this); EventManager::unsubscribe(this); - EventManager::unsubscribe(this); EventManager::unsubscribe(this); } @@ -442,8 +437,6 @@ namespace hex { } if (ImGui::MenuItem("hex.view.hexeditor.menu.file.save_project"_lang, "", false, provider != nullptr && provider->isWritable())) { - EventManager::post(); - if (ProjectFile::getProjectFilePath() == "") { View::openFileBrowser("hex.view.hexeditor.save_project"_lang, DialogMode::Save, { { "Project File", "hexproj" } }, [](auto path) { if (path.ends_with(".hexproj")) { diff --git a/source/views/view_settings.cpp b/source/views/view_settings.cpp index 4d938afff..dfb46086a 100644 --- a/source/views/view_settings.cpp +++ b/source/views/view_settings.cpp @@ -43,6 +43,9 @@ namespace hex { View::doLater([]{ ImGui::OpenPopup(View::toWindowName("hex.view.settings.name").c_str()); }); this->getWindowOpenState() = true; } + if (ImGui::MenuItem("Crash")) { + *reinterpret_cast(8) = 16; + } ImGui::EndMenu(); } } diff --git a/source/window.cpp b/source/window.cpp index 5f411da85..5bbab6b93 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -173,16 +174,40 @@ namespace hex { }); EventManager::subscribe(this, [this](std::string windowTitle) { - if (windowTitle.empty()) - glfwSetWindowTitle(this->m_window, "ImHex"); - else - glfwSetWindowTitle(this->m_window, ("ImHex - " + windowTitle).c_str()); + std::string title = "ImHex"; + if (!windowTitle.empty()) + title += " - " + windowTitle; + + if (ProjectFile::hasUnsavedChanges()) + title += " (*)"; + + glfwSetWindowTitle(this->m_window, title.c_str()); + }); + + EventManager::subscribe(this, [](int signal) { + for (const auto &path : hex::getPath(ImHexPath::Config)) { + if (ProjectFile::store((std::filesystem::path(path) / "crash_backup.hexproj").string())) + break; + } }); EventManager::post(); for (const auto &path : ContentRegistry::Settings::read("hex.builtin.setting.imhex", "hex.builtin.setting.imhex.recent_files")) SharedData::recentFilePaths.push_back(path); + + + auto signalHandler = [](int signalNumber) { + EventManager::post(signalNumber); + std::abort(); + }; + + std::signal(SIGTERM, signalHandler); + std::signal(SIGSEGV, signalHandler); + std::signal(SIGINT, signalHandler); + std::signal(SIGILL, signalHandler); + std::signal(SIGABRT, signalHandler); + std::signal(SIGFPE, signalHandler); } Window::~Window() {