Skip to content

Commit b6cdc4c

Browse files
committed
add lump hashing
1 parent 9f6ae26 commit b6cdc4c

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = "2018"
1212
[features]
1313
default = ["workshop", "vtf", "bsp"]
1414
workshop = ["steamworks", "rust-lzma"]
15-
bsp = ["zip"]
15+
bsp = ["strum", "zip"]
1616

1717
[dependencies]
1818
serde = { version = "1", features = ["derive"] }
@@ -31,6 +31,7 @@ lazy_static = "1"
3131
petgraph = "0.6"
3232
vtf = { version = "0.1", optional = true }
3333
zip = { version = "0.6", default-features = false, optional = true }
34+
strum = { version = "0.24", optional = true, features = ["derive"] }
3435

3536
[[bin]]
3637
name = "gma"

src/bin/bsp.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
2+
use std::collections::hash_map::DefaultHasher;
23
use std::fs::File;
4+
use std::hash::Hasher;
35
use std::io::{self, BufWriter};
46
use std::io::{BufReader, Read};
57
use std::path::{Path, PathBuf};
68
use steamws::bsp::BSPHeader;
79
use steamws::bsp::{lump_indices::LumpIndex, BSPReader};
10+
use strum::IntoEnumIterator;
811

912
use clap::{Args, Parser, Subcommand};
1013

@@ -31,6 +34,12 @@ enum SubCommand {
3134
struct InfoCommand {
3235
/// Source bsp
3336
input: PathBuf,
37+
38+
/// Print information about lumps
39+
/// Includes length, offset, ident
40+
/// Also calculates and print lump hashes (using arbitrary hashing algorithm, just for comparison purposes)
41+
#[arg{short, long}]
42+
lumps: bool,
3443
}
3544

3645
#[derive(Args)]
@@ -67,13 +76,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6776
println!("map version = {}", bsp_reader.header().version);
6877
println!("map revision = {}", bsp_reader.header().map_revision);
6978

70-
println!("===");
79+
if t.lumps {
80+
let bsp = bsp_reader.into_buffered_bsp();
81+
println!("===");
82+
83+
for lump_index in LumpIndex::iter() {
84+
let mut hasher = DefaultHasher::new();
85+
hasher.write(bsp.lump_slice(lump_index));
86+
let hash = hasher.finish();
7187

72-
for (i, lump) in bsp_reader.header().lumps.iter().enumerate() {
73-
println!(
74-
"lump #{i}: off={} len={} ident={:?}",
75-
lump.off, lump.len, lump.ident
76-
);
88+
let lump = &bsp.header.lumps[lump_index as usize];
89+
println!(
90+
"lump #{}: off={} len={} ident={:?} hash={:x}",
91+
lump_index as usize, lump.off, lump.len, lump.ident, hash
92+
);
93+
}
7794
}
7895

7996
Ok(())

src/bsp/buffered_bsp.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub struct BufferedBSP {
1212
impl BufferedBSP {
1313
pub fn lump_slice(&self, lump_index: LumpIndex) -> &[u8] {
1414
let lump = &self.header.lumps[lump_index as usize];
15+
if lump.off < BSP_HEADER_LEN {
16+
return &[];
17+
}
1518

1619
&self.data_without_header
1720
[(lump.off - BSP_HEADER_LEN) as usize..(lump.off + lump.len - BSP_HEADER_LEN) as usize]

src/bsp/lump_indices.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use strum::{EnumIter, FromRepr};
2+
3+
#[derive(Clone, Copy, FromRepr, Debug, PartialEq, EnumIter)]
14
pub enum LumpIndex {
25
LUMP_ENTITIES = 0,
36
LUMP_PLANES = 1,

0 commit comments

Comments
 (0)