Skip to content

Commit fa4f42d

Browse files
committed
fix: fix ll.onUnload #303
1 parent d970ed9 commit fa4f42d

File tree

4 files changed

+31
-65
lines changed

4 files changed

+31
-65
lines changed

src/legacy/api/EventAPI.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "ll/api/event/world/FireSpreadEvent.h"
4141
#include "ll/api/event/world/SpawnMobEvent.h"
4242
#include "ll/api/service/Bedrock.h"
43+
#include "ll/api/service/GamingStatus.h"
4344
#include "ll/api/thread/ServerThreadExecutor.h"
4445
#include "lse/Entry.h"
4546
#include "lse/events/BlockEvents.h"
@@ -133,13 +134,19 @@ bool LLSECallEventsOnHotLoad(ScriptEngine* engine) {
133134
return true;
134135
}
135136

136-
bool LLSECallEventsOnHotUnload(ScriptEngine* engine) {
137-
ll::service::getLevel()->forEachPlayer([&](Player& pl) -> bool {
138-
FakeCallEvent(engine, EVENT_TYPES::onLeft, PlayerClass::newPlayer(&pl));
139-
return true;
140-
});
137+
bool LLSECallEventsOnUnload(ScriptEngine* engine) {
138+
if (ll::getGamingStatus() == ll::GamingStatus::Running) {
139+
ll::service::getLevel()->forEachPlayer([&](Player& pl) -> bool {
140+
FakeCallEvent(engine, EVENT_TYPES::onLeft, PlayerClass::newPlayer(&pl));
141+
return true;
142+
});
143+
}
141144
for (auto& [index, cb] : getEngineData(engine)->unloadCallbacks) {
142-
cb(engine);
145+
EngineScope scope(engine);
146+
try {
147+
cb(engine);
148+
}
149+
CATCH_IN_CALLBACK("onUnload")
143150
}
144151
getEngineData(engine)->unloadCallbacks.clear();
145152
return true;

src/legacy/api/EventAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void EnableEventListener(int eventId);
1010
bool LLSEAddEventListener(ScriptEngine* engine, const std::string& eventName, const Local<Function>& func);
1111
bool LLSERemoveAllEventListeners(ScriptEngine* engine);
1212
bool LLSECallEventsOnHotLoad(ScriptEngine* engine);
13-
bool LLSECallEventsOnHotUnload(ScriptEngine* engine);
13+
bool LLSECallEventsOnUnload(ScriptEngine* engine);
1414

1515
//////////////////// Callback ////////////////////
1616

src/legacy/engine/EngineOwnData.h

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
#pragma once
22
#include "legacy/main/Global.h"
3-
#include "ll/api/base/Macro.h"
4-
#include "ll/api/mod/Mod.h"
5-
#include "lse/Entry.h"
63
#include "lse/Plugin.h"
74
#include "utils/UsingScriptX.h"
85

96
#include <ll/api/Expected.h>
107
#include <ll/api/i18n/I18n.h>
118
#include <ll/api/io/Logger.h>
129
#include <ll/api/io/LoggerRegistry.h>
13-
#include <map>
1410
#include <string>
1511
#include <unordered_map>
1612

@@ -25,15 +21,6 @@ struct RemoteCallData {
2521
script::Global<Function> callback;
2622
};
2723

28-
/*
29-
struct SimpleCallbackData
30-
{
31-
ScriptEngine* engine;
32-
script::Global<script::Function> func;
33-
std::vector<script::Global<Value>> values;
34-
};
35-
*/
36-
3724
// It is similar to ll::mod::Mod, it stores data of an engine(usually a plugin).
3825
struct EngineOwnData {
3926
// Basic information
@@ -43,28 +30,6 @@ struct EngineOwnData {
4330
// RemoteCall Exported Functions: unordered_map<nameSpace, funcName>
4431
std::unordered_map<std::string, RemoteCallData> exportFuncs;
4532

46-
/*
47-
uint64_t simpleCallbackIndex = 0;
48-
std::unordered_map<uint64_t, SimpleCallbackData> simpleCallbacks;
49-
50-
inline uint64_t addSimpleCallback(script::Local<Function> func,
51-
std::vector<script::Local<Value>> values)
52-
{
53-
auto index = ++simpleCallbackIndex;
54-
std::vector<script::Global<Value>> globalValues;
55-
for (auto& value : values)
56-
globalValues.emplace_back(value);
57-
SimpleCallbackData data{EngineScope::currentEngine(),
58-
script::Global<Function>(func), std::move(globalValues)};
59-
simpleCallbacks.emplace(index, std::move(data));
60-
return index;
61-
}
62-
inline bool removeSimpleCallback(uint64_t index)
63-
{
64-
return simpleCallbacks.erase(index);
65-
}
66-
*/
67-
6833
// I18nAPI
6934
ll::i18n::I18n i18n;
7035
std::string defaultLocaleName;
@@ -90,7 +55,9 @@ struct EngineOwnData {
9055
EngineScope scope(engine);
9156
auto data = std::static_pointer_cast<EngineOwnData>(engine->getData());
9257
data->playerDataDB.clear();
58+
data->unloadCallbacks.clear();
9359
// LLSERemoveAllExportedFuncs(engine);
60+
assert(data->exportFuncs.empty());
9461
}
9562
};
9663

src/lse/PluginManager.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "legacy/engine/EngineOwnData.h"
77
#include "ll/api/chrono/GameChrono.h"
88
#include "ll/api/coro/CoroTask.h"
9-
#include "ll/api/io/FileUtils.h"
9+
#include "ll/api/io/FileUtils.h" // IWYU pragma: keep
1010
#include "ll/api/mod/Mod.h"
1111
#include "ll/api/mod/ModManager.h"
1212
#include "ll/api/service/GamingStatus.h"
@@ -56,14 +56,15 @@ bool LLSERemoveCmdRegister(script::ScriptEngine* engine);
5656
bool LLSERemoveCmdCallback(script::ScriptEngine* engine);
5757
bool LLSERemoveAllExportedFuncs(script::ScriptEngine* engine);
5858
bool LLSECallEventsOnHotLoad(ScriptEngine* engine);
59-
bool LLSECallEventsOnHotUnload(ScriptEngine* engine);
59+
bool LLSECallEventsOnUnload(ScriptEngine* engine);
6060

6161
namespace lse {
6262

6363
PluginManager::PluginManager() : ll::mod::ModManager(PluginManagerName) {}
6464
PluginManager::~PluginManager() = default;
6565

6666
ll::Expected<> PluginManager::load(ll::mod::Manifest manifest) {
67+
auto& logger = lse::LegacyScriptEngine::getInstance().getSelf().getLogger();
6768
#ifdef LEGACY_SCRIPT_ENGINE_BACKEND_PYTHON
6869
std::filesystem::path dirPath = ll::mod::getModsRoot() / manifest.name; // Plugin path
6970
std::string entryPath =
@@ -78,22 +79,17 @@ ll::Expected<> PluginManager::load(ll::mod::Manifest manifest) {
7879
PythonHelper::getPluginPackDependencyFilePath(ll::string_utils::u8str2str(dirPath.u8string()));
7980
if (!dependTmpFilePath.empty()) {
8081
int exitCode = 0;
81-
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().info(
82-
"Executing \"pip install\" for plugin {name}..."_tr(
83-
fmt::arg("name", ll::string_utils::u8str2str(dirPath.filename().u8string()))
84-
)
85-
);
82+
logger.info("Executing \"pip install\" for plugin {name}..."_tr(
83+
fmt::arg("name", ll::string_utils::u8str2str(dirPath.filename().u8string()))
84+
));
8685

8786
if ((exitCode = PythonHelper::executePipCommand(
8887
"pip install -r \"" + dependTmpFilePath + "\" -t \""
8988
+ ll::string_utils::u8str2str(realPackageInstallDir.u8string()) + "\" --disable-pip-version-check "
9089
))
9190
== 0) {
92-
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().info("Pip finished successfully."_tr());
93-
} else
94-
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error(
95-
"Error occurred. Exit code: {code}"_tr(fmt::arg("code", exitCode))
96-
);
91+
logger.info("Pip finished successfully."_tr());
92+
} else logger.error("Error occurred. Exit code: {code}"_tr(fmt::arg("code", exitCode)));
9793

9894
// remove temp dependency file after installation
9995
std::error_code ec;
@@ -111,19 +107,15 @@ ll::Expected<> PluginManager::load(ll::mod::Manifest manifest) {
111107
if (NodeJsHelper::doesPluginPackHasDependency(ll::string_utils::u8str2str(dirPath.u8string()))
112108
&& !std::filesystem::exists(std::filesystem::path(dirPath) / "node_modules")) {
113109
int exitCode = 0;
114-
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().info(
115-
"Executing \"npm install\" for plugin {name}..."_tr(
116-
fmt::arg("name", ll::string_utils::u8str2str(dirPath.filename().u8string()))
117-
)
118-
);
110+
logger.info("Executing \"npm install\" for plugin {name}..."_tr(
111+
fmt::arg("name", ll::string_utils::u8str2str(dirPath.filename().u8string()))
112+
));
119113
if ((exitCode = NodeJsHelper::executeNpmCommand(
120114
{"install", "--omit=dev", "--no-fund"},
121115
ll::string_utils::u8str2str(dirPath.u8string())
122116
))
123117
!= 0) {
124-
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error(
125-
"Error occurred. Exit code: {code}"_tr(fmt::arg("code", exitCode))
126-
);
118+
logger.error("Error occurred. Exit code: {code}"_tr(fmt::arg("code", exitCode)));
127119
}
128120
}
129121
#endif
@@ -260,9 +252,7 @@ ll::Expected<> PluginManager::unload(std::string_view name) {
260252
#ifndef LEGACY_SCRIPT_ENGINE_BACKEND_NODEJS
261253
LLSERemoveTimeTaskData(scriptEngine);
262254
#endif
263-
if (ll::getGamingStatus() == ll::GamingStatus::Running) {
264-
LLSECallEventsOnHotUnload(scriptEngine);
265-
}
255+
LLSECallEventsOnUnload(scriptEngine);
266256
LLSERemoveAllEventListeners(scriptEngine);
267257
LLSERemoveCmdRegister(scriptEngine);
268258
LLSERemoveCmdCallback(scriptEngine);
@@ -296,6 +286,8 @@ ll::Expected<> PluginManager::unload(std::string_view name) {
296286
}
297287

298288
return {};
289+
} catch (const script::Exception& e) {
290+
return ll::makeStringError("Failed to unload plugin {0}: {1}"_tr(name, "Unknown script exception"));
299291
} catch (const std::exception& e) {
300292
return ll::makeStringError("Failed to unload plugin {0}: {1}"_tr(name, e.what()));
301293
}

0 commit comments

Comments
 (0)