Skip to content

Commit c18a5d5

Browse files
committed
fix: add missing LLSECallEventsOnHotUnload and fix unload
1 parent dcaa8fc commit c18a5d5

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

src/lse/PluginManager.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
#include "Entry.h"
44
#include "Plugin.h"
5-
#include "legacy/api/EventAPI.h"
65
#include "legacy/engine/EngineManager.h"
76
#include "legacy/engine/EngineOwnData.h"
87
#include "ll/api/service/GamingStatus.h"
98
#include "ll/api/io/FileUtils.h"
109
#include "ll/api/mod/Mod.h"
1110
#include "ll/api/mod/ModManager.h"
1211
#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"
1315

1416
#include <ScriptX/ScriptX.h>
1517
#include <exception>
@@ -49,10 +51,12 @@ constexpr auto PluginManagerName = "lse-nodejs";
4951
// Do not use legacy headers directly, otherwise there will be tons of errors.
5052
void BindAPIs(script::ScriptEngine* engine);
5153
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);
5660

5761
namespace lse {
5862

@@ -235,26 +239,32 @@ ll::Expected<> PluginManager::load(ll::mod::Manifest manifest) {
235239

236240
ll::Expected<> PluginManager::unload(std::string_view name) {
237241
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));
242244

243245
#ifndef LEGACY_SCRIPT_ENGINE_BACKEND_NODEJS
244-
LLSERemoveTimeTaskData(&scriptEngine);
246+
LLSERemoveTimeTaskData(scriptEngine);
245247
#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;
253260
#ifdef LEGACY_SCRIPT_ENGINE_BACKEND_NODEJS
254-
NodeJsHelper::stopEngine(&scriptEngine);
261+
NodeJsHelper::stopEngine(scriptEngine);
255262
#else
256-
scriptEngine.destroy(); // TODO: use unique_ptr to manage the engine.
263+
scriptEngine->destroy(); // TODO: use unique_ptr to manage the engine.
257264
#endif
265+
co_return;
266+
}).launch(ll::thread::ServerThreadExecutor::getDefault());
267+
258268
eraseMod(name);
259269

260270
return {};

0 commit comments

Comments
 (0)