blob: d9f15229def3423d6c63263bea1d0370349478f6 (
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
|
//! Effect references carried by the version-1 explosion descriptor.
/// The contribution of an EXPL descriptor to Control's projectile capacity.
pub fn decode_explosion_capacity(bytes: &[u8]) -> Option<f32> {
if bytes.len() != 792 || !matches!(u32::from_le_bytes(bytes[..4].try_into().ok()?), 1..=3) {
return None;
}
Some(f32::from_le_bytes(bytes[4..8].try_into().ok()?))
}
/// Reads the initial effect archive and name from a version-1 EXPL payload.
/// Other versions and malformed references require separate interpretation.
pub fn decode_explosion_effect(bytes: &[u8]) -> Option<(&str, &str)> {
if bytes.len() != 792 || bytes[..4] != 1_u32.to_le_bytes() {
return None;
}
Some((
fixed_name(&bytes[0x18..0x38])?,
fixed_name(&bytes[0x38..0x58])?,
))
}
fn fixed_name(bytes: &[u8]) -> Option<&str> {
let name = bytes.split(|&byte| byte == 0).next()?;
(name.is_ascii() && !name.is_empty())
.then(|| std::str::from_utf8(name).ok())
.flatten()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_version_one_reference_and_rejects_unknown_layouts() {
let mut bytes = [0; 792];
bytes[..4].copy_from_slice(&1_u32.to_le_bytes());
bytes[0x18..0x1d].copy_from_slice(b"a.rlb");
bytes[0x38..0x3d].copy_from_slice(b"burst");
assert_eq!(decode_explosion_effect(&bytes), Some(("a.rlb", "burst")));
assert_eq!(decode_explosion_effect(&bytes[..791]), None);
bytes[0] = 2;
assert_eq!(decode_explosion_effect(&bytes), None);
}
#[test]
fn reads_capacity_in_each_observed_explosion_layout() {
let mut bytes = [0; 792];
bytes[4..8].copy_from_slice(&300.0_f32.to_le_bytes());
for version in 1..=3_u32 {
bytes[..4].copy_from_slice(&version.to_le_bytes());
assert_eq!(decode_explosion_capacity(&bytes), Some(300.0));
}
bytes[0] = 4;
assert_eq!(decode_explosion_capacity(&bytes), None);
}
}
|