Skip to content

Commit 85bb770

Browse files
committed
refactor: migrate legacy fake command to real command
1 parent 9b6e317 commit 85bb770

File tree

12 files changed

+158
-305
lines changed

12 files changed

+158
-305
lines changed

src-client/lse/PluginManager.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ constexpr auto PluginManagerName = "lse-nodejs";
4949
void BindAPIs(std::shared_ptr<ScriptEngine> const& engine);
5050
void LLSERemoveTimeTaskData(std::shared_ptr<ScriptEngine> const& engine);
5151
bool LLSERemoveAllEventListeners(std::shared_ptr<ScriptEngine> engine);
52-
bool LLSERemoveCmdRegister(std::shared_ptr<ScriptEngine> engine);
53-
bool LLSERemoveCmdCallback(std::shared_ptr<ScriptEngine> engine);
5452
bool LLSERemoveAllExportedFuncs(std::shared_ptr<ScriptEngine> const& engine);
5553
bool LLSECallEventsOnHotLoad(std::shared_ptr<ScriptEngine> const& engine);
5654
bool LLSECallEventsOnUnload(std::shared_ptr<ScriptEngine> const& engine);
@@ -238,8 +236,6 @@ ll::Expected<> PluginManager::enable(std::string_view name) {
238236
LLSERemoveTimeTaskData(scriptEngine);
239237
#endif
240238
LLSERemoveAllEventListeners(scriptEngine);
241-
LLSERemoveCmdRegister(scriptEngine);
242-
LLSERemoveCmdCallback(scriptEngine);
243239
LLSERemoveAllExportedFuncs(scriptEngine);
244240

245241
EngineOwnData::clearEngineObjects(scriptEngine);
@@ -268,8 +264,6 @@ ll::Expected<> PluginManager::disable(std::string_view name) {
268264
LLSERemoveTimeTaskData(scriptEngine);
269265
#endif
270266
LLSERemoveAllEventListeners(scriptEngine);
271-
LLSERemoveCmdRegister(scriptEngine);
272-
LLSERemoveCmdCallback(scriptEngine);
273267
LLSERemoveAllExportedFuncs(scriptEngine);
274268
EngineOwnData::clearEngineObjects(scriptEngine);
275269
}

src-server/lse/PluginManager.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ constexpr auto PluginManagerName = "lse-nodejs";
4848
void BindAPIs(std::shared_ptr<ScriptEngine> const& engine);
4949
void LLSERemoveTimeTaskData(std::shared_ptr<ScriptEngine> const& engine);
5050
bool LLSERemoveAllEventListeners(std::shared_ptr<ScriptEngine> engine);
51-
bool LLSERemoveCmdRegister(std::shared_ptr<ScriptEngine> engine);
52-
bool LLSERemoveCmdCallback(std::shared_ptr<ScriptEngine> engine);
5351
bool LLSERemoveAllExportedFuncs(std::shared_ptr<ScriptEngine> const& engine);
5452
bool LLSECallEventsOnHotLoad(std::shared_ptr<ScriptEngine> const& engine);
5553
bool LLSECallEventsOnUnload(std::shared_ptr<ScriptEngine> const& engine);
@@ -230,8 +228,6 @@ ll::Expected<> PluginManager::load(ll::mod::Manifest manifest) {
230228
LLSERemoveTimeTaskData(scriptEngine);
231229
#endif
232230
LLSERemoveAllEventListeners(scriptEngine);
233-
LLSERemoveCmdRegister(scriptEngine);
234-
LLSERemoveCmdCallback(scriptEngine);
235231
LLSERemoveAllExportedFuncs(scriptEngine);
236232

237233
EngineOwnData::clearEngineObjects(scriptEngine);
@@ -260,8 +256,6 @@ ll::Expected<> PluginManager::unload(std::string_view name) {
260256
LLSERemoveTimeTaskData(scriptEngine);
261257
#endif
262258
LLSERemoveAllEventListeners(scriptEngine);
263-
LLSERemoveCmdRegister(scriptEngine);
264-
LLSERemoveCmdCallback(scriptEngine);
265259
LLSERemoveAllExportedFuncs(scriptEngine);
266260
EngineOwnData::clearEngineObjects(scriptEngine);
267261
}

src/legacy/api/CommandAPI.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "legacy/api/ItemAPI.h"
99
#include "legacy/api/McAPI.h"
1010
#include "legacy/api/PlayerAPI.h"
11+
#include "legacy/engine/EngineManager.h"
1112
#include "legacy/engine/EngineOwnData.h"
1213
#include "legacy/engine/GlobalShareData.h"
1314
#include "legacy/engine/LocalShareData.h"
@@ -75,13 +76,11 @@ ClassDefine<CommandClass> CommandClassBuilder =
7576
//////////////////// Helper ////////////////////
7677

7778
bool LLSERemoveCmdCallback(std::shared_ptr<script::ScriptEngine> engine) {
78-
std::erase_if(localShareData->commandCallbacks, [&engine](auto& data) {
79-
return data.second.fromEngine == engine.get();
80-
});
79+
std::erase_if(localShareData->commandCallbacks, [&engine](auto& data) { return data.second.fromEngine == engine; });
8180
return true;
8281
}
8382

84-
Local<Value> convertResult(ParamStorageType const& result, CommandOrigin const& origin, CommandOutput& output) {
83+
Local<Value> convertResult(ParamStorageType const& result, CommandOrigin const& origin) {
8584
if (!result.has_value()) return {};
8685
if (result.hold(ParamKind::Kind::Enum)) {
8786
return String::newString(std::get<RuntimeEnum>(result.value()).name);
@@ -373,7 +372,7 @@ void onExecute(CommandOrigin const& origin, CommandOutput& output, RuntimeComman
373372
);
374373
return;
375374
}
376-
EngineScope enter(localShareData->commandCallbacks[commandName].fromEngine);
375+
EngineScope enter(localShareData->commandCallbacks[commandName].fromEngine.get());
377376
try {
378377
Local<Object> args = Object::newObject();
379378
auto cmd = CommandClass::newCommand(commandName);
@@ -390,17 +389,17 @@ void onExecute(CommandOrigin const& origin, CommandOutput& output, RuntimeComman
390389
if (!info.name.empty()) {
391390
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
392391
auto& param = runtime[info.enumName];
393-
args.set(info.name, convertResult(param, origin, output));
392+
args.set(info.name, convertResult(param, origin));
394393
if (!info.identifier.empty()
395394
&& info.identifier != info.name) { // Keep compatibility with old plugins
396-
args.set(info.identifier, convertResult(param, origin, output));
395+
args.set(info.identifier, convertResult(param, origin));
397396
}
398397
} else {
399398
auto& param = runtime[info.name];
400-
args.set(info.name, convertResult(param, origin, output));
399+
args.set(info.name, convertResult(param, origin));
401400
if (!info.identifier.empty()
402401
&& info.identifier != info.name) { // Keep compatibility with old plugins
403-
args.set(info.identifier, convertResult(param, origin, output));
402+
args.set(info.identifier, convertResult(param, origin));
404403
}
405404
}
406405
}
@@ -504,31 +503,33 @@ auto CommandClass::optional(Arguments const& args) const -> Local<Value> {
504503
Local<Value> CommandClass::addOverload(Arguments const& args) {
505504
try {
506505
auto overloadFunc = [e(
507-
EngineScope::currentEngine()
506+
EngineManager::checkAndGet(EngineScope::currentEngine(), true)
508507
)](RuntimeOverload& cmd, std::string const& commandName, std::string const& paramName) {
509508
auto& paramList = getEngineData(e)->plugin->registeredCommands[commandName];
510509
for (auto& info : paramList) {
511510
if (info.name == paramName || info.enumName == paramName || info.identifier == paramName) {
512511
if (info.optional) {
513512
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
514-
(void)cmd.optional(info.enumName, info.type, info.enumName).option(info.option);
513+
cmd.optional(info.enumName, info.type, info.enumName).option(info.option);
515514
} else {
516-
(void)cmd.optional(info.name, info.type).option(info.option);
515+
cmd.optional(info.name, info.type).option(info.option);
517516
}
518517
} else {
519518
if (info.type == ParamKind::Kind::Enum || info.type == ParamKind::Kind::SoftEnum) {
520-
(void)cmd.required(info.enumName, info.type, info.enumName).option(info.option);
519+
cmd.required(info.enumName, info.type, info.enumName).option(info.option);
521520
} else {
522-
(void)cmd.required(info.name, info.type).option(info.option);
521+
cmd.required(info.name, info.type).option(info.option);
523522
}
524523
}
525524
}
526525
}
527526
};
528527
auto delayRegFunc = [this, &overloadFunc](std::vector<std::string>& paramNames) {
529528
ll::coro::keepThis(
530-
[paramNames, commandName(commandName), overloadFunc, e(EngineScope::currentEngine())]()
531-
-> ll::coro::CoroTask<> {
529+
[paramNames,
530+
commandName(commandName),
531+
overloadFunc,
532+
e(EngineManager::checkAndGet(EngineScope::currentEngine(), true))]() -> ll::coro::CoroTask<> {
532533
auto cmd = CommandRegistrar::getInstance(false)
533534
.getOrCreateCommand(commandName)
534535
.runtimeOverload(getEngineData(e)->plugin);
@@ -543,7 +544,8 @@ Local<Value> CommandClass::addOverload(Arguments const& args) {
543544
if (args.size() == 0) {
544545
if (ll::getGamingStatus() == ll::GamingStatus::Starting) {
545546
ll::coro::keepThis(
546-
[commandName(commandName), e(EngineScope::currentEngine())]() -> ll::coro::CoroTask<> {
547+
[commandName(commandName),
548+
e(EngineManager::checkAndGet(EngineScope::currentEngine(), true))]() -> ll::coro::CoroTask<> {
547549
getEngineData(e)->plugin->registeredCommands[commandName].push_back({});
548550
auto cmd = CommandRegistrar::getInstance(false)
549551
.getOrCreateCommand(commandName)
@@ -603,7 +605,8 @@ Local<Value> CommandClass::addOverload(Arguments const& args) {
603605
if (arr.size() == 0) {
604606
if (ll::getGamingStatus() == ll::GamingStatus::Starting) {
605607
ll::coro::keepThis(
606-
[commandName(commandName), e(EngineScope::currentEngine())]() -> ll::coro::CoroTask<> {
608+
[commandName(commandName),
609+
e(EngineManager::checkAndGet(EngineScope::currentEngine(), true))]() -> ll::coro::CoroTask<> {
607610
getEngineData(e)->plugin->registeredCommands[commandName].push_back({});
608611
auto cmd = CommandRegistrar::getInstance(false)
609612
.getOrCreateCommand(commandName)
@@ -668,9 +671,12 @@ Local<Value> CommandClass::setCallback(Arguments const& args) const {
668671
CHECK_ARGS_COUNT(args, 1);
669672
CHECK_ARG_TYPE(args[0], ValueKind::kFunction);
670673
try {
671-
auto func = args[0].asFunction();
672-
localShareData
673-
->commandCallbacks[commandName] = {EngineScope::currentEngine(), 0, script::Global<Function>(func)};
674+
auto func = args[0].asFunction();
675+
localShareData->commandCallbacks[commandName] = {
676+
EngineManager::checkAndGet(EngineScope::currentEngine()),
677+
CommandPermissionLevel::Any,
678+
script::Global<Function>(func)
679+
};
674680
return Boolean::newBoolean(true);
675681
}
676682
CATCH_AND_THROW

src/legacy/api/CommandAPI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
2-
#include "legacy/api/CommandCompatibleAPI.h"
2+
3+
#include "legacy/utils/UsingScriptX.h"
34
#include "ll/api/command/CommandHandle.h"
45

56
extern ClassDefine<void> ParamTypeStaticBuilder;

0 commit comments

Comments
 (0)