format: reformat project

Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov
2024-08-03 19:53:48 +03:00
committed by Pugemon
parent 736cd175d5
commit bbf33e8e4d
202 changed files with 7389 additions and 5609 deletions
+22 -20
View File
@@ -1,29 +1,29 @@
#include "Content.hpp"
#include <glm/glm.hpp>
#include <memory>
#include <stdexcept>
#include <glm/glm.hpp>
#include <utility>
#include "../voxels/Block.hpp"
#include "../items/ItemDef.hpp"
#include "../logic/scripting/scripting.hpp"
#include "../objects/EntityDef.hpp"
#include "../objects/rigging.hpp"
#include "../voxels/Block.hpp"
#include "ContentPack.hpp"
#include "../logic/scripting/scripting.hpp"
ContentIndices::ContentIndices(
ContentUnitIndices<Block> blocks,
ContentUnitIndices<ItemDef> items,
ContentUnitIndices<EntityDef> entities
) : blocks(std::move(blocks)),
items(std::move(items)),
entities(std::move(entities))
{}
)
: blocks(std::move(blocks)),
items(std::move(items)),
entities(std::move(entities)) {
}
Content::Content(
std::unique_ptr<ContentIndices> indices,
std::unique_ptr<ContentIndices> indices,
std::unique_ptr<DrawGroups> drawGroups,
ContentUnitDefs<Block> blocks,
ContentUnitDefs<ItemDef> items,
@@ -32,15 +32,15 @@ Content::Content(
UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
ResourceIndicesSet resourceIndices
) : indices(std::move(indices)),
packs(std::move(packs)),
blockMaterials(std::move(blockMaterials)),
skeletons(std::move(skeletons)),
blocks(std::move(blocks)),
items(std::move(items)),
entities(std::move(entities)),
drawGroups(std::move(drawGroups))
{
)
: indices(std::move(indices)),
packs(std::move(packs)),
blockMaterials(std::move(blockMaterials)),
skeletons(std::move(skeletons)),
blocks(std::move(blocks)),
items(std::move(items)),
entities(std::move(entities)),
drawGroups(std::move(drawGroups)) {
for (size_t i = 0; i < RESOURCE_TYPES_COUNT; i++) {
this->resourceIndices[i] = std::move(resourceIndices[i]);
}
@@ -48,7 +48,8 @@ Content::Content(
Content::~Content() = default;
const rigging::SkeletonConfig* Content::getSkeleton(const std::string& id) const {
const rigging::SkeletonConfig* Content::getSkeleton(const std::string& id
) const {
auto found = skeletons.find(id);
if (found == skeletons.end()) {
return nullptr;
@@ -80,6 +81,7 @@ const UptrsMap<std::string, ContentPackRuntime>& Content::getPacks() const {
return packs;
}
const UptrsMap<std::string, rigging::SkeletonConfig>& Content::getSkeletons() const {
const UptrsMap<std::string, rigging::SkeletonConfig>& Content::getSkeletons(
) const {
return skeletons;
}
+33 -27
View File
@@ -1,20 +1,19 @@
#ifndef CONTENT_CONTENT_HPP_
#define CONTENT_CONTENT_HPP_
#include "content_fwd.hpp"
#include "../data/dynamic_fwd.hpp"
#include <string>
#include <vector>
#include <memory>
#include <optional>
#include <stdexcept>
#include <unordered_map>
#include <set>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
#include "../data/dynamic_fwd.hpp"
#include "content_fwd.hpp"
using DrawGroups = std::set<ubyte>;
template<class K, class V>
template <class K, class V>
using UptrsMap = std::unordered_map<K, std::unique_ptr<V>>;
class Block;
@@ -28,31 +27,37 @@ namespace rigging {
constexpr const char* contenttype_name(contenttype type) {
switch (type) {
case contenttype::none: return "none";
case contenttype::block: return "block";
case contenttype::item: return "item";
case contenttype::entity: return "entity";
case contenttype::none:
return "none";
case contenttype::block:
return "block";
case contenttype::item:
return "item";
case contenttype::entity:
return "entity";
default:
return "unknown";
}
}
class namereuse_error: public std::runtime_error {
class namereuse_error : public std::runtime_error {
contenttype type;
public:
namereuse_error(const std::string& msg, contenttype type)
: std::runtime_error(msg), type(type) {}
: std::runtime_error(msg), type(type) {
}
inline contenttype getType() const {
return type;
}
};
template<class T>
template <class T>
class ContentUnitIndices {
std::vector<T*> defs;
public:
ContentUnitIndices(std::vector<T*> defs) : defs(std::move(defs)) {}
ContentUnitIndices(std::vector<T*> defs) : defs(std::move(defs)) {
}
inline T* get(blockid_t id) const {
if (id >= defs.size()) {
@@ -84,12 +89,11 @@ public:
);
};
template<class T>
template <class T>
class ContentUnitDefs {
UptrsMap<std::string, T> defs;
public:
ContentUnitDefs(UptrsMap<std::string, T> defs)
: defs(std::move(defs)) {
ContentUnitDefs(UptrsMap<std::string, T> defs) : defs(std::move(defs)) {
}
T* find(const std::string& id) const {
@@ -102,7 +106,7 @@ public:
T& require(const std::string& id) const {
const auto& found = defs.find(id);
if (found == defs.end()) {
throw std::runtime_error("missing content unit "+id);
throw std::runtime_error("missing content unit " + id);
}
return *found->second;
}
@@ -113,8 +117,8 @@ class ResourceIndices {
std::unordered_map<std::string, size_t> indices;
std::unique_ptr<std::vector<dynamic::Map_sptr>> savedData;
public:
ResourceIndices()
: savedData(std::make_unique<std::vector<dynamic::Map_sptr>>()){
ResourceIndices()
: savedData(std::make_unique<std::vector<dynamic::Map_sptr>>()) {
}
static constexpr size_t MISSING = SIZE_MAX;
@@ -152,8 +156,10 @@ public:
constexpr const char* to_string(ResourceType type) {
switch (type) {
case ResourceType::CAMERA: return "camera";
default: return "unknown";
case ResourceType::CAMERA:
return "camera";
default:
return "unknown";
}
}
@@ -180,7 +186,7 @@ public:
ResourceIndicesSet resourceIndices {};
Content(
std::unique_ptr<ContentIndices> indices,
std::unique_ptr<ContentIndices> indices,
std::unique_ptr<DrawGroups> drawGroups,
ContentUnitDefs<Block> blocks,
ContentUnitDefs<ItemDef> items,
@@ -209,4 +215,4 @@ public:
const UptrsMap<std::string, rigging::SkeletonConfig>& getSkeletons() const;
};
#endif // CONTENT_CONTENT_HPP_
#endif // CONTENT_CONTENT_HPP_
+5 -6
View File
@@ -24,7 +24,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
auto groups = std::make_unique<DrawGroups>();
for (const std::string& name : blocks.names) {
Block& def = *blocks.defs[name];
// Generating runtime info
def.rt.id = blockDefsIndices.size();
def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission);
@@ -48,7 +48,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
std::vector<ItemDef*> itemDefsIndices;
for (const std::string& name : items.names) {
ItemDef& def = *items.defs[name];
// Generating runtime info
def.rt.id = itemDefsIndices.size();
def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission);
@@ -66,11 +66,10 @@ std::unique_ptr<Content> ContentBuilder::build() {
auto content = std::make_unique<Content>(
std::make_unique<ContentIndices>(
blockDefsIndices,
itemDefsIndices,
entityDefsIndices),
blockDefsIndices, itemDefsIndices, entityDefsIndices
),
std::move(groups),
blocks.build(),
blocks.build(),
items.build(),
entities.build(),
std::move(packs),
+17 -14
View File
@@ -1,17 +1,17 @@
#ifndef CONTENT_CONTENT_BUILDER_HPP_
#define CONTENT_CONTENT_BUILDER_HPP_
#include "../items/ItemDef.hpp"
#include "../voxels/Block.hpp"
#include "../objects/EntityDef.hpp"
#include <memory>
#include <unordered_map>
#include <vector>
#include "../content/Content.hpp"
#include "../content/ContentPack.hpp"
#include "../items/ItemDef.hpp"
#include "../objects/EntityDef.hpp"
#include "../voxels/Block.hpp"
#include <memory>
#include <vector>
#include <unordered_map>
template<class T>
template <class T>
class ContentUnitBuilder {
std::unordered_map<std::string, contenttype>& allNames;
contenttype type;
@@ -19,17 +19,20 @@ class ContentUnitBuilder {
void checkIdentifier(const std::string& id) {
const auto& found = allNames.find(id);
if (found != allNames.end()) {
throw namereuse_error("name "+id+" is already used", found->second);
throw namereuse_error(
"name " + id + " is already used", found->second
);
}
}
public:
UptrsMap<std::string, T> defs;
std::vector<std::string> names;
ContentUnitBuilder(
std::unordered_map<std::string, contenttype>& allNames,
contenttype type
) : allNames(allNames), type(type) {}
std::unordered_map<std::string, contenttype>& allNames, contenttype type
)
: allNames(allNames), type(type) {
}
T& create(const std::string& id) {
auto found = defs.find(id);
@@ -69,4 +72,4 @@ public:
std::unique_ptr<Content> build();
};
#endif // CONTENT_CONTENT_BUILDER_HPP_
#endif // CONTENT_CONTENT_BUILDER_HPP_
+18 -14
View File
@@ -1,26 +1,30 @@
#include "ContentLUT.hpp"
#include "Content.hpp"
#include "../constants.hpp"
#include "../files/files.hpp"
#include "../coders/json.hpp"
#include "../voxels/Block.hpp"
#include "../items/ItemDef.hpp"
#include <memory>
ContentLUT::ContentLUT(const ContentIndices* indices, size_t blocksCount, size_t itemsCount)
: blocks(blocksCount, indices->blocks, BLOCK_VOID, contenttype::block),
items(itemsCount, indices->items, ITEM_VOID, contenttype::item)
{}
#include "../coders/json.hpp"
#include "../constants.hpp"
#include "../files/files.hpp"
#include "../items/ItemDef.hpp"
#include "../voxels/Block.hpp"
#include "Content.hpp"
template<class T> static constexpr size_t get_entries_count(
const ContentUnitIndices<T>& indices, const dynamic::List_sptr& list) {
ContentLUT::ContentLUT(
const ContentIndices* indices, size_t blocksCount, size_t itemsCount
)
: blocks(blocksCount, indices->blocks, BLOCK_VOID, contenttype::block),
items(itemsCount, indices->items, ITEM_VOID, contenttype::item) {
}
template <class T>
static constexpr size_t get_entries_count(
const ContentUnitIndices<T>& indices, const dynamic::List_sptr& list
) {
return list ? std::max(list->size(), indices.count()) : indices.count();
}
std::shared_ptr<ContentLUT> ContentLUT::create(
const fs::path& filename,
const Content* content
const fs::path& filename, const Content* content
) {
auto root = files::read_json(filename);
auto blocklist = root->list("blocks");
+19 -16
View File
@@ -1,16 +1,15 @@
#ifndef CONTENT_CONTENT_LUT_HPP_
#define CONTENT_CONTENT_LUT_HPP_
#include "Content.hpp"
#include "../typedefs.hpp"
#include "../constants.hpp"
#include "../data/dynamic.hpp"
#include <filesystem>
#include <string>
#include <utility>
#include <vector>
#include <filesystem>
#include "../constants.hpp"
#include "../data/dynamic.hpp"
#include "../typedefs.hpp"
#include "Content.hpp"
namespace fs = std::filesystem;
@@ -19,7 +18,7 @@ struct contententry {
std::string name;
};
template<typename T, class U>
template <typename T, class U>
class ContentUnitLUT {
std::vector<T> indices;
std::vector<std::string> names;
@@ -28,8 +27,13 @@ class ContentUnitLUT {
T missingValue;
contenttype type;
public:
ContentUnitLUT(size_t count, const ContentUnitIndices<U>& unitIndices, T missingValue, contenttype type)
: missingValue(missingValue), type(type) {
ContentUnitLUT(
size_t count,
const ContentUnitIndices<U>& unitIndices,
T missingValue,
contenttype type
)
: missingValue(missingValue), type(type) {
for (size_t i = 0; i < count; i++) {
indices.push_back(i);
}
@@ -47,7 +51,7 @@ public:
if (auto def = defs.find(name)) {
set(i, name, def->rt.id);
} else {
set(i, name, missingValue);
set(i, name, missingValue);
}
}
}
@@ -86,7 +90,7 @@ public:
}
};
/// @brief Content indices lookup table or report
/// @brief Content indices lookup table or report
/// used to convert world with different indices
/// Building with indices.json
class ContentLUT {
@@ -97,10 +101,9 @@ public:
ContentLUT(const ContentIndices* indices, size_t blocks, size_t items);
static std::shared_ptr<ContentLUT> create(
const fs::path& filename,
const Content* content
const fs::path& filename, const Content* content
);
inline bool hasContentReorder() const {
return blocks.hasContentReorder() || items.hasContentReorder();
}
@@ -111,4 +114,4 @@ public:
std::vector<contententry> getMissingContent() const;
};
#endif // CONTENT_CONTENT_LUT_HPP_
#endif // CONTENT_CONTENT_LUT_HPP_
+96 -65
View File
@@ -1,34 +1,33 @@
#include "ContentLoader.hpp"
#include "Content.hpp"
#include "ContentPack.hpp"
#include "ContentBuilder.hpp"
#include <algorithm>
#include <glm/glm.hpp>
#include <iostream>
#include <memory>
#include <string>
#include "../coders/json.hpp"
#include "../core_defs.hpp"
#include "../data/dynamic.hpp"
#include "../debug/Logger.hpp"
#include "../files/files.hpp"
#include "../items/ItemDef.hpp"
#include "../objects/rigging.hpp"
#include "../logic/scripting/scripting.hpp"
#include "../objects/rigging.hpp"
#include "../typedefs.hpp"
#include "../util/listutil.hpp"
#include "../util/stringutil.hpp"
#include "../voxels/Block.hpp"
#include <iostream>
#include <string>
#include <memory>
#include <algorithm>
#include <glm/glm.hpp>
#include "Content.hpp"
#include "ContentBuilder.hpp"
#include "ContentPack.hpp"
namespace fs = std::filesystem;
static debug::Logger logger("content-loader");
ContentLoader::ContentLoader(ContentPack* pack, ContentBuilder& builder)
: pack(pack), builder(builder)
{
: pack(pack), builder(builder) {
auto runtime = std::make_unique<ContentPackRuntime>(
*pack, scripting::create_pack_environment(*pack)
);
@@ -97,9 +96,9 @@ bool ContentLoader::fixPackIndices(
void ContentLoader::fixPackIndices() {
auto folder = pack->folder;
auto indexFile = pack->getContentFile();
auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER;
auto itemsFolder = folder/ContentPack::ITEMS_FOLDER;
auto entitiesFolder = folder/ContentPack::ENTITIES_FOLDER;
auto blocksFolder = folder / ContentPack::BLOCKS_FOLDER;
auto itemsFolder = folder / ContentPack::ITEMS_FOLDER;
auto entitiesFolder = folder / ContentPack::ENTITIES_FOLDER;
dynamic::Map_sptr root;
if (fs::is_regular_file(indexFile)) {
@@ -113,13 +112,15 @@ void ContentLoader::fixPackIndices() {
modified |= fixPackIndices(itemsFolder, root.get(), "items");
modified |= fixPackIndices(entitiesFolder, root.get(), "entities");
if (modified){
if (modified) {
// rewrite modified json
files::write_json(indexFile, root.get());
}
}
void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::path& file) {
void ContentLoader::loadBlock(
Block& def, const std::string& name, const fs::path& file
) {
auto root = files::read_json(file);
root->str("caption", def.caption);
@@ -169,7 +170,7 @@ void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::pat
logger.error() << "unknown rotation profile " << profile;
def.rotatable = false;
}
// block hitbox AABB [x, y, z, width, height, depth]
auto boxarr = root->list("hitboxes");
if (boxarr) {
@@ -181,16 +182,16 @@ void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::pat
hitboxesIndex.b = glm::vec3(box->num(3), box->num(4), box->num(5));
hitboxesIndex.b += hitboxesIndex.a;
}
} else if ((boxarr = root->list("hitbox"))){
} else if ((boxarr = root->list("hitbox"))) {
AABB aabb;
aabb.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
aabb.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
aabb.b += aabb.a;
def.hitboxes = { aabb };
def.hitboxes = {aabb};
} else if (!def.modelBoxes.empty()) {
def.hitboxes = def.modelBoxes;
} else {
def.hitboxes = { AABB() };
def.hitboxes = {AABB()};
}
// block light emission [r, g, b] where r,g,b in range [0..15]
@@ -205,8 +206,8 @@ void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::pat
def.size.x = sizearr->num(0);
def.size.y = sizearr->num(1);
def.size.z = sizearr->num(2);
if (def.model == BlockModel::block &&
(def.size.x != 1 || def.size.y != 1 || def.size.z != 1)) {
if (def.model == BlockModel::block &&
(def.size.x != 1 || def.size.y != 1 || def.size.z != 1)) {
def.model = BlockModel::aabb;
def.hitboxes = {AABB(def.size)};
}
@@ -233,7 +234,7 @@ void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::pat
def.tickInterval = 1;
}
if (def.hidden && def.pickingItem == def.name+BLOCK_ITEM_SUFFIX) {
if (def.hidden && def.pickingItem == def.name + BLOCK_ITEM_SUFFIX) {
def.pickingItem = CORE_EMPTY;
}
}
@@ -241,12 +242,14 @@ void ContentLoader::loadBlock(Block& def, const std::string& name, const fs::pat
void ContentLoader::loadCustomBlockModel(Block& def, dynamic::Map* primitives) {
if (primitives->has("aabbs")) {
auto modelboxes = primitives->list("aabbs");
for (uint i = 0; i < modelboxes->size(); i++ ) {
for (uint i = 0; i < modelboxes->size(); i++) {
/* Parse aabb */
auto boxarr = modelboxes->list(i);
AABB modelbox;
modelbox.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
modelbox.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
modelbox.a =
glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
modelbox.b =
glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
modelbox.b += modelbox.a;
def.modelBoxes.push_back(modelbox);
@@ -270,19 +273,21 @@ void ContentLoader::loadCustomBlockModel(Block& def, dynamic::Map* primitives) {
/* Parse tetragon to points */
auto tgonobj = modeltetragons->list(i);
glm::vec3 p1(tgonobj->num(0), tgonobj->num(1), tgonobj->num(2)),
xw(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)),
yh(tgonobj->num(6), tgonobj->num(7), tgonobj->num(8));
xw(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)),
yh(tgonobj->num(6), tgonobj->num(7), tgonobj->num(8));
def.modelExtraPoints.push_back(p1);
def.modelExtraPoints.push_back(p1+xw);
def.modelExtraPoints.push_back(p1+xw+yh);
def.modelExtraPoints.push_back(p1+yh);
def.modelExtraPoints.push_back(p1 + xw);
def.modelExtraPoints.push_back(p1 + xw + yh);
def.modelExtraPoints.push_back(p1 + yh);
def.modelTextures.emplace_back(tgonobj->str(9));
}
}
}
void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::path& file) {
void ContentLoader::loadItem(
ItemDef& def, const std::string& name, const fs::path& file
) {
auto root = files::read_json(file);
root->str("caption", def.caption);
@@ -294,7 +299,7 @@ void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::pa
def.iconType = item_icon_type::block;
} else if (iconTypeStr == "sprite") {
def.iconType = item_icon_type::sprite;
} else if (iconTypeStr.length()){
} else if (iconTypeStr.length()) {
logger.error() << name << ": unknown icon type" << iconTypeStr;
}
root->str("icon", def.icon);
@@ -310,7 +315,9 @@ void ContentLoader::loadItem(ItemDef& def, const std::string& name, const fs::pa
}
}
void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs::path& file) {
void ContentLoader::loadEntity(
EntityDef& def, const std::string& name, const fs::path& file
) {
auto root = files::read_json(file);
if (auto componentsarr = root->list("components")) {
for (size_t i = 0; i < componentsarr->size(); i++) {
@@ -325,14 +332,21 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs
if (auto sensorarr = sensorsarr->list(i)) {
auto sensorType = sensorarr->str(0);
if (sensorType == "aabb") {
def.boxSensors.emplace_back(i, AABB{
{sensorarr->num(1), sensorarr->num(2), sensorarr->num(3)},
{sensorarr->num(4), sensorarr->num(5), sensorarr->num(6)}
});
def.boxSensors.emplace_back(
i,
AABB {
{sensorarr->num(1),
sensorarr->num(2),
sensorarr->num(3)},
{sensorarr->num(4),
sensorarr->num(5),
sensorarr->num(6)}}
);
} else if (sensorType == "radius") {
def.radialSensors.emplace_back(i, sensorarr->num(1));
} else {
logger.error() << name << ": sensor #" << i << " - unknown type "
logger.error()
<< name << ": sensor #" << i << " - unknown type "
<< util::quote(sensorType);
}
}
@@ -354,29 +368,33 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs
root->flag("blocking", def.blocking);
}
void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const std::string& name) {
void ContentLoader::loadEntity(
EntityDef& def, const std::string& full, const std::string& name
) {
auto folder = pack->folder;
auto configFile = folder/fs::path("entities/"+name+".json");
auto configFile = folder / fs::path("entities/" + name + ".json");
if (fs::exists(configFile)) loadEntity(def, full, configFile);
}
void ContentLoader::loadBlock(Block& def, const std::string& full, const std::string& name) {
void ContentLoader::loadBlock(
Block& def, const std::string& full, const std::string& name
) {
auto folder = pack->folder;
auto configFile = folder/fs::path("blocks/"+name+".json");
auto configFile = folder / fs::path("blocks/" + name + ".json");
if (fs::exists(configFile)) loadBlock(def, full, configFile);
auto scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua");
auto scriptfile = folder / fs::path("scripts/" + def.scriptName + ".lua");
if (fs::is_regular_file(scriptfile)) {
scripting::load_block_script(env, full, scriptfile, def.rt.funcsset);
}
if (!def.hidden) {
auto& item = builder.items.create(full+BLOCK_ITEM_SUFFIX);
auto& item = builder.items.create(full + BLOCK_ITEM_SUFFIX);
item.generated = true;
item.caption = def.caption;
item.iconType = item_icon_type::block;
item.icon = full;
item.placingBlock = full;
for (uint j = 0; j < 4; j++) {
item.emission[j] = def.emission[j];
}
@@ -384,18 +402,22 @@ void ContentLoader::loadBlock(Block& def, const std::string& full, const std::st
}
}
void ContentLoader::loadItem(ItemDef& def, const std::string& full, const std::string& name) {
void ContentLoader::loadItem(
ItemDef& def, const std::string& full, const std::string& name
) {
auto folder = pack->folder;
auto configFile = folder/fs::path("items/"+name+".json");
auto configFile = folder / fs::path("items/" + name + ".json");
if (fs::exists(configFile)) loadItem(def, full, configFile);
auto scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua");
auto scriptfile = folder / fs::path("scripts/" + def.scriptName + ".lua");
if (fs::is_regular_file(scriptfile)) {
scripting::load_item_script(env, full, scriptfile, def.rt.funcsset);
}
}
void ContentLoader::loadBlockMaterial(BlockMaterial& def, const fs::path& file) {
void ContentLoader::loadBlockMaterial(
BlockMaterial& def, const fs::path& file
) {
auto root = files::read_json(file);
root->str("steps-sound", def.stepsSound);
root->str("place-sound", def.placeSound);
@@ -409,13 +431,14 @@ void ContentLoader::load() {
auto folder = pack->folder;
fs::path scriptFile = folder/fs::path("scripts/world.lua");
fs::path scriptFile = folder / fs::path("scripts/world.lua");
if (fs::is_regular_file(scriptFile)) {
scripting::load_world_script(env, pack->id, scriptFile, runtime->worldfuncsset);
scripting::load_world_script(
env, pack->id, scriptFile, runtime->worldfuncsset
);
}
if (!fs::is_regular_file(pack->getContentFile()))
return;
if (!fs::is_regular_file(pack->getContentFile())) return;
auto root = files::read_json(pack->getContentFile());
@@ -423,10 +446,12 @@ void ContentLoader::load() {
for (size_t i = 0; i < blocksarr->size(); i++) {
std::string name = blocksarr->str(i);
auto colon = name.find(':');
std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
std::string full =
colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/';
auto& def = builder.blocks.create(full);
if (colon != std::string::npos) def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
if (colon != std::string::npos)
def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
loadBlock(def, full, name);
stats->totalBlocks++;
}
@@ -435,10 +460,12 @@ void ContentLoader::load() {
for (size_t i = 0; i < itemsarr->size(); i++) {
std::string name = itemsarr->str(i);
auto colon = name.find(':');
std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
std::string full =
colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/';
auto& def = builder.items.create(full);
if (colon != std::string::npos) def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
if (colon != std::string::npos)
def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
loadItem(def, full, name);
stats->totalItems++;
}
@@ -448,7 +475,8 @@ void ContentLoader::load() {
for (size_t i = 0; i < entitiesarr->size(); i++) {
std::string name = entitiesarr->str(i);
auto colon = name.find(':');
std::string full = colon == std::string::npos ? pack->id + ":" + name : name;
std::string full =
colon == std::string::npos ? pack->id + ":" + name : name;
if (colon != std::string::npos) name[colon] = '/';
auto& def = builder.entities.create(full);
loadEntity(def, full, name);
@@ -460,7 +488,7 @@ void ContentLoader::load() {
if (fs::is_directory(materialsDir)) {
for (const auto& entry : fs::directory_iterator(materialsDir)) {
const fs::path& file = entry.path();
std::string name = pack->id+":"+file.stem().u8string();
std::string name = pack->id + ":" + file.stem().u8string();
loadBlockMaterial(builder.createBlockMaterial(name), file);
}
}
@@ -469,9 +497,11 @@ void ContentLoader::load() {
if (fs::is_directory(skeletonsDir)) {
for (const auto& entry : fs::directory_iterator(skeletonsDir)) {
const fs::path& file = entry.path();
std::string name = pack->id+":"+file.stem().u8string();
std::string name = pack->id + ":" + file.stem().u8string();
std::string text = files::read_string(file);
builder.add(rigging::SkeletonConfig::parse(text, file.u8string(), name));
builder.add(
rigging::SkeletonConfig::parse(text, file.u8string(), name)
);
}
}
@@ -480,7 +510,7 @@ void ContentLoader::load() {
for (const auto& entry : fs::directory_iterator(componentsDir)) {
fs::path scriptfile = entry.path();
if (fs::is_regular_file(scriptfile)) {
auto name = pack->id+":"+scriptfile.stem().u8string();
auto name = pack->id + ":" + scriptfile.stem().u8string();
scripting::load_entity_component(name, scriptfile);
}
}
@@ -504,6 +534,7 @@ void ContentLoader::load() {
void ContentLoader::loadResources(ResourceType type, dynamic::List* list) {
for (size_t i = 0; i < list->size(); i++) {
builder.resourceIndices[static_cast<size_t>(type)].add(
pack->id+":"+list->str(i), nullptr);
pack->id + ":" + list->str(i), nullptr
);
}
}
+24 -12
View File
@@ -1,11 +1,11 @@
#ifndef CONTENT_CONTENT_LOADER_HPP_
#define CONTENT_CONTENT_LOADER_HPP_
#include "content_fwd.hpp"
#include <string>
#include <memory>
#include <filesystem>
#include <memory>
#include <string>
#include "content_fwd.hpp"
namespace fs = std::filesystem;
@@ -30,16 +30,28 @@ class ContentLoader {
scriptenv env;
ContentBuilder& builder;
ContentPackStats* stats;
void loadBlock(Block& def, const std::string& full, const std::string& name);
void loadItem(ItemDef& def, const std::string& full, const std::string& name);
void loadEntity(EntityDef& def, const std::string& full, const std::string& name);
void loadBlock(
Block& def, const std::string& full, const std::string& name
);
void loadItem(
ItemDef& def, const std::string& full, const std::string& name
);
void loadEntity(
EntityDef& def, const std::string& full, const std::string& name
);
static void loadCustomBlockModel(Block& def, dynamic::Map* primitives);
static void loadBlockMaterial(BlockMaterial& def, const fs::path& file);
static void loadBlock(Block& def, const std::string& name, const fs::path& file);
static void loadItem(ItemDef& def, const std::string& name, const fs::path& file);
static void loadEntity(EntityDef& def, const std::string& name, const fs::path& file);
static void loadBlock(
Block& def, const std::string& name, const fs::path& file
);
static void loadItem(
ItemDef& def, const std::string& name, const fs::path& file
);
static void loadEntity(
EntityDef& def, const std::string& name, const fs::path& file
);
void loadResources(ResourceType type, dynamic::List* list);
public:
ContentLoader(ContentPack* pack, ContentBuilder& builder);
@@ -53,4 +65,4 @@ public:
void load();
};
#endif // CONTENT_CONTENT_LOADER_HPP_
#endif // CONTENT_CONTENT_LOADER_HPP_
+39 -38
View File
@@ -1,25 +1,25 @@
#include "ContentPack.hpp"
#include <iostream>
#include <algorithm>
#include <iostream>
#include <utility>
#include "../coders/json.hpp"
#include "../files/files.hpp"
#include "../files/engine_paths.hpp"
#include "../data/dynamic.hpp"
#include "../files/engine_paths.hpp"
#include "../files/files.hpp"
namespace fs = std::filesystem;
const std::vector<std::string> ContentPack::RESERVED_NAMES = {
"res", "abs", "local", "core", "user", "world", "none", "null"
};
"res", "abs", "local", "core", "user", "world", "none", "null"};
contentpack_error::contentpack_error(
std::string packId,
fs::path folder,
const std::string& message)
: std::runtime_error(message), packId(std::move(packId)), folder(std::move(folder)) {
std::string packId, fs::path folder, const std::string& message
)
: std::runtime_error(message),
packId(std::move(packId)),
folder(std::move(folder)) {
}
std::string contentpack_error::getPackId() const {
@@ -30,36 +30,40 @@ fs::path contentpack_error::getFolder() const {
}
fs::path ContentPack::getContentFile() const {
return folder/fs::path(CONTENT_FILENAME);
return folder / fs::path(CONTENT_FILENAME);
}
bool ContentPack::is_pack(const fs::path& folder) {
return fs::is_regular_file(folder/fs::path(PACKAGE_FILENAME));
return fs::is_regular_file(folder / fs::path(PACKAGE_FILENAME));
}
static void checkContentPackId(const std::string& id, const fs::path& folder) {
if (id.length() < 2 || id.length() > 24)
throw contentpack_error(id, folder,
"content-pack id length is out of range [2, 24]");
if (isdigit(id[0]))
throw contentpack_error(id, folder,
"content-pack id must not start with a digit");
throw contentpack_error(
id, folder, "content-pack id length is out of range [2, 24]"
);
if (isdigit(id[0]))
throw contentpack_error(
id, folder, "content-pack id must not start with a digit"
);
for (char c : id) {
if (!isalnum(c) && c != '_') {
throw contentpack_error(id, folder,
"illegal character in content-pack id");
throw contentpack_error(
id, folder, "illegal character in content-pack id"
);
}
}
if (std::find(ContentPack::RESERVED_NAMES.begin(),
ContentPack::RESERVED_NAMES.end(), id)
!= ContentPack::RESERVED_NAMES.end()) {
throw contentpack_error(id, folder,
"this content-pack id is reserved");
if (std::find(
ContentPack::RESERVED_NAMES.begin(),
ContentPack::RESERVED_NAMES.end(),
id
) != ContentPack::RESERVED_NAMES.end()) {
throw contentpack_error(id, folder, "this content-pack id is reserved");
}
}
ContentPack ContentPack::read(const fs::path& folder) {
auto root = files::read_json(folder/fs::path(PACKAGE_FILENAME));
auto root = files::read_json(folder / fs::path(PACKAGE_FILENAME));
ContentPack pack;
root->str("id", pack.id);
root->str("title", pack.title);
@@ -78,26 +82,24 @@ ContentPack ContentPack::read(const fs::path& folder) {
}
if (pack.id == "none")
throw contentpack_error(pack.id, folder,
"content-pack id is not specified");
throw contentpack_error(
pack.id, folder, "content-pack id is not specified"
);
checkContentPackId(pack.id, folder);
return pack;
}
void ContentPack::scanFolder(
const fs::path& folder,
std::vector<ContentPack>& packs
const fs::path& folder, std::vector<ContentPack>& packs
) {
if (!fs::is_directory(folder)) {
return;
}
for (const auto& entry : fs::directory_iterator(folder)) {
const fs::path& folder = entry.path();
if (!fs::is_directory(folder))
continue;
if (!is_pack(folder))
continue;
if (!fs::is_directory(folder)) continue;
if (!is_pack(folder)) continue;
try {
packs.push_back(read(folder));
} catch (const contentpack_error& err) {
@@ -119,7 +121,9 @@ std::vector<std::string> ContentPack::worldPacksList(const fs::path& folder) {
return files::read_list(listfile);
}
fs::path ContentPack::findPack(const EnginePaths* paths, const fs::path& worldDir, const std::string& name) {
fs::path ContentPack::findPack(
const EnginePaths* paths, const fs::path& worldDir, const std::string& name
) {
fs::path folder = worldDir / fs::path("content") / fs::path(name);
if (fs::is_directory(folder)) {
return folder;
@@ -135,11 +139,8 @@ fs::path ContentPack::findPack(const EnginePaths* paths, const fs::path& worldDi
return folder;
}
ContentPackRuntime::ContentPackRuntime(
ContentPack info,
scriptenv env
) : info(std::move(info)), env(std::move(env))
{
ContentPackRuntime::ContentPackRuntime(ContentPack info, scriptenv env)
: info(std::move(info)), env(std::move(env)) {
}
ContentPackRuntime::~ContentPackRuntime() = default;
+15 -18
View File
@@ -1,12 +1,12 @@
#ifndef CONTENT_CONTENT_PACK_HPP_
#define CONTENT_CONTENT_PACK_HPP_
#include "../typedefs.hpp"
#include <filesystem>
#include <stdexcept>
#include <string>
#include <vector>
#include <stdexcept>
#include <filesystem>
#include "../typedefs.hpp"
class EnginePaths;
@@ -16,19 +16,20 @@ class contentpack_error : public std::runtime_error {
std::string packId;
fs::path folder;
public:
contentpack_error(std::string packId, fs::path folder, const std::string& message);
contentpack_error(
std::string packId, fs::path folder, const std::string& message
);
std::string getPackId() const;
fs::path getFolder() const;
};
enum class DependencyLevel {
required, // dependency must be installed
optional, // dependency will be installed if found
weak, // only affects packs order
required, // dependency must be installed
optional, // dependency will be installed if found
weak, // only affects packs order
};
/// @brief Content-pack that should be installed earlier the dependent
struct DependencyPack {
DependencyLevel level;
@@ -57,14 +58,13 @@ struct ContentPack {
static ContentPack read(const fs::path& folder);
static void scanFolder(
const fs::path& folder,
std::vector<ContentPack>& packs
const fs::path& folder, std::vector<ContentPack>& packs
);
static std::vector<std::string> worldPacksList(const fs::path& folder);
static fs::path findPack(
const EnginePaths* paths,
const EnginePaths* paths,
const fs::path& worldDir,
const std::string& name
);
@@ -92,10 +92,7 @@ class ContentPackRuntime {
public:
world_funcs_set worldfuncsset {};
ContentPackRuntime(
ContentPack info,
scriptenv env
);
ContentPackRuntime(ContentPack info, scriptenv env);
~ContentPackRuntime();
inline const ContentPackStats& getStats() const {
@@ -119,4 +116,4 @@ public:
}
};
#endif // CONTENT_CONTENT_PACK_HPP_
#endif // CONTENT_CONTENT_PACK_HPP_
+34 -16
View File
@@ -1,10 +1,10 @@
#include "PacksManager.hpp"
#include "../util/listutil.hpp"
#include <queue>
#include <sstream>
#include "../util/listutil.hpp"
PacksManager::PacksManager() = default;
void PacksManager::setSources(std::vector<fs::path> sources) {
@@ -13,7 +13,7 @@ void PacksManager::setSources(std::vector<fs::path> sources) {
void PacksManager::scan() {
packs.clear();
std::vector<ContentPack> packsList;
for (auto& folder : sources) {
ContentPack::scanFolder(folder, packsList);
@@ -35,7 +35,9 @@ std::vector<std::string> PacksManager::getAllNames() const {
return names;
}
std::vector<ContentPack> PacksManager::getAll(const std::vector<std::string>& names) const {
std::vector<ContentPack> PacksManager::getAll(
const std::vector<std::string>& names
) const {
std::vector<ContentPack> packsList;
for (auto& name : names) {
auto found = packs.find(name);
@@ -47,7 +49,9 @@ std::vector<ContentPack> PacksManager::getAll(const std::vector<std::string>& na
return packsList;
}
static contentpack_error on_circular_dependency(std::queue<const ContentPack*>& queue) {
static contentpack_error on_circular_dependency(
std::queue<const ContentPack*>& queue
) {
const ContentPack* lastPack = queue.back();
// circular dependency
std::stringstream ss;
@@ -66,10 +70,12 @@ static contentpack_error on_circular_dependency(std::queue<const ContentPack*>&
/// @param allNames all already done or enqueued packs
/// @param added packs with all dependencies resolved
/// @param queue current pass queue
/// @param resolveWeaks make weak dependencies resolved if found but not added to queue
/// @return true if all dependencies are already added or not found (optional/weak)
/// @param resolveWeaks make weak dependencies resolved if found but not added
/// to queue
/// @return true if all dependencies are already added or not found
/// (optional/weak)
/// @throws contentpack_error if required dependency is not found
static bool resolve_dependencies (
static bool resolve_dependencies(
const ContentPack* pack,
const std::unordered_map<std::string, ContentPack>& packs,
std::vector<std::string>& allNames,
@@ -85,7 +91,9 @@ static bool resolve_dependencies (
auto found = packs.find(dep.id);
bool exists = found != packs.end();
if (!exists && dep.level == DependencyLevel::required) {
throw contentpack_error(dep.id, fs::path(), "dependency of '"+pack->id+"'");
throw contentpack_error(
dep.id, fs::path(), "dependency of '" + pack->id + "'"
);
}
if (!exists) {
// ignored for optional or weak dependencies
@@ -93,11 +101,13 @@ static bool resolve_dependencies (
}
if (resolveWeaks && dep.level == DependencyLevel::weak) {
// dependency pack is found but not added yet
// resolveWeaks is used on second iteration, so it's will not be added
// resolveWeaks is used on second iteration, so it's will not be
// added
continue;
}
if (!util::contains(allNames, dep.id) && dep.level != DependencyLevel::weak) {
if (!util::contains(allNames, dep.id) &&
dep.level != DependencyLevel::weak) {
allNames.push_back(dep.id);
queue.push(&found->second);
}
@@ -106,7 +116,9 @@ static bool resolve_dependencies (
return satisfied;
}
std::vector<std::string> PacksManager::assembly(const std::vector<std::string>& names) const {
std::vector<std::string> PacksManager::assembly(
const std::vector<std::string>& names
) const {
std::vector<std::string> allNames = names;
std::vector<std::string> added;
std::queue<const ContentPack*> queue;
@@ -126,10 +138,14 @@ std::vector<std::string> PacksManager::assembly(const std::vector<std::string>&
while (!queue.empty()) {
auto* pack = queue.front();
queue.pop();
if (resolve_dependencies(pack, packs, allNames, added, queue, resolveWeaks)) {
if (resolve_dependencies(
pack, packs, allNames, added, queue, resolveWeaks
)) {
if (util::contains(added, pack->id)) {
throw contentpack_error(pack->id, pack->folder, "pack duplication");
throw contentpack_error(
pack->id, pack->folder, "pack duplication"
);
}
added.push_back(pack->id);
addedInIteration++;
@@ -148,7 +164,9 @@ std::vector<std::string> PacksManager::assembly(const std::vector<std::string>&
return added;
}
std::vector<std::string> PacksManager::getNames(const std::vector<ContentPack>& packs) {
std::vector<std::string> PacksManager::getNames(
const std::vector<ContentPack>& packs
) {
std::vector<std::string> result;
for (const auto& pack : packs) {
result.push_back(pack.id);
+11 -7
View File
@@ -1,11 +1,11 @@
#ifndef CONTENT_PACKS_MANAGER_HPP_
#define CONTENT_PACKS_MANAGER_HPP_
#include "ContentPack.hpp"
#include <vector>
#include <filesystem>
#include <unordered_map>
#include <vector>
#include "ContentPack.hpp"
namespace fs = std::filesystem;
@@ -31,17 +31,21 @@ public:
/// @brief Get packs by names (id)
/// @param names pack names
/// @throws contentpack_error if pack not found
std::vector<ContentPack> getAll(const std::vector<std::string>& names) const;
std::vector<ContentPack> getAll(const std::vector<std::string>& names
) const;
/// @brief Resolve all dependencies and fix packs order
/// @param names required packs (method can add extra packs)
/// @return resulting ordered vector of pack names
/// @throws contentpack_error if required dependency not found or
/// circular dependency detected
std::vector<std::string> assembly(const std::vector<std::string>& names) const;
std::vector<std::string> assembly(const std::vector<std::string>& names
) const;
/// @brief Collect all pack names (identifiers) into a new vector
static std::vector<std::string> getNames(const std::vector<ContentPack>& packs);
static std::vector<std::string> getNames(
const std::vector<ContentPack>& packs
);
};
#endif // CONTENT_PACKS_MANAGER_HPP_
#endif // CONTENT_PACKS_MANAGER_HPP_
+5 -9
View File
@@ -6,15 +6,11 @@
class Content;
class ContentPackRuntime;
enum class contenttype {
none, block, item, entity
};
enum class contenttype { none, block, item, entity };
enum class ResourceType : size_t {
CAMERA,
LAST=CAMERA
};
enum class ResourceType : size_t { CAMERA, LAST = CAMERA };
inline constexpr auto RESOURCE_TYPES_COUNT = static_cast<size_t>(ResourceType::LAST)+1;
inline constexpr auto RESOURCE_TYPES_COUNT =
static_cast<size_t>(ResourceType::LAST) + 1;
#endif // CONTENT_CONTENT_FWD_HPP_
#endif // CONTENT_CONTENT_FWD_HPP_