-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPluginManager.lua
More file actions
179 lines (132 loc) · 6.2 KB
/
Copy pathPluginManager.lua
File metadata and controls
179 lines (132 loc) · 6.2 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
-- PluginManager.lua
-- Implements the special API functions needed for cPluginManager to work
--- Implementation of the AddWebTab API functions in cPluginLua and cWebAdmin
local function addWebTab(a_Simulator, a_Title, a_Url, a_Callback)
a_Simulator.registeredWebTabs[a_Url] =
{
title = a_Title,
callback = a_Callback,
}
end
--- Returns a cPluginLua instance that acts as the current plugin
local function getCurrentPlugin(a_Simulator, a_Self)
local res = a_Simulator:createInstance({Type = "cPluginLua"})
-- Save the path to the plugin folder in the self variable
getmetatable(res).simulatorInternal_pluginPath = a_Simulator.options.pluginPath
return res
end
--- Implementation of the cPluginManager:BindCommand function
-- There are two separate API endpoints for the same implementation, so the implementation is pulled into a common function
local function BindCommand(a_Simulator, a_Self, a_Command, a_Permission, a_Callback, a_HelpString)
if (a_Command:match("%s")) then
a_Simulator.logger:trace("Registering a command containing whitespace(%s), such a handler will never be called by Cuberite.%s", a_Command, debug.traceback("", 2))
end
if (a_Simulator.registeredCommandHandlers[a_Command]) then
a_Simulator.logger:info("Registering a command that is already registered (%s). The second handler will not be called / tested.", a_Command)
return
end
a_Simulator.registeredCommandHandlers[a_Command] =
{
permission = a_Permission,
callback = a_Callback,
helpString = a_HelpString,
}
return true
end
--- Implementation of the cPluginManager:BindConsoleCommand function
-- There are two separate API endpoints for the same implementation, so the implementation is pulled into a common function
local function BindConsoleCommand(a_Simulator, a_Self, a_Command, a_Callback, a_HelpString)
if (a_Command:match("%s")) then
a_Simulator.logger:trace("Registering a console command containing whitespace(%s), such a handler will never be called by Cuberite.%s", a_Command, debug.traceback("", 2))
end
if (a_Simulator.registeredConsoleCommandHandlers[a_Command]) then
a_Simulator.logger:info("Registering a console command that is already registered (%s). The second handler will not be called / tested.", a_Command)
return
end
a_Simulator.registeredConsoleCommandHandlers[a_Command] =
{
callback = a_Callback,
helpString = a_HelpString,
}
return true
end
--- Implementation of the cPluginManager:ForEachPlugin function
-- There are two separate API endpoints for the same implementation
local function ForEachPlugin(a_Simulator, a_Self, a_Callback)
local res = a_Simulator:processCallbackRequest({
Function = a_Callback,
ParamValues = { a_Self:GetCurrentPlugin() },
Notes = "cPluginManager:ForEachPlugin() callback",
})
return (res ~= true) -- If the callback signalized an abort, return false
end
return
{
["cPlugin:GetLocalFolder()"] = function(a_Simulator, a_Self)
return getmetatable(a_Self).simulatorInternal_pluginPath or a_Simulator:createInstance({Type = "string"})
end,
["cPluginLua:AddWebTab(string, function)"] = function(a_Simulator, a_Self, a_Title, a_Callback)
a_Simulator.logger:warning("Plugin uses the deprecated function cPluginLua:AddWebTab")
return addWebTab(a_Simulator, a_Title, a_Title, a_Callback)
end,
["<static> cPluginManager:AddHook(cPluginManager#PluginHook, function)"] = function(a_Simulator, a_Self, a_HookType, a_Callback)
local hooks = a_Simulator.hooks[a_HookType] or {}
a_Simulator.registeredHooks[a_HookType] = hooks
table.insert(hooks, a_Callback)
end,
["<static> cPluginManager:BindCommand(string, string, function, string)"] = BindCommand,
["cPluginManager:BindCommand(string, string, function, string)"] = BindCommand,
["<static> cPluginManager:BindConsoleCommand(string, function, string)"] = BindConsoleCommand,
["cPluginManager:BindConsoleCommand(string, function, string)"] = BindConsoleCommand,
["<static> cPluginManager:DoWithPlugin(string, function) -> (boolean)"] = function(a_Simulator, a_Class, a_PluginName, a_Callback)
-- The currently tested plugin is the only one present, if the name matches, call with "self":
local currPlugin = getCurrentPlugin(a_Simulator)
if (a_PluginName == currPlugin:GetName()) then
local res = a_Simulator:processCallbackRequest({
Function = a_Callback,
ParamValues = { currPlugin },
Notes = "cPluginManager:DoWithPlugin() callback",
})
return (res == true) -- Convert the return value to a single bool
end
-- Plugin not found:
return false
end,
["cPluginManager:ForEachCommand(function) -> (boolean)"] = function(a_Simulator, a_Self, a_Callback)
for cmd, desc in pairs(a_Simulator.registeredCommandHandlers) do
local res = a_Simulator:processCallbackRequest({
Function = a_Callback,
ParamValues = { cmd, desc.permission, desc.helpString },
Notes = string.format("cPluginManager:ForEachCommand() callback, command %s", cmd),
})
if (res == true) then
a_Simulator.logger:trace("cPluginManager:ForEachCommand() callback has aborted the loop at command %s", cmd)
return false
end
end -- for cmd, desc
return true
end,
["cPluginManager:ForEachConsoleCommand(function) -> (boolean)"] = function(a_Simulator, a_Self, a_Callback)
for cmd, desc in pairs(a_Simulator.registeredConsoleCommandHandlers) do
local res = a_Simulator:processCallbackRequest({
Function = a_Callback,
ParamValues = { cmd, desc.helpString },
Notes = string.format("cPluginManager:ForEachConsoleCommand() callback, command %s", cmd),
})
if (res == true) then
a_Simulator.logger:trace("cPluginManager:ForEachConsoleCommand() callback has aborted the loop at command %s", cmd)
return false
end
end -- for cmd, desc
return true
end,
["<static> cPluginManager:ForEachPlugin(function) -> (boolean)"] = ForEachPlugin,
["cPluginManager:ForEachPlugin(function) -> (boolean)"] = ForEachPlugin,
["cPluginManager:GetCurrentPlugin()"] = getCurrentPlugin,
["<static> cPluginManager:GetPluginsPath()"] = function (a_Simulator)
return a_Simulator.options.pluginPath .. "/.."
end,
["<static> cWebAdmin:AddWebTab(string, string, function)"] = function(a_Simulator, a_Self, a_Title, a_Url, a_Callback)
return addWebTab(a_Simulator, a_Title, a_Url, a_Callback)
end,
}