From 8f8ca55f55cc2566686ae3dbae5359cd35cfacca Mon Sep 17 00:00:00 2001 From: triplefox Date: Wed, 12 Dec 2012 02:24:12 -0800 Subject: [PATCH] add string split+trim functions; add a fancier command line opt-parser; allow custom user conf with "-user=conf.json" --- pkg/base/client_start.lua | 9 +++++- pkg/base/common.lua | 66 +++++++++++++++++++++++++++++++++++++++ pkg/base/main_server.lua | 6 +++- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/pkg/base/client_start.lua b/pkg/base/client_start.lua index 058619c..7125ebf 100644 --- a/pkg/base/client_start.lua +++ b/pkg/base/client_start.lua @@ -38,7 +38,14 @@ if not map_fname then error("server should have sent map name by now") 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("name:", user_config.name) print("kick on join:", user_config.kick_on_join) diff --git a/pkg/base/common.lua b/pkg/base/common.lua index c414f1e..50096bd 100644 --- a/pkg/base/common.lua +++ b/pkg/base/common.lua @@ -341,3 +341,69 @@ end damage_blk = {} players = {max = 32, current = 1} 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 \ No newline at end of file diff --git a/pkg/base/main_server.lua b/pkg/base/main_server.lua index 8fb303a..e3176df 100644 --- a/pkg/base/main_server.lua +++ b/pkg/base/main_server.lua @@ -287,8 +287,12 @@ function server.hook_tick(sec_current, sec_delta) return 0.005 end +-- parse arguments + +local loose, user_toggles, user_settings = parse_commandline_options({...}) + -- load map -map_fname = ... +map_fname = loose[1] map_fname = map_fname or MAP_DEFAULT map_loaded = common.map_load(map_fname, "auto") common.map_set(map_loaded)