infinite_rs/tag/
header.rs

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
//! Tag Header containing info on the layout of the tag file.

use byteorder::{ReadBytesExt, LE};
use std::io::BufRead;

use crate::common::errors::{Error, TagError};
use crate::Result;

const HEADER_MAGIC: u32 = 0x6873_6375; // "ucsh"
const HEADER_VERSION: i32 = 27;

#[derive(Default, Debug)]
/// Tag Header structure containing info on the layout of the tag file.
pub struct TagHeader {
    /// Has to be "ucsh" (0x68736375)
    magic: u32,
    /// Should be 27.
    /// Note: this is also the tag version from Halo 5!
    version: i32,
    /// Secondary GUID to identify the root structure.
    pub root_struct_guid: i64,
    /// Checksum generated from unknown algorithm
    pub checksum: i64,
    /// Number of tags required to load tag.
    pub dependency_count: u32,
    /// Number of datablocks that exist within tag (offsets, sections etc).
    pub datablock_count: u32,
    /// Number of tag struct definitions that make up the actual structure of the tag.
    pub tagstruct_count: u32,
    /// Number of "external" data references (to other tags) in tag.
    pub data_reference_count: u32,
    /// Number of internal references to structures.
    pub tag_reference_count: u32,
    /// Size in bytes of string table inside tag.
    /// Unused after Halo 5.
    pub string_table_size: u32,
    /// Size in bytes of "zoneset" section of tag.
    /// Unknown use.
    pub zoneset_size: u32,
    /// Unknown. Possibly used to split something in memory.
    unknown: u32,
    /// Size of the header and the fields read by it (dependencies, datablocks, etc.).
    /// Important as sometimes the offset after reading those fields does not match up to where tag data starts.
    /// Might be some sort of internal padding measure.
    pub header_size: u32,
    /// Size of actual data in tag, referenced in tag structs.
    pub data_size: u32,
    /// Size of resource in tag (after data!)
    pub resource_size: u32,
    /// Size of "external" data, for instance Havok data.
    pub actual_resource_size: u32,
    /// Power of 2 to align the header to.
    header_alignment: u8,
    /// Power of 2 to align the tag data to.
    tag_alignment: u8,
    /// Power of 2 to align resource data to.
    resource_alignment: u8,
    /// Power of 2 to align actual resource to.
    actual_resource_alignment: u8,
    /// Unknown if this is consistent: Indicates if the file is a resource.
    pub is_resource: bool,
}

impl TagHeader {
    /// Reads the tag header from the given reader implementing [`BufRead`].
    /// # Arguments
    ///
    /// * `reader` - A mutable reference to a reader that implements [`BufRead`] from which to read the data.
    ///
    /// # Returns
    ///
    /// This function will return an error if:
    /// * The magic string is not "ucsh"
    /// * The version is less than or equal to 17
    /// * Any I/O error occurs while reading
    pub fn read<R: BufRead>(&mut self, reader: &mut R) -> Result<()> {
        self.magic = reader.read_u32::<LE>()?;
        if self.magic != HEADER_MAGIC {
            return Err(Error::TagError(TagError::IncorrectMagic(self.magic)));
        }

        self.version = reader.read_i32::<LE>()?;
        if self.version != HEADER_VERSION {
            return Err(Error::TagError(TagError::IncorrectVersion(self.version)));
        }

        self.root_struct_guid = reader.read_i64::<LE>()?;
        self.checksum = reader.read_i64::<LE>()?;
        self.dependency_count = reader.read_u32::<LE>()?;
        self.datablock_count = reader.read_u32::<LE>()?;
        self.tagstruct_count = reader.read_u32::<LE>()?;
        self.data_reference_count = reader.read_u32::<LE>()?;
        self.tag_reference_count = reader.read_u32::<LE>()?;
        self.string_table_size = reader.read_u32::<LE>()?;
        self.zoneset_size = reader.read_u32::<LE>()?;
        self.unknown = reader.read_u32::<LE>()?;
        self.header_size = reader.read_u32::<LE>()?;
        self.data_size = reader.read_u32::<LE>()?;
        self.resource_size = reader.read_u32::<LE>()?;
        self.actual_resource_size = reader.read_u32::<LE>()?;
        self.header_alignment = reader.read_u8()?;
        self.tag_alignment = reader.read_u8()?;
        self.resource_alignment = reader.read_u8()?;
        self.actual_resource_alignment = reader.read_u8()?;
        self.is_resource = reader.read_u32::<LE>()? != 0;
        Ok(())
    }
}