From 6ac01827846dbbaed5396fc51ae9523b8896f778 Mon Sep 17 00:00:00 2001 From: Thoronium <107363768+NotThorny@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:41:54 -0600 Subject: [PATCH 1/6] Fix args --- src-tauri/src/main.rs | 20 ++++++++++++++------ src-tauri/src/system_helpers.rs | 17 +++++++++++++---- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 326e365..db518de 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -54,13 +54,18 @@ async fn parse_args(inp: &Vec) -> Result { "no-admin", "Launch without requiring admin permissions", ); - args.flag("g", "no-gui", "Run in CLI mode"); + args.flag("g", "no-gui", "Run in CLI mode. Requires -A to be passed as well."); args.flag("s", "server", "Launch the configured GC server"); args.flag( "P", "patch", "Patch your game before launching, with whatever your game version needs", ); + args.flag( + "N", + "non-elevated-game", + "Launch the game without admin permissions" + ); args.option( "H", "host", @@ -96,17 +101,20 @@ async fn parse_args(inp: &Vec) -> Result { patch::patch_game().await; } - system_helpers::run_program(game_path.to_string(), Some(game_args)) + if args.value_of("non-elevated-game")? { + system_helpers::run_un_elevated(game_path.to_string(), Some(game_args)) + } else { + system_helpers::run_program(game_path.to_string(), Some(game_args)) + } } if args.value_of("server")? { let server_jar = config.grasscutter_path; - let mut server_path = server_jar.clone(); + // Assumes grasscutter jar is named appropriately + let server_path = server_jar.trim_end_matches("grasscutter.jar"); let java_path = config.java_path; - server_path.pop(); - - system_helpers::run_jar(server_jar, server_path.to_string(), java_path); + system_helpers::run_jar(server_jar.clone(), server_path.to_string(), java_path); } if args.value_of::("host").is_ok() && !args.value_of::("host")?.is_empty() { diff --git a/src-tauri/src/system_helpers.rs b/src-tauri/src/system_helpers.rs index 7d09fa9..9197ba5 100644 --- a/src-tauri/src/system_helpers.rs +++ b/src-tauri/src/system_helpers.rs @@ -11,7 +11,16 @@ use registry::{Data, Hive, Security}; #[tauri::command] pub fn run_program(path: String, args: Option) { // Without unwrap_or, this can crash when UAC prompt is denied - open::that(format!("{} {}", &path, &args.unwrap_or_else(|| "".into()))).unwrap_or(()); + match open::with( + format!( + "{} {}", + path, args.unwrap_or_else(|| "".into()) + ), + path.clone(), + ) { + Ok(_) => (), + Err(e) => println!("Failed to open program ({}): {}", &path, e), + }; } #[tauri::command] @@ -80,12 +89,12 @@ pub fn run_jar(path: String, execute_in: String, java_path: String) { } #[tauri::command] -pub fn run_un_elevated(path: String) { +pub fn run_un_elevated(path: String, args: Option) { // Open the program non-elevated. match open::with( format!( - "cmd /min /C \"set __COMPAT_LAYER=RUNASINVOKER && start \"\" \"{}\"\"", - path + "cmd /min /C \"set __COMPAT_LAYER=RUNASINVOKER && start \"\" \"{}\"\" {}", + path, args.unwrap_or_else(|| "".into()) ), "C:\\Windows\\System32\\cmd.exe", ) { From c21c5ac0b17da806436c89587878de31b4e73705 Mon Sep 17 00:00:00 2001 From: Thoronium <107363768+NotThorny@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:05:22 -0600 Subject: [PATCH 2/6] Fix launch server from args --- src-tauri/src/main.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index db518de..bbefc91 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -110,11 +110,19 @@ async fn parse_args(inp: &Vec) -> Result { if args.value_of("server")? { let server_jar = config.grasscutter_path; + let mut server_path = server_jar.clone(); // Assumes grasscutter jar is named appropriately - let server_path = server_jar.trim_end_matches("grasscutter.jar"); + if server_path.contains('/') { + // Can never panic because of if + let len = server_jar.rfind('/').unwrap(); + server_path.truncate(len); + } else if server_path.contains('\\') { + let len = server_jar.rfind('\\').unwrap(); + server_path.truncate(len); + } let java_path = config.java_path; - system_helpers::run_jar(server_jar.clone(), server_path.to_string(), java_path); + system_helpers::run_jar(server_jar, server_path.to_string(), java_path); } if args.value_of::("host").is_ok() && !args.value_of::("host")?.is_empty() { From df674abd94f6d4f9f40600338a0adc9e675c2c8f Mon Sep 17 00:00:00 2001 From: Thoronium <107363768+NotThorny@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:06:04 -0600 Subject: [PATCH 3/6] Format --- src-tauri/src/main.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index bbefc91..40a1a03 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -54,7 +54,11 @@ async fn parse_args(inp: &Vec) -> Result { "no-admin", "Launch without requiring admin permissions", ); - args.flag("g", "no-gui", "Run in CLI mode. Requires -A to be passed as well."); + args.flag( + "g", + "no-gui", + "Run in CLI mode. Requires -A to be passed as well.", + ); args.flag("s", "server", "Launch the configured GC server"); args.flag( "P", @@ -64,7 +68,7 @@ async fn parse_args(inp: &Vec) -> Result { args.flag( "N", "non-elevated-game", - "Launch the game without admin permissions" + "Launch the game without admin permissions", ); args.option( "H", From 0f08bb5fbfeab662c3379c9be3123d769c9536b8 Mon Sep 17 00:00:00 2001 From: Thoronium <107363768+NotThorny@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:10:07 -0600 Subject: [PATCH 4/6] Update comment --- src-tauri/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 40a1a03..e56bd8e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -115,7 +115,7 @@ async fn parse_args(inp: &Vec) -> Result { if args.value_of("server")? { let server_jar = config.grasscutter_path; let mut server_path = server_jar.clone(); - // Assumes grasscutter jar is named appropriately + // Strip jar name from path if server_path.contains('/') { // Can never panic because of if let len = server_jar.rfind('/').unwrap(); From 361737c00db3439cec3ff65e4af37e2bafe12b1e Mon Sep 17 00:00:00 2001 From: Thoronium <107363768+NotThorny@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:56:59 -0600 Subject: [PATCH 5/6] Unpatch when exiting disabled GUI --- src-tauri/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e56bd8e..a4d22fc 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -171,6 +171,7 @@ fn main() -> Result<(), ArgsError> { // For disabled GUI ctrlc::set_handler(|| { disconnect(); + block_on(patch::unpatch_game()); std::process::exit(0); }) .unwrap_or(()); From e5d151f5125ce63d869cbf83faedcd23b671b0e6 Mon Sep 17 00:00:00 2001 From: Thoronium <107363768+NotThorny@users.noreply.github.com> Date: Sat, 22 Apr 2023 17:08:39 -0600 Subject: [PATCH 6/6] Formatting For rustfmt --- src-tauri/src/system_helpers.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src-tauri/src/system_helpers.rs b/src-tauri/src/system_helpers.rs index 9197ba5..ff8bddf 100644 --- a/src-tauri/src/system_helpers.rs +++ b/src-tauri/src/system_helpers.rs @@ -12,10 +12,7 @@ use registry::{Data, Hive, Security}; pub fn run_program(path: String, args: Option) { // Without unwrap_or, this can crash when UAC prompt is denied match open::with( - format!( - "{} {}", - path, args.unwrap_or_else(|| "".into()) - ), + format!("{} {}", path, args.unwrap_or_else(|| "".into())), path.clone(), ) { Ok(_) => (), @@ -94,7 +91,8 @@ pub fn run_un_elevated(path: String, args: Option) { match open::with( format!( "cmd /min /C \"set __COMPAT_LAYER=RUNASINVOKER && start \"\" \"{}\"\" {}", - path, args.unwrap_or_else(|| "".into()) + path, + args.unwrap_or_else(|| "".into()) ), "C:\\Windows\\System32\\cmd.exe", ) {