Skip to content

Commit 407b049

Browse files
committed
fix: fix PluginManager's error
1 parent d47b61c commit 407b049

File tree

1 file changed

+84
-92
lines changed

1 file changed

+84
-92
lines changed

src/lse/PluginManager.cpp

Lines changed: 84 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -118,123 +118,115 @@ ll::Expected<> PluginManager::load(ll::plugin::Manifest manifest) {
118118
}
119119
#endif
120120

121-
try {
122-
123-
logger.info("loading plugin {}", manifest.name);
121+
logger.info("loading plugin {}", manifest.name);
124122

125-
if (hasPlugin(manifest.name)) {
126-
throw std::runtime_error("plugin already loaded");
127-
}
123+
if (hasPlugin(manifest.name)) {
124+
throw std::runtime_error("plugin already loaded");
125+
}
128126

129-
auto& scriptEngine = *EngineManager::newEngine(manifest.name);
130-
auto plugin = std::make_shared<Plugin>(manifest);
127+
auto& scriptEngine = *EngineManager::newEngine(manifest.name);
128+
auto plugin = std::make_shared<Plugin>(manifest);
131129

132-
try {
133-
script::EngineScope engineScope(scriptEngine);
130+
try {
131+
script::EngineScope engineScope(scriptEngine);
134132

135-
// Set plugins's logger title
136-
ENGINE_OWN_DATA()->logger.title = manifest.name;
137-
ENGINE_OWN_DATA()->pluginName = manifest.name;
133+
// Set plugins's logger title
134+
ENGINE_OWN_DATA()->logger.title = manifest.name;
135+
ENGINE_OWN_DATA()->pluginName = manifest.name;
138136

139137
#ifdef LEGACY_SCRIPT_ENGINE_BACKEND_PYTHON
140-
scriptEngine.eval("import sys as _llse_py_sys_module");
141-
std::error_code ec;
142-
143-
// add plugin-own site-packages to sys.path
144-
string pluginSitePackageFormatted = ll::string_utils::u8str2str(
145-
std::filesystem::canonical(realPackageInstallDir.make_preferred(), ec).u8string()
146-
);
147-
if (!ec) {
148-
scriptEngine.eval("_llse_py_sys_module.path.insert(0, r'" + pluginSitePackageFormatted + "')");
149-
}
150-
// add plugin source dir to sys.path
151-
string sourceDirFormatted =
152-
ll::string_utils::u8str2str(std::filesystem::canonical(dirPath.make_preferred()).u8string());
153-
scriptEngine.eval("_llse_py_sys_module.path.insert(0, r'" + sourceDirFormatted + "')");
154-
155-
// set __file__ and __name__
156-
string entryPathFormatted = ll::string_utils::u8str2str(
157-
std::filesystem::canonical(std::filesystem::path(entryPath).make_preferred()).u8string()
158-
);
159-
scriptEngine.set("__file__", entryPathFormatted);
160-
// engine->set("__name__", String::newString("__main__"));
138+
scriptEngine.eval("import sys as _llse_py_sys_module");
139+
std::error_code ec;
140+
141+
// add plugin-own site-packages to sys.path
142+
string pluginSitePackageFormatted = ll::string_utils::u8str2str(
143+
std::filesystem::canonical(realPackageInstallDir.make_preferred(), ec).u8string()
144+
);
145+
if (!ec) {
146+
scriptEngine.eval("_llse_py_sys_module.path.insert(0, r'" + pluginSitePackageFormatted + "')");
147+
}
148+
// add plugin source dir to sys.path
149+
string sourceDirFormatted =
150+
ll::string_utils::u8str2str(std::filesystem::canonical(dirPath.make_preferred()).u8string());
151+
scriptEngine.eval("_llse_py_sys_module.path.insert(0, r'" + sourceDirFormatted + "')");
152+
153+
// set __file__ and __name__
154+
string entryPathFormatted = ll::string_utils::u8str2str(
155+
std::filesystem::canonical(std::filesystem::path(entryPath).make_preferred()).u8string()
156+
);
157+
scriptEngine.set("__file__", entryPathFormatted);
158+
// engine->set("__name__", String::newString("__main__"));
161159
#endif
162160

163-
BindAPIs(&scriptEngine);
161+
BindAPIs(&scriptEngine);
164162

165-
auto& self = getSelfPluginInstance();
163+
auto& self = getSelfPluginInstance();
166164
#ifndef LEGACY_SCRIPT_ENGINE_BACKEND_NODEJS // NodeJs backend load depends code in another place
167-
// Load BaseLib.
168-
auto baseLibPath = self.getPluginDir() / "baselib" / BaseLibFileName;
169-
auto baseLibContent = ll::file_utils::readFile(baseLibPath);
170-
if (!baseLibContent) {
171-
throw std::runtime_error(fmt::format("failed to read BaseLib at {}", baseLibPath.string()));
172-
}
173-
scriptEngine.eval(baseLibContent.value());
165+
// Load BaseLib.
166+
auto baseLibPath = self.getPluginDir() / "baselib" / BaseLibFileName;
167+
auto baseLibContent = ll::file_utils::readFile(baseLibPath);
168+
if (!baseLibContent) {
169+
throw std::runtime_error(fmt::format("failed to read BaseLib at {}", baseLibPath.string()));
170+
}
171+
scriptEngine.eval(baseLibContent.value());
174172
#endif
175-
// Load the plugin entry.
176-
auto pluginDir = std::filesystem::canonical(ll::plugin::getPluginsRoot() / manifest.name);
177-
auto entryPath = pluginDir / manifest.entry;
178-
ENGINE_OWN_DATA()->pluginFileOrDirPath = entryPath.string();
173+
// Load the plugin entry.
174+
auto pluginDir = std::filesystem::canonical(ll::plugin::getPluginsRoot() / manifest.name);
175+
auto entryPath = pluginDir / manifest.entry;
176+
ENGINE_OWN_DATA()->pluginFileOrDirPath = entryPath.string();
179177
#ifdef LEGACY_SCRIPT_ENGINE_BACKEND_PYTHON
180-
if (!PythonHelper::loadPluginCode(&scriptEngine, entryPath.string(), dirPath.string())) {
181-
throw std::runtime_error(fmt::format("failed to load plugin code"));
182-
}
178+
if (!PythonHelper::loadPluginCode(&scriptEngine, entryPath.string(), dirPath.string())) {
179+
throw std::runtime_error(fmt::format("failed to load plugin code"));
180+
}
183181
#endif
184182
#ifdef LEGACY_SCRIPT_ENGINE_BACKEND_NODEJS
185-
if (!NodeJsHelper::loadPluginCode(&scriptEngine, entryPath.string(), dirPath.string())) {
186-
throw std::runtime_error(fmt::format("failed to load plugin code"));
187-
}
183+
if (!NodeJsHelper::loadPluginCode(&scriptEngine, entryPath.string(), dirPath.string())) {
184+
throw std::runtime_error(fmt::format("failed to load plugin code"));
185+
}
188186
#endif
189187
#if (defined LEGACY_SCRIPT_ENGINE_BACKEND_QUICKJS) || (defined LEGACY_SCRIPT_ENGINE_BACKEND_LUA)
190-
// Try loadFile
191-
try {
192-
scriptEngine.loadFile(entryPath.u8string());
193-
} catch (const script::Exception& e) {
194-
// loadFile failed, try eval
195-
auto pluginEntryContent = ll::file_utils::readFile(entryPath);
196-
if (!pluginEntryContent) {
197-
throw std::runtime_error(fmt::format("Failed to read plugin entry at {}", entryPath.string()));
198-
}
199-
scriptEngine.eval(pluginEntryContent.value());
200-
}
201-
if (ll::getServerStatus() == ll::ServerStatus::Running) { // Is hot load
202-
LLSECallEventsOnHotLoad(&scriptEngine);
188+
// Try loadFile
189+
try {
190+
scriptEngine.loadFile(entryPath.u8string());
191+
} catch (const script::Exception& e) {
192+
// loadFile failed, try eval
193+
auto pluginEntryContent = ll::file_utils::readFile(entryPath);
194+
if (!pluginEntryContent) {
195+
throw std::runtime_error(fmt::format("Failed to read plugin entry at {}", entryPath.string()));
203196
}
204-
ExitEngineScope exit;
197+
scriptEngine.eval(pluginEntryContent.value());
198+
}
199+
if (ll::getServerStatus() == ll::ServerStatus::Running) { // Is hot load
200+
LLSECallEventsOnHotLoad(&scriptEngine);
201+
}
202+
ExitEngineScope exit;
205203
#endif
206-
plugin->onLoad([](ll::plugin::Plugin& plugin) { return true; });
204+
plugin->onLoad([](ll::plugin::Plugin& plugin) { return true; });
207205
#ifndef LEGACY_SCRIPT_ENGINE_BACKEND_NODEJS
208-
plugin->onUnload([](ll::plugin::Plugin& plugin) { return true; });
206+
plugin->onUnload([](ll::plugin::Plugin& plugin) { return true; });
209207
#endif
210-
plugin->onEnable([](ll::plugin::Plugin& plugin) { return true; });
211-
plugin->onDisable([](ll::plugin::Plugin& plugin) { return true; });
212-
} catch (const Exception& e) {
213-
EngineScope engineScope(scriptEngine);
214-
ExitEngineScope exit;
215-
LLSERemoveTimeTaskData(&scriptEngine);
216-
LLSERemoveAllEventListeners(&scriptEngine);
217-
LLSERemoveCmdRegister(&scriptEngine);
218-
LLSERemoveCmdCallback(&scriptEngine);
219-
LLSERemoveAllExportedFuncs(&scriptEngine);
220-
221-
scriptEngine.getData().reset();
222-
223-
EngineManager::unregisterEngine(&scriptEngine);
224-
225-
return ll::makeStringError(
226-
"Failed to load plugin {0}: {1}\n{2}"_tr(manifest.name, e.message(), e.stacktrace())
227-
);
228-
}
208+
plugin->onEnable([](ll::plugin::Plugin& plugin) { return true; });
209+
plugin->onDisable([](ll::plugin::Plugin& plugin) { return true; });
210+
} catch (const Exception& e) {
211+
EngineScope engineScope(scriptEngine);
212+
auto error =
213+
ll::makeStringError("Failed to load plugin {0}: {1}\n{2}"_tr(manifest.name, e.message(), e.stacktrace()));
214+
ExitEngineScope exit;
215+
LLSERemoveTimeTaskData(&scriptEngine);
216+
LLSERemoveAllEventListeners(&scriptEngine);
217+
LLSERemoveCmdRegister(&scriptEngine);
218+
LLSERemoveCmdCallback(&scriptEngine);
219+
LLSERemoveAllExportedFuncs(&scriptEngine);
229220

230-
addPlugin(manifest.name, plugin);
221+
scriptEngine.getData().reset();
231222

232-
return {};
223+
EngineManager::unregisterEngine(&scriptEngine);
233224

234-
} catch (const std::exception& e) {
235-
logger.error("Failed to load plugin {0}: {1}", manifest.name, e.what());
225+
return error;
236226
}
237227

228+
addPlugin(manifest.name, plugin);
229+
238230
return {};
239231
}
240232

0 commit comments

Comments
 (0)