use clap::Parser;
use mhif::{
DEFAULT_DOWNLOAD_JOBS, DEFAULT_OUTPUT_DIR, DownloadOptions, DownloadReport, download_assets,
extract_asset_ids_cached,
};
use std::{path::PathBuf, process::ExitCode, time::Instant};
#[derive(Debug, Parser)]
#[command(
name = "mhif",
version,
about = "Download assets referenced by Roblox files"
)]
struct Arguments {
#[arg(
value_name = "INPUT",
help = "Roblox file or directory to scan recursively"
)]
input: PathBuf,
#[arg(long, value_name = "DIR", default_value = DEFAULT_OUTPUT_DIR)]
out_dir: PathBuf,
#[arg(
long,
value_name = "N",
default_value_t = DEFAULT_DOWNLOAD_JOBS,
value_parser = parse_jobs,
help = "Maximum number of concurrent downloads"
)]
jobs: usize,
#[arg(
long,
value_name = "FILE",
help = "Override automatic .ROBLOSECURITY discovery with FILE"
)]
roblosecurity: Option<PathBuf>,
#[arg(short, long)]
verbose: bool,
}
fn parse_jobs(value: &str) -> Result<usize, String> {
let jobs = value
.parse::<usize>()
.map_err(|error| format!("jobs must be a positive integer: {error}"))?;
if jobs == 0 {
return Err("jobs must be at least 1".to_owned());
}
Ok(jobs)
}
fn main() -> ExitCode {
let start_time = Instant::now();
let arguments = Arguments::parse();
match run(arguments) {
Ok(report) => {
println!(
"downloaded {}, reused {} cached assets ({} total) to {} in {:.2}s",
report.downloaded,
report.cached,
report.downloaded + report.cached,
report.output_directory.display(),
start_time.elapsed().as_secs_f64()
);
if report.failed.is_empty() {
ExitCode::SUCCESS
} else {
for failure in report.failed {
eprintln!("error: asset {}: {}", failure.asset_id, failure.error);
}
ExitCode::FAILURE
}
}
Err(error) => {
eprintln!("error: {error}");
ExitCode::FAILURE
}
}
}
fn run(arguments: Arguments) -> Result<DownloadReport, mhif::MhifError> {
let extraction = extract_asset_ids_cached(&arguments.input, &arguments.out_dir)?;
if arguments.verbose {
eprintln!(
"found {} unique asset references across {} files in {} (parsed {}, reused {} cached files)",
extraction.asset_ids.len(),
extraction.files,
arguments.input.display(),
extraction.parsed,
extraction.cached,
);
}
let mut options = DownloadOptions::new(&arguments.out_dir);
options = options.with_jobs(arguments.jobs);
if let Some(path) = arguments.roblosecurity {
options = options.with_roblosecurity_file(path);
}
let report = download_assets(&extraction.asset_ids, &options)?;
if arguments.verbose {
for asset in &report.assets {
let action = if asset.cached {
"reused cached"
} else {
"downloaded"
};
eprintln!("{action} {} -> {}", asset.asset_id, asset.path.display());
}
for failure in &report.failed {
eprintln!("failed {}: {}", failure.asset_id, failure.error);
}
}
Ok(report)
}