Skip to content

Commit

Permalink
Manually implement help/version flags in release mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Feb 19, 2024
1 parent 013a47e commit b408804
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ use clap::Parser;

/// A simple, non-invasive injector for BG3 native dll plugins
#[derive(Parser, Debug)]
#[clap(disable_help_flag = true, disable_version_flag = true)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Show console window
#[arg(long)]
pub cli: bool,

/// Print help
#[arg(short, long, conflicts_with = "version")]
pub help: bool,

/// Print version
#[arg(short = 'V', long, conflicts_with = "help")]
pub version: bool,
}
72 changes: 59 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod single_instance;
mod tray;

use std::{
io::prelude::Write,
path::{Path, PathBuf},
time::Duration,
};
Expand Down Expand Up @@ -50,6 +51,31 @@ pub fn run(run_type: RunType) -> Result<()> {

let args = Args::parse();

if args.help {
use clap::CommandFactory;

#[cfg(not(debug_assertions))]
alloc_console()?;

let mut cmd = Args::command();
cmd.print_help()?;

#[cfg(not(debug_assertions))]
enter_to_exit()?;

return Ok(());
} else if args.version {
#[cfg(not(debug_assertions))]
alloc_console()?;

println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));

#[cfg(not(debug_assertions))]
enter_to_exit()?;

return Ok(());
}

let (plugins_dir, config, _guard) = setup(&args)?;

let (bg3, bg3_dx11) = build_config_game_binary_paths(&config);
Expand Down Expand Up @@ -138,19 +164,8 @@ fn setup_logs<P: AsRef<Path>>(plugins_dir: P, args: &Args) -> Result<Option<Work
let mut worker_guard: Option<WorkerGuard> = None;

if cfg!(debug_assertions) || args.cli {
if cfg!(not(debug_assertions)) {
unsafe {
AllocConsole()?;

let handle = GetStdHandle(STD_OUTPUT_HANDLE)?;
SetConsoleMode(
handle,
ENABLE_PROCESSED_OUTPUT
| ENABLE_WRAP_AT_EOL_OUTPUT
| ENABLE_VIRTUAL_TERMINAL_PROCESSING,
)?;
}
}
#[cfg(not(debug_assertions))]
alloc_console()?;

tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_env("YABG3ML_LOG"))
Expand All @@ -173,3 +188,34 @@ fn setup_logs<P: AsRef<Path>>(plugins_dir: P, args: &Args) -> Result<Option<Work

Ok(worker_guard)
}

#[allow(unused)]
fn alloc_console() -> Result<()> {
unsafe {
AllocConsole()?;
}

let handle = unsafe { GetStdHandle(STD_OUTPUT_HANDLE)? };

unsafe {
SetConsoleMode(
handle,
ENABLE_PROCESSED_OUTPUT
| ENABLE_WRAP_AT_EOL_OUTPUT
| ENABLE_VIRTUAL_TERMINAL_PROCESSING,
)?;
}

Ok(())
}

#[allow(unused)]
fn enter_to_exit() -> Result<()> {
print!("\nPress ENTER to exit..");
std::io::stdout().flush()?;

// empty std input
std::io::stdin().read_line(&mut String::new())?;

Ok(())
}

0 comments on commit b408804

Please sign in to comment.