infinite_rs/module/header.rs
1//! Module Header containing info on the layout of the module file.
2
3use byteorder::{LE, ReadBytesExt};
4use num_enum::TryFromPrimitive;
5use std::{fs::File, io::BufReader};
6
7use crate::Result;
8use crate::common::errors::ModuleError;
9
10const HEADER_MAGIC: u32 = 0x6468_6F6D; // "mohd"
11
12#[derive(Default, Debug, PartialEq, Eq, TryFromPrimitive, PartialOrd, Ord)]
13#[repr(i32)]
14/// Revision number of a module file.
15/// This version number determines how tags should be read.
16pub enum ModuleVersion {
17 /// First "technical preview" build from July 2021.
18 Flight1 = 48,
19 /// Second technical preview (August 2021) and release version from November 2021.
20 Release = 51,
21 /// Build used in the co-op campaign flight, which introduced notable changes to the module structure.
22 CampaignFlight = 52,
23 #[default]
24 /// Builds from Season 3 and onwards.
25 Season3 = 53,
26}
27
28#[derive(Default, Debug)]
29/// Module Header structure containing info on the layout of the module file.
30pub struct ModuleHeader {
31 /// Should be "mohd" (0x64686F6D)
32 magic: u32,
33 /// Revision number of the module.
34 /// This determines how offsets are calculated and if tag names should be read.
35 pub version: ModuleVersion,
36 /// Unique identifier of module.
37 pub module_id: i64,
38 /// Number of files in the module.
39 pub file_count: u32,
40 /// Index of `loadmanifest` tag, which contains the tag ids that the module will load.
41 loadmanifest_index: i32,
42 /// Index of `runtimeloadmetadata` tag, which contains info on how tags should be loaded at runtime.
43 runtimeloadmetadata_index: i32,
44 /// Index of `resourcemetadata` tag, which contains info on how resources should be loaded.
45 resourcemetadata_index: i32,
46 /// Index of the first resource entry ([`file_count`](`ModuleHeader::file_count`) - [`resource_count`](`ModuleHeader::resource_count`)).
47 resource_index: i32,
48 /// Total size in bytes of the string table.
49 pub(super) strings_size: u32,
50 /// Number of resource files.
51 pub(super) resource_count: u32,
52 /// Number of data blocks.
53 pub(super) block_count: u32,
54 /// Same between modules, changes per build?
55 build_version: u64,
56 /// If non-zero, requires hd1 file.
57 pub(super) hd1_delta: u64,
58 /// Total size of packed data in the module.
59 /// Both compressed and uncompressed.
60 /// Starts after files, blocks, and resources have been read.
61 ///
62 /// This does NOT apply for versions before [`ModuleVersion::Season3`].
63 pub(super) data_size: u64,
64}
65
66impl ModuleHeader {
67 /// Reads the module header from the given buffered reader.
68 /// # Arguments
69 ///
70 /// * `reader` - A mutable reference to a [`BufReader<File>`] from which to read the data.
71 ///
72 /// # Errors
73 /// - If the magic number is not equal to [`HEADER_MAGIC`] [`ModuleError::IncorrectMagic`]
74 /// - If the version number is not recognized [`ModuleError::IncorrectVersion`]
75 /// - If the reader fails to read the exact number of bytes [`ReadError`](`crate::Error::ReadError`)
76 pub(super) fn read(&mut self, reader: &mut BufReader<File>) -> Result<()> {
77 self.magic = reader.read_u32::<LE>()?;
78 if self.magic != HEADER_MAGIC {
79 return Err(ModuleError::IncorrectMagic(self.magic).into());
80 }
81 self.version = ModuleVersion::try_from_primitive(reader.read_i32::<LE>()?)
82 .map_err(ModuleError::IncorrectVersion)?;
83
84 self.module_id = reader.read_i64::<LE>()?;
85 self.file_count = reader.read_u32::<LE>()?;
86 self.loadmanifest_index = reader.read_i32::<LE>()?;
87 self.runtimeloadmetadata_index = reader.read_i32::<LE>()?;
88 self.resourcemetadata_index = reader.read_i32::<LE>()?;
89 self.resource_index = reader.read_i32::<LE>()?;
90 self.strings_size = reader.read_u32::<LE>()?;
91 self.resource_count = reader.read_u32::<LE>()?;
92 self.block_count = reader.read_u32::<LE>()?;
93 self.build_version = reader.read_u64::<LE>()?;
94 self.hd1_delta = reader.read_u64::<LE>()?;
95 self.data_size = reader.read_u64::<LE>()?;
96 if self.version >= ModuleVersion::Release {
97 reader.seek_relative(8)?; // Not needed for now.
98 }
99 Ok(())
100 }
101}