From 860b319a0373e6c600fac91508c447c847ea05e95c726a9cc2dcd1dc2fb050d4 Mon Sep 17 00:00:00 2001 From: raven <7156279+RavenX8@users.noreply.github.com> Date: Sat, 14 Dec 2024 23:41:24 -0500 Subject: [PATCH] - add: command line arguments for updating and launching - add: stub for updating game --- launcher/Cargo.toml | 1 + launcher/src/main.rs | 112 +++++++++++++++++++++++++++---------------- 2 files changed, 72 insertions(+), 41 deletions(-) diff --git a/launcher/Cargo.toml b/launcher/Cargo.toml index ec255dc..a0b854e 100644 --- a/launcher/Cargo.toml +++ b/launcher/Cargo.toml @@ -8,3 +8,4 @@ shell-escape = "0.1.5" tracing = "0.1.41" tracing-subscriber = "0.3.19" url = "2.5.4" +clap = { version = "4.5.23", features = ["derive"] } diff --git a/launcher/src/main.rs b/launcher/src/main.rs index eaffe37..c719cd3 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -1,48 +1,71 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::{exit, Command, Stdio}; -use std::str::FromStr; +use clap::{Parser, Subcommand}; use std::{env, io}; -use tracing::{error, info, Level}; +use tracing::{debug, error, info, Level}; use url::Url; -fn wait_for_keypress() { - // Wait for a keypress - info!("Press Enter to close the launcher..."); - let _ = io::stdin().read_line(&mut String::new()); +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[command(subcommand)] + action: Commands, + + #[arg(short, long, action = clap::ArgAction::Count)] + verbose: u8 } -fn format_shell_command(command: &Command) -> String { - let executable = command.get_program().to_string_lossy(); - let args: Vec = command - .get_args() - .map(|arg| shell_escape::escape(arg.to_string_lossy()).into_owned()) - .collect(); - - format!("{} {}", executable, args.join(" ")) +#[derive(Subcommand, Debug)] +enum Commands { + #[command(name = "launch")] + Launch { + url: String, + }, + #[command(name = "update")] + Update } -fn main() { - tracing_subscriber::fmt() - .with_max_level( - Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) - .unwrap_or_else(|_| Level::INFO), - ) - .init(); +fn main() -> Result<(), Box> { + let args = Args::parse(); + let verbose = match args.verbose { + 0 => Level::INFO, + 1 => Level::DEBUG, + _ => Level::TRACE + }; - // Get the full command-line arguments (the URL is passed as the first argument after the executable) - let args: Vec = env::args().collect(); - if args.len() < 2 { - error!("Usage: osirose-launcher "); - wait_for_keypress(); - return; + tracing_subscriber::fmt().with_max_level(verbose).init(); + + let action = args.action; + + match action { + Commands::Launch { url } => { + debug!("launching with URL {}", url); + launch_game(url); + }, + Commands::Update => { + error!("Update action not implemented"); + return Err("Update action not implemented".into()); + } } - let url = &args[1]; - let working_directory = - env::var("GAME_CLIENT_PATH").unwrap_or_else(|_| "D:/rose_osirose-new-x64".to_string()); + Ok(()) +} + +fn launch_game(url: String) { + + let exe_dir_path = env::current_exe().unwrap().parent().unwrap().to_path_buf(); + // let working_directory = + // env::var("GAME_CLIENT_PATH").unwrap_or_else(|_| "D:/rose_osirose-new-x64".to_string()); + + // Change the working directory + if let Err(e) = env::set_current_dir(exe_dir_path) { + error!("Failed to set working directory: {}", e); + wait_for_keypress(); + exit(1); + } // Parse the URL - match Url::parse(url) { + match Url::parse(&url) { Ok(parsed_url) => { // Extract the "otp" parameter let otp = parsed_url @@ -80,13 +103,6 @@ fn main() { info!(" IP: {}", ip); info!(" Username: {}", username); - // Change the working directory - if let Err(e) = env::set_current_dir(Path::new(&working_directory)) { - eprintln!("Failed to set working directory: {}", e); - wait_for_keypress(); - exit(1); - } - // Construct the game client command let game_executable = "./TRose.exe"; let mut command = Command::new(game_executable); @@ -115,6 +131,20 @@ fn main() { } Err(e) => error!("Failed to parse URL: {}", e), } - - info!("Launcher is closing..."); } + +fn wait_for_keypress() { + // Wait for a keypress + info!("Press Enter to close the launcher..."); + let _ = io::stdin().read_line(&mut String::new()); +} + +fn format_shell_command(command: &Command) -> String { + let executable = command.get_program().to_string_lossy(); + let args: Vec = command + .get_args() + .map(|arg| shell_escape::escape(arg.to_string_lossy()).into_owned()) + .collect(); + + format!("{} {}", executable, args.join(" ")) +} \ No newline at end of file