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 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
//! Functions and types for parsing Ogmo levels. use std::fs; use std::path::{Path, PathBuf}; use either::Either; use hashbrown::HashMap; use serde::ser::SerializeStruct; use serde::{Deserialize, Serialize, Serializer}; use crate::{Error, Vec2}; /// A dynamically typed value. /// /// As Ogmo's level format does not store the type alongside the value, /// it is not possible for this enum to specify the exact type of the /// original value template. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(untagged)] pub enum Value { /// A boolean value. Boolean(bool), /// A string value. String(String), /// A numeric value. /// /// This may have originally been an integer when set, but the Ogmo /// format does not provide enough information to figure that out /// without cross-referencing the project. Number(f32), } /// An Ogmo level. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Level { /// The version of Ogmo used to export this level. pub ogmo_version: String, /// The width of the level. pub width: f32, /// The height of the level. pub height: f32, /// The offset of the level on the X axis. Useful for loading multiple chunked levels. pub offset_x: f32, /// The offset of the level on the Y axis. Useful for loading multiple chunked levels. pub offset_y: f32, /// The level's custom values. #[serde(default)] pub values: HashMap<String, Value>, /// The layers that make up the level. pub layers: Vec<Layer>, } impl Level { /// Parses an Ogmo level from a JSON string. /// /// # Errors /// /// * `Error::Json` will be returned if deserialization fails. pub fn from_json(s: &str) -> Result<Level, Error> { serde_json::from_str(s).map_err(Error::Json) } /// Parses an Ogmo level from a file. /// /// # Errors /// /// * `Error::Io` will be returned if the file cannot be read. /// * `Error::Json` will be returned if deserialization fails. pub fn from_file(path: impl AsRef<Path>) -> Result<Level, Error> { let json = fs::read_to_string(path).map_err(Error::Io)?; Level::from_json(&json) } /// Writes the Ogmo level to a JSON string. /// /// # Errors /// /// * `Error::Json` will be returned if serialization fails. pub fn to_json(&self) -> Result<String, Error> { serde_json::to_string(self).map_err(Error::Json) } /// Writes the Ogmo level to a pretty-printed JSON string. /// /// # Errors /// /// * `Error::Json` will be returned if serialization fails. pub fn to_json_pretty(&self) -> Result<String, Error> { serde_json::to_string_pretty(self).map_err(Error::Json) } } /// An entity instance. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Entity { /// The entity's name. pub name: String, /// The entity's ID. pub id: i32, /// The unique export ID of the entity. #[serde(rename = "_eid")] pub export_id: String, /// The X position of the entity. pub x: f32, /// The Y position of the entity. pub y: f32, /// The width of the entity. /// Will only be present if the entity template was defined as resizable. #[serde(skip_serializing_if = "Option::is_none")] pub width: Option<f32>, /// The width of the entity. /// Will only be present if the entity template was defined as resizable. #[serde(skip_serializing_if = "Option::is_none")] pub height: Option<f32>, /// The X origin of the entity. /// Will only be present if the entity template defined an origin. #[serde(skip_serializing_if = "Option::is_none")] pub origin_x: Option<f32>, /// The Y origin of the entity. /// Will only be present if the entity template defined an origin. #[serde(skip_serializing_if = "Option::is_none")] pub origin_y: Option<f32>, /// The rotation of the entity. /// Will only be present if the entity template was defined as rotatable. #[serde(skip_serializing_if = "Option::is_none")] pub rotation: Option<f32>, /// Whether the entity is flipped on the X axis. /// Will only be present if the entity template was defined as X-flippable. #[serde(skip_serializing_if = "Option::is_none")] pub flipped_x: Option<bool>, /// Whether the entity is flipped on the Y axis. /// Will only be present if the entity template was defined as Y-flippable. #[serde(skip_serializing_if = "Option::is_none")] pub flipped_y: Option<bool>, /// The entity's nodes. /// Will only be present if the entity template was defined as having nodes. #[serde(skip_serializing_if = "Option::is_none")] pub nodes: Option<Vec<Vec2<f32>>>, /// The entity's custom values. /// Will only be present if the entity template was defined as having custom values. #[serde(skip_serializing_if = "Option::is_none")] pub values: Option<HashMap<String, Value>>, } /// A decal instance. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Decal { /// The X position of the decal. pub x: f32, /// The Y position of the decal. pub y: f32, /// The scale of the decal on the X axis. /// Will only be present if the decal template was defined as scalable. #[serde(skip_serializing_if = "Option::is_none")] pub scale_x: Option<f32>, /// The scale of the decal on the Y axis. /// Will only be present if the decal template was defined as scalable. #[serde(skip_serializing_if = "Option::is_none")] pub scale_y: Option<f32>, /// The rotation of the decal. /// Will only be present if the decal template was defined as rotatable. #[serde(skip_serializing_if = "Option::is_none")] pub rotation: Option<f32>, /// The name of the decal's texture. pub texture: String, /// Custom values associated with the decal. pub values: HashMap<String, Value>, } /// A layer instance. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(untagged)] pub enum Layer { /// A tile layer. Tile(TileLayer), /// A tile co-ords layer. TileCoords(TileCoordsLayer), /// A grid layer. Grid(GridLayer), /// An entity layer. Entity(EntityLayer), /// A decal layer. Decal(DecalLayer), } impl Layer { /// Gets the name of the layer. pub fn name(&self) -> &str { match self { Layer::Tile(data) => &data.name, Layer::TileCoords(data) => &data.name, Layer::Grid(data) => &data.name, Layer::Entity(data) => &data.name, Layer::Decal(data) => &data.name, } } } /// A tile layer. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct TileLayer { /// The name of the layer. pub name: String, /// The unique export ID of the entity. #[serde(rename = "_eid")] pub export_id: String, /// The layer's offset on the X axis. pub offset_x: f32, /// The layer's offset on the Y axis. pub offset_y: f32, /// The width of the layer's grid cells. pub grid_cell_width: i32, /// The height of the layer's grid cells. pub grid_cell_height: i32, /// The number of grid cells on the X axis. pub grid_cells_x: i32, /// The number of grid cells on the Y axis. pub grid_cells_y: i32, /// The name of the tileset used for this layer. pub tileset: String, /// The tile data. /// /// You may want to use the `unpack` method rather than accessing this directly. #[serde(flatten)] pub data: TileLayerStorage, } impl TileLayer { /// Unpack the tile data from the layer. pub fn unpack(&self) -> impl Iterator<Item = Tile> + '_ { match &self.data { TileLayerStorage::Data(data) => { Either::Left(data.iter().enumerate().map(move |(i, &v)| { let grid_x = i as i32 % self.grid_cells_x; let grid_y = i as i32 / self.grid_cells_x; let pixel_x = grid_x * self.grid_cell_width; let pixel_y = grid_y * self.grid_cell_height; let id = if v == -1 { None } else { Some(v) }; Tile { id, grid_position: Vec2 { x: grid_x, y: grid_y, }, pixel_position: Vec2 { x: pixel_x, y: pixel_y, }, } })) } TileLayerStorage::Data2D(data) => { Either::Right(data.iter().enumerate().flat_map(move |(y, row)| { row.iter().enumerate().map(move |(x, &v)| { let grid_x = x as i32; let grid_y = y as i32; let pixel_x = grid_x * self.grid_cell_width; let pixel_y = grid_y * self.grid_cell_height; let id = if v == -1 { None } else { Some(v) }; Tile { id, grid_position: Vec2 { x: grid_x, y: grid_y, }, pixel_position: Vec2 { x: pixel_x, y: pixel_y, }, } }) })) } } } } /// An individual tile, unpacked from a `TileLayer`. #[derive(Copy, Clone, Debug)] pub struct Tile { /// The ID of the tile in the tileset. /// /// If the tile is empty, this will be `None`. pub id: Option<i32>, /// The position of the tile in grid co-ordinates. pub grid_position: Vec2<i32>, /// The position of the tile in pixel co-ordinates. pub pixel_position: Vec2<i32>, } /// Tile data from a `TileLayer`. #[derive(Clone, Debug, Deserialize)] pub enum TileLayerStorage { /// A flat list of tile IDs. /// /// Each value corresponds to the ID of a tile in a tileset (with `0` being the /// top left, and moving left to right, top to bottom). /// /// Empty tiles are represented by a `-1`. #[serde(rename = "data")] Data(Vec<i32>), /// A 2D list of tile IDs. /// /// Each value corresponds to the ID of a tile in a tileset (with `0` being the /// top left, and moving left to right, top to bottom). /// /// Empty tiles are represented by a `-1`. #[serde(rename = "data2D")] Data2D(Vec<Vec<i32>>), } impl Serialize for TileLayerStorage { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { let mut state = serializer.serialize_struct("TileLayerStorage", 3)?; match self { TileLayerStorage::Data(data) => { state.serialize_field("data", &data)?; state.serialize_field("exportMode", &0)?; state.serialize_field("arrayMode", &0)?; } TileLayerStorage::Data2D(data) => { state.serialize_field("data2D", &data)?; state.serialize_field("exportMode", &0)?; state.serialize_field("arrayMode", &1)?; } } state.end() } } /// A tile co-ords layer. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct TileCoordsLayer { /// The name of the layer. pub name: String, /// The unique export ID of the entity. #[serde(rename = "_eid")] pub export_id: String, /// The layer's offset on the X axis. pub offset_x: f32, /// The layer's offset on the Y axis. pub offset_y: f32, /// The width of the layer's grid cells. pub grid_cell_width: i32, /// The height of the layer's grid cells. pub grid_cell_height: i32, /// The number of grid cells on the X axis. pub grid_cells_x: i32, /// The number of grid cells on the Y axis. pub grid_cells_y: i32, /// The name of the tileset used for this layer. pub tileset: String, /// The tile data. /// /// You may want to use the `unpack` method rather than accessing this directly. #[serde(flatten)] pub data: TileCoordsLayerStorage, } impl TileCoordsLayer { /// Unpack the tile data from the layer. pub fn unpack(&self) -> impl Iterator<Item = TileCoords> + '_ { match &self.data { TileCoordsLayerStorage::DataCoords(data) => { Either::Left(data.iter().enumerate().map(move |(i, coords)| { let grid_x = i as i32 % self.grid_cells_x; let grid_y = i as i32 / self.grid_cells_x; let pixel_x = grid_x * self.grid_cell_width; let pixel_y = grid_y * self.grid_cell_height; let (grid_coords, pixel_coords) = if coords[0] == -1 { (None, None) } else { let grid_u = coords[0]; let grid_v = coords[1]; let pixel_u = grid_u * self.grid_cell_width; let pixel_v = grid_v * self.grid_cell_height; ( Some(Vec2 { x: grid_u, y: grid_v, }), Some(Vec2 { x: pixel_u, y: pixel_v, }), ) }; TileCoords { grid_coords, pixel_coords, grid_position: Vec2 { x: grid_x, y: grid_y, }, pixel_position: Vec2 { x: pixel_x, y: pixel_y, }, } })) } TileCoordsLayerStorage::DataCoords2D(data) => { Either::Right(data.iter().enumerate().flat_map(move |(y, row)| { row.iter().enumerate().map(move |(x, coords)| { let grid_x = x as i32; let grid_y = y as i32; let pixel_x = grid_x * self.grid_cell_width; let pixel_y = grid_y * self.grid_cell_height; let (grid_coords, pixel_coords) = if coords[0] == -1 { (None, None) } else { let grid_u = coords[0]; let grid_v = coords[1]; let pixel_u = grid_u * self.grid_cell_width; let pixel_v = grid_v * self.grid_cell_height; ( Some(Vec2 { x: grid_u, y: grid_v, }), Some(Vec2 { x: pixel_u, y: pixel_v, }), ) }; TileCoords { grid_coords, pixel_coords, grid_position: Vec2 { x: grid_x, y: grid_y, }, pixel_position: Vec2 { x: pixel_x, y: pixel_y, }, } }) })) } } } } /// An individual tile, unpacked from a `TileCoordsLayer`. #[derive(Copy, Clone, Debug)] pub struct TileCoords { /// The position of the tile in the tileset, in grid co-ordinates. /// /// If the tile is empty, this will be `None`. pub grid_coords: Option<Vec2<i32>>, /// The position of the tile in the tileset, in pixel co-ordinates. /// /// If the tile is empty, this will be `None`. pub pixel_coords: Option<Vec2<i32>>, /// The position of the tile in grid co-ordinates. pub grid_position: Vec2<i32>, /// The position of the tile in pixel co-ordinates. pub pixel_position: Vec2<i32>, } /// Tile data from a `TileCoordsLayer`. #[derive(Clone, Debug, Deserialize)] pub enum TileCoordsLayerStorage { /// A flat list of tile co-ords. /// /// Each value corresponds to the X and Y co-ordinate of a tile in a tileset. The /// values are cell-based, rather than pixel-based - multiply by `grid_cell_width` /// to get the pixel position. /// /// Empty tiles are represented by a `[-1]`. #[serde(rename = "dataCoords")] DataCoords(Vec<Vec<i32>>), /// A 2D list of tile co-ords. /// /// Each value corresponds to the X and Y co-ordinate of a tile in a tileset. The /// values are cell-based, rather than pixel-based - multiply by `grid_cell_width` /// to get the pixel position. /// /// Empty tiles are represented by a `[-1]`. #[serde(rename = "dataCoords2D")] DataCoords2D(Vec<Vec<Vec<i32>>>), } impl Serialize for TileCoordsLayerStorage { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { let mut state = serializer.serialize_struct("TileCoordsLayerStorage", 3)?; match self { TileCoordsLayerStorage::DataCoords(data) => { state.serialize_field("dataCoords", &data)?; state.serialize_field("exportMode", &1)?; state.serialize_field("arrayMode", &0)?; } TileCoordsLayerStorage::DataCoords2D(data) => { state.serialize_field("dataCoords2D", &data)?; state.serialize_field("exportMode", &1)?; state.serialize_field("arrayMode", &1)?; } } state.end() } } /// A grid layer. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct GridLayer { /// The name of the layer. pub name: String, /// The unique export ID of the entity. #[serde(rename = "_eid")] pub export_id: String, /// The layer's offset on the X axis. pub offset_x: f32, /// The layer's offset on the Y axis. pub offset_y: f32, /// The width of the layer's grid cells. pub grid_cell_width: i32, /// The height of the layer's grid cells. pub grid_cell_height: i32, /// The number of grid cells on the X axis. pub grid_cells_x: i32, /// The number of grid cells on the Y axis. pub grid_cells_y: i32, /// The grid data. /// /// You may want to use the `unpack` method rather than accessing this directly. #[serde(flatten)] pub data: GridLayerStorage, } /// Grid data from a `GridLayer`. #[derive(Clone, Debug, Deserialize)] pub enum GridLayerStorage { /// A flat list of string data. /// /// By default, `"0"` means 'empty', but this is customizable in the editor. #[serde(rename = "grid")] Grid(Vec<String>), /// A 2D list of string data. /// /// By default, `"0"` means 'empty', but this is customizable in the editor. #[serde(rename = "grid2D")] Grid2D(Vec<Vec<String>>), } impl Serialize for GridLayerStorage { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { let mut state = serializer.serialize_struct("GridLayerStorage", 2)?; match self { GridLayerStorage::Grid(data) => { state.serialize_field("grid", &data)?; state.serialize_field("arrayMode", &0)?; } GridLayerStorage::Grid2D(data) => { state.serialize_field("grid2D", &data)?; state.serialize_field("arrayMode", &1)?; } } state.end() } } /// An individual grid cell, unpacked from a `GridLayer`. #[derive(Copy, Clone, Debug)] pub struct GridCell<'a> { /// The value of the grid cell. /// /// By default, `"0"` means 'empty', but this is customizable in the editor. pub value: &'a str, /// The position of the cell in grid co-ordinates. pub grid_position: Vec2<i32>, /// The position of the cell in pixel co-ordinates. pub pixel_position: Vec2<i32>, } impl GridLayer { /// Unpack the grid data from the layer. pub fn unpack(&self) -> impl Iterator<Item = GridCell<'_>> + '_ { match &self.data { GridLayerStorage::Grid(data) => { Either::Left(data.iter().enumerate().map(move |(i, value)| { let grid_x = i as i32 % self.grid_cells_x; let grid_y = i as i32 / self.grid_cells_x; let pixel_x = grid_x * self.grid_cell_width; let pixel_y = grid_y * self.grid_cell_height; GridCell { value, grid_position: Vec2 { x: grid_x, y: grid_y, }, pixel_position: Vec2 { x: pixel_x, y: pixel_y, }, } })) } GridLayerStorage::Grid2D(data) => { Either::Right(data.iter().enumerate().flat_map(move |(y, row)| { row.iter().enumerate().map(move |(x, value)| { let grid_x = x as i32; let grid_y = y as i32; let pixel_x = grid_x * self.grid_cell_width; let pixel_y = grid_y * self.grid_cell_height; GridCell { value, grid_position: Vec2 { x: grid_x, y: grid_y, }, pixel_position: Vec2 { x: pixel_x, y: pixel_y, }, } }) })) } } } } /// An entity layer. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct EntityLayer { /// The name of the layer. pub name: String, /// The unique export ID of the entity. #[serde(rename = "_eid")] pub export_id: String, /// The layer's offset on the X axis. pub offset_x: f32, /// The layer's offset on the Y axis. pub offset_y: f32, /// The width of the layer's grid cells. pub grid_cell_width: i32, /// The height of the layer's grid cells. pub grid_cell_height: i32, /// The number of grid cells on the X axis. pub grid_cells_x: i32, /// The number of grid cells on the Y axis. pub grid_cells_y: i32, /// Entity data. pub entities: Vec<Entity>, } /// A decal layer. #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct DecalLayer { /// The name of the layer. pub name: String, /// The unique export ID of the entity. #[serde(rename = "_eid")] pub export_id: String, /// The layer's offset on the X axis. pub offset_x: f32, /// The layer's offset on the Y axis. pub offset_y: f32, /// The width of the layer's grid cells. pub grid_cell_width: i32, /// The height of the layer's grid cells. pub grid_cell_height: i32, /// The number of grid cells on the X axis. pub grid_cells_x: i32, /// The number of grid cells on the Y axis. pub grid_cells_y: i32, /// Decal data. pub decals: Vec<Decal>, /// The path containing the decal images, relative to the project. pub folder: PathBuf, }