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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*
* Copyright (C) Oscar Shrimpton 2020
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use std::str;
use super::Q3BSPFile;
use crate::coords::CoordSystem;
use crate::helpers::slice_to_u32;
use crate::traits::textures::*;
use crate::types::{ParseError, Result};
const TEXTURE_LUMP_SIZE: usize = 64 + 4 + 4;
/// Try to parse the given buffer as an entities lump.
/// # Format
/// Each entity is:
/// string[64] name Texture name.
/// int flags Surface flags.
/// int contents Content flags.
/// Length of entities is total lump size / TEXTURE_LUMP_SIZE (64 + 4 + 4)
pub fn from_data(lump: &[u8]) -> Result<Box<[Texture]>> {
if lump.is_empty() || lump.len() % TEXTURE_LUMP_SIZE != 0 {
return Err(ParseError::Invalid);
}
let length = lump.len() / TEXTURE_LUMP_SIZE;
let mut textures = Vec::with_capacity(length);
for n in 0..length {
let offset = n * TEXTURE_LUMP_SIZE;
textures.push(Texture {
name: str::from_utf8(&lump[offset..offset + 64])
.map_err(|_| ParseError::Invalid)?
.trim_matches('\0')
.to_owned(),
surface: SurfaceFlags::from_bits_truncate(slice_to_u32(
&lump[offset + 64..offset + 68],
)),
contents: ContentsFlags::from_bits_truncate(slice_to_u32(
&lump[offset + 68..offset + 72],
)),
});
}
Ok(textures.into_boxed_slice())
}
impl<T: CoordSystem> HasTextures for Q3BSPFile<T> {
type TexturesIter<'a> = std::slice::Iter<'a, Texture>;
fn textures_iter(&self) -> Self::TexturesIter<'_> {
self.textures.iter()
}
fn get_texture(&self, idx: u32) -> &Texture {
&self.textures[idx as usize]
}
}
#[test]
fn textures_single_texture() {
let buf: &[u8] = &[
b'T', b'E', b'S', b'T', b' ', b'T', b'E', b'X', b'T', b'U', b'R', b'E', 0x00, 0x00, 0x00,
0x00, // name (padded to 64 bytes)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x43, 0x00, 0x04, 0x00, // surface flags
0x09, 0x00, 0x00, 0x00, // contents flags
];
let lump = from_data(buf).unwrap();
assert_eq!(lump.len(), 1);
assert_eq!(lump[0].name, "TEST TEXTURE");
assert_eq!(
lump[0].surface,
SurfaceFlags::NO_DAMAGE | SurfaceFlags::SLICK | SurfaceFlags::FLESH | SurfaceFlags::DUST
);
assert_eq!(lump[0].contents, ContentsFlags::SOLID | ContentsFlags::LAVA);
}
#[test]
fn textures_multiple_textures() {
let buf: &[u8] = &[
b'T', b'E', b'S', b'T', b' ', b'T', b'E', b'X', b'T', b'U', b'R', b'E', b'1', 0x00, 0x00,
0x00, // name (padded to 64 bytes)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x43, 0x00, 0x04, 0x00, // surface flags
0x09, 0x00, 0x00, 0x00, // contents flags
b'T', b'E', b'S', b'T', b' ', b'T', b'E', b'X', b'T', b'U', b'R', b'E', b'2', 0x00, 0x00,
0x00, // name (padded to 64 bytes)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x2c, 0x10, 0x00, 0x00, // surface flags
0x01, 0x00, 0x00, 0x00, // contents flags
b'T', b'E', b'S', b'T', b' ', b'T', b'E', b'X', b'T', b'U', b'R', b'E', b'3', 0x00, 0x00,
0x00, // name (padded to 64 bytes)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, // surface flags
0x41, 0x00, 0x00, 0x00, // contents flags
];
let lump = from_data(buf).unwrap();
assert_eq!(lump.len(), 3);
assert_eq!(lump[0].name, "TEST TEXTURE1");
assert_eq!(lump[1].name, "TEST TEXTURE2");
assert_eq!(lump[2].name, "TEST TEXTURE3");
assert_eq!(
lump[0].surface,
SurfaceFlags::NO_DAMAGE | SurfaceFlags::SLICK | SurfaceFlags::FLESH | SurfaceFlags::DUST
);
assert_eq!(
lump[1].surface,
SurfaceFlags::METAL_STEPS
| SurfaceFlags::NO_MARKS
| SurfaceFlags::LADDER
| SurfaceFlags::SKY
);
assert_eq!(
lump[2].surface,
SurfaceFlags::POINT_LIGHT | SurfaceFlags::SKIP
);
assert_eq!(lump[0].contents, ContentsFlags::SOLID | ContentsFlags::LAVA);
assert_eq!(lump[1].contents, ContentsFlags::SOLID);
assert_eq!(lump[2].contents, ContentsFlags::SOLID | ContentsFlags::FOG);
}
|