-
-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathtoml_configuration.dm
More file actions
266 lines (233 loc) · 9.3 KB
/
toml_configuration.dm
File metadata and controls
266 lines (233 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//* This file is explicitly licensed under the MIT license. *//
//* Copyright (c) 2024 Citadel Station Developers *//
// todo: maybe rename to config? or keep it as Configuration to keep with naming scheme of other 'system / backend' modules like the MC?
GLOBAL_REAL(Configuration, /datum/controller/toml_configuration)
// todo: /datum/controller/configuration
// todo: needs stronger vv guarding; it still exposes list refs.
/datum/controller/toml_configuration
name = "TOML Configuration" // NAME YOUR FUCKING CONTROLLERS
/// Entries by type.
VAR_PRIVATE/list/datum/toml_config_entry/typed_entries
/// Entries as same structure as the underlying toml/json
VAR_PRIVATE/list/datum/toml_config_entry/keyed_entries
/// TODO: database whitelist when this is stupid
/// species id = list(ckeys)
VAR_PRIVATE/list/species_whitelist
/// TODO: database whitelist when this is stupid
/// language id = list(ckeys)
VAR_PRIVATE/list/language_whitelist
/// TODO: database whitelist when this is stupid
/// role id = list(ckeys)
VAR_PRIVATE/list/role_whitelist
/datum/controller/toml_configuration/CanProcCall(procname)
switch(procname)
if(NAMEOF_PROC(src, New), NAMEOF_PROC(src, Destroy), NAMEOF_PROC(src, Initialize))
return FALSE
if(NAMEOF_PROC(src, get_entry), NAMEOF_PROC(src, set_entry))
return FALSE
if(NAMEOF_PROC(src, get_sensitive_entry), NAMEOF_PROC(src, set_sensitive_entry))
return FALSE
if(NAMEOF_PROC(src, reload))
return FALSE
if(NAMEOF_PROC(src, reset), NAMEOF_PROC(src, load), NAMEOF_PROC(src, load_impl))
return FALSE
if(NAMEOF_PROC(src, reset_whitelist), NAMEOF_PROC(src, load_whitelist), NAMEOF_PROC(src, load_whitelist_impl))
return FALSE
return ..()
/datum/controller/toml_configuration/vv_edit_var(var_name, var_value, mass_edit, raw_edit)
switch(var_name)
if(NAMEOF(src, keyed_entries))
return FALSE
if(NAMEOF(src, typed_entries))
return FALSE
return ..()
/datum/controller/toml_configuration/New()
if(Configuration != src)
if(Configuration)
qdel(Configuration)
Configuration = src
/datum/controller/toml_configuration/Initialize()
keyed_entries = list()
typed_entries = list()
for(var/datum/toml_config_entry/path as anything in typesof(/datum/toml_config_entry))
if(initial(path.abstract_type) == path)
continue
var/datum/toml_config_entry/entry = new path
typed_entries[entry.type] = entry
var/list/nesting = splittext(entry.category, ".")
var/list/current_list = keyed_entries
for(var/i in 1 to length(nesting))
LAZYINITLIST(current_list[nesting[i]])
current_list = current_list[nesting[i]]
current_list[entry.key] = entry
reload()
/datum/controller/toml_configuration/stat_key()
return "Configuration (New):"
/datum/controller/toml_configuration/stat_entry()
return "Edit"
/**
* HEY! LISTEN! By calling this proc you are affirming that:
*
* * The entry type you are passing in is static and not a variable that can be tampered with.
* * The value you get will be immediately consumed in a non-VV-able manner.
*/
/datum/controller/toml_configuration/proc/get_sensitive_entry(datum/toml_config_entry/entry_type)
// todo: cache / optimize maybe? would help to store everything in a vv-hidden list.
var/datum/toml_config_entry/entry = typed_entries[entry_type]
if(!entry)
return
if(!entry.sensitive)
CRASH("attempted to get sensitive entry with sensitive get entry.")
return entry.value
/**
* HEY! LISTEN! By calling this proc you are affirming that:
*
* * The entry type you are passing in is static and not a variable that can be tampered with.
* * The value you are passing in is trusted and validated and not a variable that can be tampered with.
*/
/datum/controller/toml_configuration/proc/set_sensitive_entry(datum/toml_config_entry/entry_type, value)
// todo: cache / optimize maybe? would help to store everything in a vv-hidden list.
var/datum/toml_config_entry/entry = typed_entries[entry_type]
if(!entry)
return
if(entry.sensitive)
CRASH("attempted to set non-sensitive entry with sensitive set entry.")
entry.value = value
/datum/controller/toml_configuration/proc/get_entry(datum/toml_config_entry/entry_type)
// todo: cache / optimize maybe? would help to store everything in a vv-hidden list.
var/datum/toml_config_entry/entry = typed_entries[entry_type]
if(!entry)
return
if(entry.sensitive)
CRASH("attempted to get sensitive entry with normal get entry.")
return entry.value
/datum/controller/toml_configuration/proc/set_entry(datum/toml_config_entry/entry_type, value)
// todo: cache / optimize maybe? would help to store everything in a vv-hidden list.
var/datum/toml_config_entry/entry = typed_entries[entry_type]
if(!entry)
return
if(entry.sensitive)
CRASH("attempted to set sensitive entry with normal set entry.")
entry.value = value
/datum/controller/toml_configuration/proc/admin_reload()
reload()
/**
* Automatically loads default config, and the server's config file.
*
* todo: allow for overriding directories
*/
/datum/controller/toml_configuration/proc/reload()
reset()
load("config.default/config.toml")
if(fexists("config/config.toml"))
load("config/config.toml")
reset_whitelist()
load_whitelist("config.default/whitelist.toml")
if(fexists("config/whitelist.toml"))
load_whitelist("config/whitelist.toml")
/**
* Resets the configuration.
*/
/datum/controller/toml_configuration/proc/reset()
for(var/path in typed_entries)
var/datum/toml_config_entry/entry = typed_entries[path]
entry.reset()
/**
* Loads from a given layer.
* * This will not reset the configuration. Repeated calls to load will allow for layered configuration.
*
* HEY! LISTEN! By calling this proc you are affirming that:
* * The file you are passing in is trusted and not a variable that can be tampered with via VV.
*/
/datum/controller/toml_configuration/proc/load(filelike)
var/list/decoded
if(istext(filelike))
if(!fexists(filelike))
CRASH("failed to load [filelike]: does not exist")
decoded = rustg_read_toml_file(filelike)
else if(isfile(filelike))
// noa path, it might be rsc cache; rust_g can't read that directly.
fdel("tmp/config/loading.toml")
fcopy(filelike, "tmp/config/loading.toml")
decoded = rustg_read_toml_file("tmp/config/loading.toml")
fdel("tmp/config/loading.toml")
if(!decoded)
CRASH("failed to decode config [filelike]!")
load_impl(decoded, keyed_entries)
/datum/controller/toml_configuration/proc/load_impl(list/decoded_list, list/entry_list)
if(!decoded_list || !entry_list)
return
for(var/key in decoded_list)
var/value = decoded_list[key]
if(islist(value))
var/list/next_entry_list = entry_list[key]
if(!islist(next_entry_list))
// todo: warn
else
load_impl(value, next_entry_list[key])
else
var/datum/toml_config_entry/entry = entry_list[key]
if(!istype(entry))
// todo: warn
else
entry.apply(value)
/datum/controller/toml_configuration/proc/reset_whitelist()
role_whitelist = list()
species_whitelist = list()
language_whitelist = list()
/datum/controller/toml_configuration/proc/load_whitelist(filelike)
var/list/decoded
if(istext(filelike))
if(!fexists(filelike))
CRASH("failed to load [filelike]: does not exist")
decoded = rustg_read_toml_file(filelike)
else if(isfile(filelike))
// no path, it might be rsc cache; rust_g can't read that directly.
fdel("tmp/config/loading.toml")
fcopy(filelike, "tmp/config/loading.toml")
decoded = rustg_read_toml_file("tmp/config/loading.toml")
fdel("tmp/config/loading.toml")
if(!decoded)
CRASH("failed to decode config [filelike]!")
load_whitelist_impl(decoded)
/datum/controller/toml_configuration/proc/load_whitelist_impl(list/decoded_list)
if(!decoded_list)
return
var/list/role_whitelist = sanitize_islist(deep_copy_list(decoded_list["whitelist"]?["role"]))
var/list/language_whitelist = sanitize_islist(deep_copy_list(decoded_list["whitelist"]?["language"]))
var/list/species_whitelist = sanitize_islist(deep_copy_list(decoded_list["whitelist"]?["species"]))
// fixup: make everything ckeys
for(var/list/root_list as anything in list(
role_whitelist,
language_whitelist,
species_whitelist,
))
for(var/i in 1 to length(root_list))
var/id = root_list[i]
var/list/ckey_list = root_list[id]
for(var/j in 1 to length(ckey_list))
ckey_list[j] = ckey(ckey_list[j])
src.role_whitelist = merge_2_nested_list(src.role_whitelist, role_whitelist)
src.language_whitelist = merge_2_nested_list(src.language_whitelist, language_whitelist)
src.species_whitelist = merge_2_nested_list(src.species_whitelist, species_whitelist)
/datum/controller/toml_configuration/proc/check_species_whitelist(id, ckey)
return ckey(ckey) in species_whitelist[id]
/datum/controller/toml_configuration/proc/check_role_whitelist(id, ckey)
return ckey(ckey) in role_whitelist[id]
/datum/controller/toml_configuration/proc/check_language_whitelist(id, ckey)
return ckey(ckey) in language_whitelist[id]
/datum/controller/toml_configuration/proc/get_all_species_whitelists_for_ckey(ckey)
. = list()
for(var/id in species_whitelist)
if(ckey in species_whitelist[id])
. += id
/datum/controller/toml_configuration/proc/get_all_role_whitelists_for_ckey(ckey)
. = list()
for(var/id in role_whitelist)
if(ckey in role_whitelist[id])
. += id
/datum/controller/toml_configuration/proc/get_all_language_whitelists_for_ckey(ckey)
. = list()
for(var/id in language_whitelist)
if(ckey in language_whitelist[id])
. += id