refactor(block): migrate block data to JSON and resource locations

Replace TOML block definitions with JSON files loaded from asset locations. Block names are now ResourceLocation keys, IDs are resolved from registry.json, and reusable JSON parsing helpers were added.
This commit is contained in:
2026-08-08 17:50:18 +08:00
parent cc95fb0f9d
commit 4268dc6047
7 changed files with 219 additions and 82 deletions

View File

@@ -1,5 +1,7 @@
#pragma once
#include "Cubed/tools/resource_location.hpp"
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <optional>
@@ -41,7 +43,7 @@ struct LookBlock {
};
struct BlockData {
std::string name;
ResourceLocation name{};
BlockType id = 0;
bool is_liquid = false;
@@ -55,14 +57,7 @@ struct BlockData {
bool is_blend = false;
bool is_transitional = false;
float roughness = 1.0f;
BlockData(std::string_view b_name, BlockType b_id, bool liquid,
bool passable, bool cross_plane, bool transparent, bool gas,
bool discard, bool blend, bool transitional, float r)
: name(b_name), id(b_id), is_liquid(liquid), is_gas(gas),
is_passable(passable), is_cross_plane(cross_plane),
is_transparent(transparent), is_discard(discard), is_blend(blend),
is_transitional(transitional), roughness(r) {}
BlockData() { name = ""; }
BlockData() = default;
};
} // namespace Cubed

View File

@@ -10,7 +10,7 @@ public:
static void init();
static unsigned sums();
static unsigned cross_plane_sum();
static const std::string& name_form_id(BlockType id);
static const ResourceLocation& name_form_id(BlockType id);
static bool is_gas(BlockType id);
static bool is_liquid(BlockType id);
@@ -24,13 +24,15 @@ public:
static float roughness(BlockType id);
static BlockType cross_plane_index(BlockType id);
static BlockType id_from_name(const std::string& name);
static BlockType id_from_name(std::string_view name);
static BlockType id_from_name(const ResourceLocation& name);
private:
using BlockMap = tbb::concurrent_hash_map<BlockType, BlockData>;
using acc = BlockMap::accessor;
using cacc = BlockMap::const_accessor;
using IDMap = tbb::concurrent_hash_map<std::string, BlockType>;
using IDMap = tbb::concurrent_hash_map<ResourceLocation, BlockType,
ResourceLocation::Hash>;
using CrossPlaneMap = tbb::concurrent_hash_map<BlockType, BlockType>;
static inline const BlockData EMPTY;

View File

@@ -1,8 +1,72 @@
#pragma once
#include "Cubed/tools/log.hpp"
#include <filesystem>
#include <rapidjson/document.h>
#include <string>
#include <type_traits>
#include <unordered_map>
namespace Tools {
std::unordered_map<std::string, std::string>
namespace Cubed::Tools {
namespace detail {
template <typename T> inline constexpr bool always_false_v = false; // NOLINT
} // namespace detail
inline std::unordered_map<std::string, std::string>
doc_to_map(const rapidjson::Document& doc);
}
bool parse_json(rapidjson::Document& doc, const std::filesystem::path& path);
template <typename T>
bool get_json_value(const rapidjson::Value& value, const char* key, T& out) {
using ValueType = std::decay_t<T>;
if (!value.HasMember(key)) {
Logger::error("json don't has key {}", key);
return false;
}
const auto& v = value[key];
if constexpr (std::is_same_v<ValueType, bool>) {
if (!v.IsBool()) {
Logger::error("json key {} value is not bool", key);
return false;
}
out = v.GetBool();
} else if constexpr (std::is_same_v<ValueType, std::string>) {
if (!v.IsString()) {
Logger::error("json key {} value is not string", key);
return false;
}
out = v.GetString();
} else if constexpr (std::is_same_v<ValueType, float>) {
if (!v.IsNumber()) {
Logger::error("json key {} value is not number", key);
return false;
}
out = v.GetFloat();
} else if constexpr (std::is_same_v<ValueType, double>) {
if (!v.IsNumber()) {
Logger::error("json key {} value is not number", key);
return false;
}
out = v.GetDouble();
} else if constexpr (std::is_integral_v<ValueType> &&
std::is_unsigned_v<ValueType>) {
if (!v.IsUint64()) {
Logger::error("json key {} value is not uint", key);
return false;
}
out = static_cast<ValueType>(v.GetUint64());
} else if constexpr (std::is_integral_v<ValueType> &&
std::is_signed_v<ValueType>) {
if (!v.IsInt64()) {
Logger::error("json key {} value is not int", key);
return false;
}
out = static_cast<ValueType>(v.GetInt64());
} else {
static_assert(detail::always_false_v<ValueType>,
"get_json_value: unsupported type");
}
return true;
}
} // namespace Cubed::Tools

View File

@@ -28,9 +28,30 @@ struct ResourceLocation {
std::string(str.substr(it + 1))};
}
static std::string get_assets_path(std::string_view ns) {
if (ns == "cubed") {
return ASSETS_PATH "cubed";
}
return std::string(ns);
}
bool operator==(const ResourceLocation& o) const {
return (ns == o.ns) && (path == o.path);
}
std::size_t hash() const { return std::hash<std::string>()(to_string()); }
struct Hash {
std::size_t hash(const ResourceLocation& p) const {
return ResourceLocation::hash(p);
}
bool equal(const ResourceLocation& a, const ResourceLocation& b) const {
return a == b;
}
};
static std::size_t hash(const ResourceLocation& p) {
return std::hash<std::string>()(p.to_string());
}
std::string assets_path() const { return get_assets_path(ns); }
};
} // namespace Cubed