summaryrefslogtreecommitdiff
path: root/server/src/matcher/find_lib.lua
blob: 7d0574c013cf34e96210131f86390e4c1aad4cbe (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
local lni = require 'lni'

local function mergeEnum(lib, locale)
    if not lib or not locale then
        return
    end
    local pack = {}
    for _, enum in ipairs(lib) do
        if enum.enum then
            pack[enum.enum] = enum
        end
    end
    for _, enum in ipairs(locale) do
        if pack[enum.enum] then
            pack[enum.enum].description = enum.description
        end
    end
end

local function mergeField(lib, locale)
    if not lib or not locale then
        return
    end
    local pack = {}
    for _, field in ipairs(lib) do
        if field.field then
            pack[field.field] = field
        end
    end
    for _, field in ipairs(locale) do
        if pack[field.field] then
            pack[field.field].description = field.description
        end
    end
end

local function mergeLocale(libs, locale)
    for name in pairs(locale) do
        if libs[name] then
            libs[name].description = locale[name].description
            mergeEnum(libs[name].enums, locale[name].enums)
            mergeField(libs[name].fields, locale[name].fields)
        end
    end
end

local Libs
local function getLibs()
    if Libs then
        return Libs
    end
    Libs = table.container()
    for path in io.scan(ROOT / 'libs') do
        local buf = io.load(path)
        if buf then
            xpcall(lni.classics, log.error, buf, path:string(), {Libs})
        end
    end

    local language = require 'language'
    local locale = table.container()
    for path in io.scan(ROOT / 'locale' / language / 'libs') do
        local buf = io.load(path)
        if buf then
            xpcall(lni.classics, log.error, buf, path:string(), {locale})
        end
    end

    mergeLocale(Libs, locale)

    return Libs
end

return function (var)
    local key = var.key
    local libs = getLibs()
    return libs[key], key
end