vmf_forge/vmf_file/
mod.rs

1use crate::VmfSerializable;
2
3use super::vmf::entities::Entities;
4use super::vmf::metadata::{VersionInfo, ViewSettings, VisGroups};
5use super::vmf::regions::{Cameras, Cordons};
6use super::vmf::world::World;
7
8mod io;
9mod merge;
10mod visgroup_ops;
11
12/// Represents a parsed VMF file.
13#[derive(Debug, Clone, PartialEq)]
14#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
15pub struct VmfFile {
16    /// The path to the VMF file, if known.
17    #[cfg_attr(
18        feature = "serialization",
19        serde(default, skip_serializing_if = "Option::is_none")
20    )]
21    pub path: Option<String>,
22    /// The version info of the VMF file.
23    pub versioninfo: VersionInfo,
24    /// The visgroups in the VMF file.
25    pub visgroups: VisGroups,
26    /// The view settings in the VMF file.
27    pub viewsettings: ViewSettings,
28    /// The world data in the VMF file.
29    pub world: World,
30    /// The entities in the VMF file.
31    pub entities: Entities,
32    /// The hidden entities in the VMF file.
33    pub hiddens: Entities,
34    /// The camera data in the VMF file.
35    pub cameras: Cameras,
36    /// The cordon data in the VMF file.
37    pub cordons: Cordons,
38}
39
40impl Default for VmfFile {
41    fn default() -> Self {
42        Self {
43            path: None,
44            versioninfo: Default::default(),
45            visgroups: Default::default(),
46            viewsettings: Default::default(),
47            world: Default::default(),
48            entities: Entities(Vec::with_capacity(128)),
49            hiddens: Entities(Vec::with_capacity(16)),
50            cameras: Default::default(),
51            cordons: Default::default(),
52        }
53    }
54}
55
56impl VmfFile {
57    /// Converts the `VmfFile` to a string in VMF format.
58    ///
59    /// # Returns
60    ///
61    /// A string representing the `VmfFile` in VMF format.
62    pub fn to_vmf_string(&self) -> String {
63        let mut output = String::new();
64
65        // metadatas
66        output.push_str(&self.versioninfo.to_vmf_string(0));
67        output.push_str(&self.visgroups.to_vmf_string(0));
68        output.push_str(&self.viewsettings.to_vmf_string(0));
69        output.push_str(&self.world.to_vmf_string(0));
70
71        // entities
72        for entity in &*self.entities {
73            output.push_str(&entity.to_vmf_string(0));
74        }
75
76        for entity in &*self.hiddens {
77            output.push_str("hidden\n{\n");
78            output.push_str(&entity.to_vmf_string(1));
79            output.push_str("}\n");
80        }
81
82        // regions
83        output.push_str(&self.cameras.to_vmf_string(0));
84        output.push_str(&self.cordons.to_vmf_string(0));
85
86        output
87    }
88}