wolfrpg_map_parser/command/event_control_command/
loop_command.rs

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