add string split+trim functions; add a fancier command line opt-parser; allow custom user conf with "-user=conf.json"
This commit is contained in:
parent
3f42bb4031
commit
8f8ca55f55
@ -38,7 +38,14 @@ if not map_fname then
|
|||||||
error("server should have sent map name by now")
|
error("server should have sent map name by now")
|
||||||
end
|
end
|
||||||
|
|
||||||
user_config = common.json_load("clsave/pub/user.json")
|
local loose, user_toggles, user_settings = parse_commandline_options({...})
|
||||||
|
|
||||||
|
local user_config_filename = user_settings['user'] or
|
||||||
|
"clsave/pub/user.json"
|
||||||
|
|
||||||
|
-- FIXME: we don't expose documentation for valid user settings anywhere
|
||||||
|
|
||||||
|
user_config = common.json_load(user_config_filename)
|
||||||
print("json done!")
|
print("json done!")
|
||||||
print("name:", user_config.name)
|
print("name:", user_config.name)
|
||||||
print("kick on join:", user_config.kick_on_join)
|
print("kick on join:", user_config.kick_on_join)
|
||||||
|
@ -341,3 +341,69 @@ end
|
|||||||
damage_blk = {}
|
damage_blk = {}
|
||||||
players = {max = 32, current = 1}
|
players = {max = 32, current = 1}
|
||||||
intent = {}
|
intent = {}
|
||||||
|
|
||||||
|
function string.split(s, sep, plain)
|
||||||
|
local start = 1
|
||||||
|
local done = false
|
||||||
|
local function pass(i, j, ...)
|
||||||
|
if i then
|
||||||
|
local seg = s:sub(start, i - 1)
|
||||||
|
start = j + 1
|
||||||
|
return seg, ...
|
||||||
|
else
|
||||||
|
done = true
|
||||||
|
return s:sub(start)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local result = {}
|
||||||
|
while not done do
|
||||||
|
if sep == '' then done = true result[#result+1]=s end
|
||||||
|
result[#result+1]=pass(s:find(sep, start, plain))
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
-- trim the character 'sep' from the left hand side of the string
|
||||||
|
function string.triml(s, sep)
|
||||||
|
sep = string.byte(sep)
|
||||||
|
if s == '' then return s end
|
||||||
|
local pos = 1
|
||||||
|
while string.byte(s,pos)==sep and #s<=pos do pos = pos + 1 end
|
||||||
|
return string.sub(s, pos+1)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- trim the character 'sep' from the right hand side of the string
|
||||||
|
function string.trimr(s, sep)
|
||||||
|
sep = string.byte(sep)
|
||||||
|
if s == '' then return s end
|
||||||
|
local pos = #s
|
||||||
|
while string.byte(s, pos)==sep and pos>=1 do pos = pos - 1 end
|
||||||
|
return string.sub(s, 1, pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- trim the character 'sep' from both sides of the string
|
||||||
|
function string.trim(s, sep)
|
||||||
|
return string.triml(string.trimr(s, sep), sep)
|
||||||
|
end
|
||||||
|
|
||||||
|
function parse_commandline_options(options)
|
||||||
|
local user_toggles = {} -- toggle options (key is name, value is position)
|
||||||
|
local user_settings = {} -- key-value pairs
|
||||||
|
local loose = {} -- loose strings, filenames, etc.
|
||||||
|
|
||||||
|
for k, v in pairs(options) do
|
||||||
|
local setting_pair = string.split(v, "=")
|
||||||
|
local first = string.byte(v,1)
|
||||||
|
if first==string.byte('-') then -- we are toggling an option or setting a value
|
||||||
|
if #setting_pair == 2 then -- we are setting a key to a value
|
||||||
|
user_settings[string.triml(setting_pair[1], '-')]=setting_pair[2]
|
||||||
|
print(string.triml(setting_pair[1], '-'),"trimmed")
|
||||||
|
else
|
||||||
|
user_toggles[string.triml(v, '-')]=k
|
||||||
|
end
|
||||||
|
else -- add to the loose values
|
||||||
|
loose[#loose+1] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return loose, user_toggles, user_settings
|
||||||
|
end
|
@ -287,8 +287,12 @@ function server.hook_tick(sec_current, sec_delta)
|
|||||||
return 0.005
|
return 0.005
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- parse arguments
|
||||||
|
|
||||||
|
local loose, user_toggles, user_settings = parse_commandline_options({...})
|
||||||
|
|
||||||
-- load map
|
-- load map
|
||||||
map_fname = ...
|
map_fname = loose[1]
|
||||||
map_fname = map_fname or MAP_DEFAULT
|
map_fname = map_fname or MAP_DEFAULT
|
||||||
map_loaded = common.map_load(map_fname, "auto")
|
map_loaded = common.map_load(map_fname, "auto")
|
||||||
common.map_set(map_loaded)
|
common.map_set(map_loaded)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user