From edcf15bebdec1c82a6b328fbd5c33b867a485d19 Mon Sep 17 00:00:00 2001 From: "Adrian (Zyian)" Date: Thu, 27 Jun 2024 14:01:14 -0600 Subject: [PATCH] feat(cleanup): add cleaning up old backups --- Cargo.lock | 31 ++++++------ Cargo.toml | 6 +-- src/lib.rs | 123 +++++++++++++++++++++++++++--------------------- src/settings.rs | 25 ++++++++++ 4 files changed, 114 insertions(+), 71 deletions(-) create mode 100644 src/settings.rs diff --git a/Cargo.lock b/Cargo.lock index 88fd51a..5d905ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -477,19 +477,20 @@ dependencies = [ [[package]] name = "nexus" -version = "0.2.0" -source = "git+https://github.com/Zerthox/nexus-rs.git?rev=8dec2c8ea5872b42c315c419493912ce5d7565fb#8dec2c8ea5872b42c315c419493912ce5d7565fb" +version = "0.4.0" +source = "git+https://github.com/Zerthox/nexus-rs.git?rev=29062458472273522c77f38fdb20811a6913fe6b#29062458472273522c77f38fdb20811a6913fe6b" dependencies = [ "arcdps-imgui", "bitflags 2.4.2", "nexus_codegen", "num_enum", + "paste", "windows", ] [[package]] name = "nexus-config-backup" -version = "0.1.5" +version = "0.1.6" dependencies = [ "chrono", "dirs-next", @@ -504,7 +505,7 @@ dependencies = [ [[package]] name = "nexus_codegen" version = "0.2.0" -source = "git+https://github.com/Zerthox/nexus-rs.git?rev=8dec2c8ea5872b42c315c419493912ce5d7565fb#8dec2c8ea5872b42c315c419493912ce5d7565fb" +source = "git+https://github.com/Zerthox/nexus-rs.git?rev=29062458472273522c77f38fdb20811a6913fe6b#29062458472273522c77f38fdb20811a6913fe6b" dependencies = [ "cfg-if", "paste", @@ -888,9 +889,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "walkdir" -version = "2.4.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", @@ -989,11 +990,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows" -version = "0.56.0" +version = "0.57.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1de69df01bdf1ead2f4ac895dc77c9351aefff65b2f3db429a343f9cbf05e132" +checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" dependencies = [ - "windows-core 0.56.0", + "windows-core 0.57.0", "windows-targets", ] @@ -1008,9 +1009,9 @@ dependencies = [ [[package]] name = "windows-core" -version = "0.56.0" +version = "0.57.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4698e52ed2d08f8658ab0c39512a7c00ee5fe2688c65f8c0a4f06750d729f2a6" +checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d" dependencies = [ "windows-implement", "windows-interface", @@ -1020,9 +1021,9 @@ dependencies = [ [[package]] name = "windows-implement" -version = "0.56.0" +version = "0.57.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b" +checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" dependencies = [ "proc-macro2", "quote", @@ -1031,9 +1032,9 @@ dependencies = [ [[package]] name = "windows-interface" -version = "0.56.0" +version = "0.57.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc" +checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 7155730..3cb1bcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nexus-config-backup" -version = "0.1.5" +version = "0.1.6" edition = "2021" authors = ["Zyian"] description = "A small utility to save all of your addons configs safely away for a rainy day." @@ -11,7 +11,7 @@ crate-type = ["cdylib"] name = "nexus_config_backup" [dependencies] -walkdir = "2.4.0" +walkdir = "2.5.0" zip = "2.1.3" dirs-next = "2.0.0" chrono = "0.4.38" @@ -21,4 +21,4 @@ serde = { version = "1.0.197", features = ["derive"] } [dependencies.nexus] git = "https://github.com/Zerthox/nexus-rs.git" -rev = "8dec2c8ea5872b42c315c419493912ce5d7565fb" +rev = "29062458472273522c77f38fdb20811a6913fe6b" diff --git a/src/lib.rs b/src/lib.rs index 44c09ae..22e7292 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,27 +1,33 @@ use std::{ - fmt::Error, - fs, + fs::{self, File}, io::{Read, Write}, + path::PathBuf, ptr::addr_of_mut, thread, }; -use std::fs::File; -use std::path::PathBuf; -use nexus::{AddonFlags, log, paths, render, UpdateProvider}; -use nexus::alert::alert_notify; -use nexus::gui::{RawGuiRender, register_render, RenderType}; -use nexus::quick_access::add_simple_shortcut; +use nexus::{ + AddonFlags, + alert::alert_notify, + gui::{RawGuiRender, register_render, RenderType}, + log, + paths, + quick_access::add_simple_shortcut, + render, + UpdateProvider, +}; +use nexus::imgui::InputInt; use once_cell::sync::Lazy; -use serde::{Deserialize, Serialize}; use walkdir::WalkDir; use zip::write::SimpleFileOptions; +mod settings; + static SHORTCUT_ID: &str = "QAS_CONFIG_BACKUP"; struct ConfigBackup { pub backup_folder: Option, - settings: Option, + settings: Option, } impl ConfigBackup { @@ -45,14 +51,14 @@ impl ConfigBackup { ); return false; } - let s = Settings::default(); + let s = settings::Settings::default(); res.ok().unwrap().write_all(toml::to_string_pretty(&s).unwrap().as_bytes()).unwrap(); } } let mut content = String::new(); File::open(config_path).unwrap().read_to_string(&mut content).unwrap(); - let res: Result = toml::from_str(content.as_str()); + let res: Result = toml::from_str(content.as_str()); if res.is_err() { log::log( log::LogLevel::Critical, @@ -80,30 +86,6 @@ impl ConfigBackup { } } -#[derive(Deserialize, Serialize)] -struct Settings { - pub target_folder: String, - pub backup_on_launch: bool, - pub delete_old_on_launch: bool, - pub backups_to_keep: i32, -} - -impl Settings { - fn default() -> Settings { - Settings { - target_folder: dirs_next::document_dir() - .unwrap() - .join("nexus-configs") - .to_str() - .unwrap() - .to_string(), - backup_on_launch: false, - delete_old_on_launch: false, - backups_to_keep: 5, - } - } -} - static mut GLOBAL_CONFIG: Lazy = Lazy::new(ConfigBackup::new); nexus::export! { @@ -119,12 +101,16 @@ nexus::export! { fn load() { let g = grab_global(); g.init(); - + add_simple_shortcut(SHORTCUT_ID, addon_shortcut()).revert_on_unload(); register_render(RenderType::OptionsRender, render!(render_options)).revert_on_unload(); - + if g.settings.as_mut().unwrap().backup_on_launch { - let _ = run_backup(); + run_backup(); + } + + if g.settings.as_mut().unwrap().delete_old_on_launch { + cleanup_old_backups(); } } @@ -140,10 +126,10 @@ fn render_options(ui: &nexus::imgui::Ui) { ui.input_text("Destination Folder", &mut g.settings.as_mut().unwrap().target_folder).build(); ui.checkbox("Backup Settings on Game Launch", &mut g.settings.as_mut().unwrap().backup_on_launch); - // ui.text("Background Tasks"); - // ui.separator(); - // ui.checkbox("Automatically delete old backups", &mut g.settings.as_mut().unwrap().delete_old_on_launch); - // InputInt::new(ui, "Backups to Keep", &mut g.settings.as_mut().unwrap().backups_to_keep).build(); + ui.text("Background Tasks"); + ui.separator(); + ui.checkbox("Automatically delete old backups", &mut g.settings.as_mut().unwrap().delete_old_on_launch); + InputInt::new(ui, "Backups to Keep", &mut g.settings.as_mut().unwrap().backups_to_keep).build(); if ui.button("Save settings") { g.save(); @@ -155,7 +141,7 @@ fn grab_global() -> &'static mut Lazy { } -pub fn run_backup() -> Result<(), Error> { +pub fn run_backup() { let _ = thread::spawn(|| { let dir = paths::get_addon_dir("").unwrap(); let wd = WalkDir::new(dir.clone()); @@ -214,24 +200,55 @@ pub fn run_backup() -> Result<(), Error> { } zip.finish().unwrap(); }); - Ok(()) } -fn addon_shortcut() -> RawGuiRender { - render!(|ui| { - if ui.button("Run Backup") { - if run_backup().ok().is_some() { +pub fn cleanup_old_backups() { + thread::spawn(|| { + let s = grab_global(); + + let wd = WalkDir::new(s.settings.as_mut().unwrap().target_folder.clone()); + let wd_it = wd.sort_by(|a, b| b.file_name().cmp(a.file_name())); + + let mut skipped = 0; + let keep_num = s.settings.as_mut().unwrap().backups_to_keep; + for e in wd_it { + if skipped < keep_num { + skipped += 1; + continue; + } + let entry = e.unwrap().clone(); + let res = fs::remove_file(entry.path()); + if res.is_err() { log::log( - log::LogLevel::Info, + log::LogLevel::Critical, "Addon Config Backup", - "Finished saving backup to nexus-configs folder", + format!("Failed to delete old file: {}", res.err().unwrap()), + ); + } else { + log::log( + log::LogLevel::Debug, + "Addon Config Backup", + format!("Deleted old file: {}", entry.path().to_str().unwrap()), ); - alert_notify("Finished saving backup to nexus-configs folder"); } } + }); +} + +fn addon_shortcut() -> RawGuiRender { + render!(|ui| { + if ui.button("Run Backup") { + run_backup(); + log::log( + log::LogLevel::Info, + "Addon Config Backup", + "Saving addon configs to backup folder", + ); + alert_notify("Finished saving addon configurations to backup folder"); + } ui.same_line_with_spacing(0.0, 10.0); if ui.button("Cleanup old backups") { - + cleanup_old_backups(); } }) } \ No newline at end of file diff --git a/src/settings.rs b/src/settings.rs new file mode 100644 index 0000000..8a10d5c --- /dev/null +++ b/src/settings.rs @@ -0,0 +1,25 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize)] +pub struct Settings { + pub target_folder: String, + pub backup_on_launch: bool, + pub delete_old_on_launch: bool, + pub backups_to_keep: i32, +} + +impl Settings { + pub fn default() -> Settings { + Settings { + target_folder: dirs_next::document_dir() + .unwrap() + .join("nexus-configs") + .to_str() + .unwrap() + .to_string(), + backup_on_launch: false, + delete_old_on_launch: false, + backups_to_keep: 5, + } + } +} \ No newline at end of file