Skip to content

Commit a7a55f4

Browse files
authored
spectate.lua: refactor set_setting()
1 parent d05b2df commit a7a55f4

1 file changed

Lines changed: 50 additions & 47 deletions

File tree

plugins/lua/spectate.lua

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -167,61 +167,64 @@ local function set_setting(args)
167167
if n == 0 then
168168
qerror('missing key')
169169
end
170-
local key = table.remove(args, 1)
171-
if config[key] == nil then
172-
qerror('unknown setting: ' .. key)
173-
end
174-
n = #args
175-
if n == 0
176-
or (n == 1 and type(config[key]) == 'table')
177-
then
178-
qerror('missing value')
179-
end
180170

181-
if n == 1 then
182-
local value = args[1]
183-
if key == 'follow-seconds' then
184-
value = argparse.positiveInt(value, 'follow-seconds')
185-
elseif key == 'tooltip-follow-blink-milliseconds' then
186-
value = argparse.nonnegativeInt(value, 'tooltip-follow-blink-milliseconds')
171+
local cfg = config
172+
local v
173+
for i = 1, n do
174+
v = cfg[args[i]]
175+
if v == nil then
176+
-- probably an unknown option, but we may allow adding new keys
177+
break
178+
elseif type(v) == 'table' then
179+
if i == n then
180+
-- arrived at the very last argument, but have a table
181+
qerror('missing value for ' .. table.concat(args, '/', 1, i))
182+
end
183+
cfg = v
187184
else
188-
value = argparse.boolean(value, key)
189-
end
190-
191-
config[key] = value
192-
193-
if not key:startswith(lua_only_settings_prefix) then
194-
if type(value) == 'boolean' then
195-
value = value and 1 or 0
185+
-- arrived at something that's not a table
186+
if i == n-1 then
187+
-- if there is exactly 1 argument left, we're good
188+
break
189+
elseif i == n then
190+
qerror('missing value for ' .. table.concat(args, '/', 1, i))
191+
else -- i < n-1 then
192+
qerror('too many arguments for ' .. table.concat(args, '/', 1, i))
196193
end
197-
spectate_setSetting(key, value)
198194
end
199-
else
200-
local errorUnknownSettingIfNil = function(t)
201-
if t == nil then
202-
table.remove(args)
203-
qerror('unknown setting: ' .. key .. '/' .. table.concat(args, '/'))
204-
end
195+
end
196+
if v == nil then
197+
if n == 3 and args[1] == 'tooltip-follow-job-shortenings' then
198+
-- user should be able to add new shortenings, but not other things
199+
else
200+
qerror('unknown option: ' .. table.concat(args, '/', 1, i))
205201
end
202+
end
206203

207-
local t = config[key]
208-
for i = 1, n - 2 do
209-
errorUnknownSettingIfNil(t)
210-
t = t[args[i]]
211-
end
212-
local k = args[n-1]
213-
local v = args[n]
214-
if key ~= 'tooltip-follow-job-shortenings' then
215-
-- user should be able to add new shortenings, but not other things
216-
errorUnknownSettingIfNil(t[k])
217-
if key:endswith('-stress-levels') and key ~= 'tooltip-stress-levels' then
218-
v = argparse.boolean(v, key .. '/' .. k)
219-
end
204+
local path = table.concat(args, '/', 1, n-1)
205+
local key = args[n-1]
206+
local value = args[n]
207+
local entry_type = type(cfg[key])
208+
if entry_type == 'table' then
209+
-- here just in case, is already checked in the loop above
210+
qerror('missing value for ' .. path)
211+
elseif entry_type == 'boolean' then
212+
value = argparse.boolean(value, path)
213+
elseif entry_type == 'number' then
214+
if path == 'follow-seconds' then
215+
value = argparse.positiveInt(value, path)
216+
else
217+
value = argparse.nonnegativeInt(value, path)
220218
end
221-
if type(t[k]) == 'table' then
222-
qerror('missing value')
219+
end
220+
221+
cfg[key] = value
222+
223+
if n == 2 and not key:startswith(lua_only_settings_prefix) then
224+
if type(value) == 'boolean' then
225+
value = value and 1 or 0
223226
end
224-
t[k] = v
227+
spectate_setSetting(key, value)
225228
end
226229

227230
save_state()

0 commit comments

Comments
 (0)