Compare commits

...

2 Commits

Author SHA256 Message Date
860b319a03 - add: command line arguments for updating and launching
- add: stub for updating game
2024-12-14 23:41:24 -05:00
ff3f6b2148 - add: service version to metadata when registering with consul 2024-12-14 23:39:39 -05:00
3 changed files with 77 additions and 42 deletions

View File

@@ -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"] }

View File

@@ -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>");
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<String> = command
.get_args()
.map(|arg| shell_escape::escape(arg.to_string_lossy()).into_owned())
.collect();
format!("{} {}", executable, args.join(" "))
}

View File

@@ -6,6 +6,8 @@ use std::net::ToSocketAddrs;
use uuid::Uuid;
use warp::Filter;
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
#[derive(Serialize)]
struct ConsulRegistration {
id: String,
@@ -35,9 +37,11 @@ pub async fn register_service(
service_address: &str,
service_port: u16,
tags: Vec<String>,
meta: HashMap<String, String>,
mut meta: HashMap<String, String>,
health_check_url: &str,
) -> Result<(), Box<dyn std::error::Error>> {
meta.insert("version".to_string(), VERSION.to_string());
let registration = ConsulRegistration {
id: service_id.to_string(),
name: service_name.to_string(),