tiger_lib/
dds.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
109
110
111
112
113
114
115
116
117
//! Validator for the `.dds` (picture) files that are used in the game.

use std::fs::{metadata, File};
use std::io::{Read, Result};
use std::path::PathBuf;

use crate::fileset::{FileEntry, FileHandler};
use crate::helpers::TigerHashMap;
use crate::parse::ParserMemory;
use crate::report::{err, tips, warn, ErrorKey};
#[cfg(feature = "ck3")]
use crate::token::Token;

const DDS_HEADER_SIZE: usize = 124;
const DDS_HEIGHT_OFFSET: usize = 12;
const DDS_WIDTH_OFFSET: usize = 16;

fn from_le32(buffer: &[u8], offset: usize) -> u32 {
    u32::from(buffer[offset])
        | (u32::from(buffer[offset + 1]) << 8)
        | (u32::from(buffer[offset + 2]) << 16)
        | (u32::from(buffer[offset + 3]) << 24)
}

#[derive(Clone, Debug, Default)]
pub struct DdsFiles {
    dds_files: TigerHashMap<String, DdsInfo>,
}

impl DdsFiles {
    fn load_dds(entry: &FileEntry) -> Result<Option<DdsInfo>> {
        if metadata(entry.fullpath())?.len() == 0 {
            warn(ErrorKey::ImageFormat).msg("empty file").loc(entry).push();
            return Ok(None);
        }
        let mut f = File::open(entry.fullpath())?;
        let mut buffer = [0; DDS_HEADER_SIZE];
        f.read_exact(&mut buffer)?;
        if buffer.starts_with(b"\x89PNG") {
            let msg = "actually a PNG";
            let info =
                "this may be slower and lower quality because PNG format does not have mipmaps";
            tips(ErrorKey::ImageFormat).msg(msg).info(info).loc(entry).push();
            return Ok(None);
        }
        if !buffer.starts_with(b"DDS ") {
            err(ErrorKey::ImageFormat).msg("not a DDS file").loc(entry).push();
            return Ok(None);
        }
        Ok(Some(DdsInfo::new(&buffer)))
    }

    fn handle_dds(&mut self, entry: &FileEntry, info: DdsInfo) {
        self.dds_files.insert(entry.path().to_string_lossy().to_string(), info);
    }

    #[cfg(feature = "ck3")]
    pub fn validate_frame(&self, key: &Token, width: u32, height: u32, frame: u32) {
        // Note: `frame` is 1-based
        if let Some(info) = self.dds_files.get(key.as_str()) {
            if info.height != height {
                let msg = format!("texture does not match frame height of {height}");
                warn(ErrorKey::ImageFormat).msg(msg).loc(key).push();
            } else if width == 0 || (info.width % width) != 0 {
                let msg = format!("texture is not a multiple of frame width {width}");
                warn(ErrorKey::ImageFormat).msg(msg).loc(key).push();
            } else if frame * width > info.width {
                let msg = format!("texture is not large enough for frame index {frame}");
                warn(ErrorKey::ImageFormat).msg(msg).loc(key).push();
            }
        }
    }
}

impl FileHandler<DdsInfo> for DdsFiles {
    fn subpath(&self) -> PathBuf {
        PathBuf::from("gfx")
    }

    fn load_file(&self, entry: &FileEntry, _parser: &ParserMemory) -> Option<DdsInfo> {
        if !entry.filename().to_string_lossy().ends_with(".dds") {
            return None;
        }

        match Self::load_dds(entry) {
            Ok(info) => info,
            Err(e) => {
                err(ErrorKey::ReadError)
                    .msg("could not read dds header")
                    .info(format!("{e:#}"))
                    .loc(entry)
                    .push();
                None
            }
        }
    }

    fn handle_file(&mut self, entry: &FileEntry, info: DdsInfo) {
        self.handle_dds(entry, info);
    }
}

#[derive(Copy, Clone, Debug)]
pub struct DdsInfo {
    #[allow(dead_code)] // vic3 doesn't use
    width: u32,
    #[allow(dead_code)] // vic3 doesn't use
    height: u32,
}

impl DdsInfo {
    pub fn new(header: &[u8]) -> Self {
        let height = from_le32(header, DDS_HEIGHT_OFFSET);
        let width = from_le32(header, DDS_WIDTH_OFFSET);
        Self { width, height }
    }
}