aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/chassis_profile.rs
blob: 9eef4478685d6ff4f403c1ede560870e6309fbba (plain) (blame)
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
use fparkan_binary::{checked_count_bytes, read_lp_bytes, Cursor, DecodeError};

/// Locomotion booleans bound by the chassis profile, when present.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ChassisFlags {
    /// The chassis can fly.
    pub can_fly: Option<bool>,
    /// The chassis uses legs to move.
    pub walk_chassis: Option<bool>,
    /// The movement class declared by the chassis profile.
    pub chassis_type: Option<u32>,
}

/// Reads the bound `CanFly` boolean from a native chassis VAR profile.
///
/// # Errors
/// Returns an error if the profile framing is invalid.
pub fn decode_chassis_can_fly(bytes: &[u8]) -> Result<Option<bool>, DecodeError> {
    Ok(decode_chassis_flags(bytes)?.can_fly)
}

/// Reads applied locomotion flags, keeping absent flags as unknown.
///
/// # Errors
/// Returns an error if the profile framing is invalid.
pub fn decode_chassis_flags(bytes: &[u8]) -> Result<ChassisFlags, DecodeError> {
    let mut cursor = Cursor::new(bytes);
    let count = cursor.read_u32_le()?;
    checked_count_bytes(u64::from(count), 28, cursor.remaining() as u64)?;
    let mut flags = ChassisFlags::default();
    for _ in 0..count {
        let size = cursor.read_u32_le()?;
        let kind = cursor.read_u32_le()?;
        cursor.read_u32_le()?;
        let name = read_lp_bytes(&mut cursor, 64 * 1024)?;
        read_lp_bytes(&mut cursor, 64 * 1024)?;
        cursor.read_exact(size as usize)?;
        if size == 4 {
            let value = cursor.read_u32_le()?;
            let name = name.split(|byte| *byte == 0).next().unwrap_or_default();
            match (kind, name) {
                (2, b"CanFly") => {
                    flags.can_fly.get_or_insert(value == 1);
                }
                (2, b"WalkChassis") => {
                    flags.walk_chassis.get_or_insert(value == 1);
                }
                (5, b"ChassisType") => {
                    flags.chassis_type.get_or_insert(value);
                }
                _ => {}
            }
        } else {
            cursor.read_exact(size as usize)?;
        }
        cursor.read_exact(8)?;
    }
    cursor.require_eof()?;
    Ok(flags)
}

#[cfg(test)]
mod tests {
    use super::{decode_chassis_can_fly, decode_chassis_flags, ChassisFlags};

    #[test]
    fn uses_the_bound_boolean_instead_of_its_default() {
        let mut profile = 3_u32.to_le_bytes().to_vec();
        for (name, kind, default, value) in [
            ("CanFly", 2, 0, 1),
            ("WalkChassis\0", 2, 1, 0),
            ("ChassisType", 5, 2, 4),
        ] {
            for word in [4_u32, kind, 0, name.len() as u32] {
                profile.extend(word.to_le_bytes());
            }
            profile.extend(name.as_bytes());
            for word in [0_u32, default, value, 0, 1] {
                profile.extend(word.to_le_bytes());
            }
        }
        assert_eq!(decode_chassis_can_fly(&profile), Ok(Some(true)));
        assert_eq!(
            decode_chassis_flags(&profile),
            Ok(ChassisFlags {
                can_fly: Some(true),
                walk_chassis: Some(false),
                chassis_type: Some(4),
            })
        );
        profile.truncate(profile.len() - 1);
        assert!(decode_chassis_can_fly(&profile).is_err());
    }
}