From 48dff50a5dc884b0f2fbe8a019f96d5c16cf67e0 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 16:28:56 -0700 Subject: [PATCH 01/17] config reading --- .vscode/settings.json | 3 +++ src-tauri/src/config.rs | 44 +++++++++++++++++++++++++++++++++++++++++ src-tauri/src/main.rs | 9 ++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 src-tauri/src/config.rs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..53e875e0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.linkedProjects": [".\\src-tauri\\Cargo.toml"] +} diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs new file mode 100644 index 00000000..c0f7dd9e --- /dev/null +++ b/src-tauri/src/config.rs @@ -0,0 +1,44 @@ +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use std::string::String; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Configuration { + toggle_grasscutter: bool, + game_install_path: String, + grasscutter_with_game: bool, + grasscutter_path: String, + java_path: String, + close_action: u64, + startup_launch: bool, + last_ip: String, + last_port: u64, + language: String, + customBackground: String, + cert_generated: bool, + theme: String, + https_enabled: bool, + debug_enabled: bool, + patch_rsa: bool, + use_internal_proxy: bool, + wipe_login: bool, + horny_mode: bool, + auto_mongodb: bool, + un_elevated: bool, +} + +pub fn config_path() -> PathBuf { + let mut path = tauri::api::path::data_dir().unwrap(); + path.push("cultivation"); + path.push("configuration.json"); + + path +} + +pub fn get_config() -> Configuration { + let path = config_path(); + let config = std::fs::read_to_string(path).unwrap(); + let config: Configuration = serde_json::from_str(&config).unwrap(); + + config +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 3715e316..222224ff 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -18,6 +18,7 @@ use sysinfo::{Pid, ProcessExt, System, SystemExt}; use crate::admin::reopen_as_admin; mod admin; +mod config; mod downloader; mod file_helpers; mod gamebanana; @@ -41,6 +42,8 @@ fn has_arg(args: &[String], arg: &str) -> bool { } async fn arg_handler(args: &[String]) { + let config = config::get_config(); + if has_arg(args, "--proxy") { let mut pathbuf = tauri::api::path::data_dir().unwrap(); pathbuf.push("cultivation"); @@ -48,6 +51,10 @@ async fn arg_handler(args: &[String]) { connect(8035, pathbuf.to_str().unwrap().to_string()).await; } + + if has_arg(args, "--launch-game") { + let game_exe = config.game_install_path.clone(); + } } fn main() { @@ -58,7 +65,7 @@ fn main() { println!("You running as a non-elevated user. Some stuff will almost definitely not work."); println!("==============================================================================="); - reopen_as_admin(); + //reopen_as_admin(); } // Setup datadir/cultivation just in case something went funky and it wasn't made From a67eca49e55d1e4b1ecec8c9002272735dd9768e Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 16:50:53 -0700 Subject: [PATCH 02/17] real arg parse --- src-tauri/Cargo.lock | 26 +++++++++++++++++++++++ src-tauri/Cargo.toml | 3 +++ src-tauri/src/config.rs | 42 ++++++++++++++++++------------------- src-tauri/src/main.rs | 46 +++++++++++++++++++++++++++++------------ 4 files changed, 83 insertions(+), 34 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 3a89e502..ca97fd98 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -76,6 +76,16 @@ version = "1.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704" +[[package]] +name = "args" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4b7432c65177b8d5c032d56e020dd8d407e939468479fc8c300e2d93e6d970b" +dependencies = [ + "getopts", + "log", +] + [[package]] name = "asn1-rs" version = "0.3.1" @@ -780,6 +790,7 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" name = "cultivation" version = "0.1.0" dependencies = [ + "args", "cc", "ctrlc", "duct", @@ -1387,6 +1398,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + [[package]] name = "getrandom" version = "0.1.16" @@ -4624,6 +4644,12 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "unicode-xid" version = "0.2.3" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 18320634..104d2241 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -26,6 +26,9 @@ sudo = "0.6.0" serde = { version = "1.0", features = ["derive"] } tauri = { version = "1.0.7", features = ["api-all"] } +# Arg parsing +args = "2.0" + # Access system process info. sysinfo = "0.24.6" diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index c0f7dd9e..8e52edf6 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -4,27 +4,27 @@ use std::string::String; #[derive(Serialize, Deserialize, Debug)] pub struct Configuration { - toggle_grasscutter: bool, - game_install_path: String, - grasscutter_with_game: bool, - grasscutter_path: String, - java_path: String, - close_action: u64, - startup_launch: bool, - last_ip: String, - last_port: u64, - language: String, - customBackground: String, - cert_generated: bool, - theme: String, - https_enabled: bool, - debug_enabled: bool, - patch_rsa: bool, - use_internal_proxy: bool, - wipe_login: bool, - horny_mode: bool, - auto_mongodb: bool, - un_elevated: bool, + pub toggle_grasscutter: bool, + pub game_install_path: String, + pub grasscutter_with_game: bool, + pub grasscutter_path: String, + pub java_path: String, + pub close_action: u64, + pub startup_launch: bool, + pub last_ip: String, + pub last_port: u64, + pub language: String, + pub customBackground: String, + pub cert_generated: bool, + pub theme: String, + pub https_enabled: bool, + pub debug_enabled: bool, + pub patch_rsa: bool, + pub use_internal_proxy: bool, + pub wipe_login: bool, + pub horny_mode: bool, + pub auto_mongodb: bool, + pub un_elevated: bool, } pub fn config_path() -> PathBuf { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 222224ff..ab93f817 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,6 +3,7 @@ windows_subsystem = "windows" )] +use args::{Args, ArgsError}; use file_helpers::dir_exists; use once_cell::sync::Lazy; use std::fs; @@ -37,14 +38,34 @@ fn try_flush() { std::io::stdout().flush().unwrap_or(()) } -fn has_arg(args: &[String], arg: &str) -> bool { - args.contains(&arg.to_string()) -} +async fn parse_args(inp: &Vec) -> Result { + let mut args = Args::new( + "Cultivation", + "Private server helper program for an Anime Game", + ); + args.flag("h", "help", "Print various CLI args"); + args.flag("p", "proxy", "Start the proxy server"); + args.flag("g", "launch-game", "Launch the game"); + args.flag( + "na", + "no-admin", + "Launch without requiring admin permissions", + ); + args.flag("ng", "no-gui", "Run in CLI mode"); + + args.parse(inp); -async fn arg_handler(args: &[String]) { let config = config::get_config(); - if has_arg(args, "--proxy") { + if args.value_of("help")? { + println!("{}", args.full_usage()); + std::process::exit(0); + } + + if args.value_of("launch-game")? {} + + if args.value_of("proxy")? { + println!("Starting proxy server..."); let mut pathbuf = tauri::api::path::data_dir().unwrap(); pathbuf.push("cultivation"); pathbuf.push("ca"); @@ -52,15 +73,14 @@ async fn arg_handler(args: &[String]) { connect(8035, pathbuf.to_str().unwrap().to_string()).await; } - if has_arg(args, "--launch-game") { - let game_exe = config.game_install_path.clone(); - } + Ok(args) } -fn main() { +fn main() -> Result<(), ArgsError> { let args: Vec = std::env::args().collect(); + let parsed_args = block_on(parse_args(&args)).unwrap(); - if !is_elevated() && !has_arg(&args, "--no-admin") { + if !is_elevated() && parsed_args.value_of("no-admin")? { println!("==============================================================================="); println!("You running as a non-elevated user. Some stuff will almost definitely not work."); println!("==============================================================================="); @@ -78,8 +98,6 @@ fn main() { exe_path.pop(); std::env::set_current_dir(&exe_path).unwrap(); - block_on(arg_handler(&args)); - // For disabled GUI ctrlc::set_handler(|| { disconnect(); @@ -87,7 +105,7 @@ fn main() { }) .unwrap_or(()); - if !has_arg(&args, "--no-gui") { + if !parsed_args.value_of("no-gui")? { tauri::Builder::default() .invoke_handler(tauri::generate_handler![ enable_process_watcher, @@ -149,6 +167,8 @@ fn main() { // Always disconnect upon closing the program disconnect(); + + Ok(()) } #[tauri::command] From 4bcfd7ec092eb130f47f6c7386e16fe65b49c467 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 17:03:04 -0700 Subject: [PATCH 03/17] more options --- src-tauri/src/config.rs | 2 +- src-tauri/src/main.rs | 32 +++++++++++++++++++++++++------- src-tauri/src/system_helpers.rs | 2 ++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 8e52edf6..d51c4402 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -12,7 +12,7 @@ pub struct Configuration { pub close_action: u64, pub startup_launch: bool, pub last_ip: String, - pub last_port: u64, + pub last_port: String, pub language: String, pub customBackground: String, pub cert_generated: bool, diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index ab93f817..deee81e2 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -45,15 +45,16 @@ async fn parse_args(inp: &Vec) -> Result { ); args.flag("h", "help", "Print various CLI args"); args.flag("p", "proxy", "Start the proxy server"); - args.flag("g", "launch-game", "Launch the game"); + args.flag("G", "launch-game", "Launch the game"); args.flag( - "na", + "A", "no-admin", "Launch without requiring admin permissions", ); - args.flag("ng", "no-gui", "Run in CLI mode"); + args.flag("g", "no-gui", "Run in CLI mode"); + args.flag("s", "server", "Launch the configured GC server"); - args.parse(inp); + args.parse(inp).unwrap(); let config = config::get_config(); @@ -62,7 +63,24 @@ async fn parse_args(inp: &Vec) -> Result { std::process::exit(0); } - if args.value_of("launch-game")? {} + if args.value_of("launch-game")? { + let game_path = config.game_install_path; + system_helpers::run_program(game_path.to_string(), None) + } + + if args.value_of("server")? { + let server_jar = config.grasscutter_path; + let mut server_path = server_jar.clone(); + let java_path = config.java_path; + + server_path.pop(); + + system_helpers::run_jar( + server_jar.to_string(), + server_path.to_string(), + java_path.to_string(), + ); + } if args.value_of("proxy")? { println!("Starting proxy server..."); @@ -80,12 +98,12 @@ fn main() -> Result<(), ArgsError> { let args: Vec = std::env::args().collect(); let parsed_args = block_on(parse_args(&args)).unwrap(); - if !is_elevated() && parsed_args.value_of("no-admin")? { + if !is_elevated() && !parsed_args.value_of("no-admin")? { println!("==============================================================================="); println!("You running as a non-elevated user. Some stuff will almost definitely not work."); println!("==============================================================================="); - //reopen_as_admin(); + reopen_as_admin(); } // Setup datadir/cultivation just in case something went funky and it wasn't made diff --git a/src-tauri/src/system_helpers.rs b/src-tauri/src/system_helpers.rs index 50eec247..7d09fa9c 100644 --- a/src-tauri/src/system_helpers.rs +++ b/src-tauri/src/system_helpers.rs @@ -67,6 +67,8 @@ pub fn run_jar(path: String, execute_in: String, java_path: String) { format!("\"{}\" -jar \"{}\"", java_path, path) }; + println!("Launching .jar with command: {}", &command); + // Open the program from the specified path. match open::with( format!("/k cd /D \"{}\" & {}", &execute_in, &command), From 64a04e927c1738e4c2f12d9074930a03133f4575 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 17:37:21 -0700 Subject: [PATCH 04/17] set host --- src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/src/main.rs | 16 ++++++++++++++++ src-tauri/src/proxy.rs | 2 ++ 4 files changed, 20 insertions(+) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index ca97fd98..dcd6b4c7 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -796,6 +796,7 @@ dependencies = [ "duct", "file_diff", "futures-util", + "getopts", "http", "hudsucker", "is_elevated", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 104d2241..59f80127 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -28,6 +28,7 @@ tauri = { version = "1.0.7", features = ["api-all"] } # Arg parsing args = "2.0" +getopts = "0.2" # Access system process info. sysinfo = "0.24.6" diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index deee81e2..b60a00a4 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,9 +3,12 @@ windows_subsystem = "windows" )] +use args::validations::{Order, OrderValidation}; use args::{Args, ArgsError}; use file_helpers::dir_exists; +use getopts; use once_cell::sync::Lazy; +use proxy::set_proxy_addr; use std::fs; use std::io::Write; use std::{collections::HashMap, sync::Mutex}; @@ -53,6 +56,14 @@ async fn parse_args(inp: &Vec) -> Result { ); args.flag("g", "no-gui", "Run in CLI mode"); args.flag("s", "server", "Launch the configured GC server"); + args.option( + "H", + "host", + "Host to connect to (eg. 'localhost:443' or 'my.awesomeserver.com:6969)", + "SERVER_HOST", + getopts::Occur::Optional, + None, + ); args.parse(inp).unwrap(); @@ -82,6 +93,11 @@ async fn parse_args(inp: &Vec) -> Result { ); } + if !args.value_of::("host")?.is_empty() { + let host = args.value_of::("host")?; + set_proxy_addr(host); + } + if args.value_of("proxy")? { println!("Starting proxy server..."); let mut pathbuf = tauri::api::path::data_dir().unwrap(); diff --git a/src-tauri/src/proxy.rs b/src-tauri/src/proxy.rs index 87283eeb..3aa66664 100644 --- a/src-tauri/src/proxy.rs +++ b/src-tauri/src/proxy.rs @@ -47,6 +47,8 @@ pub fn set_proxy_addr(addr: String) { } else { *SERVER.lock().unwrap() = addr; } + + println!("Set server to {}", SERVER.lock().unwrap()); } #[async_trait] From 3141bcea411d7d717df175d7c50259e85b17147c Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 17:46:31 -0700 Subject: [PATCH 05/17] game options --- src-tauri/src/main.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index b60a00a4..96c0434c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -59,11 +59,19 @@ async fn parse_args(inp: &Vec) -> Result { args.option( "H", "host", - "Host to connect to (eg. 'localhost:443' or 'my.awesomeserver.com:6969)", + "Set host to connect to (eg. 'localhost:443' or 'my.awesomeserver.com:6969)", "SERVER_HOST", getopts::Occur::Optional, None, ); + args.option( + "a", + "game-args", + "Arguments to pass to the game process, if launching it", + r#""-opt-one -opt-two"#, + getopts::Occur::Optional, + None, + ); args.parse(inp).unwrap(); @@ -76,7 +84,8 @@ async fn parse_args(inp: &Vec) -> Result { if args.value_of("launch-game")? { let game_path = config.game_install_path; - system_helpers::run_program(game_path.to_string(), None) + let game_args: String = args.value_of("game-args")?; + system_helpers::run_program(game_path.to_string(), Some(game_args)) } if args.value_of("server")? { From e6492825dce864363d2ef876ca9e254485e2174c Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 18:06:53 -0700 Subject: [PATCH 06/17] optionally patch/unpatch --- src-tauri/src/main.rs | 15 +++++++++++ src-tauri/src/patch.rs | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src-tauri/src/patch.rs diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 96c0434c..468fb3a3 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -27,6 +27,7 @@ mod downloader; mod file_helpers; mod gamebanana; mod lang; +mod patch; mod proxy; mod release; mod system_helpers; @@ -56,6 +57,11 @@ async fn parse_args(inp: &Vec) -> Result { ); args.flag("g", "no-gui", "Run in CLI mode"); args.flag("s", "server", "Launch the configured GC server"); + args.flag( + "P", + "path", + "Patch your game before launching, with whatever your game version needs", + ); args.option( "H", "host", @@ -85,6 +91,12 @@ async fn parse_args(inp: &Vec) -> Result { if args.value_of("launch-game")? { let game_path = config.game_install_path; let game_args: String = args.value_of("game-args")?; + + // Patch if needed + if args.value_of("patch")? { + patch::patch_game().await; + } + system_helpers::run_program(game_path.to_string(), Some(game_args)) } @@ -211,6 +223,9 @@ fn main() -> Result<(), ArgsError> { // Always disconnect upon closing the program disconnect(); + // Always unpatch game upon closing the program + patch::unpatch_game(); + Ok(()) } diff --git a/src-tauri/src/patch.rs b/src-tauri/src/patch.rs new file mode 100644 index 00000000..5f8cc35b --- /dev/null +++ b/src-tauri/src/patch.rs @@ -0,0 +1,59 @@ +use crate::config; +use crate::file_helpers; +use crate::system_helpers; +use std::path::PathBuf; + +pub async fn patch_game() -> bool { + let patch_path = PathBuf::from(system_helpers::install_location()).join("patch/version.dll"); + + // Are we already patched with mhypbase? If so, that's fine, just continue as normal + let game_is_patched = file_helpers::are_files_identical( + patch_path.clone().to_str().unwrap(), + PathBuf::from(get_game_rsa_path().await.unwrap()) + .join("mhypbase.dll") + .to_str() + .unwrap(), + ); + + // Tell user they won't be unpatched with manual mhypbase patch + if game_is_patched { + println!( + "You are already patched using mhypbase, so you will not be auto patched and unpatched!" + ); + return true; + } + + // Copy the patch to game files + let replaced = file_helpers::copy_file_with_new_name( + patch_path.clone().to_str().unwrap().to_string(), + get_game_rsa_path().await.unwrap(), + String::from("version.dll"), + ); + + if !replaced { + return false; + } + + true +} + +pub async fn unpatch_game() -> bool { + // Just delete patch since it's not replacing any existing file + let deleted = file_helpers::delete_file( + PathBuf::from(get_game_rsa_path().await.unwrap()) + .join("version.dll") + .to_str() + .unwrap() + .to_string(), + ); + + deleted +} + +pub async fn get_game_rsa_path() -> Option { + let config = config::get_config(); + let mut game_folder = PathBuf::from(config.game_install_path); + game_folder.pop(); + + Some(format!("{}/", game_folder.to_str().unwrap()).replace("\\", "/")) +} From 45ecfcbeb3179aeb9d0fe5898e4856cfea7d6a9e Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 18:09:27 -0700 Subject: [PATCH 07/17] fix host option --- src-tauri/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 468fb3a3..2cc1b9e3 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -114,7 +114,7 @@ async fn parse_args(inp: &Vec) -> Result { ); } - if !args.value_of::("host")?.is_empty() { + if args.value_of::("host").is_ok() && !args.value_of::("host")?.is_empty() { let host = args.value_of::("host")?; set_proxy_addr(host); } @@ -224,7 +224,7 @@ fn main() -> Result<(), ArgsError> { disconnect(); // Always unpatch game upon closing the program - patch::unpatch_game(); + block_on(patch::unpatch_game()); Ok(()) } From 88b5b403000f9cbcbfcecb215eb7e01ad915fbe7 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 18:10:05 -0700 Subject: [PATCH 08/17] lint and format --- src-tauri/src/main.rs | 9 ++------- src-tauri/src/patch.rs | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 2cc1b9e3..ce5b4f05 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,10 +3,9 @@ windows_subsystem = "windows" )] -use args::validations::{Order, OrderValidation}; use args::{Args, ArgsError}; use file_helpers::dir_exists; -use getopts; + use once_cell::sync::Lazy; use proxy::set_proxy_addr; use std::fs; @@ -107,11 +106,7 @@ async fn parse_args(inp: &Vec) -> Result { server_path.pop(); - system_helpers::run_jar( - server_jar.to_string(), - server_path.to_string(), - java_path.to_string(), - ); + 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() { diff --git a/src-tauri/src/patch.rs b/src-tauri/src/patch.rs index 5f8cc35b..6399c5e1 100644 --- a/src-tauri/src/patch.rs +++ b/src-tauri/src/patch.rs @@ -55,5 +55,5 @@ pub async fn get_game_rsa_path() -> Option { let mut game_folder = PathBuf::from(config.game_install_path); game_folder.pop(); - Some(format!("{}/", game_folder.to_str().unwrap()).replace("\\", "/")) + Some(format!("{}/", game_folder.to_str().unwrap()).replace('\\', "/")) } From 12b60d3b2ae7c462595aa5138cf17750a0a836b6 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 18:13:12 -0700 Subject: [PATCH 09/17] typo oops --- 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 ce5b4f05..436e6229 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -58,7 +58,7 @@ async fn parse_args(inp: &Vec) -> Result { args.flag("s", "server", "Launch the configured GC server"); args.flag( "P", - "path", + "patch", "Patch your game before launching, with whatever your game version needs", ); args.option( From 75c6481778d1f937722dc62d348689e8ac5a6cd6 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 18:13:49 -0700 Subject: [PATCH 10/17] another typo --- 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 436e6229..1bbf9a11 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -73,7 +73,7 @@ async fn parse_args(inp: &Vec) -> Result { "a", "game-args", "Arguments to pass to the game process, if launching it", - r#""-opt-one -opt-two"#, + r#""-opt-one -opt-two""#, getopts::Occur::Optional, None, ); From 2fa203163d234eb45a0caeb7c130b27a0e6c18c6 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Fri, 21 Apr 2023 18:25:23 -0700 Subject: [PATCH 11/17] game args err check --- 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 1bbf9a11..326e3657 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -89,7 +89,7 @@ async fn parse_args(inp: &Vec) -> Result { if args.value_of("launch-game")? { let game_path = config.game_install_path; - let game_args: String = args.value_of("game-args")?; + let game_args: String = args.value_of("game-args").unwrap_or(String::new()); // Patch if needed if args.value_of("patch")? { 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 12/17] 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 326e3657..db518de4 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 7d09fa9c..9197ba52 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 13/17] 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 db518de4..bbefc91b 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 14/17] 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 bbefc91b..40a1a030 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 15/17] 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 40a1a030..e56bd8ef 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 16/17] 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 e56bd8ef..a4d22fc8 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 17/17] 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 9197ba52..ff8bddf2 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", ) {