voxelcore/src/content/loading/BlockLoader.cpp
2026-11-03 18:46:50 +03:00

295 lines
10 KiB
C++

#define VC_ENABLE_REFLECTION
#include "ContentUnitLoader.hpp"
#include "ContentLoadingCommons.hpp"
#include "../ContentBuilder.hpp"
#include "coders/json.hpp"
#include "core_defs.hpp"
#include "data/dv.hpp"
#include "data/StructLayout.hpp"
#include "debug/Logger.hpp"
#include "io/io.hpp"
#include "presets/ParticlesPreset.hpp"
#include "util/stringutil.hpp"
#include "voxels/Block.hpp"
#include "constants.hpp"
using namespace data;
static debug::Logger logger("block-content-loader");
static void perform_user_block_fields(
const std::string& blockName, StructLayout& layout
) {
if (layout.size() > MAX_USER_BLOCK_FIELDS_SIZE) {
throw std::runtime_error(
util::quote(blockName) +
" fields total size exceeds limit (" +
std::to_string(layout.size()) + "/" +
std::to_string(MAX_USER_BLOCK_FIELDS_SIZE) + ")");
}
for (const auto& field : layout) {
if (field.name.at(0) == '.') {
throw std::runtime_error(
util::quote(blockName) + " field " + field.name +
": user field may not start with '.'");
}
}
std::vector<Field> fields;
fields.insert(fields.end(), layout.begin(), layout.end());
// add built-in fields here
layout = StructLayout::create(fields);
}
static AABB load_hitbox(const dv::value& box) {
if (box.size() < 6) {
throw std::runtime_error("hitbox requires 6 elements");
}
AABB aabb;
aabb.a = glm::vec3(
box[0].asNumber(), box[1].asNumber(), box[2].asNumber()
);
aabb.b = glm::vec3(
box[3].asNumber(), box[4].asNumber(), box[5].asNumber()
);
aabb.b += aabb.a;
return aabb;
}
static void load_variant(
Variant& variant, const dv::value& root, const std::string& name
) {
// block texturing
if (root.has("texture")) {
const auto& texture = root["texture"].asString();
for (uint i = 0; i < 6; i++) {
variant.textureFaces[i] = texture;
}
} else if (root.has("texture-faces")) {
const auto& texarr = root["texture-faces"];
if (texarr.length() < 6) {
throw std::runtime_error("texture-faces requires 6 elements");
}
for (uint i = 0; i < 6; i++) {
variant.textureFaces[i] = texarr[i].asString();
}
}
// block model
auto& model = variant.model;
std::string modelTypeName = BlockModelTypeMeta.getNameString(model.type);
root.at("model").get(modelTypeName);
root.at("model-name").get(model.name);
if (BlockModelTypeMeta.getItem(modelTypeName, model.type)) {
if (model.type == BlockModelType::CUSTOM && model.customRaw == nullptr) {
if (root.has("model-primitives")) {
model.customRaw = root["model-primitives"];
} else if (model.name.empty()) {
throw std::runtime_error(
name + ": no 'model-primitives' or 'model-name' found"
);
}
}
} else if (!modelTypeName.empty()) {
logger.error() << "unknown model: " << modelTypeName;
model.type = BlockModelType::NONE;
}
std::string cullingModeName = CullingModeMeta.getNameString(variant.culling);
root.at("culling").get(cullingModeName);
if (!CullingModeMeta.getItem(cullingModeName, variant.culling)) {
logger.error() << "unknown culling mode: " << cullingModeName;
}
root.at("draw-group").get(variant.drawGroup);
if (auto found = root.at("hitboxes")) {
const auto& boxarr = *found;
variant.hitboxes.clear();
variant.hitboxes.reserve(boxarr.size());
for (uint i = 0; i < boxarr.size(); i++) {
variant.hitboxes.push_back(load_hitbox(boxarr[i]));
}
} else if (auto found = root.at("hitbox")) {
variant.hitboxes = {load_hitbox(*found)};
}
}
template<> void ContentUnitLoader<Block>::loadUnit(
Block& def, const std::string& name, dv::value root
) {
process_properties(def, name, root);
process_tags(def, root);
if (root.has("parent")) {
const auto& parentName = root["parent"].asString();
auto parentDef = builder.get(parentName);
if (parentDef == nullptr) {
throw std::runtime_error(
"failed to find parent(" + parentName + ") for " + name
);
}
parentDef->cloneTo(def);
}
root.at("caption").get(def.caption);
load_variant(def.defaults, root, name);
if (root.has("state-based")) {
const auto& stateBased = root["state-based"];
if (stateBased.has("variants")) {
const auto& variants = stateBased["variants"];
int offset = 0;
int bitsCount = 4;
stateBased.at("offset").get(offset);
stateBased.at("bits").get(bitsCount);
if (offset < 0 || bitsCount <= 0 || offset + bitsCount > 8) {
throw std::runtime_error("invalid state-based bits configuration");
}
def.variants = std::make_unique<Variants>();
def.variants->offset = offset;
def.variants->mask = (1ULL << bitsCount) - 1;
def.variants->variants.push_back(def.defaults);
for (int i = 0; i < variants.size(); i++) {
Variant variant = def.defaults;
load_variant(variant, variants[i], name);
def.variants->variants.push_back(variant);
}
while (def.variants->variants.size() < BLOCK_MAX_VARIANTS) {
def.variants->variants.push_back(def.defaults);
}
}
} else if (def.variants) {
auto& variants = def.variants->variants;
variants.at(0) = def.defaults;
for (int i = 1; i < variants.size(); i++) {
load_variant(variants.at(i), root, name);
}
}
root.at("material").get(def.material);
// rotation profile
std::string profile = def.rotations.name;
root.at("rotation").get(profile);
def.rotatable = profile != "none";
if (profile == BlockRotProfile::PIPE_NAME) {
def.rotations = BlockRotProfile::PIPE;
} else if (profile == BlockRotProfile::PANE_NAME) {
def.rotations = BlockRotProfile::PANE;
} else if (profile == BlockRotProfile::STAIRS_NAME) {
def.rotations = BlockRotProfile::STAIRS;
} else if (profile == BlockRotProfile::LADDER_NAME) {
def.rotations = BlockRotProfile::LADDER;
} else if (profile != "none") {
logger.error() << "unknown rotation profile " << profile;
def.rotatable = false;
}
// block light emission [r, g, b] where r,g,b in range [0..15]
if (auto found = root.at("emission")) {
const auto& emissionarr = *found;
for (size_t i = 0; i < 3; i++) {
def.emission[i] = glm::clamp(
emissionarr[i].asInteger(),
static_cast<integer_t>(0),
static_cast<integer_t>(15)
);
}
}
// block size
if (auto found = root.at("size")) {
const auto& sizearr = *found;
def.size.x = sizearr[0].asInteger();
def.size.y = sizearr[1].asInteger();
def.size.z = sizearr[2].asInteger();
if (def.size.x < 1 || def.size.y < 1 || def.size.z < 1) {
throw std::runtime_error(
"block " + util::quote(def.name) + ": invalid block size"
);
}
if (def.size.x > EXTENDED_BLOCK_LIMIT ||
def.size.y > EXTENDED_BLOCK_LIMIT ||
def.size.z > EXTENDED_BLOCK_LIMIT) {
throw std::runtime_error(
"extended block " + util::quote(def.name) + " size limit " +
std::to_string(EXTENDED_BLOCK_LIMIT) + " exceeded"
);
}
if (def.size.x != 1 || def.size.y != 1 || def.size.z != 1) {
auto applySize = [&def](Variant& variant) {
if (variant.model.type == BlockModelType::BLOCK) {
variant.model.type = BlockModelType::AABB;
variant.hitboxes = {AABB(def.size)};
}
};
applySize(def.defaults);
if (def.variants) {
for (auto& variant : def.variants->variants) {
applySize(variant);
}
}
}
// grounding behaviour
if (root.has("grounding-behaviour")) {
std::string groundingBehaviourName = GroundingBehaviourMeta.getNameString(def.groundingBehaviour);
root.at("grounding-behaviour").get(groundingBehaviourName);
if (!GroundingBehaviourMeta.getItem(groundingBehaviourName, def.groundingBehaviour)) {
logger.error() << "unknown grounding behaviour: " << groundingBehaviourName;
}
}
}
// primitive properties
root.at("obstacle").get(def.obstacle);
root.at("replaceable").get(def.replaceable);
root.at("light-passing").get(def.lightPassing);
root.at("sky-light-passing").get(def.skyLightPassing);
root.at("shadeless").get(def.shadeless);
root.at("ambient-occlusion").get(def.ambientOcclusion);
root.at("uv-lock").get(def.uvLock);
root.at("breakable").get(def.breakable);
root.at("selectable").get(def.selectable);
root.at("grounded").get(def.grounded);
root.at("hidden").get(def.hidden);
root.at("picking-item").get(def.pickingItem);
root.at("surface-replacement").get(def.surfaceReplacement);
root.at("script-name").get(def.scriptName);
root.at("ui-layout").get(def.uiLayout);
root.at("inventory-size").get(def.inventorySize);
root.at("tick-interval").get(def.tickInterval);
root.at("overlay-texture").get(def.overlayTexture);
root.at("translucent").get(def.translucent);
root.at("solid").get(def.explictlySolid);
if (root.has("fields")) {
def.dataStruct = std::make_unique<StructLayout>();
def.dataStruct->deserialize(root["fields"]);
perform_user_block_fields(def.name, *def.dataStruct);
}
if (root.has("particles")) {
def.particles = std::make_unique<ParticlesPreset>();
def.particles->deserialize(root["particles"]);
}
if (def.tickInterval == 0) {
def.tickInterval = 1;
}
if (def.hidden && def.pickingItem == def.name + BLOCK_ITEM_SUFFIX) {
def.pickingItem = CORE_EMPTY;
}
if (root.has("script-name") || def.scriptFile.empty()) {
def.scriptFile = pack.id + ":scripts/" + def.scriptName + ".lua";
}
}