From ca69ac28173674eb80f822161ed6379e6070d272 Mon Sep 17 00:00:00 2001 From: sumneko Date: Tue, 16 Apr 2019 17:12:40 +0800 Subject: =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=96=87=E4=BB=B6=E5=85=B3=E8=81=94?= =?UTF-8?q?=E6=9D=A5=E6=90=9C=E7=B4=A2lua=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/config.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) (limited to 'server/src/config.lua') diff --git a/server/src/config.lua b/server/src/config.lua index 125ac655..5272a964 100644 --- a/server/src/config.lua +++ b/server/src/config.lua @@ -57,7 +57,27 @@ local function Array(checker) end end -local Template = { +local function Hash(keyChecker, valueChecker) + return function (tbl) + if type(tbl) ~= 'table' then + return false + end + local t = {} + for k, v in pairs(tbl) do + local ok1, key = keyChecker(k) + local ok2, value = valueChecker(v) + if ok1 and ok2 then + t[key] = value + end + end + if not next(t) then + return false + end + return true, t + end +end + +local ConfigTemplate = { runtime = { version = {'Lua 5.3', String}, library = {{}, Str2Hash ';'}, @@ -80,33 +100,50 @@ local Template = { } } -local Config +local OtherTemplate = { + associations = {{}, Hash(String, String)}, +} + +local Config, Other local function init() if Config then return end + Config = {} - for c, t in pairs(Template) do + for c, t in pairs(ConfigTemplate) do Config[c] = {} for k, info in pairs(t) do Config[c][k] = info[1] end end + + Other = {} + for k, v in pairs(OtherTemplate) do + Other[k] = v + end end -local function setConfig(self, config) +local function setConfig(self, config, other) pcall(function () for c, t in pairs(config) do for k, v in pairs(t) do - local info = Template[c][k] + local info = ConfigTemplate[c][k] local suc, v = info[2](v) if suc then Config[c][k] = v end end end - log.debug('Config update: ', table.dump(Config)) + for k, v in pairs(other) do + local info = OtherTemplate[k] + local suc, v = info[2](v) + if suc then + Other[k] = v + end + end + log.debug('Config update: ', table.dump(Config), table.dump(Other)) end) end @@ -115,4 +152,5 @@ init() return { setConfig = setConfig, config = Config, + other = Other, } -- cgit v1.2.3