From 1c82eeafd207944381260d24dfc4015ee6e9eb3a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 4 Apr 2024 01:54:52 +0300 Subject: [PATCH] pack.get_info(packid) --- src/logic/scripting/lua/libpack.cpp | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/logic/scripting/lua/libpack.cpp b/src/logic/scripting/lua/libpack.cpp index 67335a01..f2e2554c 100644 --- a/src/logic/scripting/lua/libpack.cpp +++ b/src/logic/scripting/lua/libpack.cpp @@ -35,8 +35,51 @@ static int l_pack_get_installed(lua_State* L) { return 1; } +/// @brief pack.get_info(packid: str) -> { +/// title: str, +/// creator: str, +/// description: str, +/// version: str, +/// [optional] has_indices: bool +/// } or nil +static int l_pack_get_info(lua_State* L) { + auto packid = lua_tostring(L, 1); + + auto content = scripting::engine->getContent(); + auto& packs = scripting::engine->getContentPacks(); + auto found = std::find_if(packs.begin(), packs.end(), [packid](auto& pack) { + return pack.id == packid; + }); + if (found == packs.end()) { + return 0; + } + const auto& pack = *found; + + lua_createtable(L, 0, 5); + + lua_pushstring(L, pack.title.c_str()); + lua_setfield(L, -2, "title"); + + lua_pushstring(L, pack.creator.c_str()); + lua_setfield(L, -2, "creator"); + + lua_pushstring(L, pack.description.c_str()); + lua_setfield(L, -2, "description"); + + lua_pushstring(L, pack.version.c_str()); + lua_setfield(L, -2, "version"); + + auto runtime = content ? content->getPackRuntime(pack.id) : nullptr; + if (runtime) { + lua_pushboolean(L, runtime->getStats().hasSavingContent()); + lua_setfield(L, -2, "has_indices"); + } + return 1; +} + const luaL_Reg packlib [] = { {"get_folder", lua_wrap_errors}, {"get_installed", lua_wrap_errors}, + {"get_info", lua_wrap_errors}, {NULL, NULL} };