-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.lua
287 lines (273 loc) · 7.1 KB
/
config.lua
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
--[[
|| config.lua
|| Helpers to manage the configuration file.
||
|| Part of the Firearms Modpack for Minetest.
|| Copyright (C) 2013 Diego Martínez <kaeza>
|| See `LICENSE.txt' for details.
--]]
firearms.config = { }
local conf_name = minetest.get_worldpath().."/firearms.conf"
local conf = Settings(conf_name)
local conf_dirty = false
--[[
| get(name, def)
|
| Gets the value for the specified configuration variable.
|
| Arguments:
| name Configuration variable name.
| def Default value if variable is not defined.
|
| Return value:
| Value of the variable as a string, or `def' if the
| variable does not exist.
--]]
function firearms.config.get(name, def)
local v = conf:get(name)
if (not v) or (v == "") then
name = ("%s.%s"):format(minetest.get_current_modname(), name)
v = minetest.setting_get(name)
end
if (not v) or (v == "") then v = def end
return v
end
--[[
| get_bool(name, def)
|
| Gets the value for the specified configuration variable.
|
| Arguments:
| name Configuration variable name.
| def Default value if variable is not defined.
|
| Return value:
| Value of the variable converted to a boolean, or `def'
| if the variable does not exist.
--]]
function firearms.config.get_bool(name, def)
local v = (conf:get(name) or ""):lower()
if v == "true" then
return true
elseif v == "false" then
return false
else
v = tonumber(v)
if v then
return (v ~= 0)
else
return def
end
end
end
--[[
| get_table(name, def)
|
| Gets the value for the specified configuration variable.
| This assumes that the value was stored with `set_table'.
|
| Arguments:
| name Configuration variable name.
| def Default value if variable is not defined.
|
| Return value:
| Value of the variable deserialized into a table, or
| `def' if the variable does not exist or cannot be
| parsed.
--]]
function firearms.config.get_table(name, def)
local v = firearms.config.get(name)
if not v then return def end
local ok
ok, v = pcall(minetest.deserialize, v)
if not ok then return def end
return v
end
--[[
| get_list(name, def)
|
| Gets the value for the specified configuration variable.
| This assumes that the value was stored with `set_list',
| or `set_table_as_list',.
|
| Arguments:
| name Configuration variable name.
| def Default value if variable is not defined.
|
| Return value:
| Value of the variable as a table, or `def' if the
| variable does not exist or cannot be parsed.
--]]
function firearms.config.get_list(name, def)
local v = firearms.config.get(name)
if not v then return def end
return v:split(",")
end
--[[
| get_list_as_table(name, def)
|
| Gets the value for the specified configuration variable.
| This assumes that the value was stored with `set_list',
| or `set_table_as_list',.
|
| Arguments:
| name Configuration variable name.
| def Default value if variable is not defined.
|
| Return value:
| Value of the variable as a table, or `def' if the
| variable does not exist or cannot be parsed.
--]]
function firearms.config.get_list_as_table(name, def)
local v = firearms.config.get(name)
if not v then return def end
local t
for _, nm in ipairs(v:split(",")) do
t[nm] = true
end
return t
end
--[[
| set(name, value)
|
| Modifies the value for the specified configuration variable.
| The value is stored plainly; no special formatting is done.
|
| The resulting line in `firearms.conf' is as follows:
| name = value
|
| Arguments:
| name Configuration variable name.
| value New value.
|
| Return value:
| None.
--]]
function firearms.config.set(name, value)
conf_dirty = true
conf:set(name, value)
end
--[[
| set_bool(name, value)
|
| Modifies the value for the specified configuration variable.
| The value may be of any type. If it is a boolean, it's string
| representation is stored directly; if it is a number, "false"
| is stored if it equals zero, else "true" is stored; strings
| are considered false if they equal "false" (case insensitive),
| else they a are considered true; other values are considered
| true if they are not nil.
|
| The resulting line in `firearms.conf' is as follows:
| name = value
|
| Arguments:
| name Configuration variable name.
| value New value.
|
| Return value:
| None.
--]]
function firearms.config.set_bool(name, value)
if type(value) == "boolean" then
value = tostring(value)
elseif type(value) == "number" then
value = tostring(value ~= 0)
elseif type(value) == "string" then
value = tostring(value:lower() ~= "false")
else
value = tostring(value ~= nil)
end
firearms.config.set(name, value)
end
--[[
| set_table(name, value)
|
| Modifies the value for the specified configuration variable.
| The value must be a table, and `minetest.serialize' is called
| to turn it into a string.
|
| The resulting line in `firearms.conf' is as follows:
| name = return { ... }
|
| Arguments:
| name Configuration variable name.
| value New value.
|
| Return value:
| None.
--]]
function firearms.config.set_table(name, value)
local ok, v = pcall(minetest.serialize, value)
if not ok then
minetest.log("warning", ("error serializing table: %s"):format(name, v))
return
end
firearms.config.set(name, v)
end
--[[
| set_table_as_list(name, value)
|
| Modifies the value for the specified configuration variable.
| The value must be a table, and every field which has true
| truth value is added to a list. The final value stored is
| a comma separated list of values.
|
| The resulting line in `firearms.conf' is as follows:
| name = foo,bar,baz
|
| Arguments:
| name Configuration variable name.
| value New value.
|
| Return value:
| None.
--]]
function firearms.config.set_table_as_list(name, value)
local l = { }
for k, v in pairs(value) do
if v then table.insert(l, k) end
end
firearms.config.set(name, table.concat(l, ","))
end
--[[
| set_list(name, value)
|
| Modifies the value for the specified configuration variable.
| The value must be an array. The final value stored is a comma
| separated list of values.
|
| The resulting line in `firearms.conf' is as follows:
| name = foo,bar,baz
|
| Arguments:
| name Configuration variable name.
| value New value.
|
| Return value:
| None.
--]]
function firearms.config.set_list(name, value)
firearms.config.set(name, table.concat(value, ","))
end
--[[
| save()
|
| Saves changes back to configuration file. This function
| has no effect if the configuration was not modified since
| the last save. Also, it only saves the `firearms.conf'
| file; it does not touch `minetest.conf'.
|
| Arguments:
| None.
|
| Return value:
| None.
--]]
function firearms.config.save()
if not conf_dirty then
minetest.log("info", "config is not modified; not saving")
elseif not conf:write() then
minetest.log("warning", ("error writing config file to `%s'"):format(conf_name))
end
end