pack.get_available

This commit is contained in:
MihailRis
2024-04-16 04:56:36 +03:00
parent 04f0d44c23
commit 9a0e8f4540
3 changed files with 89 additions and 39 deletions
+54 -19
View File
@@ -3,6 +3,9 @@
#include "../scripting.h"
#include "../../../engine.h"
#include "../../../files/engine_paths.h"
#include "../../../files/WorldFiles.h"
#include "../../../world/Level.h"
#include "../../../world/World.h"
#include <string>
#include <filesystem>
@@ -35,26 +38,27 @@ 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;
/// @brief pack.get_available() -> array<string>
static int l_pack_get_available(lua_State* L) {
auto worldFolder = scripting::level->getWorld()->wfile->getFolder();
auto manager = scripting::engine->createPacksManager(worldFolder);
manager.scan();
auto& installed = scripting::engine->getContentPacks();
for (auto& pack : installed) {
manager.exclude(pack.id);
}
auto names = manager.getAllNames();
lua_createtable(L, names.size(), 0);
for (size_t i = 0; i < names.size(); i++) {
lua_pushstring(L, names[i].c_str());
lua_rawseti(L, -2, i + 1);
}
return 1;
}
static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* content) {
lua_createtable(L, 0, 5);
lua_pushstring(L, pack.title.c_str());
@@ -77,9 +81,40 @@ static int l_pack_get_info(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()) {
// TODO: optimize
auto worldFolder = scripting::level->getWorld()->wfile->getFolder();
auto manager = scripting::engine->createPacksManager(worldFolder);
manager.scan();
auto vec = manager.getAll({packid});
if (!vec.empty()) {
return l_pack_get_info(L, vec.at(0), content);
}
return 0;
}
const auto& pack = *found;
return l_pack_get_info(L, pack, content);
}
const luaL_Reg packlib [] = {
{"get_folder", lua_wrap_errors<l_pack_get_folder>},
{"get_installed", lua_wrap_errors<l_pack_get_installed>},
{"get_available", lua_wrap_errors<l_pack_get_available>},
{"get_info", lua_wrap_errors<l_pack_get_info>},
{NULL, NULL}
};