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