rpfm_lib/files/bmd/common/building_reference/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 reference data structure for BMD files.
12//!
13//! This module defines the [`BuildingReference`] structure which provides a reference
14//! to a building in the battlefield building list by its index.
15//!
16//! # Supported Versions
17//!
18//! - **Version 1**: Current format
19//!
20//! # Usage
21//!
22//! ```ignore
23//! use rpfm_lib::files::bmd::common::building_reference::BuildingReference;
24//! use rpfm_lib::files::Decodeable;
25//!
26//! let reference = BuildingReference::decode(&mut reader, &None)?;
27//! println!("Building index: {}", reference.building_index());
28//! ```
29
30use serde_derive::{Serialize, Deserialize};
31
32use crate::binary::{ReadBytes, WriteBytes};
33use crate::error::{Result, RLibError};
34use crate::files::{Decodeable, EncodeableExtraData, Encodeable};
35
36use super::*;
37
38mod v1;
39
40//---------------------------------------------------------------------------//
41// Enum & Structs
42//---------------------------------------------------------------------------//
43
44/// Reference to a building by its index in the building list.
45///
46/// Provides a simple index-based reference to a building instance in the
47/// battlefield building list. Used by building links and other structures
48/// to refer to specific buildings.
49///
50/// # Fields
51///
52/// - `serialise_version`: Format version (currently only version 1)
53/// - `building_index`: Index of the referenced building
54///
55/// # Example
56///
57/// ```ignore
58/// use rpfm_lib::files::bmd::common::building_reference::BuildingReference;
59///
60/// let mut reference = BuildingReference::default();
61/// reference.set_serialise_version(1);
62/// reference.set_building_index(42);
63/// ```
64#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
65#[getset(get = "pub", get_mut = "pub", set = "pub")]
66pub struct BuildingReference {
67 /// Format version number (currently only version 1).
68 serialise_version: u16,
69
70 /// Index of the referenced building in the building list.
71 building_index: i32,
72}
73
74//---------------------------------------------------------------------------//
75// Implementation of Properties
76//---------------------------------------------------------------------------//
77
78impl Decodeable for BuildingReference {
79
80 fn decode<R: ReadBytes>(data: &mut R, extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
81 let mut flags = Self::default();
82 flags.serialise_version = data.read_u16()?;
83
84 match flags.serialise_version {
85 1 => flags.read_v1(data, extra_data)?,
86 _ => return Err(RLibError::DecodingFastBinUnsupportedVersion(String::from("BuildingReference"), flags.serialise_version)),
87 }
88
89 Ok(flags)
90 }
91}
92
93impl Encodeable for BuildingReference {
94
95 fn encode<W: WriteBytes>(&mut self, buffer: &mut W, extra_data: &Option<EncodeableExtraData>) -> Result<()> {
96 buffer.write_u16(self.serialise_version)?;
97
98 match self.serialise_version {
99 1 => self.write_v1(buffer, extra_data)?,
100 _ => return Err(RLibError::EncodingFastBinUnsupportedVersion(String::from("BuildingReference"), self.serialise_version)),
101 }
102
103 Ok(())
104 }
105}