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
7778bool 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> {
504503Local<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
0 commit comments