world creation content menu

This commit is contained in:
MihailRis
2024-05-04 21:27:28 +03:00
parent 5402872c28
commit bab806c2f8
9 changed files with 82 additions and 23 deletions
+25 -15
View File
@@ -178,7 +178,6 @@ void EngineController::createWorld(
EnginePaths* paths = engine->getPaths();
auto folder = paths->getWorldsFolder()/fs::u8path(name);
try {
engine->loadAllPacks();
engine->loadContent();
paths->setWorldFolder(folder);
} catch (const contentpack_error& error) {
@@ -223,7 +222,6 @@ void EngineController::reconfigPacks(
std::vector<std::string> packsToRemove
) {
auto content = engine->getContent();
auto world = controller->getLevel()->getWorld();
bool hasIndices = false;
std::stringstream ss;
@@ -238,21 +236,33 @@ void EngineController::reconfigPacks(
}
runnable removeFunc = [=]() {
controller->saveWorld();
auto manager = engine->createPacksManager(world->wfile->getFolder());
manager.scan();
if (controller == nullptr) {
auto manager = engine->createPacksManager(fs::path(""));
manager.scan();
std::vector<std::string> names = engine->getBasePacks();
for (auto& name : packsToAdd) {
names.push_back(name);
}
engine->getContentPacks() = manager.getAll(names);
} else {
auto world = controller->getLevel()->getWorld();
auto wfile = world->wfile.get();
controller->saveWorld();
auto manager = engine->createPacksManager(wfile->getFolder());
manager.scan();
auto names = PacksManager::getNames(world->getPacks());
for (const auto& id : packsToAdd) {
names.push_back(id);
auto names = PacksManager::getNames(world->getPacks());
for (const auto& id : packsToAdd) {
names.push_back(id);
}
for (const auto& id : packsToRemove) {
manager.exclude(id);
names.erase(std::find(names.begin(), names.end(), id));
}
wfile->removeIndices(packsToRemove);
wfile->writePacks(manager.getAll(names));
reopenWorld(world);
}
for (const auto& id : packsToRemove) {
manager.exclude(id);
names.erase(std::find(names.begin(), names.end(), id));
}
world->wfile->removeIndices(packsToRemove);
world->wfile->writePacks(manager.getAll(names));
reopenWorld(world);
};
if (hasIndices) {
+21 -3
View File
@@ -30,6 +30,7 @@ static int l_pack_get_folder(lua_State* L) {
return 1;
}
/// @brief pack.get_installed() -> array<string>
static int l_pack_get_installed(lua_State* L) {
auto& packs = scripting::engine->getContentPacks();
lua_createtable(L, packs.size(), 0);
@@ -40,9 +41,12 @@ static int l_pack_get_installed(lua_State* L) {
return 1;
}
/// @brief pack.get_available() -> array<string>
/// @brief pack.get_available() -> array<string>
static int l_pack_get_available(lua_State* L) {
auto worldFolder = scripting::level->getWorld()->wfile->getFolder();
fs::path worldFolder("");
if (scripting::level) {
worldFolder = scripting::level->getWorld()->wfile->getFolder();
}
auto manager = scripting::engine->createPacksManager(worldFolder);
manager.scan();
@@ -131,7 +135,10 @@ static int l_pack_get_info(lua_State* L) {
});
if (found == packs.end()) {
// TODO: optimize
auto worldFolder = scripting::level->getWorld()->wfile->getFolder();
fs::path worldFolder("");
if (scripting::level) {
worldFolder = scripting::level->getWorld()->wfile->getFolder();
}
auto manager = scripting::engine->createPacksManager(worldFolder);
manager.scan();
auto vec = manager.getAll({packid});
@@ -144,10 +151,21 @@ static int l_pack_get_info(lua_State* L) {
return l_pack_get_info(L, pack, content);
}
static int l_pack_get_base_packs(lua_State* L) {
auto& packs = scripting::engine->getBasePacks();
lua_createtable(L, packs.size(), 0);
for (size_t i = 0; i < packs.size(); i++) {
lua_pushstring(L, packs[i].c_str());
lua_rawseti(L, -2, i + 1);
}
return 1;
}
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>},
{"get_base_packs", lua_wrap_errors<l_pack_get_base_packs>},
{NULL, NULL}
};