summaryrefslogtreecommitdiff
path: root/script/config/loader.lua
blob: 8310edfe8def042b7f1ce73123e7601103439e9f (plain)
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
local fs     = require 'bee.filesystem'
local fsu    = require 'fs-utility'
local json   = require 'json'
local proto  = require 'proto'
local lang   = require 'language'
local config = require 'config.config'

local function errorMessage(msg)
    proto.notify('window/showMessage', {
        type = 3,
        message = msg,
    })
end

local m = {}

function m.loadLocalConfig(filename)
    local path = fs.path(filename)
    local ext  = path:extension():string():lower()
    local buf  = fsu.loadFile(path)
    if not buf then
        errorMessage(lang.script('无法读取设置文件:{}', filename))
        return
    end
    if ext == '.json' then
        local suc, res = pcall(json.decode, buf)
        if not suc then
            errorMessage(lang.script('设置文件加载错误:{}', res))
            return
        end
        return res
    elseif ext == '.lua' then
        local suc, res = pcall(function ()
            assert(load(buf, '@' .. filename, 't'))()
        end)
        if not suc then
            errorMessage(lang.script('设置文件加载错误:{}', res))
            return
        end
        return res
    else
        errorMessage(lang.script('设置文件必须是lua或json格式:{}', filename))
        return
    end
end

return m