wolfrpg_map_parser/command/
party_graphics_command.rs

1pub mod options;
2pub mod operation;
3pub mod special_operation;
4
5use crate::byte_utils::{as_u32_le, parse_string};
6use crate::common::u32_or_string::U32OrString;
7use crate::command::party_graphics_command::operation::Operation;
8use crate::command::party_graphics_command::options::Options;
9use crate::byte_utils::parse_optional_string;
10#[cfg(feature = "serde")]
11use serde::{Serialize, Deserialize};
12
13#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
14#[derive(PartialEq, Clone)]
15pub struct PartyGraphicsCommand {
16    options: Options,
17    member: Option<u32>,
18    graphics: Option<U32OrString>,
19}
20
21impl PartyGraphicsCommand {
22    pub(crate) fn parse(bytes: &[u8]) -> (usize, Self) {
23        let mut offset: usize = 0;
24
25        let options: u32 = as_u32_le(&bytes[offset..offset + 4]);
26        let options: Options = Options::new(options);
27        offset += 4;
28
29        let member: Option<u32> = match *options.operation() {
30            Operation::Remove | Operation::Insert | Operation::Replace => {
31                let member: u32 = as_u32_le(&bytes[offset..offset + 4]);
32                offset += 4;
33
34                Some(member)
35            }
36            _ => None
37        };
38
39        let graphics_variable: Option<u32> = if options.graphics_is_variable() {
40            let graphics_variable: u32 = as_u32_le(&bytes[offset..offset + 4]);
41            offset += 4;
42
43            Some(graphics_variable)
44        } else {
45            None
46        };
47
48        offset += 1; // Padding
49
50        let is_graphics_string: bool = bytes[offset] != 0;
51        offset += 1;
52
53        let graphics_string: Option<String> 
54            = parse_optional_string!(bytes, offset, is_graphics_string);
55
56        let graphics: Option<U32OrString> = match (graphics_variable, graphics_string) {
57            (Some(variable), None) => Some(U32OrString::U32(variable)),
58            (None, Some(string)) => Some(U32OrString::String(string)),
59            (None, None) => None,
60            _ => unreachable!()
61        };
62
63        offset += 1; // Command end signature
64
65        (offset, Self {
66            options,
67            member,
68            graphics
69        })
70    }
71
72    pub fn options(&self) -> &Options {
73        &self.options
74    }
75    
76    pub fn options_mut(&mut self) -> &mut Options {
77        &mut self.options
78    }
79
80    pub fn member(&self) -> Option<u32> {
81        self.member
82    }
83    
84    pub fn member_mut(&mut self) -> &mut Option<u32> {
85        &mut self.member
86    }
87
88    pub fn graphics(&self) -> &Option<U32OrString> {
89        &self.graphics
90    }
91    
92    pub fn graphics_mut(&mut self) -> &mut Option<U32OrString> {
93        &mut self.graphics
94    }
95}