Worlds indexing
This commit is contained in:
@@ -1,69 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
#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_
|
||||
@@ -0,0 +1,53 @@
|
||||
#include "ContentLUT.h"
|
||||
|
||||
#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::make_unique;
|
||||
using std::filesystem::path;
|
||||
|
||||
#include <iostream>
|
||||
|
||||
ContentLUT::ContentLUT(size_t blocksCount, const Content* content) {
|
||||
ContentIndices* indices = content->indices;
|
||||
for (size_t i = 0; i < blocksCount; i++) {
|
||||
blocks.push_back(i);
|
||||
blockNames.push_back(indices->getBlockDef(i)->name);
|
||||
}
|
||||
}
|
||||
|
||||
ContentLUT* ContentLUT::create(const path& filename,
|
||||
const Content* content) {
|
||||
unique_ptr<json::JObject> root(files::read_json(filename));
|
||||
json::JArray* blocksarr = root->arr("blocks");
|
||||
|
||||
auto& indices = content->indices;
|
||||
size_t blocks_c = blocksarr
|
||||
? std::max(blocksarr->size(), indices->countBlockDefs())
|
||||
: indices->countBlockDefs();
|
||||
|
||||
auto lut = make_unique<ContentLUT>(blocks_c, content);
|
||||
if (blocksarr) {
|
||||
for (size_t i = 0; i < blocksarr->size(); i++) {
|
||||
string name = blocksarr->str(i);
|
||||
Block* def = content->findBlock(name);
|
||||
if (def) {
|
||||
lut->setBlock(i, name, def->rt.id);
|
||||
} else {
|
||||
lut->setBlock(i, name, BLOCK_VOID);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lut->hasContentReorder() || lut->hasMissingContent()) {
|
||||
return lut.release();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#ifndef CONTENT_CONTENT_LUT_H_
|
||||
#define CONTENT_CONTENT_LUT_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../constants.h"
|
||||
|
||||
class Content;
|
||||
|
||||
/* Content indices lookup table or report
|
||||
used to convert world with different indices
|
||||
Building with indices.json */
|
||||
class ContentLUT {
|
||||
std::vector<blockid_t> blocks;
|
||||
std::vector<std::string> blockNames;
|
||||
|
||||
bool reorderContent = false;
|
||||
bool missingContent = false;
|
||||
public:
|
||||
ContentLUT(size_t blocks, const Content* content);
|
||||
|
||||
inline const std::string& getBlockName(blockid_t index) const {
|
||||
return blockNames[index];
|
||||
}
|
||||
|
||||
inline blockid_t getBlockId(blockid_t index) const {
|
||||
return blocks[index];
|
||||
}
|
||||
|
||||
inline void setBlock(blockid_t index, std::string name, blockid_t id) {
|
||||
blocks[index] = id;
|
||||
blockNames[index] = name;
|
||||
if (id == BLOCK_VOID) {
|
||||
missingContent = true;
|
||||
} else if (index != id) {
|
||||
reorderContent = true;
|
||||
}
|
||||
}
|
||||
|
||||
static ContentLUT* create(const std::filesystem::path& filename,
|
||||
const Content* content);
|
||||
|
||||
inline bool hasContentReorder() const {
|
||||
return reorderContent;
|
||||
}
|
||||
inline bool hasMissingContent() const {
|
||||
return missingContent;
|
||||
}
|
||||
|
||||
inline size_t countBlocks() const {
|
||||
return blocks.size();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // CONTENT_CONTENT_LUT_H_
|
||||
Reference in New Issue
Block a user