diff --git a/Changelog.md b/Changelog.md index 31d7627..f798e41 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,7 @@ - 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 FS.ListFiles and FS.ListDirectories # v3.0.1 diff --git a/include/LuaAPI.h b/include/LuaAPI.h index e317a23..23d40c0 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -23,7 +23,9 @@ namespace MP { void Sleep(size_t Ms); void PrintRaw(sol::variadic_args); std::string JsonEncode(const sol::table& object); + std::string JsonDiff(const std::string& a, const std::string& b); } + namespace FS { std::pair CreateDirectory(const std::string& Path); std::pair Remove(const std::string& Path); diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 973841d..64d1a9c 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -192,6 +192,8 @@ private: sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port); sol::table Lua_JsonDecode(const std::string& str); 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::atomic_bool& mShutdown; diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index 1baa467..a7ced6f 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -431,3 +431,17 @@ std::string LuaAPI::MP::JsonEncode(const sol::table& object) { } 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(); +} diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 21adcc2..5ae4b0d 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -421,6 +421,32 @@ int TLuaEngine::StateThreadData::Lua_GetPlayerIDByName(const std::string& Name) 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) { auto MaybeClient = GetClient(mEngine->Server(), ID); if (MaybeClient && !MaybeClient.value().expired()) { @@ -532,7 +558,7 @@ sol::table TLuaEngine::StateThreadData::Lua_JsonDecode(const std::string& str) { sol::state_view StateView(mState); auto table = StateView.create_table(); 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; } 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) { return Lua_JsonDecode(str); }); + MPTable.set_function("JsonDiff", &LuaAPI::MP::JsonDiff); auto HttpTable = StateView.create_named_table("Http"); 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("IsFile", &LuaAPI::FS::IsFile); 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(); }