tiger_lib/data/
effect_localization.rs

1use crate::block::Block;
2use crate::db::{Db, DbKind};
3use crate::everything::Everything;
4use crate::game::{Game, GameFlags};
5use crate::item::{Item, ItemLoader};
6use crate::report::{ErrorKey, warn};
7use crate::token::Token;
8use crate::tooltipped::Tooltipped;
9use crate::validator::Validator;
10
11#[derive(Clone, Debug)]
12pub struct EffectLocalization {}
13
14inventory::submit! {
15    ItemLoader::Normal(GameFlags::jomini(), Item::EffectLocalization, EffectLocalization::add)
16}
17
18impl EffectLocalization {
19    pub fn add(db: &mut Db, key: Token, block: Block) {
20        db.add(Item::EffectLocalization, key, block, Box::new(Self {}));
21    }
22
23    pub fn validate_use(
24        key: &Token,
25        block: &Block,
26        data: &Everything,
27        caller: &Token,
28        tooltipped: Tooltipped,
29    ) {
30        match tooltipped {
31            Tooltipped::No => (),
32            Tooltipped::Yes | Tooltipped::FailuresOnly => {
33                for field in &["global", "first", "third"] {
34                    if block.has_key(field) {
35                        return;
36                    }
37                }
38                let msg = "missing present perspective";
39                warn(ErrorKey::MissingPerspective).msg(msg).loc(caller).loc_msg(key, "here").push();
40            }
41            Tooltipped::Past => {
42                for field in &["global_past", "first_past", "third_past"] {
43                    if block.has_key(field) {
44                        return;
45                    }
46                }
47                for field in &["global", "first", "third"] {
48                    if let Some(token) = block.get_field_value(field) {
49                        // TODO: check if these are auto-guessed if _past key is missing
50                        let loca = format!("{token}_PAST");
51                        if data.item_exists(Item::Localization, &loca) {
52                            return;
53                        }
54                    }
55                }
56                let msg = "missing `_past` perspective";
57                warn(ErrorKey::MissingPerspective).msg(msg).loc(caller).loc_msg(key, "here").push();
58            }
59            #[cfg(feature = "hoi4")]
60            Tooltipped::Inner => unimplemented!(),
61        }
62    }
63}
64
65impl DbKind for EffectLocalization {
66    fn validate(&self, _key: &Token, block: &Block, data: &Everything) {
67        let mut vd = Validator::new(block, data);
68        #[cfg(feature = "eu5")]
69        vd.field_item("none", Item::Localization);
70        #[cfg(feature = "eu5")]
71        vd.field_item("none_past", Item::Localization);
72        vd.field_item("global", Item::Localization);
73        vd.field_item("global_past", Item::Localization);
74        vd.field_item("global_neg", Item::Localization);
75        vd.field_item("global_past_neg", Item::Localization);
76        vd.field_item("first", Item::Localization);
77        vd.field_item("first_past", Item::Localization);
78        vd.field_item("first_neg", Item::Localization);
79        vd.field_item("first_past_neg", Item::Localization);
80        vd.field_item("third", Item::Localization);
81        vd.field_item("third_past", Item::Localization);
82        vd.field_item("third_neg", Item::Localization);
83        vd.field_item("third_past_neg", Item::Localization);
84    }
85}
86
87pub fn validate_effect_localization(caller: &Token, data: &Everything, tooltipped: Tooltipped) {
88    if let Some((key, block)) = data.get_key_block(Item::EffectLocalization, caller.as_str()) {
89        EffectLocalization::validate_use(key, block, data, caller, tooltipped);
90        return;
91    }
92
93    // As of CK3 1.18, effect localizations don't have to be defined and can just be present as
94    // localizations.
95    if Game::is_ck3() {
96        match tooltipped {
97            Tooltipped::No => (),
98            Tooltipped::Yes | Tooltipped::FailuresOnly => {
99                if data.item_exists(Item::Localization, caller.as_str()) {
100                    return;
101                }
102                for sfx in &["global", "first", "third"] {
103                    let loca = format!("{caller}_{sfx}");
104                    if data.item_exists(Item::Localization, &loca) {
105                        return;
106                    }
107                }
108                let msg = "missing present perspective";
109                warn(ErrorKey::MissingPerspective).msg(msg).loc(caller).push();
110            }
111            Tooltipped::Past => {
112                for sfx in &["global_part", "first_part", "third_past"] {
113                    let loca = format!("{caller}_{sfx}");
114                    if data.item_exists(Item::Localization, &loca) {
115                        return;
116                    }
117                }
118                let msg = "missing past perspective";
119                warn(ErrorKey::MissingPerspective).msg(msg).loc(caller).push();
120            }
121            #[cfg(feature = "hoi4")]
122            Tooltipped::Inner => unimplemented!(),
123        }
124    } else {
125        data.verify_exists(Item::EffectLocalization, caller);
126    }
127}