wolfrpg_map_parser/command/
set_variable_plus_command.rs1#[cfg(feature = "serde")]
2use serde::{Serialize, Deserialize};
3use state::State;
4use crate::byte_utils::as_u32_le;
5use crate::command::set_variable_plus_command::assignment::Assignment;
6use crate::command::set_variable_plus_command::options::Options;
7use crate::command::set_variable_plus_command::variable_type::VariableType;
8
9pub mod state;
10pub mod character;
11pub mod options;
12pub mod variable_type;
13pub mod assignment_operator;
14pub mod assignment;
15pub mod position;
16pub mod picture;
17pub mod picture_field;
18pub mod other;
19pub mod target;
20pub mod character_field;
21
22#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
23#[derive(PartialEq, Clone)]
24pub struct SetVariablePlusCommand {
25 variable: u32,
26 options: Options,
27 assignment: Assignment,
28 state: State
29}
30
31impl SetVariablePlusCommand {
32 fn parse(bytes: &[u8], parse_state: fn(&[u8]) -> (usize, State)) -> (usize, Self) {
33 let mut offset: usize = 0;
34
35 let variable: u32 = as_u32_le(&bytes[offset..offset + 4]);
36 offset += 4;
37
38 let options: u8 = bytes[offset];
39 let options:Options = Options::new(options);
40 offset += 1;
41
42 let assignment: u8 = bytes[offset];
43 let assignment: Assignment = Assignment::new(assignment);
44 offset += 1;
45
46 let (bytes_read, state): (usize, State) = parse_state(&bytes[offset..]);
47 offset += bytes_read;
48
49 offset += 3; (offset, Self {
52 variable,
53 options,
54 assignment,
55 state
56 })
57 }
58
59 pub(crate) fn parse_base(bytes: &[u8]) -> (usize, Self) {
60 match Assignment::new(bytes[5]).variable_type() {
61 VariableType::Character => Self::parse(bytes, State::parse_character),
62 VariableType::Position => Self::parse(bytes, State::parse_position),
63 VariableType::PictureNumber => Self::parse(bytes, State::parse_picture),
64 _ => panic!("Invalid variable type: {:x}", bytes[5] & 0x0f)
65 }
66 }
67
68 pub(crate) fn parse_other(bytes: &[u8]) -> (usize, Self) {
69 Self::parse(bytes, State::parse_other)
70 }
71
72 pub fn variable(&self) -> u32 {
73 self.variable
74 }
75
76 pub fn variable_mut(&mut self) -> &mut u32 {
77 &mut self.variable
78 }
79
80 pub fn options(&self) -> &Options {
81 &self.options
82 }
83
84 pub fn options_mut(&mut self) -> &mut Options {
85 &mut self.options
86 }
87
88 pub fn assignment(&self) -> &Assignment {
89 &self.assignment
90 }
91
92 pub fn assignment_mut(&mut self) -> &mut Assignment {
93 &mut self.assignment
94 }
95
96 pub fn state(&self) -> &State {
97 &self.state
98 }
99
100 pub fn state_mut(&mut self) -> &mut State {
101 &mut self.state
102 }
103}