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
+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
);
}
}