Skip to content

Commit a107fec

Browse files
committed
refactor: replace more macros with function
1 parent bb59091 commit a107fec

File tree

5 files changed

+87
-74
lines changed

5 files changed

+87
-74
lines changed

src/legacy/api/APIHelp.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,20 @@ std::string ValueKindToString(const ValueKind& kind);
3636
#if !defined(NEW_DEFINES)
3737

3838
// 输出脚本调用堆栈,API名称,以及插件名
39-
#define LOG_ERROR_WITH_SCRIPT_INFO(...) \
40-
PrintScriptStackTrace(__VA_ARGS__); \
41-
lse::getSelfPluginInstance().getLogger().error("In API: " __FUNCTION__); \
42-
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName)
39+
inline void LOG_ERROR_WITH_SCRIPT_INFO(std::string const& msg = "") {
40+
PrintScriptStackTrace(msg);
41+
lse::getSelfPluginInstance().getLogger().error("In API: " __FUNCTION__);
42+
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
43+
}
4344

4445
// 参数类型错误输出
45-
#define LOG_WRONG_ARG_TYPE() LOG_ERROR_WITH_SCRIPT_INFO("Wrong type of argument!");
46+
inline void LOG_WRONG_ARG_TYPE() { LOG_ERROR_WITH_SCRIPT_INFO("Wrong type of argument!"); }
4647

4748
// 参数数量错误输出
48-
#define LOG_TOO_FEW_ARGS() LOG_ERROR_WITH_SCRIPT_INFO("Too Few arguments!");
49+
inline void LOG_TOO_FEW_ARGS() { LOG_ERROR_WITH_SCRIPT_INFO("Too Few arguments!"); }
4950

5051
// 参数数量错误输出
51-
#define LOG_WRONG_ARGS_COUNT() LOG_ERROR_WITH_SCRIPT_INFO("Wrong number of arguments!");
52+
inline void LOG_WRONG_ARGS_COUNT() { LOG_ERROR_WITH_SCRIPT_INFO("Wrong number of arguments!"); }
5253

5354
// 至少COUNT个参数
5455
#define CHECK_ARGS_COUNT(ARGS, COUNT) \

src/legacy/api/DatabaseAPI.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ Local<Value> DBSessionClass::prepare(const Arguments& args) {
349349
}
350350

351351
Local<Value> DBSessionClass::close(const Arguments& args) {
352-
CHECK_ARGS_COUNT(args, 0);
353352

354353
try {
355354
session->close();
@@ -359,7 +358,6 @@ Local<Value> DBSessionClass::close(const Arguments& args) {
359358
return Boolean::newBoolean(false);
360359
}
361360
Local<Value> DBSessionClass::isOpen(const Arguments& args) {
362-
CHECK_ARGS_COUNT(args, 0);
363361

364362
try {
365363
return Boolean::newBoolean(session->isOpen());
@@ -436,8 +434,6 @@ Local<Value> DBStmtClass::bind(const Arguments& args) {
436434
}
437435

438436
Local<Value> DBStmtClass::execute(const Arguments& args) {
439-
CHECK_ARGS_COUNT(args, 0);
440-
441437
try {
442438
stmt->execute();
443439
return this->getScriptObject();
@@ -446,8 +442,6 @@ Local<Value> DBStmtClass::execute(const Arguments& args) {
446442
}
447443

448444
Local<Value> DBStmtClass::step(const Arguments& args) {
449-
CHECK_ARGS_COUNT(args, 0);
450-
451445
try {
452446
return Boolean::newBoolean(stmt->step());
453447
}
@@ -456,8 +450,6 @@ Local<Value> DBStmtClass::step(const Arguments& args) {
456450
}
457451

458452
Local<Value> DBStmtClass::fetch(const Arguments& args) {
459-
CHECK_ARGS_COUNT(args, 0);
460-
461453
try {
462454
return RowToLocalValue(stmt->fetch());
463455
}
@@ -487,8 +479,6 @@ Local<Value> DBStmtClass::fetchAll(const Arguments& args) {
487479
}
488480

489481
Local<Value> DBStmtClass::reset(const Arguments& args) {
490-
CHECK_ARGS_COUNT(args, 0);
491-
492482
try {
493483
stmt->reset();
494484
return this->getScriptObject();
@@ -497,8 +487,6 @@ Local<Value> DBStmtClass::reset(const Arguments& args) {
497487
}
498488

499489
Local<Value> DBStmtClass::reexec(const Arguments& args) {
500-
CHECK_ARGS_COUNT(args, 0);
501-
502490
try {
503491
stmt->reexec();
504492
return this->getScriptObject();
@@ -507,8 +495,6 @@ Local<Value> DBStmtClass::reexec(const Arguments& args) {
507495
}
508496

509497
Local<Value> DBStmtClass::clear(const Arguments& args) {
510-
CHECK_ARGS_COUNT(args, 0);
511-
512498
try {
513499
stmt->clear();
514500
return this->getScriptObject();

src/legacy/api/NetworkAPI.cpp

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -445,45 +445,69 @@ using namespace httplib;
445445

446446
// ll::thread::TickSyncTaskPool taskPool;
447447

448-
#define ADD_CALLBACK(METHOD, path, func) \
449-
callbacks.emplace(make_pair( \
450-
path, \
451-
HttpServerCallback{ \
452-
EngineScope::currentEngine(), \
453-
script::Global<Function>{func}, \
454-
HttpRequestType::METHOD, \
455-
path \
456-
} \
457-
)); \
458-
svr->METHOD(path.c_str(), [this, engine = EngineScope::currentEngine()](const Request& req, Response& resp) { \
459-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(engine) \
460-
|| engine->isDestroying()) \
461-
return; \
462-
ll::coro::keepThis([this, engine, req, &resp]() -> ll::coro::CoroTask<> { \
463-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(engine) \
464-
|| engine->isDestroying()) \
465-
co_return; \
466-
\
467-
EngineScope enter(engine); \
468-
try { \
469-
for (auto& [k, v] : this->callbacks) { \
470-
if (v.type != HttpRequestType::METHOD) continue; \
471-
std::regex rgx(k); \
472-
std::smatch matches; \
473-
if (std::regex_match(req.path, matches, rgx)) { \
474-
if (matches == req.matches) { \
475-
auto reqObj = new HttpRequestClass(req); \
476-
auto respObj = new HttpResponseClass(resp); \
477-
v.func.get().call({}, reqObj, respObj); \
478-
resp = *respObj->get(); \
479-
break; \
480-
} \
481-
} \
482-
} \
483-
} \
484-
CATCH_CALLBACK_IN_CORO("Fail in NetworkAPI callback") \
485-
}).launch(ll::thread::ThreadPoolExecutor::getDefault()); \
486-
});
448+
void ADD_CALLBACK(
449+
std::shared_ptr<httplib::Server> svr,
450+
std::multimap<std::string, HttpServerCallback>& callbacks,
451+
HttpRequestType const& method,
452+
std::string const& path,
453+
Local<Function> const& func
454+
) {
455+
callbacks.emplace(
456+
make_pair(path, HttpServerCallback{EngineScope::currentEngine(), script::Global<Function>{func}, method, path})
457+
);
458+
auto receiveMethod =
459+
[engine = EngineScope::currentEngine(), method, callbacks](const Request& req, Response& resp) {
460+
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(engine)
461+
|| engine->isDestroying())
462+
return;
463+
ll::coro::keepThis([engine, req, &resp, method, callbacks]() -> ll::coro::CoroTask<> {
464+
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(engine)
465+
|| engine->isDestroying())
466+
co_return;
467+
468+
EngineScope enter(engine);
469+
try {
470+
for (auto& [k, v] : callbacks) {
471+
if (v.type != method) continue;
472+
std::regex rgx(k);
473+
std::smatch matches;
474+
if (std::regex_match(req.path, matches, rgx)) {
475+
if (matches == req.matches) {
476+
auto reqObj = new HttpRequestClass(req);
477+
auto respObj = new HttpResponseClass(resp);
478+
v.func.get().call({}, reqObj, respObj);
479+
resp = *respObj->get();
480+
break;
481+
}
482+
}
483+
}
484+
}
485+
CATCH_CALLBACK_IN_CORO("Fail in NetworkAPI callback")
486+
}).launch(ll::thread::ThreadPoolExecutor::getDefault());
487+
};
488+
switch (method) {
489+
case HttpRequestType::Get:
490+
svr->Get(path.c_str(), receiveMethod);
491+
break;
492+
case HttpRequestType::Post:
493+
svr->Post(path.c_str(), receiveMethod);
494+
break;
495+
case HttpRequestType::Put:
496+
svr->Put(path.c_str(), receiveMethod);
497+
break;
498+
case HttpRequestType::Delete:
499+
svr->Delete(path.c_str(), receiveMethod);
500+
break;
501+
case HttpRequestType::Options:
502+
svr->Options(path.c_str(), receiveMethod);
503+
break;
504+
case HttpRequestType::Patch:
505+
svr->Patch(path.c_str(), receiveMethod);
506+
break;
507+
default:
508+
break;
509+
}
510+
}
487511

488512
HttpServerClass::HttpServerClass(const Local<Object>& scriptObj) : ScriptClass(scriptObj), svr(new Server) {}
489513
HttpServerClass::HttpServerClass() : ScriptClass(ScriptClass::ConstructFromCpp<HttpServerClass>{}), svr(new Server) {}
@@ -500,7 +524,7 @@ Local<Value> HttpServerClass::onGet(const Arguments& args) {
500524
try {
501525
auto path = args[0].asString().toString();
502526
auto func = args[1].asFunction();
503-
ADD_CALLBACK(Get, path, func);
527+
ADD_CALLBACK(svr, callbacks, HttpRequestType::Get, path, func);
504528
return this->getScriptObject();
505529
}
506530
CATCH("Fail in onGet")
@@ -514,7 +538,7 @@ Local<Value> HttpServerClass::onPut(const Arguments& args) {
514538
try {
515539
auto path = args[0].asString().toString();
516540
auto func = args[1].asFunction();
517-
ADD_CALLBACK(Put, path, func);
541+
ADD_CALLBACK(svr, callbacks, HttpRequestType::Put, path, func);
518542
return this->getScriptObject();
519543
}
520544
CATCH("Fail in onPut!");
@@ -528,7 +552,7 @@ Local<Value> HttpServerClass::onPost(const Arguments& args) {
528552
try {
529553
auto path = args[0].asString().toString();
530554
auto func = args[1].asFunction();
531-
ADD_CALLBACK(Post, path, func);
555+
ADD_CALLBACK(svr, callbacks, HttpRequestType::Post, path, func);
532556
return this->getScriptObject();
533557
}
534558
CATCH("Fail in onPost!");
@@ -542,7 +566,7 @@ Local<Value> HttpServerClass::onPatch(const Arguments& args) {
542566
try {
543567
auto path = args[0].asString().toString();
544568
auto func = args[1].asFunction();
545-
ADD_CALLBACK(Patch, path, func);
569+
ADD_CALLBACK(svr, callbacks, HttpRequestType::Patch, path, func);
546570
return this->getScriptObject();
547571
}
548572
CATCH("Fail in onPatch!");
@@ -556,7 +580,7 @@ Local<Value> HttpServerClass::onDelete(const Arguments& args) {
556580
try {
557581
auto path = args[0].asString().toString();
558582
auto func = args[1].asFunction();
559-
ADD_CALLBACK(Delete, path, func);
583+
ADD_CALLBACK(svr, callbacks, HttpRequestType::Delete, path, func);
560584
return this->getScriptObject();
561585
}
562586
CATCH("Fail in onDelete!");
@@ -570,7 +594,7 @@ Local<Value> HttpServerClass::onOptions(const Arguments& args) {
570594
try {
571595
auto path = args[0].asString().toString();
572596
auto func = args[1].asFunction();
573-
ADD_CALLBACK(Options, path, func);
597+
ADD_CALLBACK(svr, callbacks, HttpRequestType::Options, path, func);
574598
return this->getScriptObject();
575599
}
576600
CATCH("Fail in onOptions!");

src/legacy/api/PacketAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ Local<Value> BinaryStreamClass::writeVec3(const Arguments& args) {
391391
return Local<Value>();
392392
}
393393
if (!IsInstanceOf<FloatPos>(args[0])) {
394-
LOG_WRONG_ARG_TYPE()
394+
LOG_WRONG_ARG_TYPE();
395395
return Local<Value>();
396396
}
397397
FloatPos* posObj = FloatPos::extractPos(args[0]);
@@ -412,7 +412,7 @@ Local<Value> BinaryStreamClass::writeCompoundTag(const Arguments& args) {
412412
}
413413
auto nbt = NbtCompoundClass::extract(args[0]);
414414
if (!nbt) {
415-
LOG_WRONG_ARG_TYPE()
415+
LOG_WRONG_ARG_TYPE();
416416
return Local<Value>();
417417
}
418418
pkt->writeType(*nbt);

src/legacy/engine/MessageSystem.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
using namespace script;
2121

22-
#define GET_MESSAGE_TYPE(ENGINE) (ModuleMessage::MessageType) ENGINE.data0
23-
#define GET_MESSAGE_HEADER(ENGINE) (ModuleMessage::MessageHeader*)ENGINE.ptr0
24-
#define GET_MESSAGE_DATA_PTR(ENGINE) (std::string*)ENGINE.ptr1
25-
#define MESSAGE_TYPE data0
26-
#define MESSAGE_HEADER ptr0
27-
#define MESSAGE_DATA_PTR ptr1
22+
inline ModuleMessage::MessageType GET_MESSAGE_TYPE(utils::Message& e) { return (ModuleMessage::MessageType)e.data0; }
23+
inline ModuleMessage::MessageHeader* GET_MESSAGE_HEADER(utils::Message& e) {
24+
return (ModuleMessage::MessageHeader*)e.ptr0;
25+
}
26+
inline std::string* GET_MESSAGE_DATA_PTR(utils::Message& e) { return (std::string*)e.ptr1; }
27+
#define MESSAGE_TYPE data0
28+
#define MESSAGE_HEADER ptr0
29+
#define MESSAGE_DATA_PTR ptr1
2830

2931
//////////////////// 消息处理注册 ////////////////////
3032

0 commit comments

Comments
 (0)