Skip to content

Commit 48d40ee

Browse files
committed
refactor: remove some legacy code
feat: add nodejs plugin migration
1 parent a533105 commit 48d40ee

21 files changed

+98
-129
lines changed

src/legacy/api/CommandAPI.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "engine/EngineOwnData.h"
1313
#include "engine/GlobalShareData.h"
1414
#include "engine/LocalShareData.h"
15-
#include "legacyapi/utils/STLHelper.h"
1615
#include "ll/api/command/CommandRegistrar.h"
1716
#include "ll/api/service/Bedrock.h"
1817
#include "ll/api/service/ServerInfo.h"
@@ -76,7 +75,7 @@ ClassDefine<CommandClass> CommandClassBuilder =
7675
//////////////////// Helper ////////////////////
7776

7877
bool LLSERemoveCmdCallback(script::ScriptEngine* engine) {
79-
erase_if(localShareData->commandCallbacks, [&engine](auto& data) { return data.second.fromEngine == engine; });
78+
std::erase_if(localShareData->commandCallbacks, [&engine](auto& data) { return data.second.fromEngine == engine; });
8079
return true;
8180
}
8281

src/legacy/api/CommandCompatibleAPI.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "engine/EngineOwnData.h"
66
#include "engine/GlobalShareData.h"
77
#include "engine/LocalShareData.h"
8-
#include "legacyapi/utils/STLHelper.h"
98
#include "ll/api/event/EventBus.h"
109
#include "ll/api/event/server/ServerStartedEvent.h"
1110
#include "ll/api/memory/Memory.h"
@@ -56,8 +55,12 @@ void LLSERegisterNewCmd(
5655
}
5756

5857
bool LLSERemoveCmdRegister(ScriptEngine* engine) {
59-
erase_if(localShareData->playerCmdCallbacks, [&engine](auto& data) { return data.second.fromEngine == engine; });
60-
erase_if(localShareData->consoleCmdCallbacks, [&engine](auto& data) { return data.second.fromEngine == engine; });
58+
std::erase_if(localShareData->playerCmdCallbacks, [&engine](auto& data) {
59+
return data.second.fromEngine == engine;
60+
});
61+
std::erase_if(localShareData->consoleCmdCallbacks, [&engine](auto& data) {
62+
return data.second.fromEngine == engine;
63+
});
6164
return true;
6265
}
6366
// Helper

src/legacy/api/DataAPI.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// #include <llapi/PlayerInfoAPI.h>
1111
#include "legacyapi/Base64.h"
1212
#include "legacyapi/utils/FileHelper.h"
13+
#include "ll/api/io/FileUtils.h"
1314
#include "ll/api/service/Bedrock.h"
1415
#include "ll/api/service/PlayerInfo.h"
1516
#include "ll/api/utils/CryptoUtils.h"
@@ -104,7 +105,7 @@ Local<Value> ConfBaseClass::getPath(const Arguments& args) {
104105

105106
Local<Value> ConfBaseClass::read(const Arguments& args) {
106107
try {
107-
auto content = ReadAllFile(confPath);
108+
auto content = ll::file_utils::readFile(confPath);
108109
if (!content) return Local<Value>();
109110
else return String::newString(*content);
110111
}
@@ -226,7 +227,7 @@ Local<Value> ConfJsonClass::write(const Arguments& args) {
226227
CHECK_ARG_TYPE(args[0], ValueKind::kString);
227228

228229
try {
229-
bool res = WriteAllFile(confPath, args[0].toStr(), false);
230+
bool res = ll::file_utils::writeFile(confPath, args[0].toStr(), false);
230231
reload();
231232
return Boolean::newBoolean(res);
232233
}
@@ -248,7 +249,7 @@ bool ConfJsonClass::close() {
248249
}
249250

250251
bool ConfJsonClass::reload() {
251-
auto jsonTexts = ReadAllFile(confPath);
252+
auto jsonTexts = ll::file_utils::readFile(confPath);
252253
if (!jsonTexts) return false;
253254

254255
try {
@@ -501,7 +502,7 @@ Local<Value> ConfIniClass::write(const Arguments& args) {
501502
CHECK_ARG_TYPE(args[0], ValueKind::kString);
502503

503504
try {
504-
bool res = WriteAllFile(confPath, args[0].toStr(), false);
505+
bool res = ll::file_utils::writeFile(confPath, args[0].toStr(), false);
505506
reload();
506507
return Boolean::newBoolean(res);
507508
}

src/legacy/api/FileSystemAPI.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ FileClass* FileClass::constructor(const Arguments& args) {
113113
string path = args[0].toStr();
114114
std::optional<std::string> dirPath = getDirectoryPath(path);
115115
if (dirPath.has_value()) {
116-
CreateDirs(dirPath.value());
116+
std::filesystem::create_directory(dirPath.value());
117117
} else {
118118
LOG_ERROR_WITH_SCRIPT_INFO("Fail to create directory " + dirPath.value() + "!\n");
119119
return nullptr;
@@ -520,7 +520,7 @@ Local<Value> DirCreate(const Arguments& args) {
520520
CHECK_ARG_TYPE(args[0], ValueKind::kString);
521521

522522
try {
523-
return Boolean::newBoolean(CreateDirs(args[0].toStr()));
523+
return Boolean::newBoolean(std::filesystem::create_directory(args[0].toStr()));
524524
} catch (const filesystem_error& e) {
525525
LOG_ERROR_WITH_SCRIPT_INFO("Fail to Create Dir " + args[0].asString().toString() + "!\n");
526526
return Boolean::newBoolean(false);
@@ -648,7 +648,7 @@ Local<Value> GetFilesList(const Arguments& args) {
648648
CHECK_ARG_TYPE(args[0], ValueKind::kString);
649649

650650
try {
651-
auto fileList = GetFileNameList(args[0].toStr());
651+
auto fileList = lse::legacy::GetFileNameList(args[0].toStr());
652652

653653
Local<Array> arr = Array::newArray();
654654
for (auto& file : fileList) arr.add(String::newString(file));
@@ -662,9 +662,9 @@ Local<Value> FileReadFrom(const Arguments& args) {
662662
CHECK_ARG_TYPE(args[0], ValueKind::kString);
663663

664664
try {
665-
auto content = ReadAllFile(args[0].toStr());
665+
auto content = ll::file_utils::readFile(args[0].toStr());
666666
if (!content) return {}; // Null
667-
return String::newString(*content);
667+
return String::newString(content.value());
668668
}
669669
CATCH("Fail in FileReadAll!");
670670
}
@@ -678,12 +678,12 @@ Local<Value> FileWriteTo(const Arguments& args) {
678678
string path = args[0].toStr();
679679
std::optional<std::string> dirPath = getDirectoryPath(path);
680680
if (dirPath.has_value()) {
681-
CreateDirs(dirPath.value());
681+
std::filesystem::create_directory(dirPath.value());
682682
} else {
683683
LOG_ERROR_WITH_SCRIPT_INFO("Fail to create directory " + dirPath.value() + "!\n");
684684
return Boolean::newBoolean(false);
685685
}
686-
return Boolean::newBoolean(WriteAllFile(path, args[1].toStr(), false));
686+
return Boolean::newBoolean(ll::file_utils::writeFile(path, args[1].toStr(), false));
687687
}
688688
CATCH("Fail in FileWriteAll!");
689689
}
@@ -697,7 +697,7 @@ Local<Value> FileWriteLine(const Arguments& args) {
697697
string path = args[0].toStr();
698698
std::optional<std::string> dirPath = getDirectoryPath(path);
699699
if (dirPath.has_value()) {
700-
CreateDirs(dirPath.value());
700+
std::filesystem::create_directory(dirPath.value());
701701
} else {
702702
LOG_ERROR_WITH_SCRIPT_INFO("Fail to create directory " + dirPath.value() + "!\n");
703703
return Boolean::newBoolean(false);
@@ -723,7 +723,7 @@ Local<Value> OpenFile(const Arguments& args) {
723723
string path = args[0].toStr();
724724
std::optional<std::string> dirPath = getDirectoryPath(path);
725725
if (dirPath.has_value()) {
726-
CreateDirs(dirPath.value());
726+
std::filesystem::create_directory(dirPath.value());
727727
} else {
728728
LOG_ERROR_WITH_SCRIPT_INFO("Fail to create directory " + dirPath.value() + "!\n");
729729
return {};

src/legacy/api/LlAPI.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Local<Value> LlClass::getAllPluginInfo(const Arguments& args) {
215215
plugins.add(pluginObject);
216216
return true;
217217
});
218+
return plugins;
218219
}
219220
CATCH("Fail in LLAPI");
220221
}

src/legacy/api/NbtAPI.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void TagToJson_List_Helper(ordered_json& res, ListTag* nbt) {
191191
break;
192192
case Tag::Type::ByteArray: {
193193
auto& bytes = tag->as<ByteArrayTag>().data;
194-
char* tmpData;
194+
char tmpData[1024];
195195
for (unsigned int i = 0; i < bytes.size(); ++i) {
196196
tmpData[i] = bytes[i];
197197
}
@@ -246,7 +246,7 @@ void TagToJson_Compound_Helper(ordered_json& res, CompoundTag* nbt) {
246246
break;
247247
case Tag::Type::ByteArray: {
248248
auto& bytes = tag.as<ByteArrayTag>().data;
249-
char* tmpData;
249+
char tmpData[1024];
250250
for (unsigned int i = 0; i < bytes.size(); ++i) {
251251
tmpData[i] = bytes[i];
252252
}
@@ -893,7 +893,7 @@ Local<Value> NbtByteArrayClass::getType(const Arguments& args) { return Number::
893893
Local<Value> NbtByteArrayClass::get(const Arguments& args) {
894894
try {
895895
auto& data = nbt->data;
896-
char* buf;
896+
char buf[1024];
897897
for (unsigned int i = 0; i < data.size(); ++i) {
898898
buf[i] = data[i];
899899
}
@@ -2035,7 +2035,7 @@ Local<Value> Tag2Value_ListHelper(ListTag* nbt, bool autoExpansion = false) {
20352035
break;
20362036
case Tag::Type::ByteArray: {
20372037
auto& data = tag->as_ptr<ByteArrayTag>()->data;
2038-
char* buf;
2038+
char buf[1024];
20392039
for (unsigned int i = 0; i < data.size(); ++i) {
20402040
buf[i] = data[i];
20412041
}
@@ -2087,7 +2087,7 @@ Local<Value> Tag2Value_CompoundHelper(CompoundTag* nbt, bool autoExpansion) {
20872087
break;
20882088
case Tag::Type::ByteArray: {
20892089
auto& data = tag.get().as_ptr<ByteArrayTag>()->data;
2090-
char* buf;
2090+
char buf[1024];
20912091
for (unsigned int i = 0; i < data.size(); ++i) {
20922092
buf[i] = data[i];
20932093
}
@@ -2137,7 +2137,7 @@ Local<Value> Tag2Value(Tag* nbt, bool autoExpansion) {
21372137
break;
21382138
case Tag::Type::ByteArray: {
21392139
auto& data = nbt->as_ptr<ByteArrayTag>()->data;
2140-
char* buf;
2140+
char buf[1024];
21412141
for (unsigned int i = 0; i < data.size(); ++i) {
21422142
buf[i] = data[i];
21432143
}

src/legacy/api/ScoreboardAPI.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Local<Value> ObjectiveClass::setScore(const Arguments& args) {
122122
LOG_WRONG_ARG_TYPE();
123123
return Local<Value>();
124124
}
125+
return Local<Value>();
125126
}
126127
CATCH("Fail in setScore");
127128
}
@@ -167,6 +168,7 @@ Local<Value> ObjectiveClass::addScore(const Arguments& args) {
167168
LOG_WRONG_ARG_TYPE();
168169
return Local<Value>();
169170
}
171+
return Local<Value>();
170172
}
171173
CATCH("Fail in addScore");
172174
}
@@ -212,6 +214,7 @@ Local<Value> ObjectiveClass::reduceScore(const Arguments& args) {
212214
LOG_WRONG_ARG_TYPE();
213215
return Local<Value>();
214216
}
217+
return Local<Value>();
215218
}
216219
CATCH("Fail in removeScore");
217220
}

src/legacy/engine/EngineManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "engine/EngineOwnData.h"
44
#include "engine/GlobalShareData.h"
5-
#include "legacyapi/utils/STLHelper.h"
65
#include "ll/api/utils/StringUtils.h"
76
#include "main/NodeJsHelper.h"
87

src/legacy/engine/RemoteCall.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "api/LlAPI.h"
55
#include "engine/GlobalShareData.h"
66
#include "engine/MessageSystem.h"
7-
#include "legacyapi/utils/STLHelper.h"
87
#include "main/Configs.h"
98

109
#include <map>
@@ -146,7 +145,7 @@ bool LLSERemoveAllExportedFuncs(ScriptEngine* engine)
146145
{
147146
return LLSERemoveAllExportedFuncs_Debug(engine);
148147
#if 0
149-
erase_if(globalShareData->exportedFuncs, [&engine](auto& data) {
148+
std::erase_if(globalShareData->exportedFuncs, [&engine](auto& data) {
150149
return data.second.engine == engine;
151150
});
152151
return true;

src/legacy/engine/TimeTaskSystem.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "engine/EngineManager.h"
55
#include "engine/EngineOwnData.h"
66
#include "engine/MessageSystem.h"
7-
#include "legacyapi/utils/STLHelper.h"
87
#include "ll/api/schedule/Scheduler.h"
98

109
#include <chrono>

src/legacy/events/EventHooks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ LL_TYPE_INSTANCE_HOOK(
533533
);
534534
}
535535
IF_LISTENED_END(EVENT_TYPES::onBlockExplode);
536-
origin(region, source, pos, explosionRadius, fire, breaksBlocks, maxResistance, allowUnderwater);
536+
return origin(region, source, pos, explosionRadius, fire, breaksBlocks, maxResistance, allowUnderwater);
537537
}
538538

539539
LL_TYPE_STATIC_HOOK(

src/legacy/legacyapi/utils/FileHelper.cpp

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,7 @@
77
#include <filesystem>
88
#include <io.h>
99

10-
std::optional<std::string> ReadAllFile(const std::string& filePath, bool isBinary) {
11-
std::ifstream fRead;
12-
13-
std::ios_base::openmode mode = std::ios_base::in;
14-
if (isBinary) mode |= std::ios_base::binary;
15-
16-
fRead.open(ll::string_utils::str2wstr(filePath), mode);
17-
if (!fRead.is_open()) {
18-
return std::nullopt;
19-
}
20-
std::string data((std::istreambuf_iterator<char>(fRead)), std::istreambuf_iterator<char>());
21-
fRead.close();
22-
return data;
23-
}
24-
25-
bool WriteAllFile(const std::string& filePath, const std::string& content, bool isBinary) {
26-
std::ofstream fWrite;
27-
28-
std::ios_base::openmode mode = std::ios_base::out;
29-
if (isBinary) mode |= std::ios_base::binary;
30-
31-
fWrite.open(ll::string_utils::str2wstr(filePath), mode);
32-
if (!fWrite.is_open()) {
33-
return false;
34-
}
35-
fWrite << content;
36-
fWrite.close();
37-
return true;
38-
}
10+
namespace lse::legacy {
3911

4012
std::vector<std::string> GetFileNameList(const std::string& dir) {
4113
std::filesystem::directory_entry d(dir);
@@ -49,25 +21,6 @@ std::vector<std::string> GetFileNameList(const std::string& dir) {
4921
return list;
5022
}
5123

52-
bool CreateDirs(const std::string path) {
53-
std::error_code ec;
54-
auto ret = std::filesystem::create_directories(std::filesystem::path(ll::string_utils::str2wstr(path)), ec);
55-
if (ec.value() != 0) {
56-
lse::getSelfPluginInstance().getLogger().error("Fail to create dir, err code: {}", ec.value());
57-
lse::getSelfPluginInstance().getLogger().error(ec.message());
58-
}
59-
return ret;
60-
}
61-
62-
// From LiteLoaderBDSv2 llapi/utils/WinHelper.cpp
63-
wchar_t* str2cwstr(const std::string& str) {
64-
auto len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
65-
auto* buffer = new wchar_t[len + 1];
66-
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buffer, len + 1);
67-
buffer[len] = L'\0';
68-
return buffer;
69-
}
70-
7124
std::pair<int, std::string> NewProcessSync(const std::string& process, int timeLimit = -1, bool noReadOutput = true) {
7225
SECURITY_ATTRIBUTES sa;
7326
HANDLE hRead, hWrite;
@@ -84,9 +37,19 @@ std::pair<int, std::string> NewProcessSync(const std::string& process, int timeL
8437
si.hStdOutput = si.hStdError = hWrite;
8538
si.dwFlags = STARTF_USESTDHANDLES;
8639

87-
auto wCmd = str2cwstr(process);
88-
if (!CreateProcessW(nullptr, wCmd, nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi)) {
89-
delete[] wCmd;
40+
auto wCmd = ll::string_utils::str2wstr(process);
41+
if (!CreateProcessW(
42+
nullptr,
43+
const_cast<wchar_t*>(wCmd.c_str()),
44+
nullptr,
45+
nullptr,
46+
TRUE,
47+
0,
48+
nullptr,
49+
nullptr,
50+
&si,
51+
&pi
52+
)) {
9053
return {-1, ""};
9154
}
9255
CloseHandle(hWrite);
@@ -101,7 +64,6 @@ std::pair<int, std::string> NewProcessSync(const std::string& process, int timeL
10164
std::string strOutput;
10265
DWORD bytesRead, exitCode;
10366

104-
delete[] wCmd;
10567
GetExitCodeProcess(pi.hProcess, &exitCode);
10668
if (!noReadOutput) {
10769
while (true) {
@@ -118,10 +80,9 @@ std::pair<int, std::string> NewProcessSync(const std::string& process, int timeL
11880
std::pair<int, std::string> UncompressFile(const std::string& filePath, const std::string& toDir, int processTimeout) {
11981
std::error_code ec;
12082
std::filesystem::create_directories(toDir, ec);
121-
std::string realToDir = toDir.ends_with('/') ? toDir : toDir + "/";
122-
auto&& [exitCode, output] = NewProcessSync(
123-
fmt::format(R"({} x "{}" -o"{}" -aoa)", "./plugins/LegacyScriptEngine/7z/7za.exe", filePath, realToDir),
124-
processTimeout
125-
);
83+
std::string realToDir = toDir.ends_with('/') ? toDir : toDir + "/";
84+
auto&& [exitCode, output] =
85+
NewProcessSync(fmt::format(R"({} x "{}" -o"{}" -aoa)", "7za.exe", filePath, realToDir), processTimeout);
12686
return {exitCode, std::move(output)};
12787
}
88+
} // namespace lse::legacy

0 commit comments

Comments
 (0)