1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Main abstraction file for tags.

use anyhow::Result;

use super::{
    data_reference::TagDataReference,
    datablock::TagDataBlock,
    dependency::TagDependency,
    header::TagHeader,
    reference::TagReference,
    structure::TagStruct,
    types::structs::{
        cmsw::CoatingSwatchPODTag, jssc::JsonSourceFileTagDefinition, luas::LuaScriptTagDefinition,
    },
    zoneset::{header::TagZonesetHeader, instance::TagZoneset},
};
use crate::common::extensions::BufReaderExt;
use std::any::Any;
use std::io::{BufRead, Seek, SeekFrom};

#[derive(Default, Debug)]
/// Tag structure containing structure of entire tag file.
pub struct TagFile {
    /// Header containing info on how to read other parts of the file.
    pub header: TagHeader,
    /// Tags that are referenced by this tag and that need to be lazy loaded.
    pub dependencies: Vec<TagDependency>,
    /// Blocks making up the entire tag (Internal and External)
    pub datablock: Vec<TagDataBlock>,
    /// Internal structure units of the tag.
    pub structs: Vec<TagStruct>,
    /// References to external data from the tag.
    pub data_references: Vec<TagDataReference>,
    /// Tags that are referenced by this tag inside the module.
    pub tag_references: Vec<TagReference>,
    /// Zoneset header for reading the other zonesets.
    pub zoneset_header: TagZonesetHeader,
    /// Zonesets, unknown what these are.
    pub zonesets: Vec<TagZoneset>,
    /// Actual structure depending on their tag group.
    /// Access this using get_struct.
    pub structure: Option<Box<dyn Any>>,
}

impl TagFile {
    /// Allocate new TagFile and set it to default values.
    pub fn new() -> Self {
        Self::default()
    }
    /// Reads the tag fike from the given readers implementing BufRead, BufReaderExt and Seek.
    /// # Arguments
    ///
    /// * `reader` - A mutable reference to a reader that implements `BufRead + BufReaderExt + Seek` from which to read the data.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the header is successfully read, or an `(anyhow) Error` if an I/O error occurs
    /// or if the header data is invalid.
    pub fn read<R: BufRead + BufReaderExt + Seek>(&mut self, mut reader: &mut R) -> Result<()> {
        self.header.read(reader)?;
        self.dependencies = (0..self.header.dependency_count as usize)
            .map(|_| {
                let mut dependency = TagDependency::new();
                dependency.read(reader).unwrap();
                dependency
            })
            .collect();

        self.datablock = (0..self.header.datablock_count as usize)
            .map(|_| {
                let mut block = TagDataBlock::new();
                block.read(reader).unwrap();
                block
            })
            .collect();

        self.structs = (0..self.header.tagstruct_count as usize)
            .map(|_| {
                let mut block = TagStruct::new();
                block.read(reader).unwrap();
                block
            })
            .collect();

        self.data_references = (0..self.header.data_reference_count as usize)
            .map(|_| {
                let mut block = TagDataReference::new();
                block.read(reader).unwrap();
                block
            })
            .collect();

        self.tag_references = (0..self.header.tag_reference_count as usize)
            .map(|_| {
                let mut block = TagReference::new();
                block.read(reader).unwrap();
                block
            })
            .collect();

        self.zoneset_header.read(&mut reader)?;
        self.zonesets = (0..self.zoneset_header.zoneset_count as usize)
            .map(|_| {
                let mut zoneset = TagZoneset::new();
                zoneset.read(reader).unwrap();
                zoneset
            })
            .collect();

        // Ensure that tag data starts where it is supposed to.
        reader.seek(SeekFrom::Start(self.header.header_size as u64))?;
        Ok(())
    }

    /// Reads the "structure" according to a tag's group.
    ///
    /// This function creates a Box<> smart pointer depending on the type of the tag.
    /// The structure can later be obtained using get_struct.
    ///
    /// # Arguments
    ///
    /// * `reader` - A mutable reference to a reader that implements `BufRead, BufReaderExt and Seek` from which to read the data.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` if the header is successfully read, or an `Err` if an I/O error occurs
    /// or if the header data is invalid.
    pub fn read_struct<R: BufRead + BufReaderExt + Seek>(
        &mut self,
        tag_group: &str,
        reader: &mut R,
    ) -> std::io::Result<()> {
        match tag_group {
            "jssc" => {
                let mut jssc = JsonSourceFileTagDefinition::new();
                jssc.read(reader)?;
                self.structure = Some(Box::new(jssc));
            }
            "cmsw" => {
                let mut cmsw = CoatingSwatchPODTag::new();
                cmsw.read(reader)?;
                self.structure = Some(Box::new(cmsw));
            }
            "luas" => {
                let mut luas = LuaScriptTagDefinition::new();
                luas.read(reader)?;
                self.structure = Some(Box::new(luas));
            }
            _ => (),
        }
        Ok(())
    }

    /// Get the "structure" according to a tag's group.
    ///
    /// This function returns an Option<&T> where T is the type specified when calling the function.
    ///
    /// # Type Parameters
    ///
    /// * `T`: The type to cast the structure to.
    ///
    /// # Returns
    ///
    /// Returns `Some(&T)` if the structure exists and can be cast to type T, or `None` otherwise.
    pub fn get_struct<T: 'static>(&self) -> Option<&T> {
        self.structure
            .as_ref()
            .and_then(|data| data.downcast_ref::<T>())
    }
}