Skip to content

Commit 9f6ae26

Browse files
committed
fix entity lump extraction
1 parent 7588660 commit 9f6ae26

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/bin/bsp.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6767
println!("map version = {}", bsp_reader.header().version);
6868
println!("map revision = {}", bsp_reader.header().map_revision);
6969

70+
println!("===");
71+
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+
);
77+
}
78+
7079
Ok(())
7180
}
7281
SubCommand::ListPackedFiles(t) => {
@@ -106,8 +115,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
106115
.to_owned()
107116
});
108117

109-
// generate lump file
118+
// create a new slice where worldspawn (i.e. entity number one) is kept in the entity lump
119+
let mut slice = bsp.lump_slice(LumpIndex::LUMP_ENTITIES);
120+
let brace_pos = slice.iter().position(|b| *b == b'}');
121+
let new_slice = match brace_pos {
122+
Some(p) => slice[0..p + 1].to_owned(),
123+
None => Vec::new(),
124+
};
110125

126+
// generate extracted lump file
111127
let lump_header = &bsp.header.lumps[LumpIndex::LUMP_ENTITIES as usize];
112128
let lump_file = File::create(lump_output)?;
113129
let mut lump_writer = BufWriter::new(lump_file);
@@ -118,13 +134,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
118134
lump_writer.write_u32::<LittleEndian>(lump_header.len)?; // lumpLength
119135
lump_writer.write_u32::<LittleEndian>(bsp.header.map_revision)?; // mapRevision
120136

121-
io::copy(
122-
&mut bsp.lump_slice(LumpIndex::LUMP_ENTITIES),
123-
&mut lump_writer,
124-
)?;
137+
io::copy(&mut slice, &mut lump_writer)?;
125138

126-
// remove entity lump from input bsp
127-
bsp.replace_lump(LumpIndex::LUMP_ENTITIES, &[]);
139+
// replace bsp entity lump with a stripped one
140+
bsp.replace_lump(LumpIndex::LUMP_ENTITIES, &new_slice);
128141

129142
let lump_file = File::create(t.output)?;
130143
let mut lump_writer = BufWriter::new(lump_file);

src/bsp/buffered_bsp.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io::Write;
22

33
use byteorder::{LittleEndian, WriteBytesExt};
44

5-
use super::{lump_indices::LumpIndex, BSPHeader, BSP_LUMP_COUNT};
5+
use super::{lump_indices::LumpIndex, BSPHeader, BSP_HEADER_LEN, BSP_LUMP_COUNT};
66

77
pub struct BufferedBSP {
88
pub header: BSPHeader,
@@ -13,7 +13,8 @@ impl BufferedBSP {
1313
pub fn lump_slice(&self, lump_index: LumpIndex) -> &[u8] {
1414
let lump = &self.header.lumps[lump_index as usize];
1515

16-
&self.data_without_header[lump.off as usize..(lump.off + lump.len) as usize]
16+
&self.data_without_header
17+
[(lump.off - BSP_HEADER_LEN) as usize..(lump.off + lump.len - BSP_HEADER_LEN) as usize]
1718
}
1819

1920
pub fn replace_lump(&mut self, lump_index: LumpIndex, new_lump: &[u8]) {
@@ -24,14 +25,15 @@ impl BufferedBSP {
2425
);
2526

2627
// zero old slice
27-
let old_slice =
28-
&mut self.data_without_header[lump.off as usize..(lump.off + lump.len) as usize];
28+
let old_slice = &mut self.data_without_header
29+
[(lump.off - BSP_HEADER_LEN) as usize..(lump.off + lump.len - BSP_HEADER_LEN) as usize];
2930
for i in old_slice {
3031
*i = 0;
3132
}
3233

3334
// set new slice
34-
self.data_without_header[lump.off as usize..(lump.off) as usize + new_lump.len()]
35+
self.data_without_header[(lump.off - BSP_HEADER_LEN) as usize
36+
..(lump.off - BSP_HEADER_LEN) as usize + new_lump.len()]
3537
.copy_from_slice(new_lump);
3638

3739
// set length in header
@@ -46,7 +48,7 @@ impl BufferedBSP {
4648
writer.write_u32::<LittleEndian>(self.header.lumps[i].off)?;
4749
writer.write_u32::<LittleEndian>(self.header.lumps[i].len)?;
4850
writer.write_u32::<LittleEndian>(self.header.lumps[i].version)?;
49-
writer.write_u32::<LittleEndian>(self.header.lumps[i].ident)?;
51+
writer.write_all(&self.header.lumps[i].ident)?;
5052
}
5153

5254
writer.write_u32::<LittleEndian>(self.header.map_revision)?;

src/bsp/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Lump {
1717
pub off: u32,
1818
pub len: u32,
1919
pub version: u32,
20-
pub ident: u32,
20+
pub ident: [u8; 4],
2121
}
2222

2323
pub struct BSPHeader {
@@ -52,13 +52,15 @@ impl<R: Read> BSPReader<R> {
5252
let off = reader.read_u32::<LittleEndian>()?;
5353
let len = reader.read_u32::<LittleEndian>()?;
5454
let vers = reader.read_u32::<LittleEndian>()?;
55-
let ident = reader.read_u32::<LittleEndian>()?;
55+
56+
let mut ident_buf = [0; 4];
57+
reader.read_exact(&mut ident_buf)?;
5658

5759
lumps[i] = Lump {
5860
off: off,
5961
len: len,
6062
version: vers,
61-
ident: ident,
63+
ident: ident_buf,
6264
};
6365
}
6466
let map_revision = reader.read_u32::<LittleEndian>()?;

0 commit comments

Comments
 (0)