Skip to content

Commit

Permalink
fix: fix debug command
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Jan 23, 2024
1 parent c682c34 commit 8e57b13
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 72 deletions.
82 changes: 39 additions & 43 deletions src/api/EventAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "api/EventAPI.h"

#include "../main/BuiltinCommands.h"
#include "CommandCompatibleAPI.h"
#include "EntityAPI.h"
#include "api/APIHelp.h"
#include "api/McAPI.h"
Expand All @@ -8,6 +10,7 @@
#include "engine/GlobalShareData.h"
#include "ll/api/chrono/GameChrono.h"
#include "ll/api/event/EventBus.h"
#include "ll/api/event/command/ExecuteCommandEvent.h"
#include "ll/api/event/player/PlayerChatEvent.h"
#include "ll/api/event/player/PlayerConnectEvent.h"
#include "ll/api/event/player/PlayerDieEvent.h"
Expand All @@ -18,13 +21,16 @@
#include "ll/api/schedule/Task.h"
#include "ll/api/service/Bedrock.h"
#include "main/Global.h"
#include "mc/server/commands/CommandOriginType.h"
#include "mc/world/actor/player/Player.h"
#include "mc/world/level/dimension/Dimension.h"

#include <exception>
#include <list>
#include <shared_mutex>



//////////////////// Listeners ////////////////////

enum class EVENT_TYPES : int {
Expand Down Expand Up @@ -1319,6 +1325,7 @@ void EnableEventListener(int eventId) {

void InitBasicEventListeners() {
using namespace ll::event;
EventBus& bus = EventBus::getInstance();

// Event::PlayerCmdEvent::subscribe([](const PlayerCmdEvent &ev) {
// string cmd = ev.mCommand;
Expand Down Expand Up @@ -1356,49 +1363,38 @@ void InitBasicEventListeners() {
// }
// return true;
// });

// Event::ConsoleCmdEvent::subscribe_ref([](ConsoleCmdEvent &ev) {
// string cmd = ev.mCommand;

// // PreProcess
// if (!ProcessDebugEngine(cmd))
// return false;
// if (!ProcessOldHotManageCommand(ev.mCommand))
// return false;
// #ifdef LLSE_BACKEND_NODEJS
// if (!NodeJsHelper::processConsoleNpmCmd(ev.mCommand))
// return false;
// #elif defined(LLSE_BACKEND_PYTHON)
// if (!PythonHelper::processConsolePipCmd(ev.mCommand))
// return false;
// #endif
// // CallEvents
// vector<string> paras;
// bool isFromOtherEngine = false;
// string prefix = LLSEFindCmdReg(false, cmd, paras, &isFromOtherEngine);

// if (!prefix.empty()) {
// // LLSE Registered Cmd

// bool callbackRes = CallServerCmdCallback(prefix, paras);
// IF_LISTENED(EVENT_TYPES::onConsoleCmd) {
// CallEvent(EVENT_TYPES::onConsoleCmd, String::newString(cmd));
// }
// IF_LISTENED_END(EVENT_TYPES::onConsoleCmd);
// if (!callbackRes)
// return false;
// } else {
// if (isFromOtherEngine)
// return false;

// // Other Cmd
// IF_LISTENED(EVENT_TYPES::onConsoleCmd) {
// CallEvent(EVENT_TYPES::onConsoleCmd, String::newString(cmd));
// }
// IF_LISTENED_END(EVENT_TYPES::onConsoleCmd);
// }
// return true;
// });
bus.emplaceListener<ExecuteCommandEvent>([](ExecuteCommandEvent& ev) {
if (ev.commandContext().getCommandOrigin().getOriginType() == CommandOriginType::DedicatedServer) {
string cmd = ev.commandContext().mCommand;

if (!ProcessDebugEngine(cmd)) return false;
#ifdef LLSE_BACKEND_NODEJS
if (!NodeJsHelper::processConsoleNpmCmd(ev.mCommand)) return false;
#elif defined(LLSE_BACKEND_PYTHON)
if (!PythonHelper::processConsolePipCmd(ev.mCommand)) return false;
#endif
// CallEvents
vector<string> paras;
bool isFromOtherEngine = false;
string prefix = LLSEFindCmdReg(false, cmd, paras, &isFromOtherEngine);

if (!prefix.empty()) {
// LLSE Registered Cmd

bool callbackRes = CallServerCmdCallback(prefix, paras);
IF_LISTENED(EVENT_TYPES::onConsoleCmd) { CallEvent(EVENT_TYPES::onConsoleCmd, String::newString(cmd)); }
IF_LISTENED_END(EVENT_TYPES::onConsoleCmd);
if (!callbackRes) return false;
} else {
if (isFromOtherEngine) return false;

// Other Cmd
IF_LISTENED(EVENT_TYPES::onConsoleCmd) { CallEvent(EVENT_TYPES::onConsoleCmd, String::newString(cmd)); }
IF_LISTENED_END(EVENT_TYPES::onConsoleCmd);
}
}
return true;
});

// // Plugin Hot Management
// Event::ScriptPluginManagerEvent::subscribe_ref(
Expand Down
28 changes: 0 additions & 28 deletions src/main/BuiltinCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,3 @@ bool ProcessDebugEngine(const std::string& cmd) {
}
return true;
}

bool StartsWith(const std::string& str, const std::string& start) {
size_t srcLen = str.size();
size_t startLen = start.size();
if (srcLen >= startLen) {
string temp = str.substr(0, startLen);
if (temp == start) return true;
}

return false;
}

#define FIX_OLD_COMMAND(OLDCMD, NEWCMD) \
if (StartsWith(cmd, OLDCMD)) { \
logger.warn("* Please use command " #NEWCMD " instead."); \
cmd.replace(0, 3, "ll"); \
}

bool ProcessOldHotManageCommand(std::string& cmd) {
FIX_OLD_COMMAND("lxl list", "ll list");
FIX_OLD_COMMAND("lxl load", "ll load");
FIX_OLD_COMMAND("lxl unload", "ll unload");
FIX_OLD_COMMAND("lxl reload", "ll reload");
FIX_OLD_COMMAND("lxl version", "ll version");
FIX_OLD_COMMAND("lxl update", "ll upgrade");

return true;
}
1 change: 0 additions & 1 deletion src/main/BuiltinCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
#include <string>

bool ProcessDebugEngine(const std::string& cmd);
bool ProcessOldHotManageCommand(std::string& cmd);

0 comments on commit 8e57b13

Please sign in to comment.