wolfrpg_map_parser/command/
party_graphics_command.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
pub mod options;
pub mod operation;
pub mod special_operation;

use crate::byte_utils::{as_u32_le, parse_string};
use crate::command::common::u32_or_string::U32OrString;
use crate::command::party_graphics_command::operation::Operation;
use crate::command::party_graphics_command::options::Options;
use crate::byte_utils::parse_optional_string;
#[cfg(feature = "serde")]
use serde::Serialize;

#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct PartyGraphicsCommand {
    options: Options,
    member: Option<u32>,
    graphics: Option<U32OrString>,
}

impl PartyGraphicsCommand {
    pub fn parse(bytes: &[u8]) -> (usize, Self) {
        let mut offset: usize = 0;

        let options: u32 = as_u32_le(&bytes[offset..offset + 4]);
        let options: Options = Options::new(options);
        offset += 4;

        let member: Option<u32> = match *options.operation() {
            Operation::Remove | Operation::Insert | Operation::Replace => {
                let member: u32 = as_u32_le(&bytes[offset..offset + 4]);
                offset += 4;

                Some(member)
            }
            _ => None
        };

        let graphics_variable: Option<u32> = if options.graphics_is_variable() {
            let graphics_variable: u32 = as_u32_le(&bytes[offset..offset + 4]);
            offset += 4;

            Some(graphics_variable)
        } else {
            None
        };

        offset += 1; // Padding

        let is_graphics_string: bool = bytes[offset] != 0;
        offset += 1;

        let graphics_string: Option<String> 
            = parse_optional_string!(bytes, offset, is_graphics_string);

        let graphics: Option<U32OrString> = match (graphics_variable, graphics_string) {
            (Some(variable), None) => Some(U32OrString::U32(variable)),
            (None, Some(string)) => Some(U32OrString::String(string)),
            (None, None) => None,
            _ => unreachable!()
        };

        offset += 1; // Command end signature

        (offset, Self {
            options,
            member,
            graphics
        })
    }

    pub fn options(&self) -> &Options {
        &self.options
    }
    
    pub fn options_mut(&mut self) -> &mut Options {
        &mut self.options
    }

    pub fn member(&self) -> Option<u32> {
        self.member
    }
    
    pub fn member_mut(&mut self) -> &mut Option<u32> {
        &mut self.member
    }

    pub fn graphics(&self) -> &Option<U32OrString> {
        &self.graphics
    }
    
    pub fn graphics_mut(&mut self) -> &mut Option<U32OrString> {
        &mut self.graphics
    }
}