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"
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"

View File

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

View File

@@ -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 {

View File

@@ -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<String>) -> Result<Args, ArgsError> {
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<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!("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]