From 65094d47016568dda8666f7fc7646d9462347538 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Thu, 14 Sep 2023 10:23:48 +0200 Subject: [PATCH] add --pass to allow passing args to the client --- README.md | 4 +++- src/main.rs | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 20dbaf7..412eb24 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,10 @@ - ```--path```, ```-p``` - Set the game path - Do not include a trailing backslash in the path +-- ```--pass``` + - Pass additional arguments to the game -Example: ```alterware-launcher.exe iw4x --bonus -u --path "C:\Games\IW4x"``` +Example: ```alterware-launcher.exe iw4x --bonus -u --path "C:\Games\IW4x" --pass "-console"``` Some arguments can be set in alterware-launcher.json, args generally override the values of the config. diff --git a/src/main.rs b/src/main.rs index a4c9f31..965b646 100644 --- a/src/main.rs +++ b/src/main.rs @@ -263,9 +263,10 @@ fn update(game: &Game, dir: &Path, bonus_content: bool, force: bool) { fs::write(dir.join(".sha-sums"), hash_file_content).unwrap(); } -fn launch(file_path: &PathBuf) { +fn launch(file_path: &PathBuf, args: &str) { println!("Launching {}...", file_path.display()); std::process::Command::new(file_path) + .args(args.split(' ')) .spawn() .expect("Failed to launch the game") .wait() @@ -281,16 +282,9 @@ fn setup_env() { } fn arg_value(args: &[String], arg: &str) -> Option { - let val = args - .iter() + args.iter() .position(|r| r == arg) - .map(|e| args[e + 1].clone()); - if let Some(ref val) = val { - if val.starts_with('-') { - return None; - } - } - val + .map(|e| args[e + 1].clone()) } fn arg_bool(args: &[String], arg: &str) -> bool { @@ -302,10 +296,10 @@ fn arg_remove(args: &mut Vec, arg: &str) { } fn arg_remove_value(args: &mut Vec, arg: &str) { - args.iter().position(|r| r == arg).map(|e| { + if let Some(e) = args.iter().position(|r| r == arg) { args.remove(e); args.remove(e); - }); + }; } fn main() { @@ -350,6 +344,14 @@ fn main() { arg_remove(&mut args, "-f"); } + let client_args: String; + if let Some(pass) = arg_value(&args, "--pass") { + client_args = pass; + arg_remove_value(&mut args, "--pass"); + } else { + client_args = String::new(); + } + let games_json = http::get_body_string(format!("{}/games.json", MASTER).as_str()); let games: Vec = serde_json::from_str(&games_json).unwrap(); @@ -411,7 +413,7 @@ fn main() { cfg.force_update, ); if !cfg.update_only { - launch(&install_path.join(format!("{}.exe", c))); + launch(&install_path.join(format!("{}.exe", c)), &client_args); } return; }