mnk-vmf 0.1.0

A library for parsing Valve Map Format (VMF) files.
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use chumsky::IterParser;
use chumsky::Parser as ChumskyParser;

use crate::impl_block_properties_parser;
use crate::parser::{
    close_block, key_value, key_value_numeric, open_block, skip_unknown_block, InternalParser,
    TokenError, TokenSource,
};
use crate::types::point::key_value_plane;
use crate::types::textureaxis::key_value_texture_axis;
use crate::Parser;

use super::point::Point3D;
use super::textureaxis::TextureAxis;
use super::DispInfo;

/// Represents a side (face) of a solid brush
#[derive(Debug, Default, Clone)]
pub struct Side<'src> {
    pub id: u32,
    pub plane: (Point3D, Point3D, Point3D),
    pub material: &'src str,
    pub uaxis: TextureAxis,
    pub vaxis: TextureAxis,
    pub rotation: f32,
    pub lightmapscale: u32,
    pub smoothing_groups: u32,
    pub dispinfo: Option<DispInfo>, // Displacement information for terrain
}

/// Side properties used for parser impl
enum SideProperty<'src> {
    Id(u32),
    Plane((Point3D, Point3D, Point3D)),
    Material(&'src str),
    UAxis(TextureAxis),
    VAxis(TextureAxis),
    Rotation(f32),
    LightmapScale(u32),
    SmoothingGroups(u32),
    DispInfo(DispInfo),
}

/// Public parser trait implementation that allows [`Side`] to use ::parse(input) call.
impl<'src> Parser<'src> for Side<'src> {}

/// A [`Side`] implementation for [`Side`].
/// Every key-value pair needs to be in order, like in the example bellow.
///
/// usage: `let side = Side::parser().parse(input);`.
///
/// The format that is being parsed here is:
/// side
/// {
///     "id" "1"
///     "plane" "(-320 -320 0) (-320 320 0) (320 320 0)"
///     "material" "DEV/DEV_MEASUREGENERIC01B"
///     "uaxis" "[1 0 0 0] 0.25"
///     "vaxis" "[0 -1 0 0] 0.25"
///     "rotation" "0"
///     "lightmapscale" "16"
///     "smoothing_groups" "0"
/// }
impl<'src> InternalParser<'src> for Side<'src> {
    fn parser<I>() -> impl ChumskyParser<'src, I, Self, TokenError<'src>>
    where
        I: TokenSource<'src>,
    {
        impl_block_properties_parser! {
            property_list: SideProperty = {
                p_id                  = key_value_numeric("id")                 => SideProperty::Id,
                p_plane               = key_value_plane("plane")                => SideProperty::Plane,
                p_material            = key_value("material")                   => SideProperty::Material,
                p_uaxis               = key_value_texture_axis("uaxis")         => SideProperty::UAxis,
                p_vaxis               = key_value_texture_axis("vaxis")         => SideProperty::VAxis,
                p_rotation            = key_value_numeric("rotation")           => SideProperty::Rotation,
                p_lightmap_scale      = key_value_numeric("lightmapscale")      => SideProperty::LightmapScale,
                p_smoothing_groups    = key_value_numeric("smoothing_groups")   => SideProperty::SmoothingGroups,
            }
        }

        let dispinfo_parser = DispInfo::parser().map(SideProperty::DispInfo);
        let any_property_or_block = property_list
            .or(dispinfo_parser)
            .map(Some)
            .or(skip_unknown_block().map(|_| None));

        open_block("side")
            .ignore_then(
                any_property_or_block
                    .repeated()
                    .collect::<Vec<Option<SideProperty>>>(),
            )
            .then_ignore(close_block())
            .map(|properties: Vec<Option<SideProperty>>| {
                let mut side = Side::default();
                for prop_opt in properties {
                    if let Some(prop) = prop_opt {
                        match prop {
                            SideProperty::Id(val) => side.id = val,
                            SideProperty::Plane(val) => side.plane = val,
                            SideProperty::Material(val) => side.material = val,
                            SideProperty::UAxis(val) => side.uaxis = val,
                            SideProperty::VAxis(val) => side.vaxis = val,
                            SideProperty::Rotation(val) => side.rotation = val,
                            SideProperty::LightmapScale(val) => side.lightmapscale = val,
                            SideProperty::SmoothingGroups(val) => side.smoothing_groups = val,
                            SideProperty::DispInfo(val) => side.dispinfo = Some(val),
                        }
                    }
                }
                side
            })
    }
}

#[cfg(test)]
mod tests {
    use crate::util::lex;

    use super::*;
    use chumsky::Parser as ChumskyParser;

    #[test]
    fn test_parse_side_complete_valid_order() {
        let input = r#"
        side
        {
            "id" "1"
            "plane" "(-320 -320 0) (-320 320 0) (320 320 0)"
            "material" "DEV/DEV_MEASUREGENERIC01B"
            "uaxis" "[1 0 0 0] 0.25"
            "vaxis" "[0 -1 0 0] 0.25"
            "rotation" "0"
            "lightmapscale" "16"
            "smoothing_groups" "0"
        }
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();

        assert!(result.is_ok(), "Parsing failed: {:?}", result.err());
        let side = result.unwrap();

        let expected_plane = (
            Point3D {
                x: -320.0,
                y: -320.0,
                z: 0.0,
            },
            Point3D {
                x: -320.0,
                y: 320.0,
                z: 0.0,
            },
            Point3D {
                x: 320.0,
                y: 320.0,
                z: 0.0,
            },
        );
        let expected_uaxis = TextureAxis {
            x: 1.0,
            y: 0.0,
            z: 0.0,
            shift: 0.0,
            scale: 0.25,
        };
        let expected_vaxis = TextureAxis {
            x: 0.0,
            y: -1.0,
            z: 0.0,
            shift: 0.0,
            scale: 0.25,
        };

        assert_eq!(side.id, 1);
        assert_eq!(side.plane, expected_plane);
        assert_eq!(side.material, "DEV/DEV_MEASUREGENERIC01B");
        assert_eq!(side.uaxis, expected_uaxis);
        assert_eq!(side.vaxis, expected_vaxis);
        assert_eq!(side.rotation, 0.0);
        assert_eq!(side.lightmapscale, 16);
        assert_eq!(side.smoothing_groups, 0);
    }

    #[test]
    fn test_parse_side_properties_out_of_order() {
        let input = r#"
        side
        {
            "material" "BRICK/BRICKWALL001A"
            "id" "42"
            "uaxis" "[0 1 0 10] 0.125"
            "smoothing_groups" "1"
            "plane" "(0 0 0) (100 0 0) (100 100 0)"
            "lightmapscale" "32"
            "vaxis" "[1 0 0 20] 0.125"
            "rotation" "90"
        }
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();

        assert!(result.is_ok(), "Parsing failed: {:?}", result.err());
        let side = result.unwrap();

        let expected_plane = (
            Point3D {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            },
            Point3D {
                x: 100.0,
                y: 0.0,
                z: 0.0,
            },
            Point3D {
                x: 100.0,
                y: 100.0,
                z: 0.0,
            },
        );
        let expected_uaxis = TextureAxis {
            x: 0.0,
            y: 1.0,
            z: 0.0,
            shift: 10.0,
            scale: 0.125,
        };
        let expected_vaxis = TextureAxis {
            x: 1.0,
            y: 0.0,
            z: 0.0,
            shift: 20.0,
            scale: 0.125,
        };

        assert_eq!(side.id, 42);
        assert_eq!(side.plane, expected_plane);
        assert_eq!(side.material, "BRICK/BRICKWALL001A");
        assert_eq!(side.uaxis, expected_uaxis);
        assert_eq!(side.vaxis, expected_vaxis);
        assert_eq!(side.rotation, 90.0);
        assert_eq!(side.lightmapscale, 32);
        assert_eq!(side.smoothing_groups, 1);
    }

    #[test]
    fn test_parse_side_missing_optional_properties() {
        let input = r#"
        side
        {
            "id" "3"
            "plane" "(1 1 1) (2 2 2) (3 3 3)"
            "material" "CONCRETE/CONCRETEFLOOR001"
            "uaxis" "[1 0 0 0] 1"
            "vaxis" "[0 1 0 0] 1"
        }
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();

        assert!(result.is_ok(), "Parsing failed: {:?}", result.err());
        let side = result.unwrap();
        let default_side_for_missing_fields = Side::default(); // To get default values

        let expected_plane = (
            Point3D {
                x: 1.0,
                y: 1.0,
                z: 1.0,
            },
            Point3D {
                x: 2.0,
                y: 2.0,
                z: 2.0,
            },
            Point3D {
                x: 3.0,
                y: 3.0,
                z: 3.0,
            },
        );
        let expected_uaxis = TextureAxis {
            x: 1.0,
            y: 0.0,
            z: 0.0,
            shift: 0.0,
            scale: 1.0,
        };
        let expected_vaxis = TextureAxis {
            x: 0.0,
            y: 1.0,
            z: 0.0,
            shift: 0.0,
            scale: 1.0,
        };

        assert_eq!(side.id, 3);
        assert_eq!(side.plane, expected_plane);
        assert_eq!(side.material, "CONCRETE/CONCRETEFLOOR001");
        assert_eq!(side.uaxis, expected_uaxis);
        assert_eq!(side.vaxis, expected_vaxis);
        assert_eq!(side.rotation, default_side_for_missing_fields.rotation);
        assert_eq!(
            side.lightmapscale,
            default_side_for_missing_fields.lightmapscale
        );
        assert_eq!(
            side.smoothing_groups,
            default_side_for_missing_fields.smoothing_groups
        );
    }

    #[test]
    fn test_parse_side_empty_block() {
        let input = r#"
        side
        {
        }
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();

        assert!(result.is_ok(), "Parsing failed: {:?}", result.err());
        let side = result.unwrap();
        let expected_side = Side::default(); // Assumes all fields get their default values

        assert_eq!(side.id, expected_side.id);
        assert_eq!(side.plane, expected_side.plane);
        assert_eq!(side.material, expected_side.material);
        assert_eq!(side.uaxis, expected_side.uaxis);
        assert_eq!(side.vaxis, expected_side.vaxis);
        assert_eq!(side.rotation, expected_side.rotation);
        assert_eq!(side.lightmapscale, expected_side.lightmapscale);
        assert_eq!(side.smoothing_groups, expected_side.smoothing_groups);
    }

    #[test]
    fn test_parse_side_malformed_id() {
        let input = r#"
        side
        {
            "id" "not_a_number"
            "plane" "(-320 -320 0) (-320 320 0) (320 320 0)"
            "material" "DEV/DEV_MEASUREGENERIC01B"
            "uaxis" "[1 0 0 0] 0.25"
            "vaxis" "[0 -1 0 0] 0.25"
            "rotation" "0"
            "lightmapscale" "16"
            "smoothing_groups" "0"
        }
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();
        assert!(
            result.is_err(),
            "Parsing should have failed for malformed id"
        );
    }

    #[test]
    fn test_parse_side_malformed_plane() {
        let input = r#"
        side
        {
            "id" "1"
            "plane" "this_is_not_a_plane"
            "material" "DEV/DEV_MEASUREGENERIC01B"
            "uaxis" "[1 0 0 0] 0.25"
            "vaxis" "[0 -1 0 0] 0.25"
            "rotation" "0"
            "lightmapscale" "16"
            "smoothing_groups" "0"
        }
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();
        assert!(
            result.is_err(),
            "Parsing should have failed for malformed plane"
        );
    }

    #[test]
    fn test_parse_side_malformed_uaxis() {
        let input = r#"
        side
        {
            "id" "1"
            "plane" "(-320 -320 0) (-320 320 0) (320 320 0)"
            "material" "DEV/DEV_MEASUREGENERIC01B"
            "uaxis" "not_a_uaxis"
            "vaxis" "[0 -1 0 0] 0.25"
            "rotation" "0"
            "lightmapscale" "16"
            "smoothing_groups" "0"
        }
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();
        assert!(
            result.is_err(),
            "Parsing should have failed for malformed uaxis"
        );
    }

    #[test]
    fn test_parse_side_missing_closing_brace_for_block() {
        let input = r#"
        side
        {
            "id" "1"
            "plane" "(-320 -320 0) (-320 320 0) (320 320 0)"
            "material" "DEV/DEV_MEASUREGENERIC01B"
            "uaxis" "[1 0 0 0] 0.25"
            "vaxis" "[0 -1 0 0] 0.25"
            "rotation" "0"
            "lightmapscale" "16"
            "smoothing_groups" "0"
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();
        assert!(
            result.is_err(),
            "Parsing should have failed for missing closing brace"
        );
    }

    #[test]
    fn test_parse_side_unknown_property() {
        let input = r#"
        side
        {
            "id" "1"
            "unknown_property" "some_value"
            "material" "DEV/DEV_MEASUREGENERIC01B"
        }
        "#;
        let stream = lex(input);
        let result = Side::parser().parse(stream).into_result();

        assert!(
            result.is_err(),
            "Parsing should fail on unknown property if not explicitly skipped"
        );
    }
}