Skip to content

Commit b274544

Browse files
committed
fix: fix ScriptEngine lifecycle
1 parent 3293980 commit b274544

File tree

9 files changed

+81
-60
lines changed

9 files changed

+81
-60
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.17.2] - 2026-02-04
11+
12+
### Fixed
13+
14+
- Fixed ScriptEngine lifecycle
15+
1016
## [0.17.1] - 2026-01-27
1117

1218
### Fixed
@@ -1139,7 +1145,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
11391145
[#339]: https://github.com/LiteLDev/LegacyScriptEngine/issues/339
11401146
[#348]: https://github.com/LiteLDev/LegacyScriptEngine/issues/348
11411147

1142-
[Unreleased]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.17.1...HEAD
1148+
[Unreleased]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.17.2...HEAD
1149+
[0.17.2]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.17.1...v0.17.2
11431150
[0.17.1]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.17.0...v0.17.1
11441151
[0.17.0]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.17.0-rc.2...v0.17.0
11451152
[0.17.0-rc.2]: https://github.com/LiteLDev/LegacyScriptEngine/compare/v0.16.8...v0.17.0-rc.2

src/legacy/api/EventAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,11 +954,11 @@ void InitBasicEventListeners() {
954954
try {
955955
std::list<std::shared_ptr<ScriptEngine>> tmpList;
956956
{
957-
std::shared_lock<std::shared_mutex> lock(globalShareData->engineListLock);
957+
std::shared_lock lock(globalShareData->engineListLock);
958958
// low efficiency
959959
tmpList = globalShareData->globalEngineList;
960960
}
961-
for (auto engine : tmpList) {
961+
for (auto& engine : tmpList) {
962962
if (EngineManager::isValid(engine.get())
963963
&& EngineManager::getEngineType(engine) == LLSE_BACKEND_TYPE) {
964964
EngineScope enter(engine.get());

src/legacy/engine/EngineManager.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ using namespace script;
1717
///////////////////////////////// API /////////////////////////////////
1818

1919
bool EngineManager::unregisterEngine(std::shared_ptr<ScriptEngine> toDelete) {
20-
std::unique_lock<std::shared_mutex> lock(globalShareData->engineListLock);
20+
std::unique_lock lock(globalShareData->engineListLock);
2121
for (auto engine = globalShareData->globalEngineList.begin(); engine != globalShareData->globalEngineList.end();
22-
++engine)
22+
++engine) {
2323
if (*engine == toDelete) {
2424
globalShareData->globalEngineList.erase(engine);
2525
return true;
2626
}
27+
}
2728
return false;
2829
}
2930

3031
bool EngineManager::registerEngine(std::shared_ptr<ScriptEngine> engine) {
31-
std::unique_lock<std::shared_mutex> lock(globalShareData->engineListLock);
32+
std::unique_lock lock(globalShareData->engineListLock);
3233
globalShareData->globalEngineList.push_back(engine);
3334
return true;
3435
}
@@ -53,23 +54,36 @@ std::shared_ptr<ScriptEngine> EngineManager::newEngine(std::string pluginName) {
5354
}
5455

5556
bool EngineManager::isValid(ScriptEngine* engine, bool onlyCheckLocal) {
56-
std::shared_lock<std::shared_mutex> lock(globalShareData->engineListLock);
57-
for (auto i = globalShareData->globalEngineList.begin(); i != globalShareData->globalEngineList.end(); ++i)
58-
if (i->get() == engine) {
57+
std::shared_lock lock(globalShareData->engineListLock);
58+
for (auto& i : globalShareData->globalEngineList) {
59+
if (i.get() == engine) {
5960
if (engine->isDestroying()) return false;
6061
if (onlyCheckLocal && getEngineType(engine) != LLSE_BACKEND_TYPE) return false;
61-
else return true;
62+
return true;
6263
}
64+
}
6365
return false;
6466
}
6567

6668
bool EngineManager::isValid(std::shared_ptr<ScriptEngine> engine, bool onlyCheckLocal) {
6769
return isValid(engine.get(), onlyCheckLocal);
6870
}
6971

72+
std::shared_ptr<ScriptEngine> EngineManager::checkAndGet(ScriptEngine* engine, bool onlyCheckLocal) {
73+
std::shared_lock lock(globalShareData->engineListLock);
74+
for (auto& i : globalShareData->globalEngineList) {
75+
if (i.get() == engine) {
76+
if (engine->isDestroying()) return nullptr;
77+
if (onlyCheckLocal && getEngineType(engine) != LLSE_BACKEND_TYPE) return nullptr;
78+
return i;
79+
}
80+
}
81+
return nullptr;
82+
}
83+
7084
std::vector<std::shared_ptr<ScriptEngine>> EngineManager::getLocalEngines() {
7185
std::vector<std::shared_ptr<ScriptEngine>> res;
72-
std::shared_lock<std::shared_mutex> lock(globalShareData->engineListLock);
86+
std::shared_lock lock(globalShareData->engineListLock);
7387
for (auto& engine : globalShareData->globalEngineList) {
7488
if (getEngineType(engine) == LLSE_BACKEND_TYPE) res.push_back(engine);
7589
}
@@ -78,15 +92,15 @@ std::vector<std::shared_ptr<ScriptEngine>> EngineManager::getLocalEngines() {
7892

7993
std::vector<std::shared_ptr<ScriptEngine>> EngineManager::getGlobalEngines() {
8094
std::vector<std::shared_ptr<ScriptEngine>> res;
81-
std::shared_lock<std::shared_mutex> lock(globalShareData->engineListLock);
95+
std::shared_lock lock(globalShareData->engineListLock);
8296
for (auto& engine : globalShareData->globalEngineList) {
8397
res.push_back(engine);
8498
}
8599
return res;
86100
}
87101

88102
std::shared_ptr<ScriptEngine> EngineManager::getEngine(std::string name, bool onlyLocalEngine) {
89-
std::shared_lock<std::shared_mutex> lock(globalShareData->engineListLock);
103+
std::shared_lock lock(globalShareData->engineListLock);
90104
for (auto& engine : globalShareData->globalEngineList) {
91105
if (onlyLocalEngine && getEngineType(engine) != LLSE_BACKEND_TYPE) continue;
92106
auto ownerData = getEngineData(engine);
@@ -99,4 +113,4 @@ std::string EngineManager::getEngineType(std::shared_ptr<ScriptEngine> engine) {
99113
return getEngineData(engine)->engineType;
100114
}
101115

102-
std::string EngineManager::getEngineType(ScriptEngine* engine) { return getEngineData(engine)->engineType; }
116+
std::string EngineManager::getEngineType(ScriptEngine* engine) { return getEngineData(engine)->engineType; }

src/legacy/engine/EngineManager.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
#pragma once
22
#include "ScriptX/ScriptX.h"
3+
#include "utils/UsingScriptX.h"
34

45
#include <map>
56
#include <string>
67
#include <vector>
78

89
class EngineManager {
910
public:
10-
static std::shared_ptr<script::ScriptEngine> newEngine(std::string pluginName = "");
11-
static bool registerEngine(std::shared_ptr<script::ScriptEngine> engine);
12-
static bool unregisterEngine(std::shared_ptr<script::ScriptEngine> engine);
13-
static bool isValid(script::ScriptEngine* engine, bool onlyCheckLocal = false);
14-
static bool isValid(std::shared_ptr<script::ScriptEngine> engine, bool onlyCheckLocal = false);
11+
static std::shared_ptr<ScriptEngine> newEngine(std::string pluginName = "");
12+
static bool registerEngine(std::shared_ptr<ScriptEngine> engine);
13+
static bool unregisterEngine(std::shared_ptr<ScriptEngine> engine);
14+
static bool isValid(ScriptEngine* engine, bool onlyCheckLocal = false);
15+
static bool isValid(std::shared_ptr<ScriptEngine> engine, bool onlyCheckLocal = false);
16+
static std::shared_ptr<ScriptEngine> checkAndGet(ScriptEngine* engine, bool onlyCheckLocal = false);
1517

16-
static std::vector<std::shared_ptr<script::ScriptEngine>> getLocalEngines();
17-
static std::vector<std::shared_ptr<script::ScriptEngine>> getGlobalEngines();
18-
static std::shared_ptr<script::ScriptEngine> getEngine(std::string name, bool onlyLocalEngine = false);
18+
static std::vector<std::shared_ptr<ScriptEngine>> getLocalEngines();
19+
static std::vector<std::shared_ptr<ScriptEngine>> getGlobalEngines();
20+
static std::shared_ptr<ScriptEngine> getEngine(std::string name, bool onlyLocalEngine = false);
1921

20-
static std::string getEngineType(std::shared_ptr<script::ScriptEngine> engine);
21-
static std::string getEngineType(script::ScriptEngine* engine);
22+
static std::string getEngineType(std::shared_ptr<ScriptEngine> engine);
23+
static std::string getEngineType(ScriptEngine* engine);
2224
};

src/legacy/engine/GlobalShareData.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ struct MessageHandlers {
2727
// 全局共享数据
2828
struct GlobalDataType {
2929
// 引擎管理器表
30-
std::shared_mutex engineListLock;
31-
std::list<std::shared_ptr<script::ScriptEngine>> globalEngineList;
30+
std::shared_mutex engineListLock;
31+
std::list<std::shared_ptr<ScriptEngine>> globalEngineList;
3232

3333
// 注册过的命令
3434
std::unordered_map<std::string, std::string> playerRegisteredCmd;

src/legacy/engine/MessageSystem.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,11 @@ void MessageSystemLoopOnce() {
324324
// return;
325325
std::list<std::shared_ptr<ScriptEngine>> tmpList;
326326
{
327-
std::unique_lock<std::shared_mutex> lock(globalShareData->engineListLock);
327+
std::unique_lock lock(globalShareData->engineListLock);
328328
// low efficiency
329329
tmpList = globalShareData->globalEngineList;
330330
}
331-
for (auto engine : tmpList) {
331+
for (auto& engine : tmpList) {
332332
if (EngineManager::isValid(engine) && EngineManager::getEngineType(engine) == LLSE_BACKEND_TYPE) {
333333
try {
334334
if (EngineScope::currentEngine() == engine.get())
@@ -339,17 +339,11 @@ void MessageSystemLoopOnce() {
339339
}
340340
} catch (const Exception& e) {
341341
EngineScope scope(engine.get());
342-
lse::LegacyScriptEngine::getLogger().error(
343-
"Error occurred in Engine Message Loop!"
344-
);
342+
lse::LegacyScriptEngine::getLogger().error("Error occurred in Engine Message Loop!");
345343
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
346-
lse::LegacyScriptEngine::getLogger().error(
347-
"In Plugin: " + getEngineOwnData()->pluginName
348-
);
344+
lse::LegacyScriptEngine::getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
349345
} catch (...) {
350-
lse::LegacyScriptEngine::getLogger().error(
351-
"Error occurred in Engine Message Loop!"
352-
);
346+
lse::LegacyScriptEngine::getLogger().error("Error occurred in Engine Message Loop!");
353347
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger());
354348
}
355349
}

src/legacy/engine/TimeTaskSystem.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@ std::unordered_map<uint64, ScriptEngine*> timeTaskMap;
2626
#define TIMETASK_CATCH(TASK_TYPE) \
2727
catch (const Exception& e) { \
2828
EngineScope scope(data.engine); \
29-
lse::LegacyScriptEngine::getLogger().error("Error occurred in {}", TASK_TYPE); \
30-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger()); \
31-
lse::LegacyScriptEngine::getLogger().error( \
32-
"In Plugin: " + getEngineData(data.engine)->pluginName \
33-
); \
29+
lse::LegacyScriptEngine::getLogger().error("Error occurred in {}", TASK_TYPE); \
30+
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger()); \
31+
lse::LegacyScriptEngine::getLogger().error("In Plugin: " + getEngineData(data.engine)->pluginName); \
3432
} \
3533
catch (...) { \
36-
lse::LegacyScriptEngine::getLogger().error("Error occurred in {}", TASK_TYPE); \
37-
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger()); \
38-
lse::LegacyScriptEngine::getLogger().error( \
39-
"In Plugin: " + getEngineData(data.engine)->pluginName \
40-
); \
34+
lse::LegacyScriptEngine::getLogger().error("Error occurred in {}", TASK_TYPE); \
35+
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger()); \
36+
lse::LegacyScriptEngine::getLogger().error("In Plugin: " + getEngineData(data.engine)->pluginName); \
4137
}
4238

4339
int NewTimeout(Local<Function> func, std::vector<Local<Value>> paras, int timeout) {
@@ -55,12 +51,14 @@ int NewTimeout(Local<Function> func, std::vector<Local<Value>> paras, int timeou
5551
co_return;
5652
}
5753

58-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) {
54+
std::shared_ptr<ScriptEngine> engine;
55+
if (engine = EngineManager::checkAndGet(data.engine);
56+
!engine || ll::getGamingStatus() == ll::GamingStatus::Stopping) {
5957
ClearTimeTask(tid);
6058
co_return;
6159
}
6260

63-
EngineScope scope(data.engine);
61+
EngineScope scope(engine.get());
6462
if (!data.func.isEmpty()) {
6563
std::vector<Local<Value>> args;
6664
for (auto& para : data.paras) {
@@ -93,12 +91,14 @@ int NewTimeout(Local<String> func, int timeout) {
9391
co_return;
9492
}
9593

96-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) {
94+
std::shared_ptr<ScriptEngine> engine;
95+
if (engine = EngineManager::checkAndGet(data.engine);
96+
!engine || ll::getGamingStatus() == ll::GamingStatus::Stopping) {
9797
ClearTimeTask(tid);
9898
co_return;
9999
}
100100

101-
EngineScope scope(data.engine);
101+
EngineScope scope(engine.get());
102102
if (!data.code.isEmpty()) {
103103
auto code = data.code.get().toString();
104104
data.engine->eval(code);
@@ -129,12 +129,14 @@ int NewInterval(Local<Function> func, std::vector<Local<Value>> paras, int timeo
129129
co_return;
130130
}
131131

132-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) {
132+
std::shared_ptr<ScriptEngine> engine;
133+
if (engine = EngineManager::checkAndGet(data.engine);
134+
!engine || ll::getGamingStatus() == ll::GamingStatus::Stopping) {
133135
ClearTimeTask(tid);
134136
co_return;
135137
}
136138

137-
EngineScope scope(data.engine);
139+
EngineScope scope(engine.get());
138140

139141
if (!data.func.isEmpty()) {
140142
std::vector<Local<Value>> args;
@@ -168,12 +170,14 @@ int NewInterval(Local<String> func, int timeout) {
168170
if (!CheckTimeTask(tid)) {
169171
co_return;
170172
}
171-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) {
173+
std::shared_ptr<ScriptEngine> engine;
174+
if (engine = EngineManager::checkAndGet(data.engine);
175+
!engine || ll::getGamingStatus() == ll::GamingStatus::Stopping) {
172176
ClearTimeTask(tid);
173177
co_return;
174178
}
175179

176-
EngineScope scope(data.engine);
180+
EngineScope scope(engine.get());
177181
if (!data.code.isEmpty()) {
178182
data.engine->eval(data.code.get().toString());
179183
}
@@ -189,13 +193,13 @@ int NewInterval(Local<String> func, int timeout) {
189193

190194
bool CheckTimeTask(int const& id) {
191195
std::lock_guard lock(locker);
192-
return timeTaskMap.find(id) != timeTaskMap.end();
196+
return timeTaskMap.contains(id);
193197
}
194198

195199
bool ClearTimeTask(int const& id) {
196200
try {
197201
std::lock_guard lock(locker);
198-
if (timeTaskMap.find(id) != timeTaskMap.end()) {
202+
if (timeTaskMap.contains(id)) {
199203
timeTaskMap.erase(id);
200204
}
201205
} catch (...) {

tooth.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"format_version": 3,
33
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
44
"tooth": "github.com/LiteLDev/LegacyScriptEngine",
5-
"version": "0.17.1",
5+
"version": "0.17.2",
66
"info": {
77
"name": "LegacyScriptEngine",
88
"description": "A plugin engine for running LLSE plugins on LeviLamina",

xmake.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ add_rules("mode.debug", "mode.release")
33
add_repositories("levimc-repo " .. (get_config("levimc_repo") or "https://github.com/LiteLDev/xmake-repo.git"))
44

55
if is_config("target_type", "server") then
6-
add_requires("levilamina 1.9.2", {configs = {target_type = "server"}})
6+
add_requires("levilamina 1.9.4", {configs = {target_type = "server"}})
77
else
8-
add_requires("levilamina 1.9.2", {configs = {target_type = "client"}})
8+
add_requires("levilamina 1.9.4", {configs = {target_type = "client"}})
99
end
1010

1111
add_requires("levibuildscript")

0 commit comments

Comments
 (0)