Skip to main content

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