- add: command line arguments for updating and launching
- add: stub for updating game
This commit is contained in:
@@ -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"] }
|
||||
|
||||
@@ -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<String> = 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<dyn std::error::Error>> {
|
||||
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<String> = env::args().collect();
|
||||
if args.len() < 2 {
|
||||
error!("Usage: osirose-launcher <url>");
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
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
|
||||
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<String> = command
|
||||
.get_args()
|
||||
.map(|arg| shell_escape::escape(arg.to_string_lossy()).into_owned())
|
||||
.collect();
|
||||
|
||||
format!("{} {}", executable, args.join(" "))
|
||||
}
|
||||
Reference in New Issue
Block a user