diff --git a/include/helpers/project_file_handler.hpp b/include/helpers/project_file_handler.hpp index a80a80dd1..b4939784a 100644 --- a/include/helpers/project_file_handler.hpp +++ b/include/helpers/project_file_handler.hpp @@ -36,6 +36,10 @@ namespace hex { return ProjectFile::s_currProjectFilePath; } + static void clearProjectFilePath() { + ProjectFile::s_currProjectFilePath.clear(); + } + [[nodiscard]] static std::string getFilePath() { return ProjectFile::s_filePath; diff --git a/include/window.hpp b/include/window.hpp index 722c0956c..948735e48 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -26,6 +26,7 @@ namespace hex { friend void ImHexSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buf); bool setFont(const std::filesystem::path &font_path); + private: void frameBegin(); void frame(); @@ -54,6 +55,8 @@ namespace hex { u32 m_bannerWidth = 0, m_bannerHeight = 0; void *m_bannerTexture = nullptr; + + std::filesystem::path m_safetyBackupPath; }; } \ No newline at end of file diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index ba008b315..96c6350b4 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -48,6 +48,11 @@ namespace hex::plugin::builtin { { "hex.welcome.learn.plugins.link", "https://github.com/WerWolv/ImHex/wiki/Plugins-Development-Guide" }, { "hex.welcome.header.various", "Verschiedenes" }, + { "hex.safety_backup.title", "Verlorene Daten wiederherstellen" }, + { "hex.safety_backup.desc", "Oh nein, ImHex ist letztes mal abgestürtzt.\nWillst du das verherige Projekt wiederherstellen?"}, + { "hex.safety_backup.restore", "Ja, Wiederherstellen" }, + { "hex.safety_backup.delete", "Nein, Entfernen" }, + { "hex.common.little_endian", "Little Endian" }, { "hex.common.big_endian", "Big Endian" }, { "hex.common.decimal", "Dezimal" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 4551c23fb..f26c277e0 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -48,6 +48,12 @@ namespace hex::plugin::builtin { { "hex.welcome.learn.plugins.link", "https://github.com/WerWolv/ImHex/wiki/Plugins-Development-Guide" }, { "hex.welcome.header.various", "Various" }, + { "hex.safety_backup.title", "Restore lost data" }, + { "hex.safety_backup.desc", "Oh no, ImHex crashed last time.\nDo you want to restore your past work?"}, + { "hex.safety_backup.restore", "Yes, Restore" }, + { "hex.safety_backup.delete", "No, Delete" }, + + { "hex.common.little_endian", "Little Endian" }, { "hex.common.big_endian", "Big Endian" }, { "hex.common.decimal", "Decimal" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index f5a70eda1..090ac58b0 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -48,6 +48,11 @@ namespace hex::plugin::builtin { { "hex.welcome.learn.plugins.link", "https://github.com/WerWolv/ImHex/wiki/Plugins-Development-Guide" }, { "hex.welcome.header.various", "Varie" }, + //{ "hex.safety_backup.title", "Restore lost data" }, + //{ "hex.safety_backup.desc", "Oh no, ImHex crashed last time.\nDo you want to restore your past work?"}, + //{ "hex.safety_backup.restore", "Yes, Restore" }, + //{ "hex.safety_backup.delete", "No, Delete" }, + { "hex.common.little_endian", "Little Endian" }, { "hex.common.big_endian", "Big Endian" }, { "hex.common.decimal", "Decimale" }, diff --git a/source/window.cpp b/source/window.cpp index 5bbab6b93..58a14a6ad 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -184,13 +184,22 @@ namespace hex { glfwSetWindowTitle(this->m_window, title.c_str()); }); - EventManager::subscribe(this, [](int signal) { + constexpr auto CrashBackupFileName = "crash_backup.hexproj"; + + EventManager::subscribe(this, [CrashBackupFileName](int signal) { for (const auto &path : hex::getPath(ImHexPath::Config)) { - if (ProjectFile::store((std::filesystem::path(path) / "crash_backup.hexproj").string())) + if (ProjectFile::store((std::filesystem::path(path) / CrashBackupFileName).string())) break; } }); + for (const auto &path : hex::getPath(ImHexPath::Config)) { + if (auto filePath = std::filesystem::path(path) / CrashBackupFileName; std::filesystem::exists(filePath)) { + this->m_safetyBackupPath = filePath; + View::doLater([]{ ImGui::OpenPopup("hex.safety_backup.title"_lang); }); + } + } + EventManager::post(); for (const auto &path : ContentRegistry::Settings::read("hex.builtin.setting.imhex", "hex.builtin.setting.imhex.recent_files")) @@ -399,6 +408,35 @@ namespace hex { ImGui::TextUnformatted("To find out where your plugin folder is, check ImHex' Readme."); ImGui::EndPopup(); } + + // Popup for if there is a safety backup present because ImHex crashed + ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5F, 0.5F)); + if (ImGui::BeginPopupModal("hex.safety_backup.title"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove)) { + ImGui::TextUnformatted("hex.safety_backup.desc"_lang); + ImGui::NewLine(); + + auto width = ImGui::GetWindowWidth(); + ImGui::SetCursorPosX(width / 9); + if (ImGui::Button("hex.safety_backup.restore"_lang, ImVec2(width / 3, 0))) { + ProjectFile::load(this->m_safetyBackupPath.string()); + EventManager::post(); + ProjectFile::markDirty(); + + ProjectFile::clearProjectFilePath(); + std::filesystem::remove(this->m_safetyBackupPath); + + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + ImGui::SetCursorPosX(width / 9 * 5); + if (ImGui::Button("hex.safety_backup.delete"_lang, ImVec2(width / 3, 0))) { + std::filesystem::remove(this->m_safetyBackupPath); + + ImGui::CloseCurrentPopup(); + } + + ImGui::EndPopup(); + } } void Window::frame() {