Skip to content

Commit 78705bc

Browse files
committed
refactor: adapt to LeviLamina 1.0.0 (WIP)
1 parent 0b7960d commit 78705bc

25 files changed

+146
-81
lines changed

src/legacy/api/DataAPI.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "ll/api/io/FileUtils.h"
1414
#include "ll/api/service/Bedrock.h"
1515
#include "ll/api/service/PlayerInfo.h"
16-
#include "ll/api/utils/CryptoUtils.h"
16+
#include "mc/deps/crypto/hash/Hash.h"
1717
#include "ll/api/utils/StringUtils.h"
1818
#include "main/EconomicSystem.h"
1919
#include "mc/world/actor/player/Player.h"
@@ -876,7 +876,7 @@ Local<Value> DataClass::toMD5(const Arguments& args) {
876876
CHECK_ARGS_COUNT(args, 1);
877877

878878
try {
879-
string data;
879+
std::string data;
880880
if (args[0].isString()) data = args[0].toStr();
881881
else if (args[0].isByteBuffer()) {
882882
Local<ByteBuffer> buf = args[0].asByteBuffer();
@@ -885,7 +885,7 @@ Local<Value> DataClass::toMD5(const Arguments& args) {
885885
LOG_WRONG_ARG_TYPE();
886886
return Local<Value>();
887887
}
888-
return String::newString(ll::crypto_utils::md5(data));
888+
return String::newString(Crypto::Hash::hash(Crypto::Hash::HashType::Md5, data));
889889
}
890890
CATCH("Fail in ToMD5!");
891891
}
@@ -894,7 +894,7 @@ Local<Value> DataClass::toSHA1(const Arguments& args) {
894894
CHECK_ARGS_COUNT(args, 1);
895895

896896
try {
897-
string data;
897+
std::string data;
898898
if (args[0].isString()) data = args[0].toStr();
899899
else if (args[0].isByteBuffer()) {
900900
Local<ByteBuffer> buf = args[0].asByteBuffer();
@@ -903,7 +903,7 @@ Local<Value> DataClass::toSHA1(const Arguments& args) {
903903
LOG_WRONG_ARG_TYPE();
904904
return Local<Value>();
905905
}
906-
return String::newString(ll::crypto_utils::sha1(data));
906+
return String::newString(Crypto::Hash::hash(Crypto::Hash::HashType::Md5, data));
907907
}
908908
CATCH("Fail in ToSHA1!");
909909
}

src/legacy/api/DeviceAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include "api/APIHelp.h"
3-
#include "mc/entity/WeakEntityRef.h"
4-
#include "mc/world/ActorRuntimeID.h"
3+
#include "mc/deps/ecs/WeakEntityRef.h"
4+
#include "mc/common/ActorRuntimeID.h"
55

66
//////////////////// Classes ////////////////////
77
class Player;

src/legacy/api/EntityAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22
#include "api/APIHelp.h"
3-
#include "mc/entity/WeakEntityRef.h"
3+
#include "mc/deps/ecs/WeakEntityRef.h"
44
#include "mc/world/ActorRuntimeID.h"
55

66
//////////////////// Classes ////////////////////

src/legacy/api/LoggerAPI.cpp

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
#include "api/APIHelp.h"
44
#include "api/PlayerAPI.h"
55
#include "engine/EngineOwnData.h"
6-
#include "ll/api/Logger.h"
7-
#include "ll/api/service/Bedrock.h"
6+
#include "ll/api/io/Logger.h"
7+
#include "ll/api/io/FileSink.h"
8+
#include "ll/api/io/PatternFormatter.h"
9+
#include "ll/api/data/IndirectValue.h"
810
#include "mc/world/actor/player/Player.h"
9-
#include "mc/world/level/Level.h"
1011
#include "utils/Utils.h"
12+
#include "lse/api/PlayerSink.h"
1113

1214
#include <fstream>
1315
#include <iostream>
1416
#include <string>
1517
#include <string_view>
1618

19+
using ll::io::LogLevel;
20+
1721
//////////////////// Classes ////////////////////
1822

1923
ClassDefine<void> LoggerClassBuilder = defineClass("logger")
@@ -45,18 +49,36 @@ string& StrReplace(string& str, const string& to_replaced, const string& new_str
4549
}
4650
////////////////// Helper //////////////////
4751

48-
void inline LogDataHelper(ll::OutputStream* outStream, const Arguments& args) {
52+
void inline LogDataHelper(LogLevel level, const Arguments& args) {
4953
std::string res;
5054
for (int i = 0; i < args.size(); ++i) res += ValueToString(args[i]);
51-
(*outStream)(res);
55+
switch (level) {
56+
case LogLevel::Fatal:
57+
ENGINE_OWN_DATA()->getModInstance()->getLogger().fatal(res);
58+
break;
59+
case LogLevel::Error:
60+
ENGINE_OWN_DATA()->getModInstance()->getLogger().error(res);
61+
break;
62+
case LogLevel::Warn:
63+
ENGINE_OWN_DATA()->getModInstance()->getLogger().warn(res);
64+
break;
65+
case LogLevel::Info:
66+
ENGINE_OWN_DATA()->getModInstance()->getLogger().info(res);
67+
break;
68+
case LogLevel::Debug:
69+
ENGINE_OWN_DATA()->getModInstance()->getLogger().debug(res);
70+
break;
71+
default:
72+
break;
73+
}
5274
}
5375

5476
Local<Value> LoggerClass::log(const Arguments& args) {
5577
CHECK_ARGS_COUNT(args, 1)
5678

5779
try {
5880
auto globalConf = ENGINE_OWN_DATA();
59-
LogDataHelper(&globalConf->logger.info, args);
81+
LogDataHelper(LogLevel::Info, args);
6082
return Boolean::newBoolean(true);
6183
}
6284
CATCH("Fail in LoggerLog!")
@@ -67,7 +89,7 @@ Local<Value> LoggerClass::debug(const Arguments& args) {
6789

6890
try {
6991
auto globalConf = ENGINE_OWN_DATA();
70-
LogDataHelper(&globalConf->logger.debug, args);
92+
LogDataHelper(LogLevel::Debug, args);
7193
return Boolean::newBoolean(true);
7294
}
7395
CATCH("Fail in LoggerDebug!")
@@ -78,7 +100,7 @@ Local<Value> LoggerClass::info(const Arguments& args) {
78100

79101
try {
80102
auto globalConf = ENGINE_OWN_DATA();
81-
LogDataHelper(&globalConf->logger.info, args);
103+
LogDataHelper(LogLevel::Info, args);
82104
return Boolean::newBoolean(true);
83105
}
84106
CATCH("Fail in LoggerInfo!")
@@ -89,7 +111,7 @@ Local<Value> LoggerClass::warn(const Arguments& args) {
89111

90112
try {
91113
auto globalConf = ENGINE_OWN_DATA();
92-
LogDataHelper(&globalConf->logger.warn, args);
114+
LogDataHelper(LogLevel::Warn, args);
93115
return Boolean::newBoolean(true);
94116
}
95117
CATCH("Fail in LoggerWarn!")
@@ -100,7 +122,7 @@ Local<Value> LoggerClass::error(const Arguments& args) {
100122

101123
try {
102124
auto globalConf = ENGINE_OWN_DATA();
103-
LogDataHelper(&globalConf->logger.error, args);
125+
LogDataHelper(LogLevel::Error, args);
104126
return Boolean::newBoolean(true);
105127
}
106128
CATCH("Fail in LoggerError!")
@@ -111,30 +133,21 @@ Local<Value> LoggerClass::fatal(const Arguments& args) {
111133

112134
try {
113135
auto globalConf = ENGINE_OWN_DATA();
114-
LogDataHelper(&globalConf->logger.fatal, args);
136+
LogDataHelper(LogLevel::Fatal, args);
115137
return Boolean::newBoolean(true);
116138
}
117139
CATCH("Fail in LoggerFatal!")
118140
}
119141

142+
// Deprecated
120143
Local<Value> LoggerClass::setTitle(const Arguments& args) {
121144
CHECK_ARGS_COUNT(args, 1)
122145
CHECK_ARG_TYPE(args[0], ValueKind::kString)
123146

124-
try {
125-
ENGINE_OWN_DATA()->logger.title = args[0].asString().toString();
126-
return Boolean::newBoolean(true);
127-
}
128-
CATCH("Fail in LoggerSetTitle!")
129-
}
147+
return Boolean::newBoolean(false);
130148

131-
///////////////// Helper /////////////////
132-
void UpdateMaxLogLevel() {
133-
auto data = ENGINE_OWN_DATA();
134-
data->maxLogLevel = data->logger.consoleLevel;
135-
if (data->maxLogLevel < data->logger.fileLevel) data->maxLogLevel = data->logger.fileLevel;
136-
if (data->maxLogLevel < data->logger.playerLevel) data->maxLogLevel = data->logger.playerLevel;
137149
}
150+
138151
///////////////// Helper /////////////////
139152

140153
Local<Value> LoggerClass::setConsole(const Arguments& args) {
@@ -144,12 +157,12 @@ Local<Value> LoggerClass::setConsole(const Arguments& args) {
144157

145158
try {
146159
if (args.size() >= 2) {
147-
ENGINE_OWN_DATA()->logger.consoleLevel = args[1].toInt();
160+
ENGINE_OWN_DATA()->getModInstance()->getLogger().getSink(0)->setFlushLevel(static_cast<ll::io::LogLevel>(args[1].toInt() + 1
161+
)); // See LSE's definition https://legacy-script-engine.levimc.org/apis/ScriptAPI/Logger/
148162
}
149163
if (!args[0].asBoolean().value()) {
150-
ENGINE_OWN_DATA()->logger.consoleLevel = 0;
164+
ENGINE_OWN_DATA()->getModInstance()->getLogger().getSink(0)->setFlushLevel(ll::io::LogLevel::Off);
151165
}
152-
UpdateMaxLogLevel();
153166
return Boolean::newBoolean(true);
154167
}
155168
CATCH("Fail in LoggerSetConsole!")
@@ -161,14 +174,12 @@ Local<Value> LoggerClass::setFile(const Arguments& args) {
161174
if (args.size() >= 2) CHECK_ARG_TYPE(args[1], ValueKind::kNumber)
162175

163176
try {
164-
string newFile = args[0].asString().toString();
165-
ENGINE_OWN_DATA()->logger.setFile(newFile, std::ios::app);
166-
177+
std::filesystem::path newFile = std::filesystem::path(args[0].asString().toString());
178+
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);
167179
if (args.size() >= 2) {
168-
ENGINE_OWN_DATA()->logger.fileLevel = args[1].toInt();
169-
UpdateMaxLogLevel();
180+
sink->setFlushLevel(static_cast<LogLevel>(args[1].toInt() + 1));
170181
}
171-
return Boolean::newBoolean(ENGINE_OWN_DATA()->logger.ofs.value().is_open());
182+
return Boolean::newBoolean(ENGINE_OWN_DATA()->getModInstance()->getLogger().addSink(sink));
172183
}
173184
CATCH("Fail in LoggerSetFile!")
174185
}
@@ -180,14 +191,11 @@ Local<Value> LoggerClass::setPlayer(const Arguments& args) {
180191
if (!player) {
181192
return Boolean::newBoolean(false);
182193
}
183-
ENGINE_OWN_DATA()->logger.setPlayerOutputFunc([uuid(player->getUuid())](std::string_view str) {
184-
ll::service::getLevel()->getPlayer(uuid)->sendMessage(str);
185-
});
194+
std::shared_ptr<lse::io::PlayerSink> sink = std::make_shared<lse::io::PlayerSink>(player->getUuid());
186195
if (args.size() >= 2) {
187-
ENGINE_OWN_DATA()->logger.fileLevel = args[1].toInt();
188-
UpdateMaxLogLevel();
196+
sink->setFlushLevel(static_cast<LogLevel>(args[1].toInt() + 1));
189197
}
190-
return Boolean::newBoolean(true);
198+
return Boolean::newBoolean(ENGINE_OWN_DATA()->getModInstance()->getLogger().addSink(sink));
191199
}
192200
CATCH("Fail in LoggerSetPlayer!")
193201
}
@@ -198,8 +206,7 @@ Local<Value> LoggerClass::setLogLevel(const Arguments& args) {
198206

199207
try {
200208
auto conf = ENGINE_OWN_DATA();
201-
conf->maxLogLevel = conf->logger.consoleLevel = conf->logger.fileLevel = conf->logger.playerLevel =
202-
args[0].toInt();
209+
conf->getModInstance()->getLogger().setFlushLevel(static_cast<LogLevel>(args[0].toInt() + 1));
203210
return Boolean::newBoolean(true);
204211
}
205212
CATCH("Fail in SetLogLevel!")

src/legacy/api/PlayerAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include "api/APIHelp.h"
3-
#include "mc/entity/WeakEntityRef.h"
4-
#include "mc/world/ActorRuntimeID.h"
3+
#include "mc/deps/ecs/WeakEntityRef.h"
4+
#include "mc/common/ActorRuntimeID.h"
55

66
class SimulatedPlayer;
77

src/legacy/api/ScriptAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Local<Value> FastLog(const Arguments& args) {
104104
for (int i = 0; i < args.size(); ++i) PrintValue(sout, args[i]);
105105

106106
pool.addTask([str{sout.str()}, pluginName{ENGINE_OWN_DATA()->pluginName}]() {
107-
ll::Logger fastLogger(pluginName);
107+
ll::io::Logger fastLogger(pluginName);
108108
fastLogger.info(str);
109109
});
110110
return Boolean::newBoolean(true);

src/legacy/engine/EngineOwnData.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include "utils/UsingScriptX.h"
66

77
#include <ll/api/Expected.h>
8-
#include <ll/api/Logger.h>
8+
#include <ll/api/io/Logger.h>
9+
#include <ll/api/io/LoggerRegistry.h>
910
#include <ll/api/i18n/I18n.h>
1011
#include <map>
1112
#include <string>
@@ -68,11 +69,7 @@ struct EngineOwnData {
6869
*/
6970

7071
// I18nAPI
71-
ll::i18n::I18N* i18n = nullptr;
72-
73-
// LoggerAPI
74-
ll::Logger logger = ll::Logger("");
75-
int maxLogLevel = 4;
72+
ll::i18n::I18n* i18n = nullptr;
7673

7774
// 玩家绑定数据
7875
std::unordered_map<std::string, script::Global<Value>> playerDataDB;
@@ -94,6 +91,10 @@ struct EngineOwnData {
9491
dynamicCallVM = dcNewCallVM(4096);
9592
dcMode(dynamicCallVM, DC_CALL_C_DEFAULT);
9693
}
94+
95+
inline std::shared_ptr<ll::mod::Mod> getModInstance() {
96+
return lse::getPluginManager().getMod(pluginName);
97+
}
9798
};
9899

99100
// 引擎附加数据

src/legacy/legacyapi/command/DynamicCommand.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <algorithm>
3434
#include <cstddef>
3535
#include <cstdint>
36-
#include <ll/api/Logger.h>
36+
#include <ll/api/io/Logger.h>
3737
#include <ll/api/command/CommandRegistrar.h>
3838
#include <memory>
3939
#include <sstream>
@@ -132,7 +132,7 @@ auto const ParameterSizeMap = std::unordered_map<ParameterType, size_t>{
132132
};
133133

134134
inline void OutputError(const std::string& command, const std::string& func = __builtin_FUNCTION()) {
135-
auto lock = ll::Logger::lock();
135+
auto lock = ll::io::Logger::lock();
136136
ll::error_utils::printCurrentException(logger);
137137
logger.error("In Function ({})", func);
138138
logger.error("In Command <{}>", command);

src/legacy/legacyapi/command/DynamicCommand.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include "ll/api/memory/Memory.h"
88
#include "ll/api/service/Bedrock.h"
99
#include "magic_enum.hpp"
10-
#include "mc/deps/core/common/bedrock/typeid_t.h"
10+
#include "mc/deps/core/utility/typeid_t.h"
1111
#include "mc/deps/json/Value.h"
12-
#include "mc/math/Vec3.h"
12+
#include "mc/deps/core/math/Vec3.h"
1313
#include "mc/server/commands/CommandBlockName.h"
1414
#include "mc/server/commands/CommandFlag.h"
1515
#include "mc/server/commands/CommandItem.h"
@@ -29,7 +29,7 @@
2929
#include "mc/world/actor/player/Player.h"
3030
#include "mc/world/effect/MobEffect.h"
3131
#include "mc/world/level/BlockPos.h"
32-
#include "mc/world/level/Command.h"
32+
#include "mc/server/commands/Command.h"
3333

3434
#include <algorithm>
3535
#include <cstddef>
@@ -126,7 +126,7 @@ class DynamicCommandInstance;
126126
*/
127127
class DynamicCommand : public Command {
128128
template <class T>
129-
static constexpr bool valid_type_v = ll::concepts::is_one_of_v<
129+
static constexpr bool valid_type_v = ll::traits::is_one_of_v<
130130
T,
131131
bool,
132132
int,

src/legacy/legacyapi/db/ConnParams.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "ll/api/utils/StringUtils.h"
44

5-
#include <ll/api/Logger.h>
5+
#include <ll/api/io/Logger.h>
66

77
namespace DB {
88

@@ -78,7 +78,7 @@ URL ParseURL(const std::string& url) {
7878
return result;
7979
}
8080

81-
extern ll::Logger dbLogger;
81+
extern ll::io::Logger dbLogger;
8282
void PrintURL(const URL& url) {
8383
dbLogger.debug("Parsed URL");
8484
dbLogger.debug("scheme: {}", url.scheme);

0 commit comments

Comments
 (0)