fix: missing pack.has_indices if content is not loaded

This commit is contained in:
MihailRis 2025-07-13 15:36:09 +03:00
parent aca9b49c77
commit b02b454573
3 changed files with 40 additions and 11 deletions

View File

@ -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<ContentPackStats> 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(

View File

@ -3,6 +3,7 @@
#include <stdexcept>
#include <string>
#include <vector>
#include <optional>
#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<ContentPackStats> 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;

View File

@ -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;
}