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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use bevy::reflect::Reflect;
use serde::{
de::{Error, IgnoredAny, Visitor},
Deserialize, Deserializer, Serialize,
};
use crate::{transfer_enum, transfer_enum_arr, transfer_field, unwrap_field};
use super::{
definitions::{LayerType, TilesetRect},
LdtkColor,
};
/*
* Level
*/
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Level {
/// Background color of the level (same as `bgColor`, except
/// the default value is automatically used here if its value is `null`)
#[serde(rename = "__bgColor")]
pub bg_color: LdtkColor,
/// Position informations of the background image, if there is one.
#[serde(rename = "__bgPos")]
pub bg_pos: Option<ImagePosition>,
/// An array listing all other levels touching this one on the world map.
/// Since 1.4.0, this includes levels that overlap in the same world layer,
/// or in nearby world layers.
///
/// Only relevant for world layouts where level spatial positioning is manual
/// (ie. GridVania, Free). For Horizontal and Vertical layouts,
/// this array is always empty.
#[serde(rename = "__neighbours")]
pub neighbours: Vec<Neighbour>,
/// The optional relative path to the level background image.
pub bg_rel_path: Option<String>,
/// This value is not null if the project option
/// "Save levels separately" is enabled. In this case,
/// this relative path points to the level Json file.
pub external_rel_path: Option<String>,
/// An array containing this level custom field values.
pub field_instances: Vec<FieldInstance>,
/// User defined unique identifier
pub identifier: String,
/// Unique instance identifier
pub iid: String,
/// An array containing all Layer instances.
/// ## IMPORTANT:
/// if the project option "Save levels separately" is enabled,
/// this field will be null.
///
/// This array is **sorted in display order**: the 1st layer is
/// the top-most and the last is behind.
pub layer_instances: Vec<LayerInstance>,
/// Height of the level in pixels
pub px_hei: i32,
/// Width of the level in pixels
pub px_wid: i32,
/// Unique Int identifier
pub uid: i32,
/// Index that represents the "depth" of the level in the world.
/// Default is 0, greater means "above", lower means "below".
///
/// This value is mostly used for display only and is intended to
/// make stacking of levels easier to manage.
pub world_depth: i32,
/// World X coordinate in pixels.
///
/// Only relevant for world layouts where level spatial positioning is manual
/// (ie. GridVania, Free).
/// For Horizontal and Vertical layouts, the value is always -1 here.
pub world_x: i32,
/// World Y coordinate in pixels.
///
/// Only relevant for world layouts where level spatial positioning is manual
/// (ie. GridVania, Free).
/// For Horizontal and Vertical layouts, the value is always -1 here.
pub world_y: i32,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ImagePosition {
/// An array of 4 float values describing the cropped sub-rectangle
/// of the displayed background image. This cropping happens when
/// original is larger than the level bounds
///
/// Array format: `[ cropX, cropY, cropWidth, cropHeight ]`
pub crop_rect: [f32; 4],
/// An array containing the `[scaleX,scaleY]` values of the cropped
/// background image, depending on `bgPos` option.
pub scale: [f32; 2],
/// An array containing the `[x,y]` pixel coordinates of the top-left
/// corner of the cropped background image, depending on `bgPos` option.
pub top_left_px: [i32; 2],
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Neighbour {
/// A single lowercase character tipping on the level location
/// (`n`orth, `s`outh, `w`est, `e`ast).
///
/// Since 1.4.0, this character value can also be
/// `<` (neighbour depth is lower),
/// `>` (neighbour depth is greater)
/// or `o` (levels overlap and share the same world depth).
pub dir: String,
/// Neighbour Instance Identifier
pub level_iid: String,
}
/*
* Layer Instance
*/
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct LayerInstance {
/// Grid-based height
#[serde(rename = "__cHei")]
pub c_hei: i32,
/// Grid-based width
#[serde(rename = "__cWid")]
pub c_wid: i32,
/// Grid size
#[serde(rename = "__gridSize")]
pub grid_size: i32,
/// Layer definition identifier
#[serde(rename = "__identifier")]
pub identifier: String,
/// Layer opacity as Float [0-1]
#[serde(rename = "__opacity")]
pub opacity: f32,
/// Total layer X pixel offset, including both instance and definition offsets.
#[serde(rename = "__pxTotalOffsetX")]
pub px_total_offset_x: i32,
/// Total layer Y pixel offset, including both instance and definition offsets.
#[serde(rename = "__pxTotalOffsetY")]
pub px_total_offset_y: i32,
/// The definition UID of corresponding Tileset, if any.
#[serde(rename = "__tilesetDefUid")]
pub tileset_def_uid: Option<i32>,
/// The relative path to corresponding Tileset, if any.
#[serde(rename = "__tilesetRelPath")]
pub tileset_rel_path: Option<String>,
/// Layer type (possible values: IntGrid, Entities, Tiles or AutoLayer)
#[serde(rename = "__type")]
pub ty: LayerType,
/// An array containing all tiles generated by Auto-layer rules.
/// The array is already sorted in display order
/// (ie. 1st tile is beneath 2nd, which is beneath 3rd etc.).
///
/// Note: if multiple tiles are stacked in the same cell as the result of different rules,
/// all tiles behind opaque ones will be discarded.
pub auto_layer_tiles: Vec<TileInstance>,
pub entity_instances: Vec<EntityInstance>,
pub grid_tiles: Vec<TileInstance>,
/// Unique layer instance identifier
pub iid: String,
/// A list of all values in the IntGrid layer, stored in CSV format (Comma Separated Values).
///
/// Order is from left to right, and top to bottom (ie. first row from left to right, followed by second row, etc).
///
/// `0` means "empty cell" and IntGrid values start at 1.
///
/// The array size is `__cWid` x `__cHei` cells.
pub int_grid_csv: Vec<i32>,
/// Reference the Layer definition UID
pub layer_def_uid: i32,
/// Reference to the UID of the level containing this layer instance
pub level_id: i32,
/// This layer can use another tileset by overriding the tileset UID here.
pub override_tileset_uid: Option<i32>,
/// X offset in pixels to render this layer, usually 0
/// ## IMPORTANT:
/// this should be added to the LayerDef optional offset,
/// so you should probably prefer using `__pxTotalOffsetX`
/// which contains the total offset value)
pub px_offset_x: i32,
pub px_offset_y: i32,
pub visible: bool,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TileInstance {
/// Alpha/opacity of the tile (0-1, defaults to 1)
#[serde(rename = "a")]
pub alpha: f32,
/// "Flip bits", a 2-bits integer to represent the mirror transformations of the tile.
/// - Bit 0 = X flip
/// - Bit 1 = Y flip
///
/// Examples: f=0 (no flip), f=1 (X flip only), f=2 (Y flip only), f=3 (both flips)
///
/// (This is the same as the `TileFlip`)
#[serde(rename = "f")]
pub flip: i32,
/// Pixel coordinates of the tile in the layer (`[x,y]` format).
/// Don't forget optional layer offsets, if they exist!
pub px: [i32; 2],
/// Pixel coordinates of the tile in the tileset ([x,y] format)
pub src: [i32; 2],
/// The Tile ID in the corresponding tileset.
#[serde(rename = "t")]
pub tile_id: i32,
}
/*
* Entity Instance
*/
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct EntityInstance {
/// Grid-based coordinates (`[x,y]` format)
#[serde(rename = "__grid")]
pub grid: [i32; 2],
/// Entity definition identifier
#[serde(rename = "__identifier")]
pub identifier: String,
/// Pivot coordinates (`[x,y]` format, values are from 0 to 1) of the Entity
#[serde(rename = "__pivot")]
pub pivot: [f32; 2],
/// The entity "smart" color, guessed from either Entity definition,
/// or one its field instances.
#[serde(rename = "__smartColor")]
pub smart_color: LdtkColor,
/// Array of tags defined in this Entity definition.
#[serde(rename = "__tags")]
pub tags: Vec<String>,
/// Optional TilesetRect used to display this entity
/// (it could either be the default Entity tile,
/// or some tile provided by a field value, like an Enum).
#[serde(rename = "__tile")]
pub tile: Option<TilesetRect>,
/// X world coordinate in pixels
#[serde(rename = "__worldX")]
pub world_x: i32,
/// Y world coordinate in pixels
#[serde(rename = "__worldY")]
pub world_y: i32,
/// Reference of the Entity definition UID
pub def_uid: i32,
/// An array of all custom fields and their values.
pub field_instances: Vec<FieldInstance>,
/// Unique instance identifier
pub iid: String,
/// Pixel coordinates (`[x,y]` format) in current level coordinate space.
/// Don't forget optional layer offsets, if they exist!
#[serde(rename = "px")]
pub local_pos: [i32; 2],
/// Entity width in pixels.
/// For non-resizable entities, it will be the same as Entity definition.
pub width: i32,
/// Entity height in pixels.
/// For non-resizable entities, it will be the same as Entity definition.
pub height: i32,
}
/*
* Field Instance
*/
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct FieldInstance {
/// Reference of the Field definition UID
pub def_uid: i32,
/// Type of the field, such as Int, Float, String, Enum(my_enum_name), Bool, etc.
///
/// NOTE: if you enable the advanced option Use Multilines type,
/// you will have "Multilines" instead of "String" when relevant.
///
/// This is not required because we can use enum.
/// So the type of the `value` = `type`
/// #[serde(rename = "__type")]
/// pub ty: FieldType,
/// 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<TilesetRect>,
/// Actual value of the field instance. The value type varies, depending on `__type`
/// If the field is an array, then this `__value` will also be a JSON array.
#[serde(rename = "__value")]
pub value: Option<FieldValue>,
}
const FIELDS: &[&str] = &["defUid", "__identifier", "__tile", "__type", "__value"];
impl<'de> Deserialize<'de> for FieldInstance {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
pub struct FieldInstanceVisitor;
impl<'de> Visitor<'de> for FieldInstanceVisitor {
type Value = FieldInstance;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a field instance")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut def_uid = None;
let mut identifier = None;
let mut tile = None;
let mut ty = None;
let mut value = None;
while let Some(key) = map.next_key()? {
match key {
FieldInstanceFields::DefUid => transfer_field!(def_uid, "defUid", map),
FieldInstanceFields::Identifier => {
transfer_field!(identifier, "__identifier", map)
}
FieldInstanceFields::Tile => transfer_field!(tile, "__tile", map),
FieldInstanceFields::Type => transfer_field!(ty, "__type", map),
FieldInstanceFields::Value => transfer_field!(value, "__value", map),
FieldInstanceFields::Skip => {
map.next_value::<IgnoredAny>()?;
}
}
}
let def_uid = unwrap_field!(def_uid, "defUid");
let identifier = unwrap_field!(identifier, "__identifier");
let tile = unwrap_field!(tile, "__tile");
let ty = unwrap_field!(ty, "__type");
let value = unwrap_field!(value, "__value");
let value = match ty {
SpecialFieldType::LocalEnum(name) => {
transfer_enum!(LocalEnum, "local enum", name, value)
}
SpecialFieldType::ExternEnum(name) => {
transfer_enum!(ExternEnum, "extern enum", name, value)
}
SpecialFieldType::Color => {
if let Some(v) = value {
if let FieldValue::String(s) = v {
Some(FieldValue::Color(LdtkColor::from(s)))
} else {
return Err(A::Error::custom(format!(
"expected color, got {:?}",
v
)));
}
} else {
None
}
}
SpecialFieldType::LocalEnumArray(name) => {
transfer_enum_arr!(LocalEnumArray, "string array", name, value)
}
SpecialFieldType::ExternEnumArray(name) => {
transfer_enum_arr!(ExternEnumArray, "string array", name, value)
}
SpecialFieldType::None => value,
};
Ok(FieldInstance {
def_uid,
identifier,
tile,
value,
})
}
}
deserializer.deserialize_struct("FieldInstance", FIELDS, FieldInstanceVisitor)
}
}
pub enum FieldInstanceFields {
DefUid,
Identifier,
Tile,
Type,
Value,
Skip,
}
impl<'de> Deserialize<'de> for FieldInstanceFields {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
pub struct FieldInstanceFieldsVisitor;
impl<'de> Visitor<'de> for FieldInstanceFieldsVisitor {
type Value = FieldInstanceFields;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a field instance field")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
"defUid" => Ok(FieldInstanceFields::DefUid),
"__identifier" => Ok(FieldInstanceFields::Identifier),
"__tile" => Ok(FieldInstanceFields::Tile),
"__type" => Ok(FieldInstanceFields::Type),
"__value" => Ok(FieldInstanceFields::Value),
_ => Ok(FieldInstanceFields::Skip),
}
}
}
deserializer.deserialize_identifier(FieldInstanceFieldsVisitor)
}
}
#[derive(Serialize, Debug)]
pub enum SpecialFieldType {
None,
LocalEnum(String),
ExternEnum(String),
LocalEnumArray(String),
ExternEnumArray(String),
Color,
}
impl<'de> Deserialize<'de> for SpecialFieldType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
pub struct FieldTypeVisitor;
impl<'de> Visitor<'de> for FieldTypeVisitor {
type Value = SpecialFieldType;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a field type")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v.starts_with("LocalEnum") {
return Ok(SpecialFieldType::LocalEnum(
v.split(".").last().unwrap().to_string(),
));
}
if v.starts_with("ExternEnum") {
return Ok(SpecialFieldType::ExternEnum(
v.split(".").last().unwrap().to_string(),
));
}
if v.starts_with("Array") {
let ty = v.split("<").nth(1).unwrap().split(">").nth(0).unwrap();
if ty.starts_with("LocalEnum") {
return Ok(SpecialFieldType::LocalEnumArray(
ty.split(".").last().unwrap().to_string(),
));
}
if ty.starts_with("ExternEnum") {
return Ok(SpecialFieldType::ExternEnumArray(
ty.split(".").last().unwrap().to_string(),
));
}
}
match v {
"Color" => Ok(SpecialFieldType::Color),
_ => Ok(SpecialFieldType::None),
}
}
}
deserializer.deserialize_str(FieldTypeVisitor)
}
}
/// - For classic types (ie. Integer, Float, Boolean, String, Text and FilePath), you just get the actual value with the expected type.
/// - For Color, the value is an hexadecimal string using "#rrggbb" format.
/// - For Enum, the value is a String representing the selected enum value.
/// - For Point, the value is a GridPoint object.
/// - For Tile, the value is a TilesetRect object.
/// - For EntityRef, the value is an EntityReferenceInfos object.
#[derive(Serialize, Deserialize, Debug, Clone, Reflect)]
#[serde(untagged)]
pub enum FieldValue {
IntegerArray(Vec<i32>),
FloatArray(Vec<f32>),
BoolArray(Vec<bool>),
StringArray(Vec<String>),
LocalEnumArray((String, Vec<String>)),
ExternEnumArray((String, Vec<String>)),
ColorArray(Vec<LdtkColor>),
PointArray(Vec<GridPoint>),
EntityRefArray(Vec<EntityRef>),
Integer(i32),
Float(f32),
Bool(bool),
String(String),
LocalEnum((String, String)),
ExternEnum((String, String)),
Color(LdtkColor),
Point(GridPoint),
EntityRef(EntityRef),
}
#[derive(Serialize, Deserialize, Debug, Clone, Reflect)]
#[serde(rename_all = "camelCase")]
pub struct EntityRef {
/// IID of the refered EntityInstance
pub entity_iid: String,
/// IID of the LayerInstance containing the refered EntityInstance
pub layer_iid: String,
/// IID of the Level containing the refered EntityInstance
pub level_iid: String,
/// IID of the World containing the refered EntityInstance
pub world_iid: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, Reflect)]
#[serde(rename_all = "camelCase")]
pub struct GridPoint {
/// X grid-based coordinate
pub cx: i32,
/// Y grid-based coordinate
pub cy: i32,
}