source_vtf/header/
resource.rs1use crate::cursor::range;
2use crate::image::layout;
3use crate::{Error, Header, Result};
4
5const DICTIONARY_OFFSET: usize = 80; const ENTRY_SIZE: usize = 8; const NO_DATA_CHUNK: u32 = 0x02 << 24;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13#[cfg_attr(feature = "serde", serde(transparent))]
14pub struct ResourceTag(pub u32);
15
16impl ResourceTag {
17 pub const THUMBNAIL_DATA: Self = Self(0x01);
18 pub const IMAGE_DATA: Self = Self(0x30);
19 pub const PARTICLE_SHEET_DATA: Self = Self(0x10);
20 pub const EXTENDED_FLAGS: Self = Self::from_bytes(*b"TS0");
21 pub const CRC: Self = Self::from_bytes(*b"CRC");
22 pub const LOD_CONTROL_INFO: Self = Self::from_bytes(*b"LOD");
23 pub const STREAM_SETTINGS: Self = Self::from_bytes(*b"STR");
24 pub const KEY_VALUES_DATA: Self = Self::from_bytes(*b"KVD");
26
27 pub const fn from_bytes(bytes: [u8; 3]) -> Self {
28 Self(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], 0]))
29 }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34pub enum ResourceLocation {
35 Inline([u8; 4]),
37 Data { offset: usize, length: usize },
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
43pub struct Resource {
44 pub tag: ResourceTag,
45 pub location: ResourceLocation,
46}
47
48pub(super) fn parse_dictionary(bytes: &[u8], header: &Header, count: usize) -> Result<Vec<Resource>> {
50 let table = range(bytes, DICTIONARY_OFFSET, count.saturating_mul(ENTRY_SIZE), "resource table")?;
51 let mut resources = Vec::with_capacity(count);
52
53 for entry in table.chunks_exact(ENTRY_SIZE) {
54 let kind = u32::from_le_bytes(entry[..4].try_into().unwrap());
55 let data: [u8; 4] = entry[4..].try_into().unwrap();
56 let tag = ResourceTag(kind & 0x00ff_ffff);
57 if kind & NO_DATA_CHUNK != 0 {
58 resources.push(Resource { tag, location: ResourceLocation::Inline(data) });
59 continue;
60 }
61
62 let offset = u32::from_le_bytes(data) as usize;
63 let (offset, length) = match tag {
64 ResourceTag::IMAGE_DATA => (offset, layout::image_len(header)),
65 ResourceTag::THUMBNAIL_DATA => (offset, layout::thumbnail_len(header)),
66 _ => {
71 let size = range(bytes, offset, 4, "resource size")?;
72 (offset + 4, u32::from_le_bytes(size.try_into().unwrap()) as usize)
73 }
74 };
75
76 range(bytes, offset, length, "resource data")?;
77
78 resources.push(Resource { tag, location: ResourceLocation::Data { offset, length } });
79 }
80
81 require(&resources, ResourceTag::IMAGE_DATA)?;
82 if layout::thumbnail_len(header) != 0 {
83 require(&resources, ResourceTag::THUMBNAIL_DATA)?;
84 }
85 Ok(resources)
86}
87
88pub(super) fn legacy_resources(bytes: &[u8], header: &Header, header_size: usize) -> Result<Vec<Resource>> {
90 let thumbnail_length = layout::thumbnail_len(header);
91 let image_offset = header_size + thumbnail_length;
92 let image_length = layout::image_len(header);
93 range(bytes, image_offset, image_length, "image data")?;
94
95 let mut resources = Vec::with_capacity(2);
96 if thumbnail_length != 0 {
97 resources.push(Resource {
98 tag: ResourceTag::THUMBNAIL_DATA,
99 location: ResourceLocation::Data { offset: header_size, length: thumbnail_length },
100 });
101 }
102
103 resources.push(Resource {
104 tag: ResourceTag::IMAGE_DATA,
105 location: ResourceLocation::Data { offset: image_offset, length: image_length },
106 });
107
108 Ok(resources)
109}
110
111fn require(resources: &[Resource], tag: ResourceTag) -> Result<()> {
112 match resources
113 .iter()
114 .find(|resource| resource.tag == tag)
115 {
116 Some(Resource { location: ResourceLocation::Data { .. }, .. }) => Ok(()),
117 _ => Err(Error::MissingResource(tag)),
118 }
119}