Skip to content

Commit

Permalink
fix: fix onPlayerCmd
Browse files Browse the repository at this point in the history
fix: replace CallEventDelayed with CallEventRtnVoid because it has lock problem
refactor: modify logs path to logs/LegacyScriptEngine
refactor: fix ServerStartedEvent
refactor: provide temporary plugin load function
  • Loading branch information
ShrBox committed Jan 28, 2024
1 parent b972ee9 commit 9e71532
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 45 deletions.
28 changes: 15 additions & 13 deletions src/api/CommandCompatibleAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@

//////////////////// Helper ////////////////////

void RegisterCmd(const string& cmd, const string& describe, int cmdLevel) {
ll::event::EventBus::getInstance().emplaceListener<ll::event::ServerStartedEvent>(
[&](ll::event::ServerStartedEvent& ev) {
auto& registry = ll::service::getCommandRegistry().get();
registry.registerCommand(
cmd,
describe.c_str(),
(CommandPermissionLevel)cmdLevel,
{(CommandFlagValue)0},
{(CommandFlagValue)0x80}
);
}
void RegisterCmd(const std::string& cmd, const std::string& describe, int cmdLevel) {
auto& registry = ll::service::getCommandRegistry().get();
registry.registerCommand(
cmd,
describe.c_str(),
(CommandPermissionLevel)cmdLevel,
{(CommandFlagValue)0},
{(CommandFlagValue)0x80}
);
}
// Helper
void LLSERegisterNewCmd(bool isPlayerCmd, string cmd, const string& describe, int level, Local<Function> func) {
void LLSERegisterNewCmd(
bool isPlayerCmd,
std::string cmd,
const std::string& describe,
int level,
Local<Function> func
) {
if (cmd[0] == '/') cmd = cmd.erase(0, 1);

if (isPlayerCmd) {
Expand Down
33 changes: 18 additions & 15 deletions src/api/EventAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "ll/api/event/player/PlayerSwingEvent.h"
#include "ll/api/event/player/PlayerUseItemEvent.h"
#include "ll/api/event/player/PlayerUseItemOnEvent.h"
#include "ll/api/event/server/ServerStartedEvent.h"
#include "ll/api/memory/Hook.h"
#include "ll/api/schedule/Scheduler.h"
#include "ll/api/schedule/Task.h"
Expand Down Expand Up @@ -1416,7 +1417,10 @@ void InitBasicEventListeners() {
IF_LISTENED_END(EVENT_TYPES::onConsoleCmd);
}
} else if (ev.commandContext().mOrigin->getOriginType() == CommandOriginType::Player) {
std::string cmd = ev.commandContext().mCommand;
std::string cmd = ev.commandContext().mCommand;
if (cmd.starts_with("/")) {
cmd.erase(0, 1);
}
std::vector<std::string> paras;
bool isFromOtherEngine = false;
std::string prefix = LLSEFindCmdReg(true, cmd, paras, &isFromOtherEngine);
Expand Down Expand Up @@ -1486,6 +1490,19 @@ void InitBasicEventListeners() {
// return true;
// });

// ===== onServerStarted =====
bus.emplaceListener<ServerStartedEvent>([](ServerStartedEvent& ev) {
IF_LISTENED(EVENT_TYPES::onServerStarted) {
// CallEventDelayed(EVENT_TYPES::onServerStarted);
CallEventVoid(EVENT_TYPES::onServerStarted);
}
IF_LISTENED_END(EVENT_TYPES::onServerStarted);
isCmdRegisterEnabled = true;

// 处理延迟注册
ProcessRegCmdQueue();
});

// 植入tick
ll::schedule::ServerTimeScheduler scheduler;
scheduler.add<ll::schedule::RepeatTask>(ll::chrono::ticks(1), []() {
Expand Down Expand Up @@ -1543,20 +1560,6 @@ page, totalPages, Boolean::newBoolean(shouldDropBook));
}
*/

void LLSECallServerStartedEvent() {
IF_LISTENED(EVENT_TYPES::onServerStarted) { CallEventDelayed(EVENT_TYPES::onServerStarted); }
catch (...) {
logger.error("Event Callback Failed!");
logger.error("Uncaught Exception Detected!");
logger.error("In Event: " + EventTypeToString(EVENT_TYPES::onServerStarted));
}
}
isCmdRegisterEnabled = true;

// 处理延迟注册
ProcessRegCmdQueue();
}

bool MoneyBeforeEventCallback(LLMoneyEvent type, xuid_t from, xuid_t to, money_t value) {
switch (type) {
case LLMoneyEvent::Add: {
Expand Down
2 changes: 0 additions & 2 deletions src/api/EventAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ bool LLSECallEventsOnHotUnload(ScriptEngine* engine);

bool MoneyBeforeEventCallback(LLMoneyEvent type, xuid_t from, xuid_t to, money_t value);
bool MoneyEventCallback(LLMoneyEvent type, xuid_t from, xuid_t to, money_t value);

void LLSECallServerStartedEvent();
1 change: 0 additions & 1 deletion src/lse/LegacyScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ bool LegacyScriptEngine::enable() {

// Code for enabling the plugin goes here.
RegisterDebugCommand();
LLSECallServerStartedEvent();
return true;
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/Loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ void LoadMain() {
if (i.is_regular_file() && i.path().extension() == LLSE_SOURCE_FILE_EXTENSION) {
try {
// Todo
// if (PluginManager::loadPlugin(
// ll::string_utils::u8str2str(i.path().generic_u8string()),
// false, true))
++count;
if (PluginManager::loadPlugin(ll::string_utils::u8str2str(i.path().generic_u8string()), false, true))
++count;
} catch (...) {}
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ bool PluginManager::loadPlugin(const std::string& fileOrDirPath, bool isHotLoad,
return loadPluginPackage(realPath, fileOrDirPath, isHotLoad, isUncompressedFirstTime);
}

// Todo
// Single file plugin
// Check duplicated
if (PluginManager::getPlugin(pluginFileName)) {
// logger.error("This plugin has been loaded by LiteLoader. You cannot load
// it twice.");
return false;
}
// if (PluginManager::getPlugin(pluginFileName)) {
// // logger.error("This plugin has been loaded by LiteLoader. You cannot load
// // it twice.");
// return false;
// }

ScriptEngine* engine = nullptr;
try {
Expand Down Expand Up @@ -161,9 +162,10 @@ bool PluginManager::loadPlugin(const std::string& fileOrDirPath, bool isHotLoad,
std::string const& pluginName = ENGINE_OWN_DATA()->pluginName;
ExitEngineScope exit;

// Todo
// If plugin itself doesn't register, help it to do so
if (!PluginManager::getPlugin(pluginName))
PluginManager::registerPlugin(realPath, pluginName, pluginName, ll::Version(1, 0, 0), {});
// if (!PluginManager::getPlugin(pluginName))
// PluginManager::registerPlugin(realPath, pluginName, pluginName, ll::Version(1, 0, 0), {});

// Call necessary events when at hot load
if (isHotLoad) LLSECallEventsOnHotLoad(engine);
Expand Down Expand Up @@ -287,7 +289,9 @@ bool PluginManager::reloadAllPlugins() {
return true;
}

ll::plugin::Plugin* PluginManager::getPlugin(std::string name) { return PluginManager::getPlugin(std::move(name)); }
ll::plugin::Plugin* PluginManager::getPlugin(std::string name) { // Todo
return {};
}

// Get all plugins of current language
std::unordered_map<std::string, ll::plugin::Plugin*> PluginManager::getLocalPlugins() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/SafeGuardRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
std::ofstream record;

void InitSafeGuardRecord() {
std::filesystem::create_directories("logs/LiteLoader");
std::filesystem::create_directories("logs/LegacyScriptEngine");
record.open(
std::string("logs/LiteLoader/Sensitive_Operation_Records-") + LLSE_BACKEND_TYPE + ".log",
std::string("logs/LegacyScriptEngine/Sensitive_Operation_Records-") + LLSE_BACKEND_TYPE + ".log",
std::ios::app
);
}
Expand Down

0 comments on commit 9e71532

Please sign in to comment.