From b02b45457322e1ce8f6b9735caeb5b58b1e2ffb4 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 13 Jul 2025 15:36:09 +0300 Subject: [PATCH] fix: missing pack.has_indices if content is not loaded --- src/content/ContentPack.cpp | 21 +++++++++++++++++++++ src/content/ContentPack.hpp | 23 +++++++++++++---------- src/logic/scripting/lua/libs/libpack.cpp | 7 ++++++- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index 9114aacd..ac8c8d59 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -9,7 +9,10 @@ #include "data/dv.hpp" #include "io/engine_paths.hpp" #include "io/io.hpp" +#include "coders/commons.hpp" +#include "debug/Logger.hpp" +static debug::Logger logger("content-pack"); namespace fs = std::filesystem; @@ -47,6 +50,24 @@ bool ContentPack::is_pack(const io::path& folder) { return io::is_regular_file(folder / PACKAGE_FILENAME); } +std::optional ContentPack::loadStats() const { + auto contentFile = getContentFile(); + if (!io::exists(contentFile)) { + return std::nullopt; + } + dv::value object; + try { + object = io::read_object(contentFile); + } catch (const parsing_error& err) { + logger.error() << err.errorLog(); + } + ContentPackStats stats {}; + stats.totalBlocks = object.has("blocks") ? object["blocks"].size() : 0; + stats.totalItems = object.has("items") ? object["items"].size() : 0; + stats.totalEntities = object.has("entities") ? object["entities"].size() : 0; + return stats; +} + static void checkContentPackId(const std::string& id, const io::path& folder) { if (id.length() < 2 || id.length() > 24) throw contentpack_error( diff --git a/src/content/ContentPack.hpp b/src/content/ContentPack.hpp index d434268a..f4e44801 100644 --- a/src/content/ContentPack.hpp +++ b/src/content/ContentPack.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "typedefs.hpp" #include "content_fwd.hpp" @@ -36,6 +37,16 @@ struct DependencyPack { std::string id; }; +struct ContentPackStats { + size_t totalBlocks; + size_t totalItems; + size_t totalEntities; + + inline bool hasSavingContent() const { + return totalBlocks + totalItems + totalEntities > 0; + } +}; + struct ContentPack { std::string id = "none"; std::string title = "untitled"; @@ -48,6 +59,8 @@ struct ContentPack { io::path getContentFile() const; + std::optional loadStats() const; + static inline const std::string PACKAGE_FILENAME = "package.json"; static inline const std::string CONTENT_FILENAME = "content.json"; static inline const io::path BLOCKS_FOLDER = "blocks"; @@ -87,16 +100,6 @@ struct ContentPack { } }; -struct ContentPackStats { - size_t totalBlocks; - size_t totalItems; - size_t totalEntities; - - inline bool hasSavingContent() const { - return totalBlocks + totalItems + totalEntities > 0; - } -}; - struct WorldFuncsSet { bool onblockplaced; bool onblockreplaced; diff --git a/src/logic/scripting/lua/libs/libpack.cpp b/src/logic/scripting/lua/libs/libpack.cpp index e95cf2ba..adc45096 100644 --- a/src/logic/scripting/lua/libs/libpack.cpp +++ b/src/logic/scripting/lua/libs/libpack.cpp @@ -123,8 +123,13 @@ static int l_pack_get_info( auto runtime = content ? content->getPackRuntime(pack.id) : nullptr; if (runtime) { lua::pushboolean(L, runtime->getStats().hasSavingContent()); - lua::setfield(L, "has_indices"); + } else { + auto stats = pack.loadStats(); + lua::pushboolean( + L, stats.has_value() ? stats->hasSavingContent() : false + ); } + lua::setfield(L, "has_indices"); return 1; }