aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/helpers.rs
blob: 91a672a569e415107f06c80e4153d88f31fddff3 (plain)
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
// Copyright (C) 2019 Oscar Shrimpton
//
// This file is part of stockton-bsp.
//
// stockton-bsp 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.
//
// stockton-bsp 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 stockton-bsp.  If not, see <http://www.gnu.org/licenses/>.

//! Helper functions for parsing

use na::{Vector2, Vector3};
use std::convert::TryInto;

/// Turn a slice into a le i32, the int datatype in a bsp file.
/// # Panics
/// If slice is not 4 bytes long
pub fn slice_to_i32(slice: &[u8]) -> i32 {
    i32::from_le_bytes(slice.try_into().unwrap())
}

/// Turn a slice into a le u32, used for some bitflags.
/// # Panics
/// If slice is not 4 bytes long.
pub fn slice_to_u32(slice: &[u8]) -> u32 {
    u32::from_le_bytes(slice.try_into().unwrap())
}

/// Turn a slice into a le f32, the float datatype in a bsp file.
/// # Panics
/// If slice is not 4 bytes long
pub fn slice_to_f32(slice: &[u8]) -> f32 {
    f32::from_bits(u32::from_le_bytes(slice.try_into().unwrap()))
}

/// Turn a slice of floats into a 3D vector
/// # Panics
/// If slice isn't 12 bytes long.
pub fn slice_to_vec3(slice: &[u8]) -> Vector3<f32> {
    Vector3::new(
        slice_to_f32(&slice[0..4]),
        slice_to_f32(&slice[4..8]),
        slice_to_f32(&slice[8..12]),
    )
}

/// Turn a slice of i32s into a 3D vector
/// # Panics
/// If slice isn't 12 bytes long.
pub fn slice_to_vec3i(slice: &[u8]) -> Vector3<i32> {
    Vector3::new(
        slice_to_i32(&slice[0..4]),
        slice_to_i32(&slice[4..8]),
        slice_to_i32(&slice[8..12]),
    )
}

/// Turn a slice of u32s into a 2D vector
/// # Panics
/// If slice isn't 8 bytes long.
pub fn slice_to_vec2ui(slice: &[u8]) -> Vector2<u32> {
    Vector2::new(slice_to_u32(&slice[0..4]), slice_to_u32(&slice[4..8]))
}