rpgmv_tool/command/commands2py/
config.rs

1use serde::de::Error;
2use std::collections::BTreeMap;
3use std::path::Path;
4
5/// Config
6#[derive(Debug, serde::Deserialize, Default)]
7pub struct Config {
8    /// Switches
9    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
10    pub switches: BTreeMap<u32, String>,
11
12    /// Variables
13    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
14    pub variables: BTreeMap<u32, String>,
15
16    /// Common Events
17    #[serde(
18        default,
19        rename = "common-events",
20        deserialize_with = "deserialize_u32_key_btree_map"
21    )]
22    pub common_events: BTreeMap<u32, String>,
23
24    /// Actors
25    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
26    pub actors: BTreeMap<u32, String>,
27
28    /// Skills
29    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
30    pub skills: BTreeMap<u32, String>,
31
32    /// Items
33    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
34    pub items: BTreeMap<u32, String>,
35
36    /// States
37    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
38    pub states: BTreeMap<u32, String>,
39
40    /// Troops
41    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
42    pub troops: BTreeMap<u32, String>,
43
44    /// Armors
45    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
46    pub armors: BTreeMap<u32, String>,
47
48    /// Classes
49    #[serde(default, deserialize_with = "deserialize_u32_key_btree_map")]
50    pub classes: BTreeMap<u32, String>,
51}
52
53impl Config {
54    /// Load this from a path.
55    pub fn from_path<P>(path: P) -> anyhow::Result<Self>
56    where
57        P: AsRef<Path>,
58    {
59        let data = std::fs::read_to_string(path)?;
60        let data: Self = toml::from_str(&data)?;
61        Ok(data)
62    }
63
64    /// Get a switch name
65    pub fn get_switch_name(&self, id: u32) -> String {
66        self.switches
67            .get(&id)
68            .map(|name| name.to_string())
69            .unwrap_or_else(|| format!("game_switch_{id}"))
70    }
71
72    /// Get a variable name
73    pub fn get_variable_name(&self, id: u32) -> String {
74        self.variables
75            .get(&id)
76            .map(|name| name.to_string())
77            .unwrap_or_else(|| format!("game_variable_{id}"))
78    }
79
80    /// Get a common event name
81    pub fn get_common_event_name(&self, id: u32) -> String {
82        self.common_events
83            .get(&id)
84            .map(|name| name.to_string())
85            .unwrap_or_else(|| format!("common_event_{id}"))
86    }
87
88    /// Get an actor name
89    pub fn get_actor_name(&self, id: u32) -> String {
90        self.actors
91            .get(&id)
92            .map(|name| name.to_string())
93            .unwrap_or_else(|| format!("game_actor_{id}"))
94    }
95
96    /// Get a skill name
97    pub fn get_skill_name(&self, id: u32) -> String {
98        self.skills
99            .get(&id)
100            .map(|name| name.to_string())
101            .unwrap_or_else(|| format!("game_skill_{id}"))
102    }
103
104    /// Get an item name
105    pub fn get_item_name(&self, id: u32) -> String {
106        self.items
107            .get(&id)
108            .map(|name| name.to_string())
109            .unwrap_or_else(|| format!("game_item_{id}"))
110    }
111
112    /// Get a state name
113    pub fn get_state_name(&self, id: u32) -> String {
114        self.states
115            .get(&id)
116            .map(|name| name.to_string())
117            .unwrap_or_else(|| format!("game_state_{id}"))
118    }
119
120    /// Get a troop name
121    pub fn get_troop_name(&self, id: u32) -> String {
122        self.troops
123            .get(&id)
124            .map(|name| name.to_string())
125            .unwrap_or_else(|| format!("game_troop_{id}"))
126    }
127
128    /// Get an armor name
129    pub fn get_armor_name(&self, id: u32) -> String {
130        self.armors
131            .get(&id)
132            .map(|name| name.to_string())
133            .unwrap_or_else(|| format!("game_armor_{id}"))
134    }
135
136    /// Get a class name
137    pub fn get_class_name(&self, id: u32) -> String {
138        self.classes
139            .get(&id)
140            .map(|name| name.to_string())
141            .unwrap_or_else(|| format!("game_class_{id}"))
142    }
143}
144
145fn deserialize_u32_key_btree_map<'de, D, V>(deserializer: D) -> Result<BTreeMap<u32, V>, D::Error>
146where
147    D: serde::Deserializer<'de>,
148    V: serde::Deserialize<'de>,
149{
150    let map: BTreeMap<String, V> = serde::Deserialize::deserialize(deserializer)?;
151
152    map.into_iter()
153        .map(|(key, value)| {
154            let key: u32 = key.parse().map_err(D::Error::custom)?;
155            Ok((key, value))
156        })
157        .collect()
158}