Skip to content

Commit

Permalink
use a new library for cli args, allow prints in release
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyrAhmady committed Aug 9, 2024
1 parent ba33bc9 commit 92eb914
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 49 deletions.
99 changes: 92 additions & 7 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ md5 = "0.7"
actix-web = "4"
actix-rt = "2"
actix-cors = "0.7"
clap = "4"
winapi = "0.3"
tauri-plugin-deep-link = "0.1.2"
gumdrop = "0.8.1"

[target.'cfg(target_os = "windows")'.dependencies]
dll-syringe = "0.15"
winreg = "0.52"

[target."cfg(windows)".dependencies]
windows = { version = "0.58.0", features = ["Win32_System_Console", "Win32_Foundation"] }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
Expand Down
114 changes: 73 additions & 41 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,31 @@ mod query;
mod rpcs;
mod samp;

use std::env;
use std::process::exit;

use clap::Parser;
use gumdrop::Options;
use injector::run_samp;
use log::{error, LevelFilter};
use tauri::Manager;
use tauri::PhysicalSize;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
#[clap(disable_help_flag = true)]
struct Args {
#[clap(long, action = clap::ArgAction::HelpLong)]
help: Option<bool>,
#[derive(Debug, Options)]
struct CliArgs {
#[options(no_short, help = "print help message")]
help: bool,

/// Server IP
#[arg(short, long, default_value_t = String::new())]
host: String,
#[options(help = "target server IP address")]
host: Option<String>,

/// Server port
#[arg(short, long, default_value_t = 0)]
port: i32,
#[options(help = "target server port")]
port: Option<i32>,

/// Nickname
#[arg(short, long, default_value_t = String::new())]
name: String,
#[options(help = "nickname to join server with")]
name: Option<String>,

/// Game path
#[arg(short, long, default_value_t = String::new())]
gamepath: String,
#[options(help = "game path to use for both game executable and samp.dll")]
gamepath: Option<String>,
}

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
Expand Down Expand Up @@ -88,28 +82,66 @@ async fn main() {
tauri_plugin_deep_link::prepare("com.open.mp");
simple_logging::log_to_file("omp-launcher.log", LevelFilter::Info).unwrap();

let args = Args::parse();
if args.host.len() > 0 && args.name.len() > 0 && args.port > 0 {
if args.gamepath.len() > 0 {
let _ = run_samp(
args.name.as_str(),
args.host.as_str(),
args.port,
args.gamepath.as_str(),
format!("{}/samp.dll", args.gamepath).as_str(),
format!(
"{}/com.open.mp/omp/omp-client.dll",
dirs_next::data_local_dir().unwrap().to_str().unwrap()
)
.as_str(),
"",
)
.await;
exit(0)
} else {
println!("You must provide game path using --game or -g. Read more about arguments in --help");
exit(0)
#[cfg(windows)]
{
use windows::Win32::System::Console::{AttachConsole, ATTACH_PARENT_PROCESS};
let _ = unsafe { AttachConsole(ATTACH_PARENT_PROCESS) };
}

let raw_args: Vec<String> = env::args().collect();
let parse_args_result = CliArgs::parse_args_default::<String>(&raw_args[1..]);
match parse_args_result {
Ok(args) => {
if args.help {
println!(
"Open Multiplayer Launcher
Usage: {} [OPTIONS]
Options:
--help
-h, --host <HOST> Server IP
-p, --port <PORT> Server port
-n, --name <NAME> Nickname
-g, --gamepath <GAMEPATH> Game path
",
raw_args[0]
);
exit(0)
}

if args.host.is_some() && args.name.is_some() && args.port.is_some() {
if args.gamepath.is_some() && args.gamepath.as_ref().unwrap().len() > 0 {
let _ = run_samp(
args.name.unwrap().as_str(),
args.host.unwrap().as_str(),
args.port.unwrap(),
args.gamepath.as_ref().unwrap().as_str(),
format!("{}/samp.dll", args.gamepath.as_ref().unwrap()).as_str(),
format!(
"{}/com.open.mp/omp/omp-client.dll",
dirs_next::data_local_dir().unwrap().to_str().unwrap()
)
.as_str(),
"",
)
.await;
exit(0)
} else {
println!("You must provide game path using --game or -g. Read more about arguments in --help");
exit(0)
}
}
}
Err(e) => {
println!("{}", e);
}
};

#[cfg(windows)]
{
use windows::Win32::System::Console::FreeConsole;
let _ = unsafe { FreeConsole() };
}

std::thread::spawn(move || {
Expand Down

0 comments on commit 92eb914

Please sign in to comment.