This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
forked from LiteLDev/LeviLamina
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPluginManager.cpp
298 lines (260 loc) · 7.96 KB
/
PluginManager.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <API/APIHelp.h>
#include "PluginManager.h"
#include <Global.hpp>
#include <Configs.h>
#include <filesystem>
#include <Engine/GlobalShareData.h>
#include <Engine/LocalShareData.h>
#include <Engine/MessageSystem.h>
#include <Engine/LoaderHelper.h>
#include <Engine/RemoteCall.h>
#include <Engine/TimeTaskSystem.h>
#include <Engine/EngineManager.h>
#include <Utils/STLHelper.h>
#include <LiteLoader/Main/PluginManager.h>
#include <Loader.h>
#include <ScheduleAPI.h>
#include <API/EventAPI.h>
#include <API/CommandAPI.h>
#include <Utils/Hash.h>
#define H(x) do_hash(x)
using namespace std;
extern void BindAPIs(ScriptEngine* engine);
//Helper
string RemoveRealAllExtension(string fileName)
{
int pos = fileName.find(".");
if (pos == string::npos)
return fileName;
else
return fileName.substr(0, pos);
}
//加载插件
bool PluginManager::loadPlugin(const std::string& filePath, bool isHotLoad, bool mustBeCurrentModule)
{
if (filePath == LLSE_DEBUG_ENGINE_NAME)
return true;
string suffix = filesystem::path(str2wstr(filePath)).extension().u8string();
if (suffix != LLSE_PLUGINS_EXTENSION)
{
if (mustBeCurrentModule)
return false;
string moduleToBroadcast;
switch (H(suffix.c_str()))
{
case H(".lua"):
moduleToBroadcast = LLSE_BACKEND_LUA;
break;
case H(".js"):
moduleToBroadcast = LLSE_BACKEND_JS;
break;
default:
logger.error("Do not support this type of plugin!");
return false;
break;
}
//Remote Load
//logger.debug("Remote Load begin");
ostringstream sout;
sout << isHotLoad << "\n" << filePath;
string request = sout.str();
auto result = ModuleMessage::sendToRandom(moduleToBroadcast, ModuleMessage::MessageType::RemoteLoadRequest, request);
if (!result)
{
logger.error("Fail to send remote load request!");
return false;
}
if (!result.waitForAllResults(LLSE_MAXWAIT_REMOTE_LOAD))
{
logger.error("Remote Load Timeout!");
return false;
}
return true;
}
//判重
string pluginFileName = std::filesystem::path(str2wstr(filePath)).filename().u8string();
if (PluginManager::getPlugin(pluginFileName))
{
//logger.error("This plugin has been loaded by LiteLoader. You cannot load it twice.");
return false;
}
if (!filesystem::exists(str2wstr(filePath)))
{
logger.error("Plugin no found! Check the path you input again.");
return false;
}
ScriptEngine* engine = nullptr;
try
{
auto scripts = ReadAllFile(filePath);
if (!scripts)
throw("Fail to open plugin file!");
//启动引擎
engine = EngineManager::newEngine();
EngineScope enter(engine);
//setData
ENGINE_OWN_DATA()->pluginName = pluginFileName;
ENGINE_OWN_DATA()->pluginFilePath = filePath;
ENGINE_OWN_DATA()->logger.title = RemoveRealAllExtension(pluginFileName);
//绑定API
try {
BindAPIs(engine);
}
catch (const Exception& e)
{
logger.error("Fail in Binding APIs!\n");
throw;
}
//加载libs依赖库
try
{
for (auto& [path, content] : depends)
{
engine->eval(content, path);
}
}
catch (const Exception& e)
{
logger.error("Fail in Loading Dependence Lib!\n");
throw;
}
//加载脚本
try
{
engine->eval(*scripts, ENGINE_OWN_DATA()->pluginFilePath);
}
catch (const Exception& e)
{
logger.error("Fail in Loading Script Plugin!\n");
throw;
}
std::string const& pluginName = ENGINE_OWN_DATA()->pluginName;
ExitEngineScope exit;
//后处理
if (!PluginManager::getPlugin(pluginName))
PluginManager::registerPlugin(filePath, pluginName, pluginName, LL::Version(1, 0, 0), {});
if (isHotLoad)
LLSECallEventsOnHotLoad(engine);
logger.info(pluginName + " loaded.");
return true;
}
catch (const Exception& e)
{
logger.error("Fail to load " + filePath + "!");
if (engine)
{
EngineScope enter(engine);
logger.error("In Plugin: " + ENGINE_OWN_DATA()->pluginName);
PrintException(e);
ExitEngineScope exit;
LLSERemoveTimeTaskData(engine);
LLSERemoveAllEventListeners(engine);
LLSERemoveCmdRegister(engine);
LLSERemoveCmdCallback(engine);
LLSERemoveAllExportedFuncs(engine);
engine->getData().reset();
EngineManager::unRegisterEngine(engine);
}
if(engine)
engine->destroy();
}
catch (const std::exception& e)
{
logger.error("Fail to load " + filePath + "!");
logger.error(TextEncoding::toUTF8(e.what()));
}
catch (...)
{
logger.error("Fail to load " + filePath + "!");
}
return false;
}
//卸载插件
bool PluginManager::unloadPlugin(const std::string& name)
{
if (name == LLSE_DEBUG_ENGINE_NAME)
return false;
auto engine = EngineManager::getEngine(name);
if (!engine)
return false;
LLSECallEventsOnHotUnload(engine);
LLSERemoveTimeTaskData(engine);
LLSERemoveAllEventListeners(engine);
LLSERemoveCmdRegister(engine);
LLSERemoveCmdCallback(engine);
LLSERemoveAllExportedFuncs(engine);
EngineManager::unRegisterEngine(engine);
engine->getData().reset();
PluginManager::unRegisterPlugin(name);
Schedule::nextTick([engine]() {
engine->destroy();
});
logger.info(name + " unloaded.");
return true;
}
//重载插件
bool PluginManager::reloadPlugin(const std::string& name)
{
if (name == LLSE_DEBUG_ENGINE_NAME)
return true;
auto plugin = PluginManager::getPlugin(name);
if (!plugin)
return false;
string filePath = plugin->filePath;
if (!PluginManager::unloadPlugin(name))
return false;
return PluginManager::loadPlugin(filePath, true, true);
}
//重载全部插件
bool PluginManager::reloadAllPlugins()
{
auto pluginsList = PluginManager::getLocalPlugins();
for (auto& plugin : pluginsList)
reloadPlugin(plugin.second->name);
return true;
}
LL::Plugin* PluginManager::getPlugin(std::string name)
{
return LL::PluginManager::getPlugin(name,true);
}
//获取当前语言的所有插件
std::unordered_map<std::string, LL::Plugin*> PluginManager::getLocalPlugins()
{
std::unordered_map<std::string, LL::Plugin*> res;
auto engines = EngineManager::getLocalEngines();
for (auto& engine : engines)
{
string name = ENGINE_GET_DATA(engine)->pluginName;
if (name != LLSE_DEBUG_ENGINE_NAME)
{
LL::Plugin* plugin = PluginManager::getPlugin(name);
if (plugin)
res[plugin->name] = plugin;
}
}
return res;
}
std::unordered_map<std::string, LL::Plugin*> PluginManager::getAllScriptPlugins()
{
auto res = getAllPlugins();
erase_if(res, [](auto& item) {
return item.second->type != LL::Plugin::PluginType::ScriptPlugin;
});
return res;
}
// 获取所有的插件
std::unordered_map<std::string, LL::Plugin*> PluginManager::getAllPlugins()
{
return LL::PluginManager::getAllPlugins();
}
bool PluginManager::registerPlugin(std::string filePath, std::string name, std::string desc,
LL::Version version, std::map<std::string, std::string> others)
{
others["PluginType"] = "Script Plugin";
others["PluginFilePath"] = filePath;
return LL::PluginManager::registerPlugin(NULL, name, desc, version, others);
}
bool PluginManager::unRegisterPlugin(std::string name)
{
return LL::PluginManager::unRegisterPlugin(name);
}