From 35ebdd4118bcafaed60606fdf960aa39406ffdf3 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Tue, 15 Aug 2023 09:45:18 +0200 Subject: [PATCH] feat: support multiple clients per game - Game.client changed from str to Vec - Create launch shortcuts for multi-client games - Add prompt if multiple clients available and none specified --- src/main.rs | 71 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9dab4fd..cedfb62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod http; use mslnk::ShellLink; use semver::Version; use std::time::{SystemTime, UNIX_EPOCH}; -use std::{fs, path::PathBuf}; +use std::{fs, path::Path, path::PathBuf}; #[cfg(not(windows))] use std::{thread, time}; #[cfg(windows)] @@ -19,7 +19,7 @@ struct CdnFile { #[derive(serde::Deserialize, serde::Serialize)] struct Game<'a> { engine: &'a str, - client: &'a str, + client: Vec<&'a str>, references: Vec<&'a str>, app_id: u32, } @@ -134,6 +134,29 @@ fn get_installed_games(games: &Vec) -> Vec<(u32, PathBuf)> { installed_games } +#[cfg(windows)] +fn setup_client_links(game: &Game, game_dir: &Path) { + if game.client.len() > 1 { + println!("Multiple clients installed, use the specific shortcuts (launch-.lnk in game directory or desktop shortcuts) to launch a specific client"); + } + + let target = game_dir.join("alterware-launcher.exe"); + + for c in game.client.iter() { + let lnk = game_dir.join(format!("launch-{}.lnk", c)); + + let mut sl = ShellLink::new(target.clone()).unwrap(); + sl.set_arguments(Some(c.to_string())); + sl.set_icon_location(Some( + game_dir + .join(format!("{}.exe", c)) + .to_string_lossy() + .into_owned(), + )); + sl.create_lnk(&lnk).unwrap(); + } +} + #[cfg(windows)] fn windows_launcher_install(games: &Vec) { println!("No game specified/found. Checking for installed Steam games.."); @@ -156,6 +179,7 @@ fn windows_launcher_install(games: &Vec) { let launcher_path = std::env::current_exe().unwrap(); fs::copy(launcher_path, path.join("alterware-launcher.exe")).unwrap(); println!("Launcher copied to {}", path.display()); + setup_client_links(game, path); println!("Create Desktop shortcut? (Y/n)"); let input = get_input().to_ascii_lowercase(); @@ -167,15 +191,19 @@ fn windows_launcher_install(games: &Vec) { )); let target = path.join("alterware-launcher.exe"); - let lnk = desktop.join(format!("{}.lnk", game.client)); - let mut sl = ShellLink::new(target).unwrap(); - sl.set_icon_location(Some( - path.join(format!("{}.exe", game.client)) - .to_string_lossy() - .into_owned(), - )); - sl.create_lnk(lnk).unwrap(); + for c in game.client.iter() { + let lnk = desktop.join(format!("{}.lnk", c)); + + let mut sl = ShellLink::new(target.clone()).unwrap(); + sl.set_arguments(Some(c.to_string())); + sl.set_icon_location(Some( + path.join(format!("{}.exe", c)) + .to_string_lossy() + .into_owned(), + )); + sl.create_lnk(lnk).unwrap(); + } } break; @@ -270,7 +298,17 @@ fn main() { 'main: for g in games.iter() { for r in g.references.iter() { if std::path::Path::new(r).exists() { - game = String::from(g.client); + if g.client.len() > 1 { + setup_client_links(g, &std::env::current_dir().unwrap()); + println!("Multiple clients available, please specify the ID of the game you want to launch:"); + println!("To skip this prompt use the launch-.lnk shortcuts in the game folder."); + for (i, c) in g.client.iter().enumerate() { + println!("{}: {}", i, c); + } + game = String::from(g.client[get_input().parse::().unwrap()]); + break 'main; + } + game = String::from(g.client[0]); break 'main; } } @@ -278,13 +316,14 @@ fn main() { } for g in games.iter() { - if g.client == game { - update(g); - if update_only { + for c in g.client.iter() { + if c == &game { + update(g); + if !update_only { + launch(&PathBuf::from(format!("{}.exe", c))); + } return; } - launch(&PathBuf::from(format!("{}.exe", g.client))); - return; } }