wolfrpg_map_parser/command/db_management_command/
base.rs

1use crate::byte_utils::as_u32_le;
2use crate::command::db_management_command::state::State;
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6type DBStrings = (Option<String>, Option<String>, Option<String>);
7
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9#[derive(PartialEq, Clone)]
10pub struct Base {
11    value: u32,
12}
13
14impl Base {
15    pub(crate) fn parse(bytes: &[u8]) -> (usize, Self, DBStrings) {
16        let mut offset: usize = 0;
17
18        let value: u32 = as_u32_le(&bytes[offset..offset+4]);
19        offset += 4;
20
21        offset += 1; // padding
22
23        // I could not manually create a command with string count != 4, but I found a map 
24        // that has a command with string count 0, so we'll handle that case.
25        let string_count: u8 = bytes[offset]; 
26        offset += 1;
27
28        if string_count > 0 {
29            offset += 5; // In this variant, value should always be a number, so we skip this string
30        }
31
32        let (bytes_read, db_strings): (usize, DBStrings)
33            = State::parse_strings(string_count, &bytes[offset..]);
34        offset += bytes_read;
35        
36        let state: Self = Self {
37            value
38        };
39
40        (offset, state, db_strings)
41    }
42
43    pub fn value(&self) -> u32 {
44        self.value
45    }
46
47    pub fn value_mut(&mut self) -> &mut u32 {
48        &mut self.value
49    }
50}