rpfm_lib/files/bmd/common/building_link/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//! Building link data structure for BMD files.
12//!
13//! This module defines the [`BuildingLink`] structure which links building instances
14//! to prefab instances in BMD files. Building links establish relationships between
15//! buildings and their associated prefabs.
16//!
17//! # Supported Versions
18//!
19//! - **Version 1**: Initial format
20//! - **Version 2**: Enhanced format
21//! - **Version 3**: Current format
22//!
23//! # Usage
24//!
25//! ```ignore
26//! use rpfm_lib::files::bmd::common::building_link::BuildingLink;
27//! use rpfm_lib::files::Decodeable;
28//!
29//! let link = BuildingLink::decode(&mut reader, &None)?;
30//! println!("Building index: {}", link.building_index());
31//! println!("Prefab key: {}", link.prefab_building_key());
32//! ```
33
34use serde_derive::{Serialize, Deserialize};
35
36use crate::binary::{ReadBytes, WriteBytes};
37use crate::error::Result;
38use crate::files::{Decodeable, EncodeableExtraData, Encodeable};
39use crate::files::bmd::building_reference::BuildingReference;
40
41use super::*;
42
43mod v1;
44mod v2;
45mod v3;
46
47//---------------------------------------------------------------------------//
48// Enum & Structs
49//---------------------------------------------------------------------------//
50
51/// Links a building instance to a prefab instance.
52///
53/// Building links establish relationships between buildings in the battlefield
54/// building list and their associated prefab instances. This allows buildings
55/// to reference prefab data for models, textures, and other assets.
56///
57/// # Fields
58///
59/// - `serialise_version`: Format version (1-3)
60/// - `building_index`: Index of the building in the building list
61/// - `prefab_index`: Index of the prefab in the prefab list
62/// - `prefab_building_key`: String key identifying the prefab building
63/// - `uid`: Unique identifier for this building link
64/// - `prefab_uid`: Unique identifier of the associated prefab
65/// - `building_reference`: Reference data for the building
66///
67/// # Example
68///
69/// ```ignore
70/// use rpfm_lib::files::bmd::common::building_link::BuildingLink;
71///
72/// let mut link = BuildingLink::default();
73/// link.set_serialise_version(3);
74/// link.set_building_index(5);
75/// link.set_prefab_building_key("settlement_wall_01".to_string());
76/// ```
77#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
78#[getset(get = "pub", get_mut = "pub", set = "pub")]
79pub struct BuildingLink {
80 /// Format version number (1-3).
81 serialise_version: u16,
82
83 /// Index of the building in the building list.
84 building_index: i32,
85
86 /// Index of the prefab in the prefab list.
87 prefab_index: i32,
88
89 /// String key identifying the prefab building.
90 prefab_building_key: String,
91
92 /// Unique identifier for this building link.
93 uid: u64,
94
95 /// Unique identifier of the associated prefab.
96 prefab_uid: u64,
97
98 /// Reference data for the building.
99 building_reference: BuildingReference
100}
101
102//---------------------------------------------------------------------------//
103// Implementation of BuildingLink
104//---------------------------------------------------------------------------//
105
106impl Decodeable for BuildingLink {
107
108 fn decode<R: ReadBytes>(data: &mut R, extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
109 let mut decoded = Self::default();
110 decoded.serialise_version = data.read_u16()?;
111
112 match decoded.serialise_version {
113 1 => decoded.read_v1(data, extra_data)?,
114 2 => decoded.read_v2(data, extra_data)?,
115 3 => decoded.read_v3(data, extra_data)?,
116 _ => return Err(RLibError::DecodingFastBinUnsupportedVersion(String::from("BuildingLink"), decoded.serialise_version)),
117 }
118
119 Ok(decoded)
120 }
121}
122
123impl Encodeable for BuildingLink {
124
125 fn encode<W: WriteBytes>(&mut self, buffer: &mut W, extra_data: &Option<EncodeableExtraData>) -> Result<()> {
126 buffer.write_u16(self.serialise_version)?;
127
128 match self.serialise_version {
129 1 => self.write_v1(buffer, extra_data)?,
130 2 => self.write_v2(buffer, extra_data)?,
131 3 => self.write_v3(buffer, extra_data)?,
132 _ => return Err(RLibError::EncodingFastBinUnsupportedVersion(String::from("BuildingLink"), self.serialise_version)),
133 }
134
135 Ok(())
136 }
137}