mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-08-17 08:46:22 +00:00
add FS.ListFiles, FS.ListDirectories
This commit is contained in:
parent
dd5cf1a4af
commit
7d97c3b560
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
- ADDED lua debug facilities (type `:help` when attached to lua via `lua`)
|
- ADDED lua debug facilities (type `:help` when attached to lua via `lua`)
|
||||||
- ADDED MP.JsonEncode() and MP.JsonDecode(), which turn lua tables into json and vice-versa
|
- ADDED MP.JsonEncode() and MP.JsonDecode(), which turn lua tables into json and vice-versa
|
||||||
|
- ADDED FS.ListFiles and FS.ListDirectories
|
||||||
|
|
||||||
# v3.0.1
|
# v3.0.1
|
||||||
|
|
||||||
|
@ -23,7 +23,9 @@ namespace MP {
|
|||||||
void Sleep(size_t Ms);
|
void Sleep(size_t Ms);
|
||||||
void PrintRaw(sol::variadic_args);
|
void PrintRaw(sol::variadic_args);
|
||||||
std::string JsonEncode(const sol::table& object);
|
std::string JsonEncode(const sol::table& object);
|
||||||
|
std::string JsonDiff(const std::string& a, const std::string& b);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace FS {
|
namespace FS {
|
||||||
std::pair<bool, std::string> CreateDirectory(const std::string& Path);
|
std::pair<bool, std::string> CreateDirectory(const std::string& Path);
|
||||||
std::pair<bool, std::string> Remove(const std::string& Path);
|
std::pair<bool, std::string> Remove(const std::string& Path);
|
||||||
|
@ -192,6 +192,8 @@ private:
|
|||||||
sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port);
|
sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port);
|
||||||
sol::table Lua_JsonDecode(const std::string& str);
|
sol::table Lua_JsonDecode(const std::string& str);
|
||||||
int Lua_GetPlayerIDByName(const std::string& Name);
|
int Lua_GetPlayerIDByName(const std::string& Name);
|
||||||
|
sol::table Lua_FS_ListFiles(const std::string& Path);
|
||||||
|
sol::table Lua_FS_ListDirectories(const std::string& Path);
|
||||||
|
|
||||||
std::string mName;
|
std::string mName;
|
||||||
std::atomic_bool& mShutdown;
|
std::atomic_bool& mShutdown;
|
||||||
|
@ -431,3 +431,17 @@ std::string LuaAPI::MP::JsonEncode(const sol::table& object) {
|
|||||||
}
|
}
|
||||||
return json.dump();
|
return json.dump();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string LuaAPI::MP::JsonDiff(const std::string& a, const std::string& b) {
|
||||||
|
if (!nlohmann::json::accept(a)) {
|
||||||
|
beammp_lua_error("JsonDiff first argument is not valid json: `" + a + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (!nlohmann::json::accept(b)) {
|
||||||
|
beammp_lua_error("JsonDiff second argument is not valid json: `" + b + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
auto a_json = nlohmann::json::parse(a);
|
||||||
|
auto b_json = nlohmann::json::parse(b);
|
||||||
|
return nlohmann::json::diff(a_json, b_json).dump();
|
||||||
|
}
|
||||||
|
@ -421,6 +421,32 @@ int TLuaEngine::StateThreadData::Lua_GetPlayerIDByName(const std::string& Name)
|
|||||||
return Id;
|
return Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sol::table TLuaEngine::StateThreadData::Lua_FS_ListFiles(const std::string& Path) {
|
||||||
|
if (!std::filesystem::exists(Path)) {
|
||||||
|
return sol::lua_nil;
|
||||||
|
}
|
||||||
|
auto table = mStateView.create_table();
|
||||||
|
for (const auto& entry : std::filesystem::directory_iterator(Path)) {
|
||||||
|
if (entry.is_regular_file() || entry.is_symlink()) {
|
||||||
|
table[table.size() + 1] = entry.path().lexically_relative(Path).string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
sol::table TLuaEngine::StateThreadData::Lua_FS_ListDirectories(const std::string& Path) {
|
||||||
|
if (!std::filesystem::exists(Path)) {
|
||||||
|
return sol::lua_nil;
|
||||||
|
}
|
||||||
|
auto table = mStateView.create_table();
|
||||||
|
for (const auto& entry : std::filesystem::directory_iterator(Path)) {
|
||||||
|
if (entry.is_directory()) {
|
||||||
|
table[table.size() + 1] = entry.path().lexically_relative(Path).string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
std::string TLuaEngine::StateThreadData::Lua_GetPlayerName(int ID) {
|
std::string TLuaEngine::StateThreadData::Lua_GetPlayerName(int ID) {
|
||||||
auto MaybeClient = GetClient(mEngine->Server(), ID);
|
auto MaybeClient = GetClient(mEngine->Server(), ID);
|
||||||
if (MaybeClient && !MaybeClient.value().expired()) {
|
if (MaybeClient && !MaybeClient.value().expired()) {
|
||||||
@ -532,7 +558,7 @@ sol::table TLuaEngine::StateThreadData::Lua_JsonDecode(const std::string& str) {
|
|||||||
sol::state_view StateView(mState);
|
sol::state_view StateView(mState);
|
||||||
auto table = StateView.create_table();
|
auto table = StateView.create_table();
|
||||||
if (!nlohmann::json::accept(str)) {
|
if (!nlohmann::json::accept(str)) {
|
||||||
beammp_lua_error("string given to JsonDecode is not valid json.");
|
beammp_lua_error("string given to JsonDecode is not valid json: `" + str + "`");
|
||||||
return sol::lua_nil;
|
return sol::lua_nil;
|
||||||
}
|
}
|
||||||
nlohmann::json json = nlohmann::json::parse(str);
|
nlohmann::json json = nlohmann::json::parse(str);
|
||||||
@ -638,6 +664,7 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
|
|||||||
MPTable.set_function("JsonDecode", [this](const std::string& str) {
|
MPTable.set_function("JsonDecode", [this](const std::string& str) {
|
||||||
return Lua_JsonDecode(str);
|
return Lua_JsonDecode(str);
|
||||||
});
|
});
|
||||||
|
MPTable.set_function("JsonDiff", &LuaAPI::MP::JsonDiff);
|
||||||
|
|
||||||
auto HttpTable = StateView.create_named_table("Http");
|
auto HttpTable = StateView.create_named_table("Http");
|
||||||
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
||||||
@ -665,6 +692,12 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
|
|||||||
FSTable.set_function("IsDirectory", &LuaAPI::FS::IsDirectory);
|
FSTable.set_function("IsDirectory", &LuaAPI::FS::IsDirectory);
|
||||||
FSTable.set_function("IsFile", &LuaAPI::FS::IsFile);
|
FSTable.set_function("IsFile", &LuaAPI::FS::IsFile);
|
||||||
FSTable.set_function("ConcatPaths", &LuaAPI::FS::ConcatPaths);
|
FSTable.set_function("ConcatPaths", &LuaAPI::FS::ConcatPaths);
|
||||||
|
FSTable.set_function("ListFiles", [this](const std::string& Path) {
|
||||||
|
return Lua_FS_ListFiles(Path);
|
||||||
|
});
|
||||||
|
FSTable.set_function("ListDirectories", [this](const std::string& Path) {
|
||||||
|
return Lua_FS_ListDirectories(Path);
|
||||||
|
});
|
||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user