-
Notifications
You must be signed in to change notification settings - Fork 69
/
field_instance.rs
246 lines (218 loc) · 9.59 KB
/
field_instance.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
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
#[allow(unused_imports)]
use super::{
EntityInstance, FieldInstanceEntityReference, FieldInstanceGridPoint, Level, TilesetRectangle,
};
use bevy::prelude::*;
use regex::Regex;
use crate::ldtk::color;
#[derive(PartialEq, Debug, Clone, Serialize)]
pub struct FieldInstance {
/// Field definition identifier
#[serde(rename = "__identifier")]
pub identifier: String,
/// Optional TilesetRect used to display this field (this can be the field own Tile, or some
/// other Tile guessed from the value, like an Enum).
#[serde(rename = "__tile")]
pub tile: Option<TilesetRectangle>,
/// Type of the field, such as `Int`, `Float`, `String`, `Enum(my_enum_name)`, `Bool`,
/// etc.<br/> NOTE: if you enable the advanced option **Use Multilines type**, you will have
/// "*Multilines*" instead of "*String*" when relevant.
#[serde(rename = "__type")]
pub field_instance_type: String,
/// Actual value of the field instance. The value type varies, depending on `__type`:<br/>
/// - For **classic types** (ie. Integer, Float, Boolean, String, Text and FilePath), you
/// just get the actual value with the expected type.<br/> - For **Color**, the value is an
/// hexadecimal string using "#rrggbb" format.<br/> - For **Enum**, the value is a String
/// representing the selected enum value.<br/> - For **Point**, the value is a
/// [GridPoint](#ldtk-GridPoint) object.<br/> - For **Tile**, the value is a
/// [TilesetRect](#ldtk-TilesetRect) object.<br/> - For **EntityRef**, the value is an
/// [EntityReferenceInfos](#ldtk-EntityReferenceInfos) object.<br/><br/> If the field is an
/// array, then this `__value` will also be a JSON array.
#[serde(rename = "__value")]
pub value: FieldValue,
/// Reference of the **Field definition** UID
#[serde(rename = "defUid")]
pub def_uid: i32,
/// Editor internal raw values
#[serde(rename = "realEditorValues")]
pub real_editor_values: Vec<Option<serde_json::Value>>,
}
#[derive(Serialize, Deserialize)]
struct FieldInstanceHelper {
#[serde(rename = "__identifier")]
pub identifier: String,
#[serde(rename = "__tile")]
pub tile: Option<TilesetRectangle>,
#[serde(rename = "__type")]
pub field_instance_type: String,
#[serde(rename = "__value")]
pub value: serde_json::Value,
#[serde(rename = "defUid")]
pub def_uid: i32,
#[serde(rename = "realEditorValues")]
pub real_editor_values: Vec<Option<serde_json::Value>>,
}
#[derive(Deserialize)]
struct ColorHelper(#[serde(with = "color")] Color);
impl<'de> Deserialize<'de> for FieldInstance {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let helper = FieldInstanceHelper::deserialize(deserializer)?;
let value = match helper.field_instance_type.as_str() {
"Int" => FieldValue::Int(
Option::<i32>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Float" => FieldValue::Float(
Option::<f32>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Bool" => FieldValue::Bool(bool::deserialize(helper.value).map_err(de::Error::custom)?),
"String" => FieldValue::String(
Option::<String>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Color" => {
let value = color::deserialize(helper.value).map_err(de::Error::custom)?;
FieldValue::Color(value)
}
"FilePath" => FieldValue::FilePath(
Option::<String>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Tile" => FieldValue::Tile(
Option::<TilesetRectangle>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"EntityRef" => FieldValue::EntityRef(
Option::<FieldInstanceEntityReference>::deserialize(helper.value)
.map_err(de::Error::custom)?,
),
"Point" => {
let point_helper = Option::<FieldInstanceGridPoint>::deserialize(helper.value)
.map_err(de::Error::custom)?;
FieldValue::Point(point_helper.map(|p| IVec2::new(p.cx, p.cy)))
}
"Multilines" => FieldValue::String(
Option::<String>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Array<Multilines>" => FieldValue::Strings(
Vec::<Option<String>>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Array<Int>" => FieldValue::Ints(
Vec::<Option<i32>>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Array<Float>" => FieldValue::Floats(
Vec::<Option<f32>>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Array<Bool>" => FieldValue::Bools(
Vec::<bool>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Array<String>" => FieldValue::Strings(
Vec::<Option<String>>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Array<Color>" => {
let helpers =
Vec::<ColorHelper>::deserialize(helper.value).map_err(de::Error::custom)?;
FieldValue::Colors(helpers.iter().map(|h| h.0).collect())
}
"Array<FilePath>" => FieldValue::Strings(
Vec::<Option<String>>::deserialize(helper.value).map_err(de::Error::custom)?,
),
"Array<Tile>" => FieldValue::Tiles(
Vec::<Option<TilesetRectangle>>::deserialize(helper.value)
.map_err(de::Error::custom)?,
),
"Array<EntityRef>" => FieldValue::EntityRefs(
Vec::<Option<FieldInstanceEntityReference>>::deserialize(helper.value)
.map_err(de::Error::custom)?,
),
"Array<Point>" => {
let point_helpers =
Vec::<Option<FieldInstanceGridPoint>>::deserialize(helper.value)
.map_err(de::Error::custom)?;
let points = point_helpers
.into_iter()
.map(|ph| ph.map(|p| IVec2::new(p.cx, p.cy)))
.collect();
FieldValue::Points(points)
}
t => {
let enum_regex =
Regex::new(r"^(LocalEnum|ExternEnum)\.").expect("enum regex should be valid");
let enums_regex = Regex::new(r"^Array<(LocalEnum|ExternEnum)\.")
.expect("enums regex should be valid");
if enum_regex.is_match(t) {
FieldValue::Enum(
Option::<String>::deserialize(helper.value).map_err(de::Error::custom)?,
)
} else if enums_regex.is_match(t) {
FieldValue::Enums(
Vec::<Option<String>>::deserialize(helper.value)
.map_err(de::Error::custom)?,
)
} else {
return Err(de::Error::custom(format!(
"Encountered unknown field type: {t}"
)));
}
}
};
Ok(FieldInstance {
identifier: helper.identifier,
tile: helper.tile,
field_instance_type: helper.field_instance_type,
def_uid: helper.def_uid,
real_editor_values: helper.real_editor_values,
value,
})
}
}
#[derive(PartialEq, Debug, Clone, Serialize)]
#[serde(untagged)]
/// The actual value of a field instance on a [Level] or [EntityInstance].
///
/// This has been re-typed for this plugin.
/// In LDtk's [QuickType loader](https://ldtk.io/files/quicktype/LdtkJson.rs),
/// this is just a [serde_json::Value].
pub enum FieldValue {
Int(Option<i32>),
Float(Option<f32>),
Bool(bool),
/// Represents either a String or a Multilines
String(Option<String>),
#[serde(with = "color")]
Color(Color),
FilePath(Option<String>),
Enum(Option<String>),
Tile(Option<TilesetRectangle>),
EntityRef(Option<FieldInstanceEntityReference>),
#[serde(serialize_with = "serialize_point")]
Point(Option<IVec2>),
Ints(Vec<Option<i32>>),
Floats(Vec<Option<f32>>),
Bools(Vec<bool>),
/// Represents either Strings or Multilines
Strings(Vec<Option<String>>),
#[serde(serialize_with = "serialize_colors")]
Colors(Vec<Color>),
FilePaths(Vec<Option<String>>),
Enums(Vec<Option<String>>),
Tiles(Vec<Option<TilesetRectangle>>),
EntityRefs(Vec<Option<FieldInstanceEntityReference>>),
#[serde(serialize_with = "serialize_points")]
Points(Vec<Option<IVec2>>),
}
fn serialize_colors<S: Serializer>(colors: &[Color], serializer: S) -> Result<S::Ok, S::Error> {
let field_values: Vec<FieldValue> = colors.iter().map(|c| FieldValue::Color(*c)).collect();
field_values.serialize(serializer)
}
fn serialize_point<S: Serializer>(point: &Option<IVec2>, serializer: S) -> Result<S::Ok, S::Error> {
let point_helper = point.map(|p| FieldInstanceGridPoint { cx: p.x, cy: p.y });
point_helper.serialize(serializer)
}
fn serialize_points<S: Serializer>(
points: &[Option<IVec2>],
serializer: S,
) -> Result<S::Ok, S::Error> {
let field_values: Vec<FieldValue> = points.iter().map(|p| FieldValue::Point(*p)).collect();
field_values.serialize(serializer)
}