wolfrpg_map_parser/command/common/
case.rs1use crate::byte_utils::{as_u32_be, as_u32_le};
2use crate::command::common::case_type::CaseType;
3use crate::command::Command;
4#[cfg(feature = "serde")]
5use serde::{Serialize, Deserialize};
6
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8#[derive(PartialEq, Clone)]
9pub struct Case {
10 case_type: CaseType,
11 case_id: u32,
12 commands: Vec<Command>,
13}
14
15impl Case {
16 pub(crate) fn parse(bytes: &[u8]) -> (usize, u32, Self) {
17 let mut offset: usize = 0;
18
19 let case_type: u32 = as_u32_be(&bytes[offset..offset+4]);
20 let case_type: CaseType = CaseType::new(case_type);
21 offset += 4;
22
23 offset += 1; let case_id: u32 = as_u32_le(&bytes[offset..offset+4]);
26 offset += 4;
27
28 offset += 3; let mut command_count: u32 = 1; let (bytes_read, commands_read, commands): (usize, u32, Vec<Command>)
32 = Command::parse_multiple(&bytes[offset..]);
33 offset += bytes_read;
34 command_count += commands_read;
35
36 (offset, command_count, Self {
37 case_type,
38 case_id,
39 commands,
40 })
41 }
42
43 pub(crate) fn parse_multiple(bytes: &[u8], case_count: usize) -> (usize, u32, Vec<Case>) {
44 let mut cases: Vec<Case> = Vec::with_capacity(case_count);
45 let mut offset: usize = 0;
46 let mut commands: u32 = 0;
47
48 for _ in 0..case_count {
49 let (bytes_read, commands_read, case): (usize, u32, Case)
50 = Self::parse(&bytes[offset..]);
51 cases.push(case);
52 offset += bytes_read;
53 commands += commands_read;
54 }
55
56 (offset, commands, cases)
57 }
58
59 pub fn case_type(&self) -> &CaseType {
60 &self.case_type
61 }
62
63 pub fn case_type_mut(&mut self) -> &mut CaseType {
64 &mut self.case_type
65 }
66
67 pub fn case_id(&self) -> u32 {
68 self.case_id
69 }
70
71 pub fn case_id_mut(&mut self) -> &mut u32 {
72 &mut self.case_id
73 }
74
75 pub fn commands(&self) -> &Vec<Command> {
76 &self.commands
77 }
78
79 pub fn commands_mut(&mut self) -> &mut Vec<Command> {
80 &mut self.commands
81 }
82}