Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/wadtools/src/commands/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct ExtractArgs {
pub output: Option<String>,
pub filter_type: Option<Vec<LeagueFileKind>>,
pub pattern: Option<String>,
pub hash: Option<Vec<u64>>,
pub filter_invert: bool,
pub overwrite: bool,
}
Expand All @@ -29,6 +30,7 @@ pub fn extract(args: ExtractArgs, hashtable: &WadHashtable) -> eyre::Result<()>
let filter_pattern = create_filter_pattern(args.pattern)?;

extractor.set_filter_pattern(filter_pattern);
extractor.set_hash_filter(args.hash);
extractor.set_filter_invert(args.filter_invert);
let output_dir: Utf8PathBuf = match &args.output {
Some(path) => Utf8PathBuf::from(path.as_str()),
Expand Down
8 changes: 7 additions & 1 deletion crates/wadtools/src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::Serialize;
use std::fs::File;

use crate::{
extractor::{should_skip_pattern, should_skip_type},
extractor::{should_skip_hash, should_skip_pattern, should_skip_type},
utils::{create_filter_pattern, format_chunk_path_hash, WadHashtable},
};

Expand All @@ -26,6 +26,7 @@ pub struct ListArgs {
pub input: String,
pub filter_type: Option<Vec<LeagueFileKind>>,
pub pattern: Option<String>,
pub hash: Option<Vec<u64>>,
pub filter_invert: bool,
pub format: ListOutputFormat,
pub show_stats: bool,
Expand Down Expand Up @@ -65,6 +66,11 @@ pub fn list(args: ListArgs, hashtable: &WadHashtable) -> eyre::Result<()> {
let mut total_uncompressed: u64 = 0;

for chunk in wad.chunks() {
// Apply hash filter (cheapest check, before resolving path)
if should_skip_hash(chunk.path_hash, args.hash.as_deref(), args.filter_invert) {
continue;
}

let path_str = hashtable.resolve_path(chunk.path_hash);

// Apply pattern filter
Expand Down
26 changes: 26 additions & 0 deletions crates/wadtools/src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Extractor<'a> {
wad: &'a mut Wad<File>,
hashtable: &'a WadHashtable,
filter_pattern: Option<Regex>,
hash_filter: Option<Vec<u64>>,
filter_invert: bool,
}

Expand All @@ -39,6 +40,7 @@ impl<'a> Extractor<'a> {
wad,
hashtable,
filter_pattern: None,
hash_filter: None,
filter_invert: false,
}
}
Expand All @@ -47,6 +49,10 @@ impl<'a> Extractor<'a> {
self.filter_pattern = filter_pattern;
}

pub fn set_hash_filter(&mut self, hash_filter: Option<Vec<u64>>) {
self.hash_filter = hash_filter;
}

pub fn set_filter_invert(&mut self, filter_invert: bool) {
self.filter_invert = filter_invert;
}
Expand Down Expand Up @@ -135,6 +141,17 @@ impl<'a> Extractor<'a> {
break;
}

// Hash filter is the cheapest check — do it before resolving path
if should_skip_hash(
chunk.path_hash,
self.hash_filter.as_deref(),
self.filter_invert,
) {
let done = counter.fetch_add(1, Ordering::Relaxed) + 1;
span.pb_set_position(done as u64);
continue;
}

let chunk_path_str = self.hashtable.resolve_path(chunk.path_hash);

span.pb_set_message(&truncate_middle(chunk_path_str.as_ref(), MAX_LOG_PATH_LEN));
Expand Down Expand Up @@ -300,6 +317,15 @@ pub(crate) fn should_skip_pattern(
false
}

/// Returns true if the chunk should be skipped based on the hash filter.
pub(crate) fn should_skip_hash(
path_hash: u64,
hash_filter: Option<&[u64]>,
filter_invert: bool,
) -> bool {
hash_filter.is_some_and(|hashes| hashes.contains(&path_hash) == filter_invert)
}

/// Returns true if the chunk should be skipped based on the type filter.
pub(crate) fn should_skip_type(
chunk_kind: LeagueFileKind,
Expand Down
31 changes: 31 additions & 0 deletions crates/wadtools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ pub enum Commands {
)]
pattern: Option<String>,

/// Only include chunks whose path hash matches one of these 16-char hex values
#[arg(long, value_name = "HASH", num_args = 1..)]
hash: Option<Vec<String>>,

/// Invert the -f and -x filters (exclude matching files instead of including them)
#[arg(short = 'v', long = "filter-invert")]
filter_invert: bool,
Expand Down Expand Up @@ -186,6 +190,10 @@ pub enum Commands {
)]
pattern: Option<String>,

/// Only include chunks whose path hash matches one of these 16-char hex values
#[arg(long, value_name = "HASH", num_args = 1..)]
hash: Option<Vec<String>>,

/// Invert the -f and -x filters (exclude matching files instead of including them)
#[arg(short = 'v', long = "filter-invert")]
filter_invert: bool,
Expand Down Expand Up @@ -242,6 +250,7 @@ fn main() -> eyre::Result<()> {
hashtable,
filter_type,
pattern,
hash,
filter_invert,
list_filters,
overwrite,
Expand All @@ -256,13 +265,15 @@ fn main() -> eyre::Result<()> {
}
let hashtable_dir = args.hashtable_dir.or_else(|| config.hashtable_dir.clone());
let ht = load_hashtable(hashtable_dir.as_deref(), hashtable.as_deref())?;
let hash_filter = parse_hashes(hash)?;
for path in resolved {
extract(
ExtractArgs {
input: path,
output: output.clone(),
filter_type: filter_type.clone(),
pattern: pattern.clone(),
hash: hash_filter.clone(),
filter_invert,
overwrite,
},
Expand Down Expand Up @@ -296,6 +307,7 @@ fn main() -> eyre::Result<()> {
hashtable,
filter_type,
pattern,
hash,
filter_invert,
format,
stats,
Expand All @@ -306,12 +318,14 @@ fn main() -> eyre::Result<()> {
}
let hashtable_dir = args.hashtable_dir.or_else(|| config.hashtable_dir.clone());
let ht = load_hashtable(hashtable_dir.as_deref(), hashtable.as_deref())?;
let hash_filter = parse_hashes(hash)?;
for path in resolved {
list(
ListArgs {
input: path,
filter_type: filter_type.clone(),
pattern: pattern.clone(),
hash: hash_filter.clone(),
filter_invert,
format,
show_stats: stats,
Expand Down Expand Up @@ -422,6 +436,23 @@ fn parse_filter_type(s: &str) -> Result<LeagueFileKind, String> {
}
}

fn parse_hashes(raw: Option<Vec<String>>) -> eyre::Result<Option<Vec<u64>>> {
let Some(strings) = raw else {
return Ok(None);
};
let mut hashes = Vec::with_capacity(strings.len());
for s in &strings {
let h = u64::from_str_radix(s, 16).map_err(|_| {
eyre::eyre!(
"invalid hash '{}': expected a 16-character hexadecimal value (e.g. 0a1b2c3d4e5f6789)",
s
)
})?;
hashes.push(h);
}
Ok(Some(hashes))
}

fn cli_styles() -> Styles {
Styles::styled()
.header(AnsiColor::Yellow.on_default().bold())
Expand Down