wolfrpg_map_parser/command/
sound_command.rs1#[cfg(feature = "serde")]
2use serde::{Serialize, Deserialize};
3use crate::byte_utils::as_u16_le;
4use crate::command::sound_command::options::Options;
5use crate::command::sound_command::sound_type::SoundType;
6use crate::command::sound_command::state::State;
7
8pub mod options;
9pub mod process_type;
10pub mod operation;
11pub mod sound_type;
12pub mod state;
13pub mod filename;
14pub mod variable;
15
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[derive(PartialEq, Clone)]
18pub struct SoundCommand {
19 options: Options,
20 systemdb_entry: u16,
21 sound_type: SoundType,
22 state: State
23}
24
25impl SoundCommand {
26 fn parse(bytes: &[u8], parse_state: fn(&[u8], &Options, &SoundType) -> (usize, State))
27 -> (usize, Self) {
28 let mut offset: usize = 0;
29
30 let options: u8 = bytes[offset];
31 let options: Options = Options::new(options);
32 offset += 1;
33
34 let systemdb_entry: u16 = as_u16_le(&bytes[offset..offset + 2]);
35 offset += 2;
36
37 let sound_type: SoundType = SoundType::new(bytes[offset]);
38 offset += 1;
39
40 let (bytes_read, state): (usize, State)
41 = parse_state(&bytes[offset..], &options, &sound_type);
42 offset += bytes_read;
43
44 offset += 1; (offset, Self {
47 options,
48 systemdb_entry,
49 sound_type,
50 state
51 })
52 }
53
54 pub(crate) fn parse_filename(bytes: &[u8]) -> (usize, Self) {
55 Self::parse(bytes, State::parse_filename)
56 }
57
58 pub(crate) fn parse_variable(bytes: &[u8]) -> (usize, Self) {
59 Self::parse(bytes, State::parse_variable)
60 }
61
62 pub(crate) fn parse_free_all(bytes: &[u8]) -> (usize, Self) {
63 Self::parse(bytes, State::parse_free_all)
64 }
65
66 pub fn options(&self) -> &Options {
67 &self.options
68 }
69
70 pub fn options_mut(&mut self) -> &mut Options {
71 &mut self.options
72 }
73
74 pub fn systemdb_entry(&self) -> u16 {
75 self.systemdb_entry
76 }
77
78 pub fn systemdb_entry_mut(&mut self) -> &mut u16 {
79 &mut self.systemdb_entry
80 }
81
82 pub fn sound_type(&self) -> &SoundType {
83 &self.sound_type
84 }
85
86 pub fn sound_type_mut(&mut self) -> &mut SoundType {
87 &mut self.sound_type
88 }
89
90 pub fn state(&self) -> &State {
91 &self.state
92 }
93
94 pub fn state_mut(&mut self) -> &mut State {
95 &mut self.state
96 }
97}