From d587553845433150f9c413b72611a9a9b9c45646 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sun, 11 Jun 2023 16:16:29 +0200 Subject: [PATCH] update all game-specific files, add iw6 --- README.md | 6 ++- src/main.rs | 111 +++++++++++++++++++++++++++++----------------------- 2 files changed, 66 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index ef3d8a2..a4a3906 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,8 @@ 1. Download [latest release](https://github.com/mxve/alterware-launcher/releases/latest/download/alterware-launcher-x86_64-pc-windows-msvc.zip) 2. Unpack the archive and place alterware-launcher.exe in MW2/MW3 game directory -3. Run alterware-launcher.exe \ No newline at end of file +3. Run alterware-launcher.exe + +--- + +- Passing iw4-sp, iw5-mod or iw6-mod as the first argument will skip automatic game detection \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9d593ed..57aa29d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ struct Game<'a> { } const MASTER: &str = "https://master.alterware.dev"; -const GAMES: [Game; 2] = [ +const GAMES: [Game; 3] = [ Game { engine: "iw4", client: "iw4-sp", @@ -26,6 +26,11 @@ const GAMES: [Game; 2] = [ client: "iw5-mod", references: &["iw5sp.exe", "iw5mp.exe", "iw5mp_server.exe"], }, + Game { + engine: "iw6", + client: "iw6-mod", + references: &["iw6sp64_ship.exe", "iw6mp64_ship.exe"], + }, ]; fn file_get_sha1(path: &PathBuf) -> String { @@ -34,41 +39,7 @@ fn file_get_sha1(path: &PathBuf) -> String { sha1.digest().to_string() } -fn download_and_launch(url: &str, file_path: &PathBuf, hash: Option) { - if let Some(parent) = file_path.parent() { - if !parent.exists() { - fs::create_dir_all(parent).unwrap(); - } - } - - // clippy will suggest using if let or match, but i prefer it being somewhat readable - #[allow(clippy::unnecessary_unwrap)] - if file_path.exists() && hash.is_some() { - let sha1_local = file_get_sha1(file_path).to_lowercase(); - let sha1_remote = hash.unwrap().to_lowercase(); - if sha1_local != sha1_remote { - println!( - "Updating {}...\nLocal hash: {}\nRemote hash: {}", - file_path.display(), - sha1_local, - sha1_remote - ); - http::download_file(url, file_path); - } - } else { - println!("Downloading {}...", file_path.display()); - http::download_file(url, file_path); - } - - println!("Launching {}...", file_path.display()); - std::process::Command::new(file_path) - .spawn() - .unwrap() - .wait() - .unwrap(); -} - -fn get_hash(game: &Game) -> Option { +fn update(game: &Game) { let cdn_info: Vec = serde_json::from_str(&http::get_body_string( format!( "{}/files.json?{}", @@ -79,13 +50,62 @@ fn get_hash(game: &Game) -> Option { )) .unwrap(); + let mut files_to_update: Vec = Vec::new(); for file in cdn_info { - if file.name == format!("{}/{}.exe", game.engine, game.client) { - return Some(file.hash); + if file.name.starts_with(game.engine) { + files_to_update.push(file); } } - None + for file in files_to_update { + let file_path = PathBuf::from(&file.name.replace(&format!("{}/", game.engine), "")); + if file_path.exists() { + let sha1_local = file_get_sha1(&file_path).to_lowercase(); + let sha1_remote = file.hash.to_lowercase(); + if sha1_local != sha1_remote { + println!( + "Updating {}...\nLocal hash: {}\nRemote hash: {}", + file_path.display(), + sha1_local, + sha1_remote + ); + http::download_file( + &format!( + "{}/{}?{}", + MASTER, + file.name, + rand::Rng::gen_range(&mut rand::thread_rng(), 0..1000) + ), + &file_path, + ); + } + } else { + println!("Downloading {}...", file_path.display()); + if let Some(parent) = file_path.parent() { + if !parent.exists() { + fs::create_dir_all(parent).unwrap(); + } + } + http::download_file( + &format!( + "{}/{}?{}", + MASTER, + file.name, + rand::Rng::gen_range(&mut rand::thread_rng(), 0..1000) + ), + &file_path, + ); + } + } +} + +fn launch(file_path: &PathBuf) { + println!("Launching {}...", file_path.display()); + std::process::Command::new(file_path) + .spawn() + .unwrap() + .wait() + .unwrap(); } fn main() { @@ -106,17 +126,8 @@ fn main() { for g in GAMES.iter() { if g.client == game { - download_and_launch( - &format!( - "{}/{}/{}.exe?{}", - MASTER, - g.engine, - g.client, - rand::Rng::gen_range(&mut rand::thread_rng(), 0..1000) - ), - &PathBuf::from(format!("{}.exe", g.client)), - get_hash(g), - ); + update(&g); + launch(&PathBuf::from(format!("{}.exe", g.client))); return; } }