Skip to main content

rpfm_lib/files/bmd/civilian_shelter_list/
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
11use serde_derive::{Serialize, Deserialize};
12
13use crate::binary::{ReadBytes, WriteBytes};
14use crate::error::{Result, RLibError};
15use crate::files::{Decodeable, EncodeableExtraData, Encodeable};
16
17use super::*;
18
19/// Version of the civilian shelter records. No other is known.
20const SHELTER_VERSION: u16 = 1;
21
22//---------------------------------------------------------------------------//
23//                              Enum & Structs
24//---------------------------------------------------------------------------//
25
26/// Places where civilians shelter during a battle.
27#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
28#[getset(get = "pub", get_mut = "pub", set = "pub")]
29pub struct CivilianShelterList {
30    civilian_shelter_list: Vec<CivilianShelter>,
31}
32
33/// A place where civilians shelter.
34#[derive(Default, PartialEq, Clone, Debug, Getters, MutGetters, Setters, Serialize, Deserialize)]
35#[getset(get = "pub", get_mut = "pub", set = "pub")]
36pub struct CivilianShelter {
37
38    /// Area of the shelter.
39    area: Rectangle,
40
41    /// How many civilians fit in the shelter. `u32::MAX` in most shelters, likely meaning no limit.
42    capacity: u32,
43}
44
45//---------------------------------------------------------------------------//
46//                Implementation of CivilianShelterList
47//---------------------------------------------------------------------------//
48
49impl Decodeable for CivilianShelterList {
50
51    fn decode<R: ReadBytes>(data: &mut R, extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
52        let mut decoded = Self::default();
53
54        for _ in 0..data.read_u32()? {
55            let version = data.read_u16()?;
56            if version != SHELTER_VERSION {
57                return Err(RLibError::DecodingFastBinUnsupportedVersion(String::from("CivilianShelter"), version));
58            }
59
60            decoded.civilian_shelter_list.push(CivilianShelter {
61                area: Rectangle::decode(data, extra_data)?,
62                capacity: data.read_u32()?,
63            });
64        }
65
66        Ok(decoded)
67    }
68}
69
70impl Encodeable for CivilianShelterList {
71
72    fn encode<W: WriteBytes>(&mut self, buffer: &mut W, extra_data: &Option<EncodeableExtraData>) -> Result<()> {
73        buffer.write_u32(self.civilian_shelter_list.len() as u32)?;
74
75        for shelter in &mut self.civilian_shelter_list {
76            buffer.write_u16(SHELTER_VERSION)?;
77            shelter.area.encode(buffer, extra_data)?;
78            buffer.write_u32(shelter.capacity)?;
79        }
80
81        Ok(())
82    }
83}