real arg parse

This commit is contained in:
SpikeHD
2023-04-21 16:50:53 -07:00
parent 48dff50a5d
commit a67eca49e5
4 changed files with 83 additions and 34 deletions

26
src-tauri/Cargo.lock generated
View File

@@ -76,6 +76,16 @@ version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704" 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]] [[package]]
name = "asn1-rs" name = "asn1-rs"
version = "0.3.1" version = "0.3.1"
@@ -780,6 +790,7 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
name = "cultivation" name = "cultivation"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"args",
"cc", "cc",
"ctrlc", "ctrlc",
"duct", "duct",
@@ -1387,6 +1398,15 @@ dependencies = [
"version_check", "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]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.1.16" version = "0.1.16"
@@ -4624,6 +4644,12 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99"
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.3" version = "0.2.3"

View File

@@ -26,6 +26,9 @@ sudo = "0.6.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.7", features = ["api-all"] } tauri = { version = "1.0.7", features = ["api-all"] }
# Arg parsing
args = "2.0"
# Access system process info. # Access system process info.
sysinfo = "0.24.6" sysinfo = "0.24.6"

View File

@@ -4,27 +4,27 @@ use std::string::String;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Configuration { pub struct Configuration {
toggle_grasscutter: bool, pub toggle_grasscutter: bool,
game_install_path: String, pub game_install_path: String,
grasscutter_with_game: bool, pub grasscutter_with_game: bool,
grasscutter_path: String, pub grasscutter_path: String,
java_path: String, pub java_path: String,
close_action: u64, pub close_action: u64,
startup_launch: bool, pub startup_launch: bool,
last_ip: String, pub last_ip: String,
last_port: u64, pub last_port: u64,
language: String, pub language: String,
customBackground: String, pub customBackground: String,
cert_generated: bool, pub cert_generated: bool,
theme: String, pub theme: String,
https_enabled: bool, pub https_enabled: bool,
debug_enabled: bool, pub debug_enabled: bool,
patch_rsa: bool, pub patch_rsa: bool,
use_internal_proxy: bool, pub use_internal_proxy: bool,
wipe_login: bool, pub wipe_login: bool,
horny_mode: bool, pub horny_mode: bool,
auto_mongodb: bool, pub auto_mongodb: bool,
un_elevated: bool, pub un_elevated: bool,
} }
pub fn config_path() -> PathBuf { pub fn config_path() -> PathBuf {

View File

@@ -3,6 +3,7 @@
windows_subsystem = "windows" windows_subsystem = "windows"
)] )]
use args::{Args, ArgsError};
use file_helpers::dir_exists; use file_helpers::dir_exists;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::fs; use std::fs;
@@ -37,14 +38,34 @@ fn try_flush() {
std::io::stdout().flush().unwrap_or(()) std::io::stdout().flush().unwrap_or(())
} }
fn has_arg(args: &[String], arg: &str) -> bool { async fn parse_args(inp: &Vec<String>) -> Result<Args, ArgsError> {
args.contains(&arg.to_string()) 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(); 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(); let mut pathbuf = tauri::api::path::data_dir().unwrap();
pathbuf.push("cultivation"); pathbuf.push("cultivation");
pathbuf.push("ca"); pathbuf.push("ca");
@@ -52,15 +73,14 @@ async fn arg_handler(args: &[String]) {
connect(8035, pathbuf.to_str().unwrap().to_string()).await; connect(8035, pathbuf.to_str().unwrap().to_string()).await;
} }
if has_arg(args, "--launch-game") { Ok(args)
let game_exe = config.game_install_path.clone();
}
} }
fn main() { fn main() -> Result<(), ArgsError> {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = 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!("===============================================================================");
println!("You running as a non-elevated user. Some stuff will almost definitely not work."); println!("You running as a non-elevated user. Some stuff will almost definitely not work.");
println!("==============================================================================="); println!("===============================================================================");
@@ -78,8 +98,6 @@ fn main() {
exe_path.pop(); exe_path.pop();
std::env::set_current_dir(&exe_path).unwrap(); std::env::set_current_dir(&exe_path).unwrap();
block_on(arg_handler(&args));
// For disabled GUI // For disabled GUI
ctrlc::set_handler(|| { ctrlc::set_handler(|| {
disconnect(); disconnect();
@@ -87,7 +105,7 @@ fn main() {
}) })
.unwrap_or(()); .unwrap_or(());
if !has_arg(&args, "--no-gui") { if !parsed_args.value_of("no-gui")? {
tauri::Builder::default() tauri::Builder::default()
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
enable_process_watcher, enable_process_watcher,
@@ -149,6 +167,8 @@ fn main() {
// Always disconnect upon closing the program // Always disconnect upon closing the program
disconnect(); disconnect();
Ok(())
} }
#[tauri::command] #[tauri::command]