tiger_lib/vic3/data/
provinces.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
use std::path::PathBuf;

use image::{DynamicImage, Rgb};

use crate::everything::Everything;
use crate::fileset::{FileEntry, FileHandler};
use crate::helpers::TigerHashSet;
use crate::item::Item;
use crate::parse::ParserMemory;
use crate::report::{err, report, ErrorKey, Severity};
use crate::token::Token;

#[derive(Clone, Debug, Default)]
pub struct Vic3Provinces {
    /// Colors in the provinces.png
    colors: TigerHashSet<Rgb<u8>>,

    /// Kept and used for error reporting.
    provinces_png: Option<FileEntry>,
}

impl Vic3Provinces {
    pub fn verify_exists_implied(&self, key: &str, item: &Token, max_sev: Severity) {
        if !self.exists(key) {
            // TODO: determine the severity of a missing province. Does it cause crashes?
            let msg = "province not found on map";
            let sev = Item::Province.severity().at_most(max_sev);
            report(ErrorKey::MissingItem, sev).msg(msg).loc(item).push();
        }
    }

    pub fn exists(&self, key: &str) -> bool {
        // If we failed to load the provinces.png, then don't complain about individual provinces not being found.
        if self.provinces_png.is_none() {
            return true;
        }
        if key.len() != 7 {
            return false; // not a valid province id
        }
        if let Some(hexid) = key.strip_prefix('x') {
            if let Ok(r) = u8::from_str_radix(&hexid[0..2], 16) {
                if let Ok(g) = u8::from_str_radix(&hexid[2..4], 16) {
                    if let Ok(b) = u8::from_str_radix(&hexid[4..6], 16) {
                        return self.colors.contains(&Rgb([r, g, b]));
                    }
                }
            }
        }
        false
    }

    #[allow(clippy::unused_self)]
    pub fn validate(&self, _data: &Everything) {}
}

impl FileHandler<DynamicImage> for Vic3Provinces {
    fn subpath(&self) -> PathBuf {
        PathBuf::from("map_data/provinces.png")
    }

    fn load_file(&self, entry: &FileEntry, _parser: &ParserMemory) -> Option<DynamicImage> {
        if entry.path().components().count() == 2 {
            let img = match image::open(entry.fullpath()) {
                Ok(img) => img,
                Err(e) => {
                    let msg = format!("could not read `{}`: {e:#}", entry.path().display());
                    // TODO: does this crash?
                    err(ErrorKey::ReadError).msg(msg).loc(entry).push();
                    return None;
                }
            };
            if let DynamicImage::ImageRgb8(_) = img {
                return Some(img);
            }
            let msg = format!(
                "`{}` has wrong color format `{:?}`, should be Rgb8",
                entry.path().display(),
                img.color()
            );
            // TODO: does this crash?
            err(ErrorKey::ImageFormat).msg(msg).loc(entry).push();
        }
        None
    }

    fn handle_file(&mut self, entry: &FileEntry, img: DynamicImage) {
        self.provinces_png = Some(entry.clone());
        if let DynamicImage::ImageRgb8(img) = img {
            for pixel in img.pixels() {
                self.colors.insert(*pixel);
            }
        }
    }
}