Skip to content

Commit

Permalink
feat(pack-dlls): Add toggle to pack dlls in backups, upgrade API
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyian committed Sep 19, 2024
1 parent e746519 commit c9e52cb
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 70 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "nexus-config-backup"
version = "0.1.6"
version = "0.1.7"
edition = "2021"
authors = ["Zyian"]
authors = ["Zyian", "Mythwright"]
description = "A small utility to save all of your addons configs safely away for a rainy day."

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -16,9 +16,9 @@ zip = "2.1.3"
dirs-next = "2.0.0"
chrono = "0.4.38"
once_cell = "1.19.0"
toml = "0.8.14"
serde = { version = "1.0.197", features = ["derive"] }
toml = "0.8.19"
serde = { version = "1.0.210", features = ["derive"] }

[dependencies.nexus]
git = "https://github.com/Zerthox/nexus-rs.git"
rev = "29062458472273522c77f38fdb20811a6913fe6b"
git = "https://github.com/belst/nexus-rs.git"
rev = "0e5a808122ca2164e1c4542e89d87d252d7a3fbb"
77 changes: 42 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use std::{
thread,
};

use nexus::imgui::InputInt;
use nexus::quick_access::add_quick_access_context_menu;
use nexus::{
AddonFlags,
alert::alert_notify,
gui::{RawGuiRender, register_render, RenderType},
alert::send_alert,
gui::{register_render, RawGuiRender, RenderType},
log,
paths,
quick_access::add_simple_shortcut,
render,
AddonFlags,
UpdateProvider,
};
use nexus::imgui::InputInt;
use once_cell::sync::Lazy;
use walkdir::WalkDir;
use zip::write::SimpleFileOptions;
Expand All @@ -26,37 +26,33 @@ mod settings;
static SHORTCUT_ID: &str = "QAS_CONFIG_BACKUP";

struct ConfigBackup {
pub backup_folder: Option<PathBuf>,
settings: Option<settings::Settings>,
}

impl ConfigBackup {
fn new() -> ConfigBackup {
ConfigBackup { backup_folder: None, settings: None }
ConfigBackup { settings: None }
}

fn init(&mut self) -> bool {
self.backup_folder = dirs_next::document_dir();

let config_path = paths::get_addon_dir("addon-config-backup").unwrap();
let config_path = ConfigBackup::get_or_init_folder(&config_path).join("config.toml");
if !config_path.exists() {
{
let res = File::create(config_path.clone());
if res.is_err() {
log::log(
log::LogLevel::Critical,
"addon-config-backup",
format!("Failed to create file {:?}", res.err().unwrap()),
);
return false;
}
let s = settings::Settings::default();
res.ok().unwrap().write_all(toml::to_string_pretty(&s).unwrap().as_bytes()).unwrap();
let res = File::create(config_path.clone());
if res.is_err() {
log::log(
log::LogLevel::Critical,
"addon-config-backup",
format!("Failed to create file {:?}", res.err().unwrap()),
);
return false;
}
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<settings::Settings, toml::de::Error> = toml::from_str(content.as_str());
if res.is_err() {
Expand All @@ -67,7 +63,9 @@ impl ConfigBackup {
);
return false;
}
self.settings = Some(res.ok().unwrap());
let mut s = res.unwrap();
s.validate();
self.settings = Some(s);
true
}

Expand Down Expand Up @@ -102,14 +100,14 @@ fn load() {
let g = grab_global();
g.init();

add_simple_shortcut(SHORTCUT_ID, addon_shortcut()).revert_on_unload();
add_quick_access_context_menu(SHORTCUT_ID, None::<&str>, addon_shortcut()).revert_on_unload();
register_render(RenderType::OptionsRender, render!(render_options)).revert_on_unload();

if g.settings.as_mut().unwrap().backup_on_launch {
if g.settings.as_mut().unwrap().backup_on_launch.unwrap() {
run_backup();
}
if g.settings.as_mut().unwrap().delete_old_on_launch {

if g.settings.as_mut().unwrap().delete_old_on_launch.unwrap() {
cleanup_old_backups();
}
}
Expand All @@ -123,13 +121,14 @@ fn render_options(ui: &nexus::imgui::Ui) {

ui.text("General Settings");
ui.separator();
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.input_text("Destination Folder", &mut g.settings.as_mut().unwrap().target_folder.as_mut().unwrap()).build();
ui.checkbox("Backup Settings on Game Launch", &mut g.settings.as_mut().unwrap().backup_on_launch.as_mut().unwrap());
ui.checkbox("Backup Addon DLLs", &mut g.settings.as_mut().unwrap().package_addons.as_mut().unwrap());

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.checkbox("Automatically delete old backups", &mut g.settings.as_mut().unwrap().delete_old_on_launch.as_mut().unwrap());
InputInt::new(ui, "Backups to Keep", &mut g.settings.as_mut().unwrap().backups_to_keep.as_mut().unwrap()).build();

if ui.button("Save settings") {
g.save();
Expand All @@ -140,6 +139,14 @@ fn grab_global() -> &'static mut Lazy<ConfigBackup> {
unsafe { &mut *addr_of_mut!(GLOBAL_CONFIG) }
}

fn check_dll_pass(filename: &String) -> bool {
let g = grab_global();
// a false result means to package the file
// check if the file contains .dll if it doesn't we pack it = normal behavior
// if it is a dll and package_addons is true, we force the condition to be false = pack the dll
filename.contains(".dll") && !g.settings.as_ref().unwrap().package_addons.unwrap()
}


pub fn run_backup() {
let _ = thread::spawn(|| {
Expand All @@ -153,7 +160,7 @@ pub fn run_backup() {
});

let g = grab_global();
let bf = PathBuf::from(g.settings.as_mut().unwrap().target_folder.clone());
let bf = PathBuf::from(g.settings.as_mut().unwrap().target_folder.as_mut().unwrap().clone());
let backup_dir = ConfigBackup::get_or_init_folder(&bf);
let local_time = chrono::Local::now();
let backup_file = match File::create(backup_dir.join(format!("backup-{}.zip", local_time.format("%Y-%m-%d-%H-%M")))) {
Expand All @@ -179,7 +186,7 @@ pub fn run_backup() {
let name = path.strip_prefix(dir.clone()).unwrap();

if path.is_file() {
if name.to_str().unwrap().to_string().contains(".dll") {
if check_dll_pass(&name.to_str().unwrap().to_string()) {
// log(ELogLevel::DEBUG, format!("Skipping {path:?}...").to_string());
continue;
}
Expand All @@ -206,13 +213,13 @@ 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 = WalkDir::new(s.settings.as_mut().unwrap().target_folder.as_mut().unwrap().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 {
if skipped < keep_num.unwrap() {
skipped += 1;
continue;
}
Expand Down Expand Up @@ -244,7 +251,7 @@ fn addon_shortcut() -> RawGuiRender {
"Addon Config Backup",
"Saving addon configs to backup folder",
);
alert_notify("Finished saving addon configurations to backup folder");
send_alert("Finished saving addon configurations to backup folder");
}
ui.same_line_with_spacing(0.0, 10.0);
if ui.button("Cleanup old backups") {
Expand Down
38 changes: 29 additions & 9 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,44 @@ 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,
pub target_folder: Option<String>,
pub backup_on_launch: Option<bool>,
pub delete_old_on_launch: Option<bool>,
pub backups_to_keep: Option<i32>,
pub package_addons: Option<bool>,
}

impl Settings {
pub fn default() -> Settings {
Settings {
target_folder: dirs_next::document_dir()
target_folder: Some(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,
.to_string()),
backup_on_launch: Some(false),
delete_old_on_launch: Some(false),
backups_to_keep: Some(5),
package_addons: Some(false),
}
}
pub fn validate(&mut self) {
let defaults = Self::default();
if self.backup_on_launch.is_none() {
self.backup_on_launch = defaults.backup_on_launch;
}
if self.package_addons.is_none() {
self.package_addons = defaults.package_addons;
}
if self.delete_old_on_launch.is_none() {
self.delete_old_on_launch = defaults.delete_old_on_launch;
}
if self.backups_to_keep.is_none() {
self.backups_to_keep = defaults.backups_to_keep;
}
if self.target_folder.is_none() {
self.target_folder = defaults.target_folder;
}
}
}

0 comments on commit c9e52cb

Please sign in to comment.