summaryrefslogtreecommitdiff
path: root/server/src/matcher/library.lua
blob: 989ac1a4aa849b6ced0939c17749b6ec9baf1541 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
local lni = require 'lni'
local fs = require 'bee.filesystem'

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)
    if not libs or not locale then
        return
    end
    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 function mergeLibs(target, libs)
    if not libs then
        return
    end
    for name, lib in pairs(libs) do
        target.names[#target.names+1] = name
        target.libs[#target.libs+1] = lib
    end
end

local function loadLocale(language, relative)
    local localePath = ROOT / 'locale' / language / relative
    local localeBuf = io.load(localePath)
    if localeBuf then
        local locale = table.container()
        xpcall(lni.classics, log.error, localeBuf, localePath:string(), {locale})
        return locale
    end
    return nil
end

local function init()
    local language = require 'language'
    local alllibs = setmetatable({
        names = {},
        libs = {},
    }, {
        __pairs = function (self)
            local i = 0
            return function ()
                i = i + 1
                return self.names[i], self.libs[i]
            end
        end,
    })
    for path in io.scan(ROOT / 'libs') do
        local libs
        local buf = io.load(path)
        if buf then
            libs = table.container()
            xpcall(lni.classics, log.error, buf, path:string(), {libs})
        end
        local relative = fs.relative(path, ROOT)

        local locale = loadLocale('en-US', relative)
        mergeLocale(libs, locale)
        if language ~= 'en-US' then
            locale = loadLocale(language, relative)
            mergeLocale(libs, locale)
        end
        mergeLibs(alllibs, libs)
    end

    return alllibs
end

return init()