wolfrpg_map_parser/command/picture_command/
show.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use crate::byte_utils::{as_u32_le, parse_string};
use crate::command::common::u32_or_string::U32OrString;
use crate::command::picture_command::display_type::DisplayType;
use crate::command::picture_command::options::Options;
use crate::byte_utils::parse_optional_string;
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
use state::State;

pub mod state;
pub mod base;
pub mod free_transform;
pub mod delay;
mod parser;
pub mod zoom;
pub mod color_values;
mod range_fields;
mod color_values_fields;
mod zoom_fields;
mod free_transform_fields;
mod delay_fields;
pub mod range;
mod colors_fields;
pub mod colors;
mod parsable_fields;

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq)]
pub struct Show {
    options: Options,
    picture: u32,
    process_time: u32,
    division_width: u32,
    division_height: u32,
    pattern: u32,
    opacity: u32,
    zoom: u32,
    angle: u32,
    state: State,
    filename: Option<U32OrString>,
    string: Option<String>,
}

type StateParser = fn(&[u8], &Options) -> (usize, Option<u32>, State);

impl Show {
    fn parse(bytes: &[u8], parse_state: StateParser) -> (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 picture: u32 = as_u32_le(&bytes[offset..offset+4]);
        offset += 4;

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

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

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

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

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

        let zoom: u32 = as_u32_le(&bytes[offset+8..offset+12]);
        let angle: u32 = as_u32_le(&bytes[offset+12..offset+16]);

        let (bytes_read, filename_variable, state): (usize, Option<u32>, State)
            = parse_state(&bytes[offset..], &options);
        offset += bytes_read;

        let (bytes_read, string_value): (usize, Option<String>)
            = Self::parse_string_value(&bytes[offset..]);
        offset += bytes_read;

        let (filename, string): (Option<U32OrString>, Option<String>)
            = Self::make_filename_and_string(string_value, filename_variable, &options);

        offset += 1; // Command end signature

        (offset, Self {
            options,
            picture,
            process_time,
            division_width,
            division_height,
            pattern,
            opacity,
            zoom,
            angle,
            state,
            filename,
            string
        })
    }
    fn parse_string_value(bytes: &[u8]) -> (usize, Option<String>) {
        let mut offset: usize = 0;

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

        let string_value: Option<String>
            = parse_optional_string!(bytes, offset, is_filename_string);

        (offset, string_value)
    }

    fn make_filename_and_string(string_value: Option<String>, filename_variable: Option<u32>,
                                    options: &Options) -> (Option<U32OrString>, Option<String>) {
        let (filename, string): (Option<String>, Option<String>) = match *options.display_type() {
            DisplayType::ShowStringAsPicture => (None, string_value),
            _ => (string_value, None)
        };

        let filename: Option<U32OrString> = match filename {
            Some(filename) => Some(U32OrString::String(filename)),
            None => filename_variable.map(U32OrString::U32)
        };

        (filename, string)
    }

    pub(crate) fn parse_base(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_base)
    }

    pub(crate) fn parse_colors(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_colors)
    }

    pub(crate) fn parse_delay(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_delay)
    }

    pub(crate) fn parse_range(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_range)
    }

    pub(crate) fn parse_color_values(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_color_values)
    }

    pub(crate) fn parse_zoom(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_zoom)
    }

    pub(crate) fn parse_free_transform(bytes: &[u8]) -> (usize, Self) {
        Self::parse(bytes, State::parse_free_transform)
    }

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

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

    pub fn picture(&self) -> u32 {
        self.picture
    }

    pub fn picture_mut(&mut self) -> &mut u32 {
        &mut self.picture
    }

    pub fn process_time(&self) -> u32 {
        self.process_time
    }

    pub fn process_time_mut(&mut self) -> &mut u32 {
        &mut self.process_time
    }

    pub fn division_width(&self) -> u32 {
        self.division_width
    }

    pub fn division_width_mut(&mut self) -> &mut u32 {
        &mut self.division_width
    }

    pub fn division_height(&self) -> u32 {
        self.division_height
    }

    pub fn division_height_mut(&mut self) -> &mut u32 {
        &mut self.division_height
    }

    pub fn pattern(&self) -> u32 {
        self.pattern
    }

    pub fn pattern_mut(&mut self) -> &mut u32 {
        &mut self.pattern
    }

    pub fn opacity(&self) -> u32 {
        self.opacity
    }

    pub fn opacity_mut(&mut self) -> &mut u32 {
        &mut self.opacity
    }

    pub fn zoom(&self) -> u32 {
        self.zoom
    }

    pub fn zoom_mut(&mut self) -> &mut u32 {
        &mut self.zoom
    }

    pub fn angle(&self) -> u32 {
        self.angle
    }

    pub fn angle_mut(&mut self) -> &mut u32 {
        &mut self.angle
    }

    pub fn state(&self) -> &State {
        &self.state
    }

    pub fn state_mut(&mut self) -> &mut State {
        &mut self.state
    }

    pub fn filename(&self) -> &Option<U32OrString> {
        &self.filename
    }

    pub fn filename_mut(&mut self) -> &mut Option<U32OrString> {
        &mut self.filename
    }

    pub fn string(&self) -> &Option<String> {
        &self.string
    }

    pub fn string_mut(&mut self) -> &mut Option<String> {
        &mut self.string
    }
}