1
0
mirror of synced 2024-11-24 15:50:16 +01:00

impr: Make auto backups not remove dirty status from project

This commit is contained in:
WerWolv 2024-06-19 13:51:36 +02:00
parent 9b594d81bd
commit bf7beab0ab

View File

@ -9,18 +9,20 @@
#include <hex/helpers/fmt.hpp> #include <hex/helpers/fmt.hpp>
#include <fmt/chrono.h> #include <fmt/chrono.h>
#include <hex/helpers/logger.hpp> #include <hex/helpers/logger.hpp>
#include <hex/providers/provider.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <wolv/utils/string.hpp>
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
static bool networkInterfaceServiceEnabled = false; static bool s_networkInterfaceServiceEnabled = false;
static int autoBackupTime = 0; static int s_autoBackupTime = 0;
namespace { namespace {
void handleNetworkInterfaceService() { void handleNetworkInterfaceService() {
if (!networkInterfaceServiceEnabled) { if (!s_networkInterfaceServiceEnabled) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
return; return;
} }
@ -62,21 +64,35 @@ namespace hex::plugin::builtin {
}); });
} }
bool s_dataDirty = false;
void handleAutoBackup() { void handleAutoBackup() {
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
static std::chrono::time_point<std::chrono::steady_clock> lastBackupTime = now; static std::chrono::time_point<std::chrono::steady_clock> lastBackupTime = now;
if (autoBackupTime > 0 && (now - lastBackupTime) > std::chrono::seconds(autoBackupTime)) { if (s_autoBackupTime > 0 && (now - lastBackupTime) > std::chrono::seconds(s_autoBackupTime)) {
lastBackupTime = now; lastBackupTime = now;
if (ImHexApi::Provider::isValid() && ImHexApi::Provider::isDirty()) { if (ImHexApi::Provider::isValid() && s_dataDirty) {
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Backups)) { s_dataDirty = false;
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)) std::vector<prv::Provider *> dirtyProviders;
break; 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() { void registerBackgroundServices() {
ContentRegistry::Settings::onChange("hex.builtin.setting.general", "hex.builtin.setting.general.network_interface", [](const ContentRegistry::Settings::SettingsValue &value) { ContentRegistry::Settings::onChange("hex.builtin.setting.general", "hex.builtin.setting.general.network_interface", [](const ContentRegistry::Settings::SettingsValue &value) {
networkInterfaceServiceEnabled = value.get<bool>(false); s_networkInterfaceServiceEnabled = value.get<bool>(false);
}); });
ContentRegistry::Settings::onChange("hex.builtin.setting.general", "hex.builtin.setting.general.auto_backup_time", [](const ContentRegistry::Settings::SettingsValue &value) { ContentRegistry::Settings::onChange("hex.builtin.setting.general", "hex.builtin.setting.general.auto_backup_time", [](const ContentRegistry::Settings::SettingsValue &value) {
autoBackupTime = value.get<int>(0) * 30; s_autoBackupTime = value.get<int>(0) * 30;
}); });
ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.network_interface", handleNetworkInterfaceService); ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.network_interface", handleNetworkInterfaceService);
ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.auto_backup", handleAutoBackup); ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.auto_backup", handleAutoBackup);
EventProviderDirtied::subscribe([](prv::Provider *) {
s_dataDirty = true;
});
} }
} }