Skip to content

Commit 731a502

Browse files
committed
refactor: replace some macro with inline
1 parent 781515b commit 731a502

19 files changed

+105
-91
lines changed

src/legacy/api/APIHelp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ std::string ValueKindToString(const ValueKind& kind);
4343
#define LOG_ERROR_WITH_SCRIPT_INFO(...) \
4444
PrintScriptStackTrace(__VA_ARGS__); \
4545
lse::getSelfPluginInstance().getLogger().error("In API: " __FUNCTION__); \
46-
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + ENGINE_OWN_DATA()->pluginName)
46+
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName)
4747

4848
// 参数类型错误输出
4949
#define LOG_WRONG_ARG_TYPE() LOG_ERROR_WITH_SCRIPT_INFO("Wrong type of argument!");
@@ -148,7 +148,7 @@ std::string ValueKindToString(const ValueKind& kind);
148148
catch (const Exception& e) { \
149149
PrintException(e); \
150150
lse::getSelfPluginInstance().getLogger().error(std::string("In callback for ") + callback); \
151-
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + ENGINE_OWN_DATA()->pluginName); \
151+
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
152152
}
153153

154154
#else

src/legacy/api/DatabaseAPI.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using namespace DB;
66
catch (const Exception& e) { \
77
lse::getSelfPluginInstance().getLogger().error(LOG); \
88
PrintException(e); \
9-
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + ENGINE_OWN_DATA()->pluginName); \
9+
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
1010
return Local<Value>(); \
1111
} \
1212
catch (const std::exception& e) { \
@@ -16,7 +16,7 @@ using namespace DB;
1616
lse::getSelfPluginInstance().getLogger().error("Uncaught Exception Detected!"); \
1717
PrintScriptStackTrace(); \
1818
lse::getSelfPluginInstance().getLogger().error("In API: " __FUNCTION__); \
19-
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + ENGINE_OWN_DATA()->pluginName); \
19+
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
2020
return Local<Value>(); \
2121
}
2222

@@ -173,7 +173,7 @@ KVDBClass::KVDBClass(const Local<Object>& scriptObj, const string& dir) : Script
173173
kvdb.reset();
174174
}
175175

176-
unloadCallbackIndex = ENGINE_OWN_DATA()->addUnloadCallback([&](ScriptEngine*) { kvdb.reset(); });
176+
unloadCallbackIndex = getEngineOwnData()->addUnloadCallback([&](ScriptEngine*) { kvdb.reset(); });
177177
}
178178

179179
KVDBClass::KVDBClass(const string& dir) : ScriptClass(script::ScriptClass::ConstructFromCpp<KVDBClass>{}) {
@@ -182,7 +182,7 @@ KVDBClass::KVDBClass(const string& dir) : ScriptClass(script::ScriptClass::Const
182182
} catch (...) {
183183
kvdb.reset();
184184
}
185-
unloadCallbackIndex = ENGINE_OWN_DATA()->addUnloadCallback([&](ScriptEngine*) { kvdb.reset(); });
185+
unloadCallbackIndex = getEngineOwnData()->addUnloadCallback([&](ScriptEngine*) { kvdb.reset(); });
186186
}
187187

188188
KVDBClass::~KVDBClass() {}
@@ -240,7 +240,7 @@ Local<Value> KVDBClass::del(const Arguments& args) {
240240
}
241241

242242
Local<Value> KVDBClass::close(const Arguments&) {
243-
ENGINE_OWN_DATA()->removeUnloadCallback(unloadCallbackIndex);
243+
getEngineOwnData()->removeUnloadCallback(unloadCallbackIndex);
244244
unloadCallbackIndex = -1;
245245
try {
246246
kvdb.reset();

src/legacy/api/EventAPI.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bool LLSEAddEventListener(ScriptEngine* engine, const string& eventName, const L
9797
return true;
9898
} catch (...) {
9999
lse::getSelfPluginInstance().getLogger().error(fmt::format("Event /{} / No Found!\n", eventName));
100-
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + ENGINE_GET_DATA(engine)->pluginName);
100+
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineData(engine)->pluginName);
101101
return false;
102102
}
103103
}
@@ -129,10 +129,10 @@ bool LLSECallEventsOnHotUnload(ScriptEngine* engine) {
129129
FakeCallEvent(engine, EVENT_TYPES::onLeft, PlayerClass::newPlayer(&pl));
130130
return true;
131131
});
132-
for (auto& [index, cb] : ENGINE_GET_DATA(engine)->unloadCallbacks) {
132+
for (auto& [index, cb] : getEngineData(engine)->unloadCallbacks) {
133133
cb(engine);
134134
}
135-
ENGINE_GET_DATA(engine)->unloadCallbacks.clear();
135+
getEngineData(engine)->unloadCallbacks.clear();
136136
return true;
137137
}
138138

src/legacy/api/EventAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ inline std::string EventTypeToString(EVENT_TYPES e) { return std::string(magic_e
139139
lse::getSelfPluginInstance().getLogger().error("Event Callback Failed!"); \
140140
lse::getSelfPluginInstance().getLogger().error(e.what()); \
141141
lse::getSelfPluginInstance().getLogger().error("In Event: " + EventTypeToString(TYPE)); \
142-
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + ENGINE_OWN_DATA()->pluginName); \
142+
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
143143
} \
144144
catch (const std::exception& e) { \
145145
lse::getSelfPluginInstance().getLogger().error("Event Callback Failed!"); \
146146
lse::getSelfPluginInstance().getLogger().error("C++ Uncaught Exception Detected!"); \
147147
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what())); \
148148
PrintScriptStackTrace(); \
149149
lse::getSelfPluginInstance().getLogger().error("In Event: " + EventTypeToString(TYPE)); \
150-
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + ENGINE_OWN_DATA()->pluginName); \
150+
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
151151
}
152152

153153
// 调用事件监听函数,拦截则取消事件

src/legacy/api/InternationalAPI.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void FormatHelper(
102102
Local<Value> TrFormat(const Arguments& args, size_t offset, std::string key, const std::string& localeName = "") {
103103
try {
104104
size_t argsLength = args.size() - offset;
105-
auto i18n = ENGINE_OWN_DATA()->i18n;
105+
auto i18n = getEngineOwnData()->i18n;
106106
if (i18n) {
107107
key = i18n->get(key, localeName);
108108
}
@@ -172,7 +172,7 @@ Local<Value> I18nClass::get(const Arguments& args) {
172172
if (args.size() == 2) {
173173
localeName = args[1].toStr();
174174
}
175-
auto i18n = ENGINE_OWN_DATA()->i18n;
175+
auto i18n = getEngineOwnData()->i18n;
176176
if (!i18n) {
177177
throw Exception("I18n data has not been loaded yet!");
178178
}
@@ -192,11 +192,11 @@ Local<Value> I18nClass::load(const Arguments& args) {
192192
}
193193

194194
try {
195-
auto path = args[0].toStr();
195+
auto path = args[0].toStr();
196196
// Deprecated because it follows LeviLamina's default locale
197197
// std::string defaultLocaleName;
198198
if (args.size() > 1) {
199-
// defaultLocaleName = args[1].toStr();
199+
// defaultLocaleName = args[1].toStr();
200200
}
201201

202202
if (args.size() > 2) {
@@ -222,8 +222,9 @@ Local<Value> I18nClass::load(const Arguments& args) {
222222
}
223223
}
224224

225-
EngineOwnData().i18n = std::make_shared<ll::i18n::I18n>();;
226-
if (auto result = EngineOwnData().i18n->load(path); !result) {
225+
EngineOwnData().i18n = std::make_shared<ll::i18n::I18n>();
226+
;
227+
if (auto result = EngineOwnData().i18n->load(path); !result) {
227228
return Boolean::newBoolean(false);
228229
}
229230
return Boolean::newBoolean(true);

src/legacy/api/LoggerAPI.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#include "api/APIHelp.h"
44
#include "api/PlayerAPI.h"
55
#include "engine/EngineOwnData.h"
6-
#include "ll/api/io/Logger.h"
6+
#include "ll/api/data/IndirectValue.h"
77
#include "ll/api/io/FileSink.h"
8+
#include "ll/api/io/Logger.h"
89
#include "ll/api/io/PatternFormatter.h"
9-
#include "ll/api/data/IndirectValue.h"
10+
#include "lse/api/PlayerSink.h"
1011
#include "mc/world/actor/player/Player.h"
1112
#include "utils/Utils.h"
12-
#include "lse/api/PlayerSink.h"
1313

1414
#include <fstream>
1515
#include <iostream>
@@ -52,14 +52,14 @@ string& StrReplace(string& str, const string& to_replaced, const string& new_str
5252
void inline LogDataHelper(LogLevel level, const Arguments& args) {
5353
std::string res;
5454
for (int i = 0; i < args.size(); ++i) res += ValueToString(args[i]);
55-
ENGINE_OWN_DATA()->getModInstance()->getLogger().log(level, res);
55+
getEngineOwnData()->getModInstance()->getLogger().log(level, res);
5656
}
5757

5858
Local<Value> LoggerClass::log(const Arguments& args) {
5959
CHECK_ARGS_COUNT(args, 1)
6060

6161
try {
62-
auto globalConf = ENGINE_OWN_DATA();
62+
auto globalConf = getEngineOwnData();
6363
LogDataHelper(LogLevel::Info, args);
6464
return Boolean::newBoolean(true);
6565
}
@@ -70,7 +70,7 @@ Local<Value> LoggerClass::debug(const Arguments& args) {
7070
CHECK_ARGS_COUNT(args, 1)
7171

7272
try {
73-
auto globalConf = ENGINE_OWN_DATA();
73+
auto globalConf = getEngineOwnData();
7474
LogDataHelper(LogLevel::Debug, args);
7575
return Boolean::newBoolean(true);
7676
}
@@ -81,7 +81,7 @@ Local<Value> LoggerClass::info(const Arguments& args) {
8181
CHECK_ARGS_COUNT(args, 1)
8282

8383
try {
84-
auto globalConf = ENGINE_OWN_DATA();
84+
auto globalConf = getEngineOwnData();
8585
LogDataHelper(LogLevel::Info, args);
8686
return Boolean::newBoolean(true);
8787
}
@@ -92,7 +92,7 @@ Local<Value> LoggerClass::warn(const Arguments& args) {
9292
CHECK_ARGS_COUNT(args, 1)
9393

9494
try {
95-
auto globalConf = ENGINE_OWN_DATA();
95+
auto globalConf = getEngineOwnData();
9696
LogDataHelper(LogLevel::Warn, args);
9797
return Boolean::newBoolean(true);
9898
}
@@ -103,7 +103,7 @@ Local<Value> LoggerClass::error(const Arguments& args) {
103103
CHECK_ARGS_COUNT(args, 1)
104104

105105
try {
106-
auto globalConf = ENGINE_OWN_DATA();
106+
auto globalConf = getEngineOwnData();
107107
LogDataHelper(LogLevel::Error, args);
108108
return Boolean::newBoolean(true);
109109
}
@@ -114,7 +114,7 @@ Local<Value> LoggerClass::fatal(const Arguments& args) {
114114
CHECK_ARGS_COUNT(args, 1)
115115

116116
try {
117-
auto globalConf = ENGINE_OWN_DATA();
117+
auto globalConf = getEngineOwnData();
118118
LogDataHelper(LogLevel::Fatal, args);
119119
return Boolean::newBoolean(true);
120120
}
@@ -127,7 +127,6 @@ Local<Value> LoggerClass::setTitle(const Arguments& args) {
127127
CHECK_ARG_TYPE(args[0], ValueKind::kString)
128128

129129
return Boolean::newBoolean(false);
130-
131130
}
132131

133132
///////////////// Helper /////////////////
@@ -139,11 +138,12 @@ Local<Value> LoggerClass::setConsole(const Arguments& args) {
139138

140139
try {
141140
if (args.size() >= 2) {
142-
ENGINE_OWN_DATA()->getModInstance()->getLogger().getSink(0)->setFlushLevel(static_cast<ll::io::LogLevel>(args[1].toInt() + 1
143-
)); // See LSE's definition https://legacy-script-engine.levimc.org/apis/ScriptAPI/Logger/
141+
getEngineOwnData()->getModInstance()->getLogger().getSink(0)->setFlushLevel(
142+
static_cast<ll::io::LogLevel>(args[1].toInt() + 1)
143+
); // See LSE's definition https://legacy-script-engine.levimc.org/apis/ScriptAPI/Logger/
144144
}
145145
if (!args[0].asBoolean().value()) {
146-
ENGINE_OWN_DATA()->getModInstance()->getLogger().getSink(0)->setFlushLevel(ll::io::LogLevel::Off);
146+
getEngineOwnData()->getModInstance()->getLogger().getSink(0)->setFlushLevel(ll::io::LogLevel::Off);
147147
}
148148
return Boolean::newBoolean(true);
149149
}
@@ -156,12 +156,20 @@ Local<Value> LoggerClass::setFile(const Arguments& args) {
156156
if (args.size() >= 2) CHECK_ARG_TYPE(args[1], ValueKind::kNumber)
157157

158158
try {
159-
std::filesystem::path newFile = std::filesystem::path(args[0].asString().toString());
160-
std::shared_ptr<ll::io::FileSink> sink = std::make_shared<ll::io::FileSink>(newFile, ll::makePolymorphic<ll::io::PatternFormatter>("{3:.3%T.} {2} {1} {0}", ll::io::Formatter::supportColorLog(), 0b0010), std::ios::app);
159+
std::filesystem::path newFile = std::filesystem::path(args[0].asString().toString());
160+
std::shared_ptr<ll::io::FileSink> sink = std::make_shared<ll::io::FileSink>(
161+
newFile,
162+
ll::makePolymorphic<ll::io::PatternFormatter>(
163+
"{3:.3%T.} {2} {1} {0}",
164+
ll::io::Formatter::supportColorLog(),
165+
0b0010
166+
),
167+
std::ios::app
168+
);
161169
if (args.size() >= 2) {
162170
sink->setFlushLevel(static_cast<LogLevel>(args[1].toInt() + 1));
163171
}
164-
return Boolean::newBoolean(ENGINE_OWN_DATA()->getModInstance()->getLogger().addSink(sink));
172+
return Boolean::newBoolean(getEngineOwnData()->getModInstance()->getLogger().addSink(sink));
165173
}
166174
CATCH("Fail in LoggerSetFile!")
167175
}
@@ -177,7 +185,7 @@ Local<Value> LoggerClass::setPlayer(const Arguments& args) {
177185
if (args.size() >= 2) {
178186
sink->setFlushLevel(static_cast<LogLevel>(args[1].toInt() + 1));
179187
}
180-
return Boolean::newBoolean(ENGINE_OWN_DATA()->getModInstance()->getLogger().addSink(sink));
188+
return Boolean::newBoolean(getEngineOwnData()->getModInstance()->getLogger().addSink(sink));
181189
}
182190
CATCH("Fail in LoggerSetPlayer!")
183191
}
@@ -187,7 +195,7 @@ Local<Value> LoggerClass::setLogLevel(const Arguments& args) {
187195
CHECK_ARG_TYPE(args[0], ValueKind::kNumber)
188196

189197
try {
190-
auto conf = ENGINE_OWN_DATA();
198+
auto conf = getEngineOwnData();
191199
conf->getModInstance()->getLogger().setLevel(static_cast<LogLevel>(args[0].toInt() + 1));
192200
return Boolean::newBoolean(true);
193201
}

src/legacy/api/NetworkAPI.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ Local<Value> WSClientClass::connect(const Arguments& args) {
321321
try {
322322

323323
string target = args[0].toStr();
324-
RecordOperation(ENGINE_OWN_DATA()->pluginName, "ConnectToWebsocketServer", target);
324+
RecordOperation(getEngineOwnData()->pluginName, "ConnectToWebsocketServer", target);
325325
ws->Connect(target);
326326
return Boolean::newBoolean(true);
327327
} catch (const std::runtime_error&) {
@@ -338,14 +338,14 @@ Local<Value> WSClientClass::connectAsync(const Arguments& args) {
338338

339339
try {
340340
string target = args[0].toStr();
341-
RecordOperation(ENGINE_OWN_DATA()->pluginName, "ConnectToWebsocketServer", target);
341+
RecordOperation(getEngineOwnData()->pluginName, "ConnectToWebsocketServer", target);
342342

343343
script::Global<Function> callbackFunc{args[1].asFunction()};
344344
std::thread([ws{this->ws},
345345
target,
346346
callback{std::move(callbackFunc)},
347347
engine{EngineScope::currentEngine()},
348-
pluginName{ENGINE_OWN_DATA()->pluginName}]() mutable {
348+
pluginName{getEngineOwnData()->pluginName}]() mutable {
349349

350350
#ifdef NDEBUG
351351
ll::error_utils::initExceptionTranslator();
@@ -738,7 +738,7 @@ Local<Value> HttpServerClass::listen(const Arguments& args) {
738738
if (port < 0 || port > 65535) {
739739
throw script::Exception("Invalid port number! (0~65535)");
740740
}
741-
RecordOperation(ENGINE_OWN_DATA()->pluginName, "StartHttpServer", fmt::format("on {}:{}", addr, port));
741+
RecordOperation(getEngineOwnData()->pluginName, "StartHttpServer", fmt::format("on {}:{}", addr, port));
742742
std::thread([this, addr, port]() {
743743
try {
744744
svr->stop();
@@ -756,7 +756,7 @@ Local<Value> HttpServerClass::stop(const Arguments& args) {
756756
CHECK_ARGS_COUNT(args, 0);
757757

758758
try {
759-
RecordOperation(ENGINE_OWN_DATA()->pluginName, "StopHttpServer", "");
759+
RecordOperation(getEngineOwnData()->pluginName, "StopHttpServer", "");
760760
svr->stop();
761761
return Local<Value>();
762762
}
@@ -1158,7 +1158,7 @@ Local<Value> NetworkClass::httpGet(const Arguments& args) {
11581158

11591159
try {
11601160
string target = args[0].toStr();
1161-
RecordOperation(ENGINE_OWN_DATA()->pluginName, "HttpGet", target);
1161+
RecordOperation(getEngineOwnData()->pluginName, "HttpGet", target);
11621162
script::Global<Function> callbackFunc{args[args.size() - 1].asFunction()};
11631163

11641164
auto lambda = [callback{std::move(callbackFunc)},
@@ -1206,7 +1206,7 @@ Local<Value> NetworkClass::httpPost(const Arguments& args) {
12061206

12071207
try {
12081208
string target = args[0].toStr();
1209-
RecordOperation(ENGINE_OWN_DATA()->pluginName, "HttpPost", target);
1209+
RecordOperation(getEngineOwnData()->pluginName, "HttpPost", target);
12101210
script::Global<Function> callbackFunc{args[args.size() - 1].asFunction()};
12111211

12121212
auto lambda = [callback{std::move(callbackFunc)},
@@ -1246,7 +1246,7 @@ Local<Value> NetworkClass::httpGetSync(const Arguments& args) {
12461246

12471247
try {
12481248
string target = args[0].toStr();
1249-
RecordOperation(ENGINE_OWN_DATA()->pluginName, "HttpGetSync", target);
1249+
RecordOperation(getEngineOwnData()->pluginName, "HttpGetSync", target);
12501250

12511251
int status;
12521252
string result;

0 commit comments

Comments
 (0)