Skip to main content

factorio_api/
settings.rs

1/// Typed Factorio mod-settings tables (`settings.startup`, ...).
2///
3/// Prefer [`SettingsDictionary::get_bool`] / [`get_int`] / [`get_double`] /
4/// [`get_string`] over indexing into opaque values.
5
6/// One mod setting entry (`settings.startup["name"]`).
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct ModSettingValue {
9    pub value: crate::LuaAny,
10}
11
12pub static UNIT_MOD_SETTING: ModSettingValue = ModSettingValue {
13    value: crate::LuaAny,
14};
15
16/// A settings stage dictionary (`startup` / `global` / `player_default`).
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
18pub struct SettingsDictionary;
19
20impl SettingsDictionary {
21    /// Read a bool setting: `settings.startup["name"].value`.
22    #[must_use]
23    pub const fn get_bool(self, _name: &'static str) -> bool {
24        false
25    }
26
27    /// Read an integer setting.
28    #[must_use]
29    pub const fn get_int(self, _name: &'static str) -> i64 {
30        0
31    }
32
33    /// Read a double setting.
34    #[must_use]
35    pub const fn get_double(self, _name: &'static str) -> f64 {
36        0.0
37    }
38
39    /// Read a string setting.
40    #[must_use]
41    pub const fn get_string(self, _name: &'static str) -> &'static str {
42        ""
43    }
44
45    /// Fall back to the generic typed read used by older mods.
46    #[must_use]
47    pub const fn get<T: crate::SettingValue>(self, _name: &'static str) -> T {
48        T::STUB
49    }
50
51    /// Index into a setting entry (`.value` still opaque for uncommon types).
52    #[must_use]
53    pub fn setting(self, _name: &'static str) -> ModSettingValue {
54        UNIT_MOD_SETTING
55    }
56}
57
58impl std::ops::Index<&str> for SettingsDictionary {
59    type Output = ModSettingValue;
60
61    fn index(&self, _key: &str) -> &ModSettingValue {
62        &UNIT_MOD_SETTING
63    }
64}
65
66pub struct SettingTable {
67    pub startup: SettingsDictionary,
68    pub global: SettingsDictionary,
69    pub player_default: SettingsDictionary,
70}
71
72pub const settings: SettingTable = SettingTable {
73    startup: SettingsDictionary,
74    global: SettingsDictionary,
75    player_default: SettingsDictionary,
76};
77
78pub struct LuaDataInterface;
79
80impl LuaDataInterface {
81    /// Register one or more prototype definitions. Translates to `data:extend({...})`.
82    #[allow(unused_variables)]
83    pub fn extend<T, I: IntoIterator<Item = T>>(&self, items: I) {}
84}
85
86/// The global `data` object used to register prototypes and settings.
87pub static data: LuaDataInterface = LuaDataInterface;
88
89pub struct BoolSetting {
90    /// Internal mod-namespaced name (e.g. `"my-mod-enabled"`).
91    pub name: &'static str,
92    /// When the setting takes effect: `"startup"`, `"runtime-global"`, or `"runtime-per-user"`.
93    pub setting_type: &'static str,
94    /// The default value for this setting.
95    pub default_value: bool,
96}
97
98pub struct IntSetting {
99    /// Internal mod-namespaced name (e.g. `"my-mod-count"`).
100    pub name: &'static str,
101    /// When the setting takes effect: `"startup"`, `"runtime-global"`, or `"runtime-per-user"`.
102    pub setting_type: &'static str,
103    /// The default value for this setting.
104    pub default_value: i64,
105    /// Optional minimum allowed value.
106    pub minimum_value: Option<i64>,
107    /// Optional maximum allowed value.
108    pub maximum_value: Option<i64>,
109}
110
111pub struct DoubleSetting {
112    /// Internal mod-namespaced name.
113    pub name: &'static str,
114    /// When the setting takes effect: `"startup"`, `"runtime-global"`, or `"runtime-per-user"`.
115    pub setting_type: &'static str,
116    /// The default value for this setting.
117    pub default_value: f64,
118    /// Optional minimum allowed value.
119    pub minimum_value: Option<f64>,
120    /// Optional maximum allowed value.
121    pub maximum_value: Option<f64>,
122}
123
124pub struct StringSetting {
125    /// Internal mod-namespaced name.
126    pub name: &'static str,
127    /// When the setting takes effect: `"startup"`, `"runtime-global"`, or `"runtime-per-user"`.
128    pub setting_type: &'static str,
129    /// The default value for this setting.
130    pub default_value: &'static str,
131    /// If `true`, the value is not shown in-game (useful for internal state).
132    pub hidden: bool,
133}