-
Notifications
You must be signed in to change notification settings - Fork 7
/
GameSettings.lua
392 lines (291 loc) · 8.08 KB
/
GameSettings.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
--[[
GameSettings.lua
Game Settings Manager
Copyright (c) 2021 psiberx
]]
local GameSettings = { version = '1.0.4' }
local module = {}
function module.parsePath(setting)
return setting:match('^(/.+)/([A-Za-z0-9_]+)$')
end
function module.makePath(groupPath, varName)
return groupPath .. '/' .. varName
end
function module.isBoolType(target)
if type(target) == 'userdata' then
target = target.value
end
return target == 'Bool'
end
function module.isNameType(target)
if type(target) == 'userdata' then
target = target.value
end
return target == 'Name' or target == 'NameList'
end
function module.isNumberType(target)
if type(target) == 'userdata' then
target = target.value
end
return target == 'Int' or target == 'Float'
end
function module.isIntType(target)
if type(target) == 'userdata' then
target = target.value
end
return target == 'Int' or target == 'IntList'
end
function module.isFloatType(target)
if type(target) == 'userdata' then
target = target.value
end
return target == 'Float' or target == 'FloatList'
end
function module.isListType(target)
if type(target) == 'userdata' then
target = target.value
end
return target == 'IntList' or target == 'FloatList' or target == 'StringList' or target == 'NameList'
end
function module.exportVar(var)
local output = {}
output.path = module.makePath(Game.NameToString(var:GetGroupPath()), Game.NameToString(var:GetName()))
output.value = var:GetValue()
output.type = var:GetType().value
if module.isNameType(output.type) then
output.value = Game.NameToString(output.value)
end
if module.isNumberType(output.type) then
output.min = var:GetMinValue()
output.max = var:GetMaxValue()
output.step = var:GetStepValue()
end
if module.isListType(output.type) then
output.index = var:GetIndex() + 1
output.options = var:GetValues()
if module.isNameType(output.type) then
for i, option in ipairs(output.options) do
output.options[i] = Game.NameToString(option)
end
end
end
return output
end
function module.exportVars(isPreGame, group, output)
if type(group) ~= 'userdata' then
group = Game.GetSettingsSystem():GetRootGroup()
end
if type(isPreGame) ~= 'bool' then
isPreGame = GetSingleton('inkMenuScenario'):GetSystemRequestsHandler():IsPreGame()
end
if not output then
output = {}
end
for _, var in ipairs(group:GetVars(isPreGame)) do
table.insert(output, module.exportVar(var))
end
for _, child in ipairs(group:GetGroups(isPreGame)) do
module.exportVars(isPreGame, child, output)
end
table.sort(output, function(a, b)
return a.path < b.path
end)
return output
end
function GameSettings.Has(setting)
local path, name = module.parsePath(setting)
return Game.GetSettingsSystem():HasVar(path, name)
end
function GameSettings.Var(setting)
local path, name = module.parsePath(setting)
local var = Game.GetSettingsSystem():GetVar(path, name)
if not var then
return nil
end
return module.exportVar(var)
end
function GameSettings.Get(setting)
local path, name = module.parsePath(setting)
local var = Game.GetSettingsSystem():GetVar(path, name)
if not var then
return nil
end
return var:GetValue()
end
function GameSettings.GetIndex(setting)
local path, name = module.parsePath(setting)
local var = Game.GetSettingsSystem():GetVar(path, name)
if not var or not module.isListType(var:GetType()) then
return nil
end
return var:GetIndex() + 1
end
function GameSettings.Set(setting, value)
local path, name = module.parsePath(setting)
local var = Game.GetSettingsSystem():GetVar(path, name)
if not var then
return
end
if module.isListType(var:GetType()) then
local index = var:GetIndexFor(value)
if index then
var:SetIndex(index)
end
else
var:SetValue(value)
end
end
function GameSettings.SetIndex(setting, index)
local path, name = module.parsePath(setting)
local var = Game.GetSettingsSystem():GetVar(path, name)
if not var or not module.isListType(var:GetType()) then
return
end
var:SetIndex(index - 1)
end
function GameSettings.Toggle(setting)
local path, name = module.parsePath(setting)
local var = Game.GetSettingsSystem():GetVar(path, name)
if not var or not module.isBoolType(var:GetType()) then
return
end
var:Toggle()
end
function GameSettings.ToggleAll(settings)
local state = not GameSettings.Get(settings[1])
for _, setting in ipairs(settings) do
GameSettings.Set(setting, state)
end
end
function GameSettings.ToggleGroup(path)
local group = Game.GetSettingsSystem():GetGroup(path)
local vars = group:GetVars(false)
local state = nil
for _, var in ipairs(vars) do
if module.isBoolType(var:GetType()) then
-- Invert the first bool option
if state == nil then
state = not var:GetValue()
end
var:SetValue(state)
end
end
end
-- set all booleans in the group to val
function GameSettings.SetGroupBool(path, val)
local group = Game.GetSettingsSystem():GetGroup(path)
local vars = group:GetVars(false)
for _, var in ipairs(vars) do
if module.isBoolType(var:GetType()) then
var:SetValue(val)
end
end
end
function GameSettings.Options(setting)
local path, name = module.parsePath(setting)
local var = Game.GetSettingsSystem():GetVar(path, name)
if not var or not module.isListType(var:GetType()) then
return nil
end
return var:GetValues(), var:GetIndex() + 1
end
function GameSettings.Reset(setting)
local path, name = module.parsePath(setting)
local var = Game.GetSettingsSystem():GetVar(path, name)
if not var then
return
end
var:RestoreDefault()
end
function GameSettings.NeedsConfirmation()
return Game.GetSettingsSystem():NeedsConfirmation()
end
function GameSettings.NeedsReload()
return Game.GetSettingsSystem():NeedsLoadLastCheckpoint()
end
function GameSettings.NeedsRestart()
return Game.GetSettingsSystem():NeedsRestartToApply()
end
function GameSettings.Confirm()
Game.GetSettingsSystem():ConfirmChanges()
end
function GameSettings.Reject()
Game.GetSettingsSystem():RejectChanges()
end
function GameSettings.Save()
GetSingleton('inkMenuScenario'):GetSystemRequestsHandler():RequestSaveUserSettings()
end
function GameSettings.ExportVars(isPreGame, group, output)
return module.exportVars(isPreGame, group, output)
end
function GameSettings.Export(isPreGame)
return module.exportVars(isPreGame)
end
function GameSettings.ExportTo(exportPath, isPreGame)
local output = {}
local vars = module.exportVars(isPreGame)
for _, var in ipairs(vars) do
local value = var.value
local options
if type(value) == 'string' then
value = string.format('%q', value)
end
if var.options and #var.options > 1 then
options = {}
for i, option in ipairs(var.options) do
--if type(option) == 'string' then
-- option = string.format('%q', option)
--end
options[i] = option
end
options = ' -- ' .. table.concat(options, ' | ')
elseif var.min then
if module.isIntType(var.type) then
options = (' -- %d to %d / %d'):format(var.min, var.max, var.step)
else
options = (' -- %.2f to %.2f / %.2f'):format(var.min, var.max, var.step)
end
end
table.insert(output, (' ["%s"] = %s,%s'):format(var.path, value, options or ''))
end
table.insert(output, 1, '{')
table.insert(output, '}')
output = table.concat(output, '\n')
if exportPath then
if not exportPath:find('%.lua$') then
exportPath = exportPath .. '.lua'
end
local exportFile = io.open(exportPath, 'w')
if exportFile then
exportFile:write('return ')
exportFile:write(output)
exportFile:close()
end
else
return output
end
end
function GameSettings.Import(settings)
for setting, value in pairs(settings) do
GameSettings.Set(setting, value)
end
end
function GameSettings.ImportFrom(importPath)
local importChunk = loadfile(importPath)
if importChunk then
GameSettings.Import(importChunk())
end
end
-- For importing the result of ExportVars directly
function GameSettings.ImportVars(settings)
for _, var in ipairs(settings) do
GameSettings.Set(var.path, var.value)
end
end
function GameSettings.DumpVars(isPreGame)
return GameSettings.ExportTo(nil, isPreGame)
end
function GameSettings.PrintVars(isPreGame)
print(GameSettings.DumpVars(isPreGame))
end
return GameSettings