sys: Save project backup if ImHex crashes

This commit is contained in:
WerWolv 2021-08-17 13:41:44 +02:00
parent 551da69a4c
commit 4d677f430b
5 changed files with 33 additions and 13 deletions

View File

@ -106,6 +106,7 @@ namespace hex {
EVENT_DEF(EventProjectFileStore); EVENT_DEF(EventProjectFileStore);
EVENT_DEF(EventProjectFileLoad); EVENT_DEF(EventProjectFileLoad);
EVENT_DEF(EventSettingsChanged); EVENT_DEF(EventSettingsChanged);
EVENT_DEF(EventAbnormalTermination, int);
EVENT_DEF(RequestOpenWindow, std::string); EVENT_DEF(RequestOpenWindow, std::string);
EVENT_DEF(RequestSelectionChange, Region); EVENT_DEF(RequestSelectionChange, Region);

View File

@ -214,8 +214,6 @@ namespace hex {
void ContentRegistry::Language::addLocalizations(std::string_view languageCode, const LanguageDefinition &definition) { void ContentRegistry::Language::addLocalizations(std::string_view languageCode, const LanguageDefinition &definition) {
getLanguageDefinitions()[languageCode.data()].push_back(definition); getLanguageDefinitions()[languageCode.data()].push_back(definition);
EventManager::post<EventSettingsChanged>();
} }
std::map<std::string, std::string>& ContentRegistry::Language::getLanguages() { std::map<std::string, std::string>& ContentRegistry::Language::getLanguages() {

View File

@ -198,10 +198,6 @@ namespace hex {
} }
}); });
EventManager::subscribe<EventFileLoaded>(this, [](std::string path) {
EventManager::post<RequestChangeWindowTitle>(std::filesystem::path(path).filename().string());
});
EventManager::subscribe<EventSettingsChanged>(this, [this] { EventManager::subscribe<EventSettingsChanged>(this, [this] {
auto alpha = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.highlight_alpha"); auto alpha = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.highlight_alpha");
@ -217,7 +213,6 @@ namespace hex {
EventManager::unsubscribe<EventWindowClosing>(this); EventManager::unsubscribe<EventWindowClosing>(this);
EventManager::unsubscribe<EventPatternChanged>(this); EventManager::unsubscribe<EventPatternChanged>(this);
EventManager::unsubscribe<RequestOpenWindow>(this); EventManager::unsubscribe<RequestOpenWindow>(this);
EventManager::unsubscribe<EventFileLoaded>(this);
EventManager::unsubscribe<EventSettingsChanged>(this); EventManager::unsubscribe<EventSettingsChanged>(this);
} }
@ -442,8 +437,6 @@ namespace hex {
} }
if (ImGui::MenuItem("hex.view.hexeditor.menu.file.save_project"_lang, "", false, provider != nullptr && provider->isWritable())) { if (ImGui::MenuItem("hex.view.hexeditor.menu.file.save_project"_lang, "", false, provider != nullptr && provider->isWritable())) {
EventManager::post<EventProjectFileStore>();
if (ProjectFile::getProjectFilePath() == "") { if (ProjectFile::getProjectFilePath() == "") {
View::openFileBrowser("hex.view.hexeditor.save_project"_lang, DialogMode::Save, { { "Project File", "hexproj" } }, [](auto path) { View::openFileBrowser("hex.view.hexeditor.save_project"_lang, DialogMode::Save, { { "Project File", "hexproj" } }, [](auto path) {
if (path.ends_with(".hexproj")) { if (path.ends_with(".hexproj")) {

View File

@ -43,6 +43,9 @@ namespace hex {
View::doLater([]{ ImGui::OpenPopup(View::toWindowName("hex.view.settings.name").c_str()); }); View::doLater([]{ ImGui::OpenPopup(View::toWindowName("hex.view.settings.name").c_str()); });
this->getWindowOpenState() = true; this->getWindowOpenState() = true;
} }
if (ImGui::MenuItem("Crash")) {
*reinterpret_cast<int*>(8) = 16;
}
ImGui::EndMenu(); ImGui::EndMenu();
} }
} }

View File

@ -5,6 +5,7 @@
#include <hex/resources.hpp> #include <hex/resources.hpp>
#include <chrono> #include <chrono>
#include <csignal>
#include <iostream> #include <iostream>
#include <numeric> #include <numeric>
#include <typeinfo> #include <typeinfo>
@ -173,16 +174,40 @@ namespace hex {
}); });
EventManager::subscribe<RequestChangeWindowTitle>(this, [this](std::string windowTitle) { EventManager::subscribe<RequestChangeWindowTitle>(this, [this](std::string windowTitle) {
if (windowTitle.empty()) std::string title = "ImHex";
glfwSetWindowTitle(this->m_window, "ImHex"); if (!windowTitle.empty())
else title += " - " + windowTitle;
glfwSetWindowTitle(this->m_window, ("ImHex - " + windowTitle).c_str());
if (ProjectFile::hasUnsavedChanges())
title += " (*)";
glfwSetWindowTitle(this->m_window, title.c_str());
});
EventManager::subscribe<EventAbnormalTermination>(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<EventSettingsChanged>(); EventManager::post<EventSettingsChanged>();
for (const auto &path : ContentRegistry::Settings::read("hex.builtin.setting.imhex", "hex.builtin.setting.imhex.recent_files")) for (const auto &path : ContentRegistry::Settings::read("hex.builtin.setting.imhex", "hex.builtin.setting.imhex.recent_files"))
SharedData::recentFilePaths.push_back(path); SharedData::recentFilePaths.push_back(path);
auto signalHandler = [](int signalNumber) {
EventManager::post<EventAbnormalTermination>(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() { Window::~Window() {