|
2 | 2 |
|
3 | 3 | #include "Entry.h" |
4 | 4 | #include "Plugin.h" |
5 | | -#include "legacy/api/EventAPI.h" |
6 | 5 | #include "legacy/engine/EngineManager.h" |
7 | 6 | #include "legacy/engine/EngineOwnData.h" |
8 | 7 | #include "ll/api/service/GamingStatus.h" |
9 | 8 | #include "ll/api/io/FileUtils.h" |
10 | 9 | #include "ll/api/mod/Mod.h" |
11 | 10 | #include "ll/api/mod/ModManager.h" |
12 | 11 | #include "ll/api/utils/StringUtils.h" |
| 12 | +#include "ll/api/chrono/GameChrono.h" |
| 13 | +#include "ll/api/coro/CoroTask.h" |
| 14 | +#include "ll/api/thread/ServerThreadExecutor.h" |
13 | 15 |
|
14 | 16 | #include <ScriptX/ScriptX.h> |
15 | 17 | #include <exception> |
@@ -49,10 +51,12 @@ constexpr auto PluginManagerName = "lse-nodejs"; |
49 | 51 | // Do not use legacy headers directly, otherwise there will be tons of errors. |
50 | 52 | void BindAPIs(script::ScriptEngine* engine); |
51 | 53 | void LLSERemoveTimeTaskData(script::ScriptEngine* engine); |
52 | | -auto LLSERemoveAllEventListeners(script::ScriptEngine* engine) -> bool; |
53 | | -auto LLSERemoveCmdRegister(script::ScriptEngine* engine) -> bool; |
54 | | -auto LLSERemoveCmdCallback(script::ScriptEngine* engine) -> bool; |
55 | | -auto LLSERemoveAllExportedFuncs(script::ScriptEngine* engine) -> bool; |
| 54 | +bool LLSERemoveAllEventListeners(script::ScriptEngine* engine); |
| 55 | +bool LLSERemoveCmdRegister(script::ScriptEngine* engine); |
| 56 | +bool LLSERemoveCmdCallback(script::ScriptEngine* engine); |
| 57 | +bool LLSERemoveAllExportedFuncs(script::ScriptEngine* engine); |
| 58 | +bool LLSECallEventsOnHotLoad(ScriptEngine* engine); |
| 59 | +bool LLSECallEventsOnHotUnload(ScriptEngine* engine); |
56 | 60 |
|
57 | 61 | namespace lse { |
58 | 62 |
|
@@ -235,26 +239,32 @@ ll::Expected<> PluginManager::load(ll::mod::Manifest manifest) { |
235 | 239 |
|
236 | 240 | ll::Expected<> PluginManager::unload(std::string_view name) { |
237 | 241 | try { |
238 | | - |
239 | | - auto plugin = std::static_pointer_cast<Plugin>(getMod(name)); |
240 | | - |
241 | | - auto& scriptEngine = *EngineManager::getEngine(std::string(name)); |
| 242 | + auto plugin = std::static_pointer_cast<Plugin>(getMod(name)); |
| 243 | + auto scriptEngine = EngineManager::getEngine(std::string(name)); |
242 | 244 |
|
243 | 245 | #ifndef LEGACY_SCRIPT_ENGINE_BACKEND_NODEJS |
244 | | - LLSERemoveTimeTaskData(&scriptEngine); |
| 246 | + LLSERemoveTimeTaskData(scriptEngine); |
245 | 247 | #endif |
246 | | - LLSERemoveAllEventListeners(&scriptEngine); |
247 | | - LLSERemoveCmdRegister(&scriptEngine); |
248 | | - LLSERemoveCmdCallback(&scriptEngine); |
249 | | - LLSERemoveAllExportedFuncs(&scriptEngine); |
250 | | - |
251 | | - scriptEngine.getData().reset(); |
252 | | - EngineManager::unregisterEngine(&scriptEngine); |
| 248 | + LLSECallEventsOnHotUnload(scriptEngine); |
| 249 | + LLSERemoveAllEventListeners(scriptEngine); |
| 250 | + LLSERemoveCmdRegister(scriptEngine); |
| 251 | + LLSERemoveCmdCallback(scriptEngine); |
| 252 | + LLSERemoveAllExportedFuncs(scriptEngine); |
| 253 | + |
| 254 | + scriptEngine->getData().reset(); |
| 255 | + EngineManager::unregisterEngine(scriptEngine); |
| 256 | + |
| 257 | + ll::coro::keepThis([scriptEngine]() -> ll::coro::CoroTask<> { |
| 258 | + using namespace ll::chrono_literals; |
| 259 | + co_await 1_tick; |
253 | 260 | #ifdef LEGACY_SCRIPT_ENGINE_BACKEND_NODEJS |
254 | | - NodeJsHelper::stopEngine(&scriptEngine); |
| 261 | + NodeJsHelper::stopEngine(scriptEngine); |
255 | 262 | #else |
256 | | - scriptEngine.destroy(); // TODO: use unique_ptr to manage the engine. |
| 263 | + scriptEngine->destroy(); // TODO: use unique_ptr to manage the engine. |
257 | 264 | #endif |
| 265 | + co_return; |
| 266 | + }).launch(ll::thread::ServerThreadExecutor::getDefault()); |
| 267 | + |
258 | 268 | eraseMod(name); |
259 | 269 |
|
260 | 270 | return {}; |
|
0 commit comments