aoe2_probe/tweak/
condition.rs

1use crate::{
2    parse::{Censor, Token},
3    prebuilt::{ver1_46, ConditionConfig, CONDITION_SCHEME},
4    Scenario,
5};
6
7pub struct ConditionTweak;
8
9impl ConditionTweak {
10    pub fn translate(
11        scenario: &Scenario,
12        condition: &Token,
13    ) -> Result<(String, String), &'static str> {
14        match scenario.version() {
15            "1.46" | "1.47" => {
16                Self::is_condition(condition, scenario.version())?;
17
18                let type_id = *condition.get_by_path("/condition_type").try_i32();
19                if CONDITION_SCHEME.contains_key(&type_id) {
20                    let scheme = &CONDITION_SCHEME[&type_id];
21                    let name = scheme.name.to_string();
22                    let attrs: Vec<String> = scheme
23                        .attrs
24                        .iter()
25                        .map(|&path| match path {
26                            "xs_function" => {
27                                format!(
28                                    "{}: {:?}",
29                                    path,
30                                    condition.get_by_path(path).try_str32().content()
31                                )
32                            }
33                            _ => {
34                                format!("{}: {:?}", path, condition.get_by_path(path).try_i32())
35                            }
36                        })
37                        .collect();
38
39                    Ok((name, attrs.join(" ")))
40                } else {
41                    Ok(("Unknown Condition!".to_string(), "".to_string()))
42                }
43            }
44            _ => Err("Incompatible version!"),
45        }
46    }
47
48    pub fn scheme(version: &str, condition: &Token) -> Result<ConditionConfig, &'static str> {
49        match version {
50            "1.46" | "1.47" => {
51                Self::is_condition(condition, version)?;
52
53                let type_id = *condition.get_by_path("/condition_type").try_i32();
54                if CONDITION_SCHEME.contains_key(&type_id) {
55                    let scheme = CONDITION_SCHEME[&type_id].clone();
56
57                    Ok(scheme)
58                } else {
59                    Ok(ConditionConfig {
60                        name: "Unknown",
61                        attrs: vec![],
62                    })
63                }
64            }
65            _ => Err("Incompatible version!"),
66        }
67    }
68
69    pub fn is_condition(effect: &Token, version: &str) -> Result<(), &'static str> {
70        match version {
71            "1.46" | "1.47" => {
72                let template = ver1_46::Condition::template();
73                let res = Censor::is_template(effect, &template, 2);
74
75                if res {
76                    Ok(())
77                } else {
78                    Err("Not a effect!")
79                }
80            }
81            _ => Err("Incompatible version!"),
82        }
83    }
84}