wolfrpg_map_parser/command/
show_choice_command.rs

1use crate::byte_utils::{as_u16_le, parse_string_vec};
2use crate::command::common::case::Case;
3use crate::command::common::CASES_END_SIGNATURE;
4use crate::command::show_choice_command::options::Options;
5#[cfg(feature = "serde")]
6use serde::{Serialize, Deserialize};
7
8pub mod cancel_case;
9pub mod extra_cases;
10pub mod options;
11
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13#[derive(PartialEq, Clone)]
14pub struct ShowChoiceCommand {
15    options: Options,
16    choices: Vec<String>,
17    cases: Vec<Case>,
18}
19
20impl ShowChoiceCommand {
21    pub(crate) fn parse(bytes: &[u8]) -> (usize, u32, Self){
22        let mut offset: usize = 0;
23
24        let options: u16 = as_u16_le(&bytes[offset..offset + 2]);
25        let options: Options = Options::new(options);
26        offset += 2;
27
28        offset += 3; // Unknown, most probably padding
29
30        // Should be equal to options.selected_choices
31        let choice_count: usize = bytes[offset] as usize;
32        offset += 1;
33
34        let (bytes_read, choices): (usize, Vec<String>)
35            = parse_string_vec(&bytes[offset..], choice_count);
36        offset += bytes_read;
37        offset += 1; // Should be 0x00 to indicate end of choices
38
39        let case_count: usize = options.case_count();
40        let (bytes_read, mut commands_read, cases): (usize, u32, Vec<Case>)
41            = Case::parse_multiple(&bytes[offset..], case_count);
42        offset += bytes_read;
43
44        let cases_end: &[u8] = &bytes[offset..offset+8];
45        offset += 8;
46        commands_read += 1; // Signature counts as command
47
48        if &cases_end[..4] != CASES_END_SIGNATURE {
49            panic!("Invalid cases end.");
50        }
51
52        (offset, commands_read, Self {
53            options,
54            choices,
55            cases,
56        })
57    }
58
59    pub fn options(&self) -> &Options {
60        &self.options
61    }
62
63    pub fn options_mut(&mut self) -> &mut Options {
64        &mut self.options
65    }
66
67    pub fn choices(&self) -> &Vec<String> {
68        &self.choices
69    }
70
71    pub fn choices_mut(&mut self) -> &mut Vec<String> {
72        &mut self.choices
73    }
74
75    pub fn cases(&self) -> &Vec<Case> {
76        &self.cases
77    }
78
79    pub fn cases_mut(&mut self) -> &mut Vec<Case> {
80        &mut self.cases
81    }
82}