Skip to content

Commit

Permalink
fix: fix onServerStarted & old command registration
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Jan 23, 2024
1 parent d781399 commit 83796f9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 59 deletions.
29 changes: 11 additions & 18 deletions src/api/EventAPI.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "api/EventAPI.h"

#include "../engine/LocalShareData.h"
#include "../engine/TimeTaskSystem.h"
#include "../main/BuiltinCommands.h"
#include "CommandCompatibleAPI.h"
#include "EntityAPI.h"
Expand Down Expand Up @@ -1441,24 +1442,6 @@ void InitBasicEventListeners() {
// return true;
// });

// // For RegisterCmd...
// Event::RegCmdEvent::subscribe([](const RegCmdEvent &ev) {
// isCmdRegisterEnabled = true;

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

// // ===== onServerStarted =====
// Event::ServerStartedEvent::subscribe([](Event::ServerStartedEvent ev) {
// IF_LISTENED(EVENT_TYPES::onServerStarted) {
// CallEventDelayed(EVENT_TYPES::onServerStarted);
// }
// IF_LISTENED_END(EVENT_TYPES::onServerStarted);
// return true;
// });

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

bool LLSECallServerStartedEvent() {
IF_LISTENED(EVENT_TYPES::onServerStarted) { CallEventDelayed(EVENT_TYPES::onServerStarted); }
IF_LISTENED_END(EVENT_TYPES::onServerStarted);
isCmdRegisterEnabled = true;

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

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

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);
bool MoneyEventCallback(LLMoneyEvent type, xuid_t from, xuid_t to, money_t value);

bool LLSECallServerStartedEvent();
72 changes: 35 additions & 37 deletions src/engine/TimeTaskSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ struct TimeTaskData {
script::Global<String> code;
ScriptEngine* engine;
inline void swap(TimeTaskData& rhs) {
std::swap(rhs.task, task);
std::swap(rhs.engine, engine);
rhs.code.swap(code);
rhs.paras.swap(paras);
rhs.func.swap(func);
std::swap(rhs.task, task);
std::swap(rhs.engine, engine);
rhs.code.swap(code);
rhs.paras.swap(paras);
rhs.func.swap(func);
}
};
std::unordered_map<int, TimeTaskData> timeTaskMap;
Expand All @@ -55,38 +55,36 @@ std::unordered_map<int, TimeTaskData> timeTaskMap;

//////////////////// API ////////////////////

// void NewTimeout_s(script::Global<Function> func, vector<script::Local<Value>>
// paras, int timeout, ScriptEngine* engine)
//{
// std::vector<script::Global<Value>> tmp;
// if (paras.size() > 0) {
// EngineScope enter(engine);
// for (auto& para : paras)
// tmp.emplace_back(std::move(para));
// }
// Schedule::delay(
// [engine, func = std::move(func), paras = std::move(tmp)]() {
// if ((ll::getServerStatus() != ll::ServerStatus::Running))
// return;
// if (!EngineManager::isValid(engine))
// return;
// EngineScope enter(engine);
// if (paras.empty()) {
// func.get().call();
// }
// else
// {
// vector<Local<Value>> args;
// for (auto& para : paras)
// if (para.isEmpty())
// return;
// else
// args.emplace_back(para.get());
// func.get().call({}, args);
// }
// },
// timeout / 50);
// }
void NewTimeout_s(
script::Global<Function> func,
vector<script::Local<Value>> paras,
int timeout,
ScriptEngine* engine
) {
std::vector<script::Global<Value>> tmp;
if (paras.size() > 0) {
EngineScope enter(engine);
for (auto& para : paras) tmp.emplace_back(std::move(para));
}
ll::schedule::ServerTimeScheduler scheduler;
scheduler.add<ll::schedule::DelayTask>(
ll::chrono::ticks(timeout / 50),
[engine, func = std::move(func), paras = std::move(tmp)]() {
if ((ll::getServerStatus() != ll::ServerStatus::Running)) return;
if (!EngineManager::isValid(engine)) return;
EngineScope enter(engine);
if (paras.empty()) {
func.get().call();
} else {
vector<Local<Value>> args;
for (auto& para : paras)
if (para.isEmpty()) return;
else args.emplace_back(para.get());
func.get().call({}, args);
}
}
);
}

int NewTimeout(Local<Function> func, vector<Local<Value>> paras, int timeout) {
int tid = ++timeTaskId;
Expand Down
10 changes: 7 additions & 3 deletions src/engine/TimeTaskSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

///////////////////////// API /////////////////////////

// void NewTimeout_s(script::Global<Function> func, vector<script::Local<Value>> paras, int timeout, ScriptEngine*
// engine = EngineScope::currentEngine());
void NewTimeout_s(
script::Global<Function> func,
vector<script::Local<Value>> paras,
int timeout,
ScriptEngine* engine = EngineScope::currentEngine()
);

int NewTimeout(Local<Function> func, std::vector<Local<Value>> paras, int timeout);
int NewTimeout(Local<String> func, int timeout);
Expand All @@ -18,4 +22,4 @@ bool ClearTimeTask(int id);

///////////////////////// Func /////////////////////////

void LLSERemoveTimeTaskData(ScriptEngine* engine);
void LLSERemoveTimeTaskData(ScriptEngine* engine);

0 comments on commit 83796f9

Please sign in to comment.