wolfrpg_map_parser/command/event_control_command/
loop_count.rs

1#[cfg(feature = "serde")]
2use serde::{Serialize, Deserialize};
3use crate::byte_utils::as_u32_le;
4use crate::command::Command;
5use crate::command::common::LOOP_END_SIGNATURE;
6
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8#[derive(PartialEq, Clone)]
9pub struct LoopCount {
10    loop_count: u32,
11    commands: Vec<Command>
12}
13
14impl LoopCount {
15    pub(crate) fn parse(bytes: &[u8]) -> (usize, u32, Self) {
16        let mut offset: usize = 0;
17
18        let loop_count: u32 = as_u32_le(&bytes[offset..offset+4]);
19        offset += 4;
20
21        offset += 3; // Command end signature
22
23        let (bytes_read, mut commands_read, commands): (usize, u32, Vec<Command>)
24            = Command::parse_multiple(&bytes[offset..]);
25        offset += bytes_read;
26
27        let loop_end_signature: &[u8] = &bytes[offset..offset+8];
28        offset += 8;
29        commands_read += 1;
30
31        if loop_end_signature[..4] != LOOP_END_SIGNATURE[..4] {
32            panic!("Invalid loop end.");
33        }
34
35        (offset, commands_read, Self {
36            loop_count,
37            commands
38        })
39    }
40
41    pub fn loop_count(&self) -> u32 {
42        self.loop_count
43    }
44
45    pub fn loop_count_mut(&mut self) -> &mut u32 {
46        &mut self.loop_count
47    }
48
49    pub fn commands(&self) -> &Vec<Command> {
50        &self.commands
51    }
52
53    pub fn commands_mut(&mut self) -> &mut Vec<Command> {
54        &mut self.commands
55    }
56}