From 04293c926d8298a9d1b7372ca7665268271f8741 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 28 Jan 2024 12:38:32 +0300 Subject: [PATCH] packs dependencies fix + dofile patch --- res/scripts/stdlib.lua | 23 ++++++++++++++++--- src/engine.cpp | 51 ++++++++++++++++++++++++++++++++++++++---- src/frontend/menu.cpp | 1 - 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index fb20d468..ace88536 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -11,9 +11,6 @@ function is_array(x) return true end -local __cached_scripts = {} -local __cached_results = {} - -- Get entry-point and filename from `entry-point:filename` path function parse_path(path) local index = string.find(path, ':') @@ -23,6 +20,9 @@ function parse_path(path) return string.sub(path, 1, index-1), string.sub(path, index+1, -1) end +local __cached_scripts = {} +local __cached_results = {} + -- Load script with caching -- -- path - script path `contentpack:filename`. @@ -56,3 +56,20 @@ function sleep(timesec) end end +_dofile = dofile +-- Replaces dofile('*/content/packid/*') with load_script('packid:*') +function dofile(path) + local index = string.find(path, "/content/") + if index then + local newpath = string.sub(path, index+9) + index = string.find(newpath, "/") + if index then + local label = string.sub(newpath, 1, index-1) + newpath = label..':'..string.sub(newpath, index+1) + if file.isfile(newpath) then + return load_script(newpath, true) + end + end + end + return _dofile(path) +end diff --git a/src/engine.cpp b/src/engine.cpp index 7d5df765..4ef3a243 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -134,18 +134,62 @@ Engine::~Engine() { std::cout << "-- engine finished" << std::endl; } +size_t npos = std::numeric_limits::max(); + +inline size_t findPack(const std::string& id, const std::vector& packs) { + for (size_t i = 0; i < packs.size(); i++) { + if (packs[i].id == id) { + return i; + } + } + return npos; +} + +inline void addPack( + const ContentPack& pack, + const std::vector& srcPacks, + std::vector& contentPacks +) { + if (findPack(pack.id, contentPacks) != npos) { + return; + } + for (auto& dependecy : pack.dependencies) { + size_t index = findPack(dependecy, srcPacks); + if (index == npos) + throw contentpack_error(pack.id, pack.folder, + "missing dependency '"+dependecy+"'"); + addPack(srcPacks[index], srcPacks, contentPacks); + } + contentPacks.push_back(pack); +} + void Engine::loadContent() { auto resdir = paths->getResources(); ContentBuilder contentBuilder; setup_definitions(&contentBuilder); paths->setContentPacks(&contentPacks); - + std::vector resRoots; - for (auto& pack : contentPacks) { + std::vector srcPacks = contentPacks; + contentPacks.clear(); + + for (auto& pack : srcPacks) { + if (pack.dependencies.empty()) + continue; ContentLoader loader(&pack); loader.load(&contentBuilder); resRoots.push_back(pack.folder); + addPack(pack, srcPacks, contentPacks); } + for (auto& pack : srcPacks) { + if (!pack.dependencies.empty()) + continue; + ContentLoader loader(&pack); + loader.load(&contentBuilder); + resRoots.push_back(pack.folder); + addPack(pack, srcPacks, contentPacks); + } + content.reset(contentBuilder.build()); resPaths.reset(new ResPaths(resdir, resRoots)); @@ -163,13 +207,12 @@ void Engine::loadContent() { } } assets->extend(*new_assets.get()); - - } void Engine::loadWorldContent(const fs::path& folder) { contentPacks.clear(); auto packNames = ContentPack::worldPacksList(folder); + std::cout << folder << " " << packNames.size() << std::endl; ContentPack::readPacks(paths, contentPacks, packNames, folder); loadContent(); } diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 98e232e6..4d3d50a6 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -401,7 +401,6 @@ void create_new_world_panel(Engine* engine, PagesControl* menu) { std::cout << "world seed: " << seed << std::endl; auto folder = paths->getWorldsFolder()/fs::u8path(nameutf8); - try { engine->loadAllPacks(); engine->loadContent();