Content packs structure update
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "Content.h"
|
||||
#include "../voxels/Block.h"
|
||||
@@ -10,27 +11,21 @@
|
||||
#include "../coders/json.h"
|
||||
#include "../typedefs.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include "ContentPack.h"
|
||||
|
||||
// don't ask
|
||||
using glm::vec3;
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
using std::endl;
|
||||
using std::string;
|
||||
using std::unique_ptr;
|
||||
using std::filesystem::path;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
ContentLoader::ContentLoader(path folder) : folder(folder) {}
|
||||
ContentLoader::ContentLoader(const ContentPack* pack) : pack(pack) {
|
||||
}
|
||||
|
||||
// TODO: add basic validation and logging
|
||||
Block* ContentLoader::loadBlock(string name, path file) {
|
||||
unique_ptr<json::JObject> root(files::read_json(file));
|
||||
unique_ptr<Block> def(new Block(name));
|
||||
Block* ContentLoader::loadBlock(std::string name, fs::path file) {
|
||||
std::unique_ptr<json::JObject> root(files::read_json(file));
|
||||
std::unique_ptr<Block> def(new Block(name));
|
||||
|
||||
// block texturing
|
||||
if (root->has("texture")) {
|
||||
string texture;
|
||||
std::string texture;
|
||||
root->str("texture", texture);
|
||||
for (uint i = 0; i < 6; i++)
|
||||
def->textureFaces[i] = texture;
|
||||
@@ -42,19 +37,19 @@ Block* ContentLoader::loadBlock(string name, path file) {
|
||||
}
|
||||
|
||||
// block model
|
||||
string model = "block";
|
||||
std::string model = "block";
|
||||
root->str("model", model);
|
||||
if (model == "block") def->model = BlockModel::block;
|
||||
else if (model == "aabb") def->model = BlockModel::aabb;
|
||||
else if (model == "X") def->model = BlockModel::xsprite;
|
||||
else if (model == "none") def->model = BlockModel::none;
|
||||
else {
|
||||
cerr << "unknown model " << model << endl;
|
||||
std::cerr << "unknown model " << model << std::endl;
|
||||
def->model = BlockModel::none;
|
||||
}
|
||||
|
||||
// rotation profile
|
||||
string profile = "none";
|
||||
std::string profile = "none";
|
||||
root->str("rotation", profile);
|
||||
def->rotatable = profile != "none";
|
||||
if (profile == "pipe") {
|
||||
@@ -62,16 +57,16 @@ Block* ContentLoader::loadBlock(string name, path file) {
|
||||
} else if (profile == "pane") {
|
||||
def->rotations = BlockRotProfile::PANE;
|
||||
} else if (profile != "none") {
|
||||
cerr << "unknown rotation profile " << profile << endl;
|
||||
std::cerr << "unknown rotation profile " << profile << std::endl;
|
||||
def->rotatable = false;
|
||||
}
|
||||
|
||||
// block hitbox AABB [x, y, z, width, height, depth]
|
||||
json::JArray* hitboxobj = root->arr("hitbox");
|
||||
if (hitboxobj) {
|
||||
json::JArray* boxobj = root->arr("hitbox");
|
||||
if (boxobj) {
|
||||
AABB& aabb = def->hitbox;
|
||||
aabb.a = vec3(hitboxobj->num(0), hitboxobj->num(1), hitboxobj->num(2));
|
||||
aabb.b = vec3(hitboxobj->num(3), hitboxobj->num(4), hitboxobj->num(5));
|
||||
aabb.a = glm::vec3(boxobj->num(0), boxobj->num(1), boxobj->num(2));
|
||||
aabb.b = glm::vec3(boxobj->num(3), boxobj->num(4), boxobj->num(5));
|
||||
aabb.b += aabb.a;
|
||||
}
|
||||
|
||||
@@ -96,35 +91,17 @@ Block* ContentLoader::loadBlock(string name, path file) {
|
||||
}
|
||||
|
||||
void ContentLoader::load(ContentBuilder* builder) {
|
||||
cout << "-- loading content " << folder << endl;
|
||||
std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
|
||||
|
||||
path file = folder / path("package.json");
|
||||
string source = files::read_string(file);
|
||||
|
||||
unique_ptr<json::JObject> root = nullptr;
|
||||
try {
|
||||
root.reset(json::parse(file.filename().string(), source));
|
||||
} catch (const parsing_error& error) {
|
||||
cerr << error.errorLog() << endl;
|
||||
throw std::runtime_error("could not load content package");
|
||||
}
|
||||
|
||||
string id;
|
||||
string version;
|
||||
root->str("id", id);
|
||||
root->str("version", version);
|
||||
|
||||
cout << " id: " << id << endl;
|
||||
cout << " version: " << version << endl;
|
||||
auto folder = pack->folder;
|
||||
auto root = files::read_json(pack->getContentFile());
|
||||
|
||||
json::JArray* blocksarr = root->arr("blocks");
|
||||
if (blocksarr) {
|
||||
cout << " blocks: " << blocksarr->size() << endl;
|
||||
for (uint i = 0; i < blocksarr->size(); i++) {
|
||||
string name = blocksarr->str(i);
|
||||
cout << " loading block " << id << ":" << name << endl;
|
||||
path blockfile = folder/path("blocks/"+name+".json");
|
||||
builder->add(loadBlock(id+":"+name, blockfile));
|
||||
std::string name = blocksarr->str(i);
|
||||
fs::path blockfile = folder/fs::path("blocks/"+name+".json");
|
||||
builder->add(loadBlock(pack->id+":"+name, blockfile));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
#include <filesystem>
|
||||
|
||||
class Block;
|
||||
class ContentPack;
|
||||
class ContentBuilder;
|
||||
|
||||
class ContentLoader {
|
||||
std::filesystem::path folder;
|
||||
const ContentPack* pack;
|
||||
public:
|
||||
ContentLoader(std::filesystem::path folder);
|
||||
ContentLoader(const ContentPack* pack);
|
||||
|
||||
Block* loadBlock(std::string name, std::filesystem::path file);
|
||||
void load(ContentBuilder* builder);
|
||||
|
||||
@@ -1 +1,46 @@
|
||||
#include "ContentPack.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "../files/files.h"
|
||||
#include "../coders/json.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
const std::string ContentPack::PACKAGE_FILENAME = "package.json";
|
||||
const std::string ContentPack::CONTENT_FILENAME = "content.json";
|
||||
|
||||
std::filesystem::path ContentPack::getContentFile() const {
|
||||
return folder/fs::path(CONTENT_FILENAME);
|
||||
}
|
||||
|
||||
bool ContentPack::is_pack(std::filesystem::path folder) {
|
||||
return fs::is_regular_file(folder/fs::path(PACKAGE_FILENAME));
|
||||
}
|
||||
|
||||
ContentPack ContentPack::read(std::filesystem::path folder) {
|
||||
auto root = files::read_json(folder/fs::path(PACKAGE_FILENAME));
|
||||
ContentPack pack;
|
||||
root->str("id", pack.id);
|
||||
root->str("title", pack.title);
|
||||
root->str("version", pack.version);
|
||||
pack.folder = folder;
|
||||
if (pack.id == "none")
|
||||
throw std::runtime_error("content-pack id is none: "+folder.u8string());
|
||||
return pack;
|
||||
}
|
||||
|
||||
void ContentPack::scan(fs::path rootfolder,
|
||||
std::vector<ContentPack>& packs) {
|
||||
if (!fs::is_directory(rootfolder)) {
|
||||
return;
|
||||
}
|
||||
for (auto entry : fs::directory_iterator(rootfolder)) {
|
||||
fs::path folder = entry.path();
|
||||
if (!fs::is_directory(folder))
|
||||
continue;
|
||||
if (!is_pack(folder))
|
||||
continue;
|
||||
packs.push_back(read(folder));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,23 @@
|
||||
#define CONTENT_CONTENT_PACK_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
struct ContentPack {
|
||||
std::string id;
|
||||
std::string id = "none";
|
||||
std::string title = "untitled";
|
||||
std::string version = "0.0";
|
||||
std::filesystem::path folder;
|
||||
|
||||
std::filesystem::path getContentFile() const;
|
||||
|
||||
static const std::string PACKAGE_FILENAME;
|
||||
static const std::string CONTENT_FILENAME;
|
||||
static bool is_pack(std::filesystem::path folder);
|
||||
static ContentPack read(std::filesystem::path folder);
|
||||
static void scan(std::filesystem::path folder,
|
||||
std::vector<ContentPack>& packs);
|
||||
};
|
||||
|
||||
#endif // CONTENT_CONTENT_PACK_H_
|
||||
|
||||
Reference in New Issue
Block a user