From bf7beab0abc032d64af7280df81d18c2424c95b8 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 19 Jun 2024 13:51:36 +0200 Subject: [PATCH] impr: Make auto backups not remove dirty status from project --- .../source/content/background_services.cpp | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/plugins/builtin/source/content/background_services.cpp b/plugins/builtin/source/content/background_services.cpp index 5c07b3395..234aabfc0 100644 --- a/plugins/builtin/source/content/background_services.cpp +++ b/plugins/builtin/source/content/background_services.cpp @@ -9,18 +9,20 @@ #include #include #include +#include #include +#include namespace hex::plugin::builtin { - static bool networkInterfaceServiceEnabled = false; - static int autoBackupTime = 0; + static bool s_networkInterfaceServiceEnabled = false; + static int s_autoBackupTime = 0; namespace { void handleNetworkInterfaceService() { - if (!networkInterfaceServiceEnabled) { + if (!s_networkInterfaceServiceEnabled) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); return; } @@ -62,21 +64,35 @@ namespace hex::plugin::builtin { }); } + bool s_dataDirty = false; void handleAutoBackup() { auto now = std::chrono::steady_clock::now(); static std::chrono::time_point lastBackupTime = now; - if (autoBackupTime > 0 && (now - lastBackupTime) > std::chrono::seconds(autoBackupTime)) { + if (s_autoBackupTime > 0 && (now - lastBackupTime) > std::chrono::seconds(s_autoBackupTime)) { lastBackupTime = now; - if (ImHexApi::Provider::isValid() && ImHexApi::Provider::isDirty()) { - for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Backups)) { - const auto fileName = hex::format("auto_backup.{:%y%m%d_%H%M%S}.hexproj", fmt::gmtime(std::chrono::system_clock::now())); - if (ProjectFile::store(path / fileName, false)) - break; + if (ImHexApi::Provider::isValid() && s_dataDirty) { + s_dataDirty = false; + + std::vector dirtyProviders; + for (const auto &provider : ImHexApi::Provider::getProviders()) { + if (provider->isDirty()) { + dirtyProviders.push_back(provider); + } } - log::info("Backed up project"); + for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Backups)) { + const auto backupPath = path / hex::format("auto_backup.{:%y%m%d_%H%M%S}.hexproj", fmt::gmtime(std::chrono::system_clock::now())); + if (ProjectFile::store(backupPath, false)) { + log::info("Created auto-backup file '{}'", wolv::util::toUTF8String(backupPath)); + break; + } + } + + for (const auto &provider : dirtyProviders) { + provider->markDirty(); + } } } @@ -87,15 +103,19 @@ namespace hex::plugin::builtin { void registerBackgroundServices() { ContentRegistry::Settings::onChange("hex.builtin.setting.general", "hex.builtin.setting.general.network_interface", [](const ContentRegistry::Settings::SettingsValue &value) { - networkInterfaceServiceEnabled = value.get(false); + s_networkInterfaceServiceEnabled = value.get(false); }); ContentRegistry::Settings::onChange("hex.builtin.setting.general", "hex.builtin.setting.general.auto_backup_time", [](const ContentRegistry::Settings::SettingsValue &value) { - autoBackupTime = value.get(0) * 30; + s_autoBackupTime = value.get(0) * 30; }); ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.network_interface", handleNetworkInterfaceService); ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.auto_backup", handleAutoBackup); + + EventProviderDirtied::subscribe([](prv::Provider *) { + s_dataDirty = true; + }); } -} \ No newline at end of file +}