wolfrpg_map_parser/command/
set_string_command.rs

1pub mod base;
2pub mod options;
3pub mod content_type;
4pub mod variable_type;
5pub mod string_operation;
6pub mod operation;
7pub mod dynamic;
8pub mod input;
9pub mod state;
10
11use crate::byte_utils::as_u32_le;
12use crate::command::set_string_command::content_type::ContentType;
13use crate::command::set_string_command::operation::Operation;
14use crate::command::set_string_command::options::Options;
15use crate::command::set_string_command::state::State;
16#[cfg(feature = "serde")]
17use serde::{Serialize, Deserialize};
18
19#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
20#[derive(PartialEq, Clone)]
21pub struct SetStringCommand {
22    variable: u32,
23    options: Options,
24    operation: Operation,
25    state: State
26}
27
28impl SetStringCommand {
29    fn parse(bytes: &[u8], parse_state: fn(&[u8]) -> (usize, State)) -> (usize, Self) {
30        let mut offset: usize = 0;
31
32        let variable: u32 = as_u32_le(&bytes[offset..offset + 4]);
33        offset += 4;
34
35        let options: u8 = bytes[offset];
36        let options: Options = Options::new(options);
37        offset += 1;
38
39        let operation: u8 = bytes[offset];
40        let operation: Operation = Operation::new(operation);
41        offset += 1;
42
43        offset += 2; // Unknown, most probably padding
44
45        let (bytes_read, state): (usize, State) = parse_state(&bytes[offset..]);
46        offset += bytes_read;
47
48        (offset, Self {
49            variable,
50            options,
51            operation,
52            state
53        })
54    }
55
56    pub(crate) fn parse_base(bytes: &[u8]) -> (usize, Self) {
57        Self::parse(bytes, State::parse_base)
58    }
59
60    pub(crate) fn parse_dynamic(bytes: &[u8]) -> (usize, Self) {
61        match Options::new(bytes[4]).content_type() {
62            ContentType::UserInput => Self::parse(bytes, State::parse_input),
63            _ => Self::parse(bytes, State::parse_dynamic),
64        }
65    }
66
67    pub fn variable(&self) -> u32 {
68        self.variable
69    }
70
71    pub fn variable_mut(&mut self) -> &mut u32 {
72        &mut self.variable
73    }
74
75    pub fn options(&self) -> &Options {
76        &self.options
77    }
78
79    pub fn options_mut(&mut self) -> &mut Options {
80        &mut self.options
81    }
82
83    pub fn operation(&self) -> &Operation {
84        &self.operation
85    }
86
87    pub fn operation_mut(&mut self) -> &mut Operation {
88        &mut self.operation
89    }
90
91    pub fn state(&self) -> &State {
92        &self.state
93    }
94
95    pub fn state_mut(&mut self) -> &mut State {
96        &mut self.state
97    }
98}