quake-util 0.4.0

A utility library for using Quake file formats
Documentation
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#[cfg(feature = "std")]
extern crate std;

extern crate alloc;

use crate::qmap;
use qmap::repr::*;

use qmap::{CheckWritable, ValidationResult};

#[cfg(feature = "std")]
use crate::WriteError;

use {alloc::ffi::CString, alloc::str, core::ffi::CStr};

#[cfg(feature = "std")]
use {alloc::string::String, alloc::vec::Vec};

#[cfg(not(feature = "std"))]
use alloc::{format, vec};

const GOOD_AXES: [Vec3; 2] = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];

const BAD_AXES: [Vec3; 2] = [[f64::INFINITY, 0.0, 0.0], [0.0, 0.0, 0.0]];

const GOOD_VEC2: Vec2 = [1.0, 1.0];

const BAD_VEC2: Vec2 = [-f64::INFINITY, 0.0];

const GOOD_HALF_SPACE: [Point; 3] =
    [[-1.0, -1.0, 0.0], [1.0, -1.0, 0.0], [-1.0, 1.0, 0.0]];

const BAD_HALF_SPACE: [Point; 3] =
    [[f64::NAN, -1.0, 0.0], [1.0, -1.0, 0.0], [-1.0, 1.0, 0.0]];

const GOOD_ALIGNMENT: Alignment = Alignment {
    offset: GOOD_VEC2,
    rotation: 0.0,
    scale: GOOD_VEC2,
    axes: Some(GOOD_AXES),
};

const BAD_ALIGNMENT_ROTATION: Alignment = Alignment {
    offset: GOOD_VEC2,
    rotation: f64::NAN,
    scale: GOOD_VEC2,
    axes: Some(GOOD_AXES),
};

const Q2_EXTENSION: Quake2SurfaceExtension = Quake2SurfaceExtension {
    content_flags: 1237,
    surface_flags: -101,
    surface_value: 300.0,
};

fn expect_err_containing(res: ValidationResult, text: &str) {
    if let Err(e) = res {
        assert!(e.contains(text), "Expected {:?} to contain '{}'", e, text);
    } else {
        panic_expected_error();
    }
}

fn panic_expected_error() {
    panic!("Expected error");
}

fn simple_edict() -> Edict {
    let mut edict = Edict::new();
    edict.push((
        CString::new("classname").unwrap(),
        CString::new("worldspawn").unwrap(),
    ));
    edict
}

fn bad_edict_key() -> Edict {
    let mut edict = Edict::new();
    edict.push((CString::new("\n").unwrap(), CString::new("oops").unwrap()));
    edict
}

fn simple_surface() -> Surface {
    Surface {
        half_space: GOOD_HALF_SPACE,
        texture: CString::new("{FENCE").unwrap(),
        alignment: GOOD_ALIGNMENT,
        q2ext: Default::default(),
    }
}

fn q2_surface() -> Surface {
    Surface {
        half_space: GOOD_HALF_SPACE,
        texture: CString::new("T").unwrap(),
        alignment: GOOD_ALIGNMENT,
        q2ext: Q2_EXTENSION,
    }
}

fn simple_brush() -> Brush {
    vec![
        simple_surface(),
        simple_surface(),
        simple_surface(),
        simple_surface(),
    ]
}

fn q2_brush() -> Brush {
    vec![q2_surface(), q2_surface(), q2_surface(), q2_surface()]
}

fn simple_brush_entity() -> Entity {
    Entity {
        edict: simple_edict(),
        brushes: vec![simple_brush()],
    }
}

fn q2_brush_entity() -> Entity {
    Entity {
        edict: simple_edict(),
        brushes: vec![q2_brush()],
    }
}

fn simple_point_entity() -> Entity {
    Entity {
        edict: simple_edict(),
        brushes: vec![],
    }
}

fn bad_entity_edict() -> Entity {
    Entity {
        edict: bad_edict_key(),
        brushes: vec![simple_brush()],
    }
}

fn entity_with_texture(texture: &CStr) -> Entity {
    Entity {
        edict: Edict::new(),
        brushes: vec![vec![Surface {
            half_space: GOOD_HALF_SPACE,
            texture: CString::from(texture),
            alignment: GOOD_ALIGNMENT,
            q2ext: Default::default(),
        }]],
    }
}

fn simple_map() -> QuakeMap {
    let mut qmap = QuakeMap::new();
    qmap.entities.push(simple_brush_entity());
    qmap.entities.push(simple_point_entity());
    qmap
}

fn bad_map_edict() -> QuakeMap {
    let mut qmap = QuakeMap::new();
    let ent = bad_entity_edict();
    qmap.entities.push(ent);
    qmap
}

// Successes

#[test]
fn check_simple_map() {
    assert_eq!(simple_map().check_writable(), Ok(()));
}

// Failures

#[test]
fn check_bad_map() {
    assert!(bad_map_edict().check_writable().is_err());
}

#[test]
fn check_bad_entities() {
    let bad_edict_strings = ["\"", "\n", "\r"];
    let bad_edict_chars = bad_edict_strings
        .into_iter()
        .map(|s| s.chars().next().unwrap());
    let good_edict_strings = ["hello", "evening", "bye"].into_iter();

    let bad_char_iter = bad_edict_chars.clone().chain(bad_edict_chars.clone());

    let key_iter = bad_edict_strings
        .into_iter()
        .chain(good_edict_strings.clone());

    let value_iter = good_edict_strings.chain(bad_edict_strings);

    let trials = bad_char_iter.zip(key_iter.zip(value_iter));

    for (bad_char, (key, value)) in trials {
        let key = CString::new(key).unwrap();
        let value = CString::new(value).unwrap();
        let mut edict = Edict::new();
        edict.push((key, value));
        let ent = Entity {
            edict,
            brushes: vec![],
        };

        expect_err_containing(ent.check_writable(), &format!("{:?}", bad_char));
    }
}

#[test]
fn check_bad_surface_texture() {
    assert!(entity_with_texture(&CString::new("\"").unwrap())
        .check_writable()
        .is_err(),);
}

#[test]
fn check_bad_surface_half_space() {
    let surf = Surface {
        half_space: BAD_HALF_SPACE,
        texture: CString::new("butts").unwrap(),
        alignment: GOOD_ALIGNMENT,
        q2ext: Default::default(),
    };

    expect_err_containing(surf.check_writable(), "finite");
}

#[test]
fn check_bad_surface_alignment() {
    let surf = Surface {
        half_space: GOOD_HALF_SPACE,
        texture: CString::new("potato").unwrap(),
        alignment: BAD_ALIGNMENT_ROTATION,
        q2ext: Default::default(),
    };

    assert!(surf.check_writable().is_err());
}

#[test]
fn check_bad_valve_alignment() {
    expect_err_containing(BAD_ALIGNMENT_ROTATION.check_writable(), "finite");
}

#[test]
fn check_bad_valve_alignment_axes() {
    let aln = Alignment {
        offset: GOOD_VEC2,
        rotation: 0.0,
        scale: GOOD_VEC2,
        axes: Some(BAD_AXES),
    };

    expect_err_containing(aln.check_writable(), "finite");
}

#[test]
fn check_bad_alignment_rotation() {
    let aln = Alignment {
        offset: GOOD_VEC2,
        rotation: f64::INFINITY,
        scale: GOOD_VEC2,
        axes: None,
    };

    expect_err_containing(aln.check_writable(), "finite");
}

#[test]
fn check_bad_alignment_offset() {
    let aln = Alignment {
        offset: BAD_VEC2,
        rotation: 12345.7,
        scale: GOOD_VEC2,
        axes: None,
    };

    expect_err_containing(aln.check_writable(), "finite");
}

#[test]
fn check_bad_alignment_scale() {
    let aln = Alignment {
        offset: GOOD_VEC2,
        rotation: -125.7,
        scale: BAD_VEC2,
        axes: None,
    };

    expect_err_containing(aln.check_writable(), "finite");
}

#[cfg(feature = "std")]
mod write {
    use super::*;
    use std::io::sink;

    // Successes

    #[test]
    fn write_empty_map() {
        let map = QuakeMap::new();
        let mut dest = Vec::<u8>::new();
        assert!(map.write_to(&mut dest).is_ok());
        assert_eq!(&dest[..], b"");
    }

    #[test]
    fn write_simple_map() {
        let mut dest = Vec::<u8>::new();
        assert!(simple_map().write_to(&mut dest).is_ok());
        assert!(str::from_utf8(&dest).unwrap().contains("worldspawn"));
        assert!(str::from_utf8(&dest).unwrap().contains(" {FENCE "));
    }

    #[test]
    fn write_q2_map() {
        let mut dest = Vec::<u8>::new();
        assert!(q2_brush_entity().write_to(&mut dest).is_ok());
        let text = str::from_utf8(&dest).unwrap();
        eprintln!("{}", &text);
        assert!(text.contains("0 1 1 1237 -101 300"));
        assert!(!text.contains("300."));
    }

    #[test]
    fn write_simple_entity() {
        let mut dest = Vec::<u8>::new();
        assert!(simple_brush_entity().write_to(&mut dest).is_ok());
        assert!(str::from_utf8(&dest).unwrap().contains("worldspawn"));
        assert!(str::from_utf8(&dest).unwrap().contains(" {FENCE "));
    }

    #[test]
    fn write_entity_with_spaced_texture() {
        let ent = entity_with_texture(&CString::new("some texture").unwrap());
        let mut dest = Vec::<u8>::new();
        assert!(ent.write_to(&mut dest).is_ok());
        assert!(str::from_utf8(&dest).unwrap().contains("\"some texture\""));
    }

    #[test]
    fn write_texture_with_quote() {
        let ent = entity_with_texture(&CString::new("some\"texture").unwrap());
        let mut dest = Vec::<u8>::new();
        assert!(ent.write_to(&mut dest).is_ok());
        assert!(str::from_utf8(&dest).unwrap().contains(" some\"texture "));
    }

    #[test]
    fn write_bad_texture_empty() {
        let ent = entity_with_texture(&CString::new("").unwrap());
        let mut dest = Vec::<u8>::new();
        assert!(ent.write_to(&mut dest).is_ok());
        assert!(str::from_utf8(&dest).unwrap().contains("\"\""));
    }

    // Failure

    #[test]
    fn write_bad_map() {
        let res = bad_map_edict().write_to(&mut sink());
        if res.is_ok() {
            panic_expected_error();
        }
    }

    #[test]
    fn write_bad_entity() {
        let res = bad_entity_edict().write_to(&mut sink());
        if let Err(e) = res {
            assert!(format!("{:?}", e).contains("\\n"));
        } else {
            panic_expected_error();
        }
    }

    #[test]
    fn write_bad_texture_spaced_with_quote() {
        let ent = entity_with_texture(&CString::new("some\" texture").unwrap());
        let err = ent.write_to(&mut sink()).unwrap_err();
        assert!(format!("{:?}", err).contains("whitespace"));
    }

    #[test]
    fn write_bad_texture_leads_quote() {
        let ent = entity_with_texture(&CString::new("\"some").unwrap());
        let err = ent.write_to(&mut sink()).unwrap_err();
        assert!(format!("{:?}", err)
            .contains("not quotable and contains whitespace"));
    }

    #[test]
    fn write_validation_error_outputs_nothing() {
        let mut dest: Vec<u8> = vec![];
        let mut qmap = QuakeMap::new();
        qmap.entities.push(Entity {
            edict: simple_edict(),
            brushes: vec![vec![Surface {
                half_space: GOOD_HALF_SPACE,
                texture: CString::new("b\"").unwrap(),
                alignment: Alignment {
                    offset: GOOD_VEC2,
                    rotation: 0.0,
                    scale: BAD_VEC2,
                    axes: Some(GOOD_AXES),
                },
                q2ext: Default::default(),
            }]],
        });
        let res = qmap.write_to(&mut dest);

        if let Err(WriteError::Validation(_)) = res {
            assert_eq!(String::from_utf8(dest).unwrap(), String::from(""));
        } else {
            panic_expected_error();
        }
    }
}