World indices check
This commit is contained in:
@@ -64,7 +64,15 @@ Content::~Content() {
|
||||
delete indices;
|
||||
}
|
||||
|
||||
Block* Content::require(std::string id) const {
|
||||
Block* Content::findBlock(string id) const {
|
||||
auto found = blockDefs.find(id);
|
||||
if (found == blockDefs.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
|
||||
Block* Content::requireBlock(string id) const {
|
||||
auto found = blockDefs.find(id);
|
||||
if (found == blockDefs.end()) {
|
||||
throw std::runtime_error("missing block "+id);
|
||||
|
||||
@@ -55,7 +55,8 @@ public:
|
||||
std::unordered_map<std::string, Block*> blockDefs);
|
||||
~Content();
|
||||
|
||||
Block* require(std::string id) const;
|
||||
Block* findBlock(std::string id) const;
|
||||
Block* requireBlock(std::string id) const;
|
||||
};
|
||||
|
||||
#endif // CONTENT_CONTENT_H_
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "ContentIndexLUT.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "Content.h"
|
||||
#include "../constants.h"
|
||||
#include "../files/files.h"
|
||||
#include "../coders/json.h"
|
||||
#include "../voxels/Block.h"
|
||||
|
||||
using std::string;
|
||||
using std::unique_ptr;
|
||||
using std::filesystem::path;
|
||||
|
||||
ContentIndexLUT::ContentIndexLUT(size_t blocksCount, const Content* content) {
|
||||
blocks = new blockid_t[blocksCount];
|
||||
blockNames = new string[blocksCount];
|
||||
for (size_t i = 0; i < blocksCount; i++) {
|
||||
blocks[i] = i;
|
||||
}
|
||||
|
||||
ContentIndices* indices = content->indices;
|
||||
for (size_t i = 0; i < indices->countBlockDefs(); i++) {
|
||||
blockNames[i] = indices->getBlockDef(i)->name;
|
||||
}
|
||||
}
|
||||
|
||||
ContentIndexLUT::~ContentIndexLUT() {
|
||||
delete[] blockNames;
|
||||
delete[] blocks;
|
||||
}
|
||||
|
||||
ContentIndexLUT* ContentIndexLUT::create(const path& filename, const Content* content) {
|
||||
auto& indices = content->indices;
|
||||
unique_ptr<json::JObject> root(files::read_json(filename));
|
||||
json::JArray* blocksarr = root->arr("blocks");
|
||||
|
||||
size_t blocks_c = blocksarr
|
||||
? std::max(blocksarr->size(), indices->countBlockDefs())
|
||||
: indices->countBlockDefs();
|
||||
|
||||
unique_ptr<ContentIndexLUT> lut(new ContentIndexLUT(blocks_c, content));
|
||||
|
||||
bool conflicts = false;
|
||||
|
||||
// TODO: implement world files convert feature using ContentIndexLUT report
|
||||
for (size_t i = 0; i < blocksarr->size(); i++) {
|
||||
string name = blocksarr->str(i);
|
||||
Block* def = content->findBlock(name);
|
||||
if (def) {
|
||||
if (i != def->rt.id) {
|
||||
std::cerr << "block id has changed from ";
|
||||
std::cerr << def->rt.id << " to " << i << std::endl;
|
||||
conflicts = true;
|
||||
}
|
||||
lut->setBlock(i, name, def->rt.id);
|
||||
} else {
|
||||
std::cerr << "unknown block: " << name << std::endl;
|
||||
lut->setBlock(i, name, i);
|
||||
conflicts = true;
|
||||
}
|
||||
}
|
||||
if (conflicts) {
|
||||
return lut.release();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef CONTENT_CONTENT_INDEX_LUT_H_
|
||||
#define CONTENT_CONTENT_INDEX_LUT_H_
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
class Content;
|
||||
|
||||
/* Content indices lookup table or report
|
||||
used to convert world with different indices
|
||||
Building with indices.json */
|
||||
class ContentIndexLUT {
|
||||
blockid_t* blocks;
|
||||
std::string* blockNames;
|
||||
public:
|
||||
ContentIndexLUT(size_t blocks, const Content* content);
|
||||
~ContentIndexLUT();
|
||||
|
||||
inline blockid_t getBlockId(blockid_t index) {
|
||||
return blocks[index];
|
||||
}
|
||||
|
||||
inline void setBlock(blockid_t index, std::string name, blockid_t id) {
|
||||
blocks[index] = id;
|
||||
blockNames[index] = name;
|
||||
}
|
||||
|
||||
static ContentIndexLUT* create(const std::filesystem::path& filename,
|
||||
const Content* content);
|
||||
};
|
||||
|
||||
#endif // CONTENT_CONTENT_INDEX_LUT_H_
|
||||
Reference in New Issue
Block a user