summaryrefslogtreecommitdiff
path: root/script/plugin.lua
blob: 369afd66af66ee839d0bad5b5644f85a52e1c804 (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
local config = require 'config'
local fs     = require 'bee.filesystem'
local fsu    = require 'fs-utility'
local await  = require "await"

---@class plugin
local m = {}
m.waitingReady = {}

function m.dispatch(event, ...)
    if not m.interface then
        return false
    end
    local method = m.interface[event]
    if type(method) ~= 'function' then
        return false
    end
    tracy.ZoneBeginN('plugin dispatch:' .. event)
    local suc, res1, res2 = xpcall(method, log.error, ...)
    tracy.ZoneEnd()
    if suc then
        return true, res1, res2
    end
    return false, res1
end

function m.isReady()
    return m.interface ~= nil
end

function m.awaitReady()
    if m.isReady() then
        return
    end
    await.wait(function (waker)
        m.waitingReady[#m.waitingReady+1] = waker
    end)
end

function m.init()
    local ws    = require 'workspace'
    m.interface = {}

    local _ <close> = function ()
        local waiting = m.waitingReady
        m.waitingReady = {}
        for _, waker in ipairs(waiting) do
            waker()
        end
    end

    local pluginPath = fs.path(config.config.runtime.plugin)
    if pluginPath:is_relative() then
        if not ws.path then
            return
        end
        pluginPath = fs.path(ws.path) / pluginPath
    end
    local pluginLua = fsu.loadFile(pluginPath)
    if not pluginLua then
        return
    end
    local env = setmetatable(m.interface, { __index = _ENV })
    local f, err = load(pluginLua, '@'..pluginPath:string(), "t", env)
    if not f then
        log.error(err)
        return
    end
    xpcall(f, log.error, f)
end

return m