refactor ContentLoader
This commit is contained in:
parent
9912079ad1
commit
6f5eb6be48
@ -415,6 +415,18 @@ void ContentLoader::loadItem(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::tuple<std::string, std::string, std::string> create_unit_id(
|
||||||
|
const std::string& packid, const std::string& name
|
||||||
|
) {
|
||||||
|
size_t colon = name.find(':');
|
||||||
|
if (colon == std::string::npos) {
|
||||||
|
return {packid, packid + ":" + name, name};
|
||||||
|
}
|
||||||
|
auto otherPackid = name.substr(0, colon);
|
||||||
|
auto full = otherPackid + ":" + name;
|
||||||
|
return {otherPackid, full, otherPackid + "/" + name};
|
||||||
|
}
|
||||||
|
|
||||||
void ContentLoader::loadBlockMaterial(
|
void ContentLoader::loadBlockMaterial(
|
||||||
BlockMaterial& def, const fs::path& file
|
BlockMaterial& def, const fs::path& file
|
||||||
) {
|
) {
|
||||||
@ -445,28 +457,28 @@ void ContentLoader::load() {
|
|||||||
if (auto blocksarr = root->list("blocks")) {
|
if (auto blocksarr = root->list("blocks")) {
|
||||||
for (size_t i = 0; i < blocksarr->size(); i++) {
|
for (size_t i = 0; i < blocksarr->size(); i++) {
|
||||||
std::string name = blocksarr->str(i);
|
std::string name = blocksarr->str(i);
|
||||||
auto colon = name.find(':');
|
auto [packid, full, filename] = create_unit_id(pack->id, 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);
|
auto& def = builder.blocks.create(full);
|
||||||
if (colon != std::string::npos)
|
if (filename != name) {
|
||||||
def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
|
def.scriptName = packid + "/" + def.scriptName;
|
||||||
loadBlock(def, full, name);
|
}
|
||||||
|
|
||||||
|
loadBlock(def, full, filename);
|
||||||
stats->totalBlocks++;
|
stats->totalBlocks++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (auto itemsarr = root->list("items")) {
|
if (auto itemsarr = root->list("items")) {
|
||||||
for (size_t i = 0; i < itemsarr->size(); i++) {
|
for (size_t i = 0; i < itemsarr->size(); i++) {
|
||||||
std::string name = itemsarr->str(i);
|
std::string name = itemsarr->str(i);
|
||||||
auto colon = name.find(':');
|
auto [packid, full, filename] = create_unit_id(pack->id, 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);
|
auto& def = builder.items.create(full);
|
||||||
if (colon != std::string::npos)
|
if (filename != name) {
|
||||||
def.scriptName = name.substr(0, colon) + '/' + def.scriptName;
|
def.scriptName = packid + "/" + def.scriptName;
|
||||||
loadItem(def, full, name);
|
}
|
||||||
|
|
||||||
|
loadItem(def, full, filename);
|
||||||
stats->totalItems++;
|
stats->totalItems++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -474,11 +486,11 @@ void ContentLoader::load() {
|
|||||||
if (auto entitiesarr = root->list("entities")) {
|
if (auto entitiesarr = root->list("entities")) {
|
||||||
for (size_t i = 0; i < entitiesarr->size(); i++) {
|
for (size_t i = 0; i < entitiesarr->size(); i++) {
|
||||||
std::string name = entitiesarr->str(i);
|
std::string name = entitiesarr->str(i);
|
||||||
auto colon = name.find(':');
|
|
||||||
std::string full =
|
auto [packid, full, filename] = create_unit_id(pack->id, name);
|
||||||
colon == std::string::npos ? pack->id + ":" + name : name;
|
|
||||||
if (colon != std::string::npos) name[colon] = '/';
|
|
||||||
auto& def = builder.entities.create(full);
|
auto& def = builder.entities.create(full);
|
||||||
|
|
||||||
loadEntity(def, full, name);
|
loadEntity(def, full, name);
|
||||||
stats->totalEntities++;
|
stats->totalEntities++;
|
||||||
}
|
}
|
||||||
@ -488,8 +500,12 @@ void ContentLoader::load() {
|
|||||||
if (fs::is_directory(materialsDir)) {
|
if (fs::is_directory(materialsDir)) {
|
||||||
for (const auto& entry : fs::directory_iterator(materialsDir)) {
|
for (const auto& entry : fs::directory_iterator(materialsDir)) {
|
||||||
const fs::path& file = entry.path();
|
const fs::path& file = entry.path();
|
||||||
std::string name = pack->id + ":" + file.stem().u8string();
|
auto [packid, full, filename] =
|
||||||
loadBlockMaterial(builder.createBlockMaterial(name), file);
|
create_unit_id(pack->id, file.stem().u8string());
|
||||||
|
loadBlockMaterial(
|
||||||
|
builder.createBlockMaterial(full),
|
||||||
|
materialsDir / fs::u8path(filename + ".json")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user