1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#[macro_use]
extern crate bitflags;
extern crate bit_vec;
extern crate nalgebra as na;
#[macro_use]
mod macros;
pub mod directory;
pub mod lumps;
pub mod types;
use lumps::*;
use directory::Header;
use types::{Error, Result};
#[derive(Debug, Clone)]
pub struct BSPFile {
pub directory: Header,
pub entities: EntitiesLump,
pub textures: TexturesLump,
pub planes: PlanesLump,
pub light_vols: LightVolsLump,
pub brushes: BrushesLump,
pub vertices: VerticesLump,
pub meshverts: MeshVertsLump,
pub light_maps: LightMapsLump,
pub effects: EffectsLump,
pub faces: FaceLump,
pub tree: BSPTree,
pub visdata: VisDataLump,
pub models: ModelsLump,
pub advertisements: Option<AdvertisementsLump>
}
impl BSPFile {
pub fn from_buffer(buf: Box<[u8]>) -> Result<BSPFile> {
let header = Header::from(&buf)?;
match header.version {
0x2e | 0x2f => {
let entities = EntitiesLump::from_lump(header.get_lump(&buf, 0))?;
let textures = TexturesLump::from_lump(header.get_lump(&buf, 1))?;
let planes = PlanesLump::from_lump(header.get_lump(&buf, 2))?;
let vertices = VerticesLump::from_lump(header.get_lump(&buf, 10))?;
let meshverts = MeshVertsLump::from_lump(header.get_lump(&buf, 11))?;
let light_maps = LightMapsLump::from_lump(header.get_lump(&buf, 14))?;
let light_vols = LightVolsLump::from_lump(header.get_lump(&buf, 15))?;
let visdata = VisDataLump::from_lump(header.get_lump(&buf, 16))?;
let brushes = BrushesLump::from_lump(
header.get_lump(&buf, 8),
header.get_lump(&buf, 9),
&textures,
&planes
)?;
let effects = EffectsLump::from_lump(header.get_lump(&buf, 12), &brushes)?;
let faces = FaceLump::from_lump(
header.get_lump(&buf, 13),
&textures,
&effects,
&vertices,
&meshverts,
&light_maps,
)?;
let tree = BSPTree::from_lumps(
header.get_lump(&buf, 3),
header.get_lump(&buf, 4),
header.get_lump(&buf, 5),
header.get_lump(&buf, 6),
&faces,
&brushes,
)?;
let models = ModelsLump::from_lump(header.get_lump(&buf, 7), &faces, &brushes)?;
let advertisements = if header.version == 0x2f {
Some(AdvertisementsLump::from_lump(header.get_lump(&buf, 17))?)
} else {
None
};
Ok(BSPFile {
directory: header,
entities,
textures,
planes,
light_vols,
light_maps,
vertices,
meshverts,
visdata,
advertisements,
brushes,
effects,
faces,
tree,
models
})
}
_ => Err(Error::Unsupported {
version: header.version,
}),
}
}
}