From fcd0198cded9bd3458e4b74c47e9e0018256444e Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Fri, 2 May 2025 21:30:38 +0200 Subject: [PATCH] allow file renaming --- src/global.rs | 7 +++++++ src/main.rs | 33 +++++++++++++++++++++++++++------ src/structs.rs | 2 ++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/global.rs b/src/global.rs index 454aa9e..8493905 100644 --- a/src/global.rs +++ b/src/global.rs @@ -56,6 +56,13 @@ pub static PREFIXES: Lazy> = Lazy::new(|| { padding: 7, }, ), + ( + "renamed", + PrintPrefix { + text: "Renamed".bright_blue(), + padding: 5, + }, + ), ]) }); diff --git a/src/main.rs b/src/main.rs index 6aa8d3b..49edc3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -385,6 +385,28 @@ async fn update( cache::get_cache(dir) }; + for entry in game.rename.iter() { + let file_path = dir.join(entry.0); + let new_path = dir.join(entry.1); + if file_path.exists() { + if fs::rename(&file_path, &new_path).is_err() { + println!( + "{}Couldn't rename {} -> {}", + misc::prefix("error"), + file_path.cute_path(), + new_path.cute_path() + ); + } else { + println!( + "{}{} -> {}", + misc::prefix("renamed"), + file_path.cute_path(), + new_path.cute_path() + ); + } + } + } + if game.engine == "iw4" { iw4x::update(dir, &mut cache, prerelease).await; @@ -412,13 +434,12 @@ async fn update( .filter(|e| e.file_type().is_file()) { let rel_path = entry.path().strip_prefix(dir).unwrap_or(entry.path()); - let cdn_path = if rel_path.starts_with("iw4-dlc") { - rel_path.to_path_buf() - } else { - Path::new("iw4").join(rel_path) - }; + let cdn_paths = [ + Path::new("iw4").join(rel_path), + Path::new("iw4-dlc").join(rel_path), + ]; - if !valid_files.contains(&cdn_path) { + if !cdn_paths.iter().any(|path| valid_files.contains(path)) { crate::println_info!("{}{}", misc::prefix("removed"), entry.path().cute_path()); if std::fs::remove_file(entry.path()).is_err() { crate::println_error!( diff --git a/src/structs.rs b/src/structs.rs index e9e02a6..acc056d 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -17,6 +17,8 @@ pub struct Game<'a> { pub bonus: Vec<&'a str>, pub delete: Vec<&'a str>, pub required: Vec<&'a str>, + #[serde(default)] + pub rename: Vec<(&'a str, &'a str)>, } impl Game<'_> {