tiger-lib 1.18.0

Library used by the tools ck3-tiger, vic3-tiger, and imperator-tiger. This library holds the bulk of the code for them. It can be built either for ck3-tiger with the feature ck3, or for vic3-tiger with the feature vic3, or for imperator-tiger with the feature imperator, but not both at the same time.
Documentation
use crate::block::Block;
use crate::context::ScopeContext;
use crate::db::{Db, DbKind};
use crate::effect::{validate_effect, validate_effect_internal};
use crate::everything::Everything;
use crate::game::GameFlags;
use crate::item::{Item, ItemLoader, LoadAsFile, Recursive};
use crate::lowercase::Lowercase;
use crate::pdxfile::PdxEncoding;
use crate::scopes::Scopes;
use crate::special_tokens::SpecialTokens;
use crate::token::Token;
use crate::tooltipped::Tooltipped;
use crate::validate::ListType;
use crate::validator::Validator;

#[derive(Clone, Debug)]
pub struct CountryHistory {}

inventory::submit! {
    ItemLoader::Full(GameFlags::Hoi4, Item::CountryHistory, PdxEncoding::Utf8NoBom, ".txt", LoadAsFile::Yes, Recursive::No, CountryHistory::add)
}

impl CountryHistory {
    pub fn add(db: &mut Db, file: Token, block: Block) {
        db.add(Item::CountryHistory, file, block, Box::new(Self {}));
    }
}

impl DbKind for CountryHistory {
    fn add_subitems(&self, _file: &Token, block: &Block, db: &mut Db) {
        // Go through the entire country history, looking for `division_template` at any depth.
        let mut flatten = vec![block];
        let mut i = 0;
        // Can't use `for`, because `flatten` gets extended during the loop.
        while i < flatten.len() {
            for (key, block) in flatten[i].iter_definitions() {
                if key.is("division_template") {
                    if let Some(name) = block.get_field_value("name") {
                        db.add_flag(Item::DivisionTemplate, name.clone());
                    }
                } else {
                    flatten.push(block);
                }
            }
            i += 1;
        }
    }

    fn validate(&self, file: &Token, block: &Block, data: &Everything) {
        let mut vd = Validator::new(block, data);
        vd.set_case_sensitive(false);

        vd.validate_history_blocks(|_, key, block, data| {
            if key.is_integer() {
                // This is actually a state id, not a date
                let mut sc = ScopeContext::new(Scopes::State, key);
                validate_effect(block, data, &mut sc, Tooltipped::No);
            } else {
                let mut vd = Validator::new(block, data);
                vd.set_case_sensitive(false);
                validate_history(file, block, data, vd);
            }
        });
        validate_history(file, block, data, vd);
    }
}

fn validate_history(key: &Token, block: &Block, data: &Everything, mut vd: Validator) {
    // TODO: verify that the capital is in the country
    vd.field_item("capital", Item::State);
    vd.field_integer("set_convoys");
    vd.field_numeric("starting_train_buffer");
    vd.field_item("oob", Item::UnitHistory);
    let mut sc = ScopeContext::new(Scopes::Country, key);
    validate_effect_internal(
        Lowercase::empty(),
        ListType::None,
        block,
        data,
        &mut sc,
        &mut vd,
        Tooltipped::No,
        &mut SpecialTokens::none(),
    );
}