mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-01 21:17:44 -05:00
feat: Added automatic backups
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
#include <hex/api/content_registry.hpp>
|
||||
#include <hex/api/localization_manager.hpp>
|
||||
#include <hex/api/event_manager.hpp>
|
||||
#include <hex/api/project_file_manager.hpp>
|
||||
|
||||
#include <wolv/utils/guards.hpp>
|
||||
#include <wolv/net/socket_server.hpp>
|
||||
|
||||
#include <hex/helpers/fmt.hpp>
|
||||
#include <fmt/chrono.h>
|
||||
#include <hex/helpers/logger.hpp>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
@@ -12,6 +15,7 @@
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
static bool networkInterfaceServiceEnabled = false;
|
||||
static int autoBackupTime = 0;
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -58,14 +62,37 @@ namespace hex::plugin::builtin {
|
||||
});
|
||||
}
|
||||
|
||||
void handleAutoBackup() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
static std::chrono::time_point<std::chrono::steady_clock> lastBackupTime = now;
|
||||
|
||||
if (autoBackupTime > 0 && (now - lastBackupTime) > std::chrono::seconds(autoBackupTime)) {
|
||||
lastBackupTime = now;
|
||||
|
||||
if (ImHexApi::Provider::isValid()) {
|
||||
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;
|
||||
}
|
||||
|
||||
log::info("Backed up project");
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void registerBackgroundServices() {
|
||||
EventSettingsChanged::subscribe([]{
|
||||
networkInterfaceServiceEnabled = bool(ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.network_interface", false));
|
||||
autoBackupTime = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.auto_backup_time", 0).get<int>() * 30;
|
||||
});
|
||||
|
||||
ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.network_interface"_lang, handleNetworkInterfaceService);
|
||||
ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.auto_backup"_lang, handleAutoBackup);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -164,13 +164,13 @@ namespace hex::plugin::builtin {
|
||||
// If saveLocation is false, reset the project path (do not release the lock)
|
||||
if (updateLocation) {
|
||||
resetPath.release();
|
||||
|
||||
// Request, as this puts us into a project state
|
||||
RequestUpdateWindowTitle::post();
|
||||
}
|
||||
|
||||
AchievementManager::unlockAchievement("hex.builtin.achievement.starting_out", "hex.builtin.achievement.starting_out.save_project.name");
|
||||
|
||||
// Request, as this puts us into a project state
|
||||
RequestUpdateWindowTitle::post();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,8 +23,43 @@ namespace hex::plugin::builtin::recent {
|
||||
constexpr static auto MaxRecentEntries = 5;
|
||||
constexpr static auto BackupFileName = "crash_backup.hexproj";
|
||||
|
||||
static std::atomic_bool s_recentEntriesUpdating = false;
|
||||
static std::list<RecentEntry> s_recentEntries;
|
||||
namespace {
|
||||
|
||||
std::atomic_bool s_recentEntriesUpdating = false;
|
||||
std::list<RecentEntry> s_recentEntries;
|
||||
std::atomic_bool s_autoBackupsFound = false;
|
||||
|
||||
|
||||
class PopupAutoBackups : public Popup<PopupAutoBackups> {
|
||||
public:
|
||||
PopupAutoBackups() : Popup("hex.builtin.welcome.start.recent.auto_backups"_lang, true, true) { }
|
||||
void drawContent() override {
|
||||
if (ImGui::BeginTable("AutoBackups", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersInnerV, ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 5))) {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
for (const auto &backupPath : fs::getDefaultPaths(fs::ImHexPath::Backups)) {
|
||||
for (const auto &entry : std::fs::directory_iterator(backupPath)) {
|
||||
if (entry.is_regular_file() && entry.path().extension() == ".hexproj") {
|
||||
auto lastWriteTime = std::chrono::file_clock::to_sys(std::fs::last_write_time(entry.path()));
|
||||
if (ImGui::Selectable(hex::format("hex.builtin.welcome.start.recent.auto_backups.backup"_lang, fmt::gmtime(lastWriteTime)).c_str(), false, ImGuiSelectableFlags_DontClosePopups)) {
|
||||
ProjectFile::load(entry.path());
|
||||
Popup::close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndTable();
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
|
||||
return ImGuiWindowFlags_AlwaysAutoResize;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
void registerEventHandlers() {
|
||||
// Save every opened provider as a "recent" shortcut
|
||||
@@ -93,7 +128,7 @@ namespace hex::plugin::builtin::recent {
|
||||
}
|
||||
|
||||
void updateRecentEntries() {
|
||||
TaskManager::createBackgroundTask("Updating recent files", [](auto&){
|
||||
TaskManager::createBackgroundTask("Updating recent files", [](auto&) {
|
||||
if (s_recentEntriesUpdating)
|
||||
return;
|
||||
|
||||
@@ -145,6 +180,16 @@ namespace hex::plugin::builtin::recent {
|
||||
}
|
||||
|
||||
std::copy(uniqueProviders.begin(), uniqueProviders.end(), std::front_inserter(s_recentEntries));
|
||||
|
||||
s_autoBackupsFound = false;
|
||||
for (const auto &backupPath : fs::getDefaultPaths(fs::ImHexPath::Backups)) {
|
||||
for (const auto &entry : std::fs::directory_iterator(backupPath)) {
|
||||
if (entry.is_regular_file() && entry.path().extension() == ".hexproj") {
|
||||
s_autoBackupsFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -172,13 +217,12 @@ namespace hex::plugin::builtin::recent {
|
||||
|
||||
|
||||
void draw() {
|
||||
if (s_recentEntries.empty())
|
||||
if (s_recentEntries.empty() && !s_autoBackupsFound)
|
||||
return;
|
||||
|
||||
ImGuiExt::BeginSubWindow("hex.builtin.welcome.start.recent"_lang, ImVec2(), ImGuiChildFlags_AutoResizeX);
|
||||
{
|
||||
if (!s_recentEntriesUpdating) {
|
||||
|
||||
for (auto it = s_recentEntries.begin(); it != s_recentEntries.end();) {
|
||||
const auto &recentEntry = *it;
|
||||
bool shouldRemove = false;
|
||||
@@ -199,8 +243,38 @@ namespace hex::plugin::builtin::recent {
|
||||
loadRecentEntry(recentEntry);
|
||||
break;
|
||||
}
|
||||
if (!isProject)
|
||||
ImGui::SetItemTooltip("%s", Lang(recentEntry.type).get().c_str());
|
||||
|
||||
if (ImGui::IsItemHovered() && ImGui::GetIO().KeyShift) {
|
||||
if (ImGui::BeginTooltip()) {
|
||||
if (ImGui::BeginTable("##RecentEntryTooltip", 2, ImGuiTableFlags_RowBg)) {
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextUnformatted("hex.builtin.common.name"_lang);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextUnformatted(recentEntry.displayName.c_str());
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextUnformatted("hex.builtin.common.type"_lang);
|
||||
ImGui::TableNextColumn();
|
||||
|
||||
if (isProject) {
|
||||
ImGui::TextUnformatted("hex.builtin.common.project"_lang);
|
||||
|
||||
ImGui::TableNextRow();
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextUnformatted("hex.builtin.common.path"_lang);
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextUnformatted(recentEntry.data["path"].get<std::string>().c_str());
|
||||
} else {
|
||||
ImGui::TextUnformatted(Lang(recentEntry.type));
|
||||
}
|
||||
|
||||
ImGui::EndTable();
|
||||
}
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
}
|
||||
|
||||
// Detect right click on recent provider
|
||||
std::string popupID = hex::format("RecentEntryMenu.{}", recentEntry.getHash());
|
||||
@@ -223,6 +297,12 @@ namespace hex::plugin::builtin::recent {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
if (s_autoBackupsFound) {
|
||||
ImGui::Separator();
|
||||
if (ImGuiExt::Hyperlink(hex::format("{} {}", ICON_VS_ARCHIVE, "hex.builtin.welcome.start.recent.auto_backups"_lang).c_str()))
|
||||
PopupAutoBackups::open();
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGuiExt::EndSubWindow();
|
||||
|
||||
@@ -165,10 +165,10 @@ namespace hex::plugin::builtin {
|
||||
public:
|
||||
bool draw(const std::string &name) override {
|
||||
auto format = [this] -> std::string {
|
||||
if (this->m_value == 0)
|
||||
return "hex.builtin.setting.interface.scaling.native"_lang;
|
||||
else
|
||||
return "x%.1f";
|
||||
if (this->m_value == 0)
|
||||
return "hex.builtin.setting.interface.scaling.native"_lang;
|
||||
else
|
||||
return "x%.1f";
|
||||
}();
|
||||
|
||||
if (ImGui::SliderFloat(name.data(), &this->m_value, 0, 10, format.c_str(), ImGuiSliderFlags_AlwaysClamp)) {
|
||||
@@ -191,6 +191,39 @@ namespace hex::plugin::builtin {
|
||||
float m_value = 0;
|
||||
};
|
||||
|
||||
class AutoBackupWidget : public ContentRegistry::Settings::Widgets::Widget {
|
||||
public:
|
||||
bool draw(const std::string &name) override {
|
||||
auto format = [this] -> std::string {
|
||||
auto value = this->m_value * 30;
|
||||
if (value == 0)
|
||||
return "hex.builtin.common.off"_lang;
|
||||
else if (value < 60)
|
||||
return hex::format("hex.builtin.setting.general.auto_backup_time.format.simple"_lang, value);
|
||||
else
|
||||
return hex::format("hex.builtin.setting.general.auto_backup_time.format.extended"_lang, value / 60, value % 60);
|
||||
}();
|
||||
|
||||
if (ImGui::SliderInt(name.data(), &this->m_value, 0, (30 * 60) / 30, format.c_str(), ImGuiSliderFlags_AlwaysClamp | ImGuiSliderFlags_NoInput)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &data) override {
|
||||
if (data.is_number())
|
||||
this->m_value = data.get<int>();
|
||||
}
|
||||
|
||||
nlohmann::json store() override {
|
||||
return this->m_value;
|
||||
}
|
||||
|
||||
private:
|
||||
int m_value = 0;
|
||||
};
|
||||
|
||||
class KeybindingWidget : public ContentRegistry::Settings::Widgets::Widget {
|
||||
public:
|
||||
KeybindingWidget(View *view, const Shortcut &shortcut) : m_view(view), m_shortcut(shortcut), m_drawShortcut(shortcut), m_defaultShortcut(shortcut) {}
|
||||
@@ -337,6 +370,7 @@ namespace hex::plugin::builtin {
|
||||
|
||||
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "", "hex.builtin.setting.general.show_tips", false);
|
||||
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "", "hex.builtin.setting.general.save_recent_providers", true);
|
||||
ContentRegistry::Settings::add<AutoBackupWidget>("hex.builtin.setting.general", "", "hex.builtin.setting.general.auto_backup_time");
|
||||
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "hex.builtin.setting.general.patterns", "hex.builtin.setting.general.auto_load_patterns", true);
|
||||
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "hex.builtin.setting.general.patterns", "hex.builtin.setting.general.sync_pattern_source", false);
|
||||
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "hex.builtin.setting.general.network", "hex.builtin.setting.general.network_interface", false);
|
||||
|
||||
Reference in New Issue
Block a user