rpfm_lib/files/tile_database/tile_link_target/mod.rs
1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! Tile link target definitions for the tile database.
12//!
13//! This module contains the [`TileLinkTarget`] struct, which specifies the destination
14//! of a tile link. It identifies which tile set and position a link points to.
15
16use serde_derive::{Serialize, Deserialize};
17
18use crate::binary::{ReadBytes, WriteBytes};
19use crate::error::{Result, RLibError};
20use crate::files::{Decodeable, EncodeableExtraData, Encodeable};
21
22use super::*;
23
24mod v1;
25
26//---------------------------------------------------------------------------//
27// Enum & Structs
28//---------------------------------------------------------------------------//
29
30/// Target destination for a tile link.
31///
32/// Identifies the tile set and coordinates that a [`TileLink`](super::tile_link::TileLink)
33/// connects to. This allows the terrain system to resolve link references to actual tiles.
34#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
35#[getset(get = "pub", get_mut = "pub", set = "pub")]
36pub struct TileLinkTarget {
37 /// Serialization version for this structure.
38 serialise_version: u16,
39
40 /// Name of the tile set containing the target tile.
41 tile_set: String,
42 /// X coordinate of the target tile within the tile set.
43 x: i32,
44 /// Y coordinate of the target tile within the tile set.
45 y: i32,
46}
47
48//---------------------------------------------------------------------------//
49// Implementation of TileLinkTarget
50//---------------------------------------------------------------------------//
51
52impl Decodeable for TileLinkTarget {
53
54 fn decode<R: ReadBytes>(data: &mut R, extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
55 let mut decoded = Self::default();
56 decoded.serialise_version = data.read_u16()?;
57
58 match decoded.serialise_version {
59 1 => decoded.read_v1(data, extra_data)?,
60 _ => return Err(RLibError::DecodingFastBinUnsupportedVersion(String::from("TileLinkTarget"), decoded.serialise_version)),
61 }
62
63 Ok(decoded)
64 }
65}
66
67impl Encodeable for TileLinkTarget {
68
69 fn encode<W: WriteBytes>(&mut self, buffer: &mut W, extra_data: &Option<EncodeableExtraData>) -> Result<()> {
70 buffer.write_u16(self.serialise_version)?;
71
72 match self.serialise_version {
73 1 => self.write_v1(buffer, extra_data)?,
74 _ => return Err(RLibError::EncodingFastBinUnsupportedVersion(String::from("TileLinkTarget"), self.serialise_version)),
75 }
76
77 Ok(())
78 }
79}