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

use crate::block::Block;
use crate::everything::Everything;
use crate::fileset::{FileEntry, FileHandler};
use crate::helpers::{dup_error, TigerHashMap};
use crate::item::Item;
use crate::parse::ParserMemory;
use crate::pdxfile::PdxFile;
use crate::report::{err, ErrorKey};
use crate::token::Token;
use crate::validator::Validator;

#[derive(Clone, Debug, Default)]
pub struct CharacterInteractionCategories {
    categories: TigerHashMap<&'static str, Category>,
}

impl CharacterInteractionCategories {
    pub fn load_item(&mut self, key: Token, block: Block) {
        if let Some(other) = self.categories.get(key.as_str()) {
            if other.key.loc.kind == key.loc.kind {
                dup_error(&key, &other.key, "interaction category");
            }
        }
        self.categories.insert(key.as_str(), Category::new(key, block));
    }

    pub fn exists(&self, key: &str) -> bool {
        self.categories.contains_key(key)
    }

    pub fn iter_keys(&self) -> impl Iterator<Item = &Token> {
        self.categories.values().map(|item| &item.key)
    }

    pub fn validate(&self, data: &Everything) {
        for item in self.categories.values() {
            item.validate(data);
        }
    }
}

impl FileHandler<Block> for CharacterInteractionCategories {
    fn subpath(&self) -> PathBuf {
        PathBuf::from("common/character_interaction_categories")
    }

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

        PdxFile::read(entry, parser)
    }

    fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
        for (key, block) in block.drain_definitions_warn() {
            self.load_item(key, block);
        }
    }

    fn finalize(&mut self) {
        let mut taken = vec![None; self.categories.len()];
        for item in self.categories.values() {
            if let Some(index) = item.index {
                if index >= 0 {
                    let index = usize::try_from(index).expect("internal error");
                    if index < taken.len() {
                        if let Some(other) = taken[index] {
                            let msg = format!("index duplicates the index of {other}");
                            err(ErrorKey::DuplicateItem).msg(msg).loc(&item.key).push();
                        } else {
                            taken[index] = Some(&item.key);
                        }
                        continue;
                    }
                }
                let msg = "index needs to be from 0 to the number of categories";
                err(ErrorKey::Range).msg(msg).loc(&item.key).push();
            }
            // if no index, the item will warn about that in validate
        }
    }
}

#[derive(Clone, Debug)]
pub struct Category {
    key: Token,
    block: Block,
    index: Option<i64>,
}

impl Category {
    pub fn new(key: Token, block: Block) -> Self {
        let index = block.get_field_integer("index");
        Category { key, block, index }
    }

    pub fn validate(&self, data: &Everything) {
        let mut vd = Validator::new(&self.block, data);
        vd.req_field("index");
        vd.req_field("desc");
        vd.field_integer("index");
        vd.field_item("desc", Item::Localization);
        vd.field_bool("default");
    }
}