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
254
255
use serde::{Deserialize, Serialize};
use tracing::warn;
use super::{dimensions::Dimensions, phf_table::CONDITION_TAGS, tokens::Condition};
/// A struct representing a `SpriteLayer` object.
#[allow(clippy::module_name_repetitions)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, specta::Type)]
#[serde(rename_all = "camelCase")]
pub struct SpriteLayer {
layer_name: String,
tile_page_id: String,
offset: Dimensions,
#[serde(skip_serializing_if = "Option::is_none")]
offset_2: Option<Dimensions>,
#[serde(skip_serializing_if = "Option::is_none")]
large_image: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
conditions: Option<Vec<(Condition, String)>>,
}
impl SpriteLayer {
/// Returns the `tile_page_id` of the `SpriteLayer`.
///
/// # Returns
///
/// * `&str` - The `tile_page_id` of the `SpriteLayer`.
pub fn get_tile_page_id(&self) -> &str {
self.tile_page_id.as_str()
}
/// Parse a condition token into a `LayerCondition`.
///
/// # Parameters
///
/// * `key` - The key of the condition token.
/// * `value` - The value of the condition token.
pub fn parse_condition_token(&mut self, key: &str, value: &str) {
// Condition is the key, and it should match a value in LAYER_CONDITION_TAGS
if let Some(condition) = CONDITION_TAGS.get(key) {
if self.conditions.is_none() {
self.conditions = Some(Vec::new());
}
if let Some(conditions) = &mut self.conditions {
// It's true that some conditions have a value, some have a tag, and some are standalone.
// At the moment we only care about saving the tag, so we'll just save the value as a string.
conditions.push((*condition, String::from(value)));
}
} else {
// Manually avoid ISSUE_MIN_LENGTH which is a typo in one of the mods.. This hack should be removed once the mod is fixed.
if key == "ISSUE_MIN_LENGTH" {
return;
}
warn!(
"Failed to parse {} as LayerCondition, unknown key {}",
value, key
);
}
}
/// Parse a layer value into a `SpriteLayer`.
///
/// # Parameters
///
/// * `value` - The value to parse.
///
/// # Returns
///
/// * `Option<SpriteLayer>` - The parsed `SpriteLayer`.
#[must_use]
pub fn parse_layer_from_value(value: &str) -> Option<Self> {
// ...BODY:CREATURES_DOMESTIC:0:21]
let mut split = value.split(':');
let layer_name = match split.next() {
Some(v) => String::from(v),
_ => {
return None;
}
};
let tile_page_id = match split.next() {
Some(v) => String::from(v),
_ => {
return None;
}
};
let fourth_position_token = match split.next() {
Some(v) => String::from(v),
_ => {
return None;
}
};
let large_image = matches!(fourth_position_token.as_str(), "LARGE_IMAGE");
if large_image {
return Self::parse_large_layer_with_split(
layer_name.as_str(),
tile_page_id.as_str(),
split.collect::<Vec<&str>>().as_slice(),
);
}
let tile_offset_y = match split.next() {
Some(v) => String::from(v),
_ => {
return None;
}
};
let offset_x: i32 = match fourth_position_token.parse() {
Ok(n) => n,
Err(_e) => {
warn!(
"parse_layer_from_value: Failed to parse {} as offset_x, {}",
fourth_position_token, value
);
return None;
}
};
let offset_y: i32 = match tile_offset_y.parse() {
Ok(n) => n,
Err(_e) => {
warn!(
"parse_layer_from_value: Failed to parse {} as offset_y, {}",
tile_offset_y, value
);
return None;
}
};
Some(Self {
layer_name,
tile_page_id,
offset: Dimensions::from_xy(offset_x, offset_y),
..Self::default()
})
}
/// Parse a large layer value into a `SpriteLayer`.
///
/// # Parameters
///
/// * `layer_name` - The name of the layer.
/// * `tile_page_id` - The `tile_page_id` of the layer.
/// * `split` - The split of the value.
///
/// # Returns
///
/// * `Option<SpriteLayer>` - The parsed `SpriteLayer`.
#[must_use]
fn parse_large_layer_with_split(
layer_name: &str,
tile_page_id: &str,
split: &[&str],
) -> Option<Self> {
let x1: i32 = match split.first() {
Some(v) => match v.parse() {
Ok(n) => n,
Err(_e) => {
warn!(
"parse_large_creature_with_split: Failed to parse {} as offset_x1 {:?}",
v, split
);
return None;
}
},
_ => {
return None;
}
};
let y1: i32 = match split.get(1) {
Some(v) => match v.parse() {
Ok(n) => n,
Err(_e) => {
warn!(
"parse_large_creature_with_split: Failed to parse {} as offset_y1 {:?}",
v, split
);
return None;
}
},
_ => {
return None;
}
};
let x2: i32 = match split.get(2) {
Some(v) => match v.parse() {
Ok(n) => n,
Err(_e) => {
warn!(
"parse_large_creature_with_split: Failed to parse {} as offset_x2 {:?}",
v, split
);
return None;
}
},
_ => {
return None;
}
};
let y2: i32 = match split.get(3) {
Some(v) => match v.parse() {
Ok(n) => n,
Err(_e) => {
warn!(
"parse_large_creature_with_split: Failed to parse {} as offset_y2 {:?}",
v, split
);
return None;
}
},
_ => {
return None;
}
};
Some(Self {
layer_name: String::from(layer_name),
tile_page_id: String::from(tile_page_id),
large_image: Some(true),
offset: Dimensions::from_xy(x1, y1),
offset_2: Some(Dimensions::from_xy(x2, y2)),
..Self::default()
})
}
/// Function to "clean" the creature. This is used to remove any empty list or strings,
/// and to remove any default values. By "removing" it means setting the value to None.
///
/// This also will remove the metadata if `is_metadata_hidden` is true.
///
/// Steps for all "Option" fields:
/// - Set any metadata to None if `is_metadata_hidden` is true.
/// - Set any empty string to None.
/// - Set any empty list to None.
/// - Set any default values to None.
///
/// # Returns
///
/// * `SpriteLayer` - The cleaned `SpriteLayer`.
#[must_use]
pub fn cleaned(&self) -> Self {
let mut cleaned = self.clone();
if let Some(conditions) = &cleaned.conditions {
if conditions.is_empty() {
cleaned.conditions = None;
}
}
cleaned
}
}