cs2-dumper/src/main.rs

104 lines
2.5 KiB
Rust
Raw Normal View History

2024-03-28 20:19:20 +08:00
use std::path::PathBuf;
2024-04-20 22:30:00 +08:00
use std::str::FromStr;
2024-03-01 23:02:58 +08:00
use std::time::Instant;
2023-10-20 13:23:43 +08:00
2024-03-28 20:19:20 +08:00
use clap::*;
2023-10-26 13:41:34 +08:00
2024-03-28 20:19:20 +08:00
use log::{info, Level};
2023-09-28 22:41:21 +08:00
2024-03-28 20:19:20 +08:00
use memflow::prelude::v1::*;
2023-10-26 13:41:34 +08:00
2024-03-28 20:19:20 +08:00
use simplelog::{ColorChoice, TermLogger};
2023-10-26 13:41:34 +08:00
2024-03-28 20:19:20 +08:00
use error::Result;
use output::Output;
2023-09-25 22:46:10 +08:00
2024-03-28 20:19:20 +08:00
mod analysis;
mod error;
2024-04-06 01:20:08 +08:00
mod mem;
2024-03-28 20:19:20 +08:00
mod output;
mod source2;
2024-04-20 22:30:00 +08:00
#[derive(Debug, Parser)]
#[command(author, version)]
struct Args {
/// The name of the memflow connector to use.
#[arg(short, long)]
connector: Option<String>,
2023-09-28 22:41:21 +08:00
2024-04-20 22:30:00 +08:00
/// Additional arguments to pass to the memflow connector.
#[arg(short = 'a', long)]
connector_args: Option<String>,
2024-03-28 20:19:20 +08:00
2024-04-20 22:30:00 +08:00
/// The types of files to generate.
#[arg(short, long, value_delimiter = ',', default_values = ["cs", "hpp", "json", "rs"])]
file_types: Vec<String>,
2024-03-28 20:19:20 +08:00
2024-04-20 22:30:00 +08:00
/// The number of spaces to use per indentation level.
#[arg(short, long, default_value_t = 4)]
indent_size: usize,
2024-03-28 20:19:20 +08:00
2024-04-20 22:30:00 +08:00
/// The output directory to write the generated files to.
#[arg(short, long, default_value = "output")]
output: PathBuf,
2023-09-25 22:46:10 +08:00
2024-04-20 22:30:00 +08:00
/// The name of the game process.
#[arg(short, long, default_value = "cs2.exe")]
process_name: String,
2023-10-26 13:41:34 +08:00
2024-04-20 22:30:00 +08:00
/// Increase logging verbosity. Can be specified multiple times.
#[arg(short, action = ArgAction::Count)]
verbose: u8,
2024-03-28 20:19:20 +08:00
}
2024-04-20 22:30:00 +08:00
fn main() -> Result<()> {
let args = Args::parse();
let now = Instant::now();
2023-09-28 22:41:21 +08:00
2024-04-20 22:30:00 +08:00
let log_level = match args.verbose {
2024-03-28 20:19:20 +08:00
0 => Level::Error,
1 => Level::Warn,
2 => Level::Info,
3 => Level::Debug,
_ => Level::Trace,
};
2023-09-28 22:41:21 +08:00
2024-03-28 20:19:20 +08:00
TermLogger::init(
log_level.to_level_filter(),
Default::default(),
Default::default(),
ColorChoice::Auto,
)
.unwrap();
2023-09-25 22:46:10 +08:00
2024-04-20 22:30:00 +08:00
let conn_args = args
.connector_args
2024-03-28 20:19:20 +08:00
.map(|s| ConnectorArgs::from_str(&s).expect("unable to parse connector arguments"))
.unwrap_or_default();
2023-09-25 22:46:10 +08:00
2024-04-20 22:30:00 +08:00
let os = if let Some(conn) = args.connector {
let inventory = Inventory::scan();
2024-04-20 22:30:00 +08:00
inventory
.builder()
.connector(&conn)
.args(conn_args)
.os("win32")
.build()?
} else {
// Fallback to the native OS layer if no connector name was specified.
memflow_native::create_os(&Default::default(), Default::default())?
};
let mut process = os.into_process_by_name(&args.process_name)?;
let result = analysis::analyze_all(&mut process)?;
let output = Output::new(&args.file_types, args.indent_size, &args.output, &result)?;
2024-04-20 22:30:00 +08:00
output.dump_all(&mut process)?;
info!("finished in {:?}", now.elapsed());
Ok(())
}