mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 21:47:40 -05:00
feat: Add option to create auto backups of files before they're modified
This commit is contained in:
@@ -239,6 +239,14 @@ EXPORT_MODULE namespace hex {
|
||||
nlohmann::json store() override { return {}; }
|
||||
};
|
||||
|
||||
class Spacer : public Widget {
|
||||
public:
|
||||
bool draw(const std::string &name) override;
|
||||
|
||||
void load(const nlohmann::json &) override {}
|
||||
nlohmann::json store() override { return {}; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace impl {
|
||||
|
||||
@@ -72,6 +72,21 @@ namespace hex::prv {
|
||||
[[nodiscard]] virtual std::vector<Description> getDataDescription() const = 0;
|
||||
};
|
||||
|
||||
class IProviderDataBackupable {
|
||||
public:
|
||||
explicit IProviderDataBackupable(Provider *provider);
|
||||
virtual ~IProviderDataBackupable() = default;
|
||||
|
||||
void createBackupIfNeeded(const std::fs::path &inputFilePath);
|
||||
private:
|
||||
Provider *m_provider = nullptr;
|
||||
bool m_backupCreated = false;
|
||||
|
||||
bool m_shouldCreateBackups = true;
|
||||
u64 m_maxSize;
|
||||
std::string m_backupExtension;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Represent the data source for a tab in the UI
|
||||
*/
|
||||
|
||||
@@ -594,6 +594,12 @@ namespace hex {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Spacer::draw(const std::string& name) {
|
||||
std::ignore = name;
|
||||
ImGui::NewLine();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,14 @@
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <optional>
|
||||
#include <hex/api/content_registry/settings.hpp>
|
||||
|
||||
#include <hex/helpers/magic.hpp>
|
||||
#include <wolv/io/file.hpp>
|
||||
#include <wolv/literals.hpp>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <wolv/utils/string.hpp>
|
||||
|
||||
namespace hex::prv {
|
||||
|
||||
@@ -24,6 +26,28 @@ namespace hex::prv {
|
||||
|
||||
}
|
||||
|
||||
IProviderDataBackupable::IProviderDataBackupable(Provider* provider) : m_provider(provider) {
|
||||
m_shouldCreateBackups = ContentRegistry::Settings::read<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.backups.file_backup.enable", true);
|
||||
m_maxSize = ContentRegistry::Settings::read<u32>("hex.builtin.setting.general", "hex.builtin.setting.general.backups.file_backup.max_size", 1_MiB);
|
||||
m_backupExtension = ContentRegistry::Settings::read<std::string>("hex.builtin.setting.general", "hex.builtin.setting.general.backups.file_backup.extension", ".bak");
|
||||
}
|
||||
|
||||
void IProviderDataBackupable::createBackupIfNeeded(const std::fs::path &inputFilePath) {
|
||||
if (!m_shouldCreateBackups || m_backupCreated)
|
||||
return;
|
||||
|
||||
if (m_provider->getActualSize() > m_maxSize)
|
||||
return;
|
||||
|
||||
const std::fs::path backupFilePath = wolv::util::toUTF8String(inputFilePath) + m_backupExtension;
|
||||
if (wolv::io::fs::copyFile(inputFilePath, backupFilePath, std::fs::copy_options::overwrite_existing)) {
|
||||
if (wolv::io::fs::exists(backupFilePath)) {
|
||||
m_backupCreated = true;
|
||||
log::info("Created backup of provider data at '{}'", backupFilePath.string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Provider::Provider() : m_undoRedoStack(this), m_id(s_idCounter++) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user