summaryrefslogtreecommitdiff
path: root/script/plugin.lua
blob: 7f2165dbfb9233e336ab821a21e60b7e642a5a17 (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 config = require 'config'
local util   = require 'utility'
local client = require 'client'
local lang   = require 'language'
local await  = require 'await'

---@class plugin
local m = {}

function m.showError(err)
    if m._hasShowedError then
        return
    end
    m._hasShowedError = true
    client.showMessage('Error', lang.script('PLUGIN_RUNTIME_ERROR', m.pluginPath, err))
end

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
    local clock = os.clock()
    tracy.ZoneBeginN('plugin dispatch:' .. event)
    local suc, res1, res2 = xpcall(method, log.error, ...)
    tracy.ZoneEnd()
    local passed = os.clock() - clock
    if passed > 0.1 then
        log.warn(('Call plugin event [%s] takes [%.3f] sec'):format(event, passed))
    end
    if suc then
        return true, res1, res2
    else
        m.showError(res1)
    end
    return false, res1
end

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

---@async
local function checkTrustLoad()
    local filePath = LOGPATH .. '/trusted'
    local trusted = util.loadFile(filePath)
    local lines = {}
    if trusted then
        for line in util.eachLine(trusted) do
            lines[#lines+1] = line
            if line == m.pluginPath then
                return true
            end
        end
    end
    local _, index = client.awaitRequestMessage('Warning', lang.script('PLUGIN_TRUST_LOAD', m.pluginPath), {
        lang.script('PLUGIN_TRUST_YES'),
        lang.script('PLUGIN_TRUST_NO'),
    })
    if not index then
        return false
    end
    lines[#lines+1] = m.pluginPath
    util.saveFile(filePath, table.concat(lines, '\n'))
    return true
end

---@param scp scope
function m.init(scp)
    await.call(function () ---@async
        local ws    = require 'workspace'
        m.interface = {}

        local pluginPath = ws.getAbsolutePath(scp.uri, config.get(scp.uri, 'Lua.runtime.plugin'))
        log.info('plugin path:', pluginPath)
        if not pluginPath then
            return
        end
        local pluginLua = util.loadFile(pluginPath)
        if not pluginLua then
            return
        end
        m.pluginPath = pluginPath
        local env = setmetatable(m.interface, { __index = _ENV })
        local f, err = load(pluginLua, '@'..pluginPath, "t", env)
        if not f then
            log.error(err)
            m.showError(err)
            return
        end
        if not client.isVSCode() and not checkTrustLoad() then
            return
        end
        local suc, err = xpcall(f, log.error, f)
        if not suc then
            m.showError(err)
            return
        end

        ws.resetFiles(scp)
    end)
end

return m