- add: command line arguments for updating and launching

- add: stub for updating game
This commit is contained in:
2024-12-14 23:41:24 -05:00
parent ff3f6b2148
commit 860b319a03
2 changed files with 72 additions and 41 deletions

View File

@@ -8,3 +8,4 @@ shell-escape = "0.1.5"
tracing = "0.1.41" tracing = "0.1.41"
tracing-subscriber = "0.3.19" tracing-subscriber = "0.3.19"
url = "2.5.4" url = "2.5.4"
clap = { version = "4.5.23", features = ["derive"] }

View File

@@ -1,48 +1,71 @@
use std::path::Path; use std::path::{Path, PathBuf};
use std::process::{exit, Command, Stdio}; use std::process::{exit, Command, Stdio};
use std::str::FromStr; use clap::{Parser, Subcommand};
use std::{env, io}; use std::{env, io};
use tracing::{error, info, Level}; use tracing::{debug, error, info, Level};
use url::Url; use url::Url;
fn wait_for_keypress() { #[derive(Parser, Debug)]
// Wait for a keypress #[command(version, about, long_about = None)]
info!("Press Enter to close the launcher..."); struct Args {
let _ = io::stdin().read_line(&mut String::new()); #[command(subcommand)]
action: Commands,
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8
} }
fn format_shell_command(command: &Command) -> String { #[derive(Subcommand, Debug)]
let executable = command.get_program().to_string_lossy(); enum Commands {
let args: Vec<String> = command #[command(name = "launch")]
.get_args() Launch {
.map(|arg| shell_escape::escape(arg.to_string_lossy()).into_owned()) url: String,
.collect(); },
#[command(name = "update")]
format!("{} {}", executable, args.join(" ")) Update
} }
fn main() { fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt() let args = Args::parse();
.with_max_level( let verbose = match args.verbose {
Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())) 0 => Level::INFO,
.unwrap_or_else(|_| Level::INFO), 1 => Level::DEBUG,
) _ => Level::TRACE
.init(); };
// Get the full command-line arguments (the URL is passed as the first argument after the executable) tracing_subscriber::fmt().with_max_level(verbose).init();
let args: Vec<String> = env::args().collect();
if args.len() < 2 { let action = args.action;
error!("Usage: osirose-launcher <url>");
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());
}
}
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(); wait_for_keypress();
return; exit(1);
} }
let url = &args[1];
let working_directory =
env::var("GAME_CLIENT_PATH").unwrap_or_else(|_| "D:/rose_osirose-new-x64".to_string());
// Parse the URL // Parse the URL
match Url::parse(url) { match Url::parse(&url) {
Ok(parsed_url) => { Ok(parsed_url) => {
// Extract the "otp" parameter // Extract the "otp" parameter
let otp = parsed_url let otp = parsed_url
@@ -80,13 +103,6 @@ fn main() {
info!(" IP: {}", ip); info!(" IP: {}", ip);
info!(" Username: {}", username); 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 // Construct the game client command
let game_executable = "./TRose.exe"; let game_executable = "./TRose.exe";
let mut command = Command::new(game_executable); let mut command = Command::new(game_executable);
@@ -115,6 +131,20 @@ fn main() {
} }
Err(e) => error!("Failed to parse URL: {}", e), 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<String> = command
.get_args()
.map(|arg| shell_escape::escape(arg.to_string_lossy()).into_owned())
.collect();
format!("{} {}", executable, args.join(" "))
} }