Merge branch 'main' into blocks-metadata
This commit is contained in:
+30
-36
@@ -13,7 +13,6 @@
|
||||
#include "constants.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "core_defs.hpp"
|
||||
#include "data/dynamic.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "items/Inventory.hpp"
|
||||
#include "items/ItemDef.hpp"
|
||||
@@ -104,24 +103,24 @@ void WorldFiles::writePacks(const std::vector<ContentPack>& packs) {
|
||||
|
||||
template <class T>
|
||||
static void write_indices(
|
||||
const ContentUnitIndices<T>& indices, dynamic::List& list
|
||||
const ContentUnitIndices<T>& indices, dv::value& list
|
||||
) {
|
||||
for (auto unit : indices.getIterable()) {
|
||||
list.put(unit->name);
|
||||
list.add(unit->name);
|
||||
}
|
||||
}
|
||||
|
||||
void WorldFiles::writeIndices(const ContentIndices* indices) {
|
||||
dynamic::Map root;
|
||||
root.put("region-version", REGION_FORMAT_VERSION);
|
||||
write_indices(indices->blocks, root.putList("blocks"));
|
||||
write_indices(indices->items, root.putList("items"));
|
||||
write_indices(indices->entities, root.putList("entities"));
|
||||
files::write_json(getIndicesFile(), &root);
|
||||
dv::value root = dv::object();
|
||||
root["region-version"] = REGION_FORMAT_VERSION;
|
||||
write_indices(indices->blocks, root.list("blocks"));
|
||||
write_indices(indices->items, root.list("items"));
|
||||
write_indices(indices->entities, root.list("entities"));
|
||||
files::write_json(getIndicesFile(), root);
|
||||
}
|
||||
|
||||
void WorldFiles::writeWorldInfo(const WorldInfo& info) {
|
||||
files::write_json(getWorldFile(), info.serialize().get());
|
||||
files::write_json(getWorldFile(), info.serialize());
|
||||
}
|
||||
|
||||
std::optional<WorldInfo> WorldFiles::readWorldInfo() {
|
||||
@@ -132,23 +131,22 @@ std::optional<WorldInfo> WorldFiles::readWorldInfo() {
|
||||
}
|
||||
auto root = files::read_json(file);
|
||||
WorldInfo info {};
|
||||
info.deserialize(root.get());
|
||||
info.deserialize(root);
|
||||
return info;
|
||||
}
|
||||
|
||||
static void read_resources_data(
|
||||
const Content* content, const dynamic::List_sptr& list, ResourceType type
|
||||
const Content* content, const dv::value& list, ResourceType type
|
||||
) {
|
||||
const auto& indices = content->getIndices(type);
|
||||
for (size_t i = 0; i < list->size(); i++) {
|
||||
auto map = list->map(i);
|
||||
std::string name;
|
||||
map->str("name", name);
|
||||
for (size_t i = 0; i < list.size(); i++) {
|
||||
auto& map = list[i];
|
||||
const auto& name = map["name"].asString();
|
||||
size_t index = indices.indexOf(name);
|
||||
if (index == ResourceIndices::MISSING) {
|
||||
logger.warning() << "discard " << name;
|
||||
} else {
|
||||
indices.saveData(index, map->map("saved"));
|
||||
indices.saveData(index, map["saved"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,11 +158,9 @@ bool WorldFiles::readResourcesData(const Content* content) {
|
||||
return false;
|
||||
}
|
||||
auto root = files::read_json(file);
|
||||
for (const auto& [key, _] : root->values) {
|
||||
for (const auto& [key, arr] : root.asObject()) {
|
||||
if (auto resType = ResourceType_from(key)) {
|
||||
if (auto arr = root->list(key)) {
|
||||
read_resources_data(content, arr, *resType);
|
||||
}
|
||||
read_resources_data(content, arr, *resType);
|
||||
} else {
|
||||
logger.warning() << "unknown resource type: " << key;
|
||||
}
|
||||
@@ -179,35 +175,33 @@ void WorldFiles::patchIndicesVersion(const std::string& field, uint version) {
|
||||
return;
|
||||
}
|
||||
auto root = files::read_json(file);
|
||||
root->put(field, version);
|
||||
files::write_json(file, root.get(), true);
|
||||
root[field] = version;
|
||||
files::write_json(file, root, true);
|
||||
}
|
||||
|
||||
static void erase_pack_indices(dynamic::Map* root, const std::string& id) {
|
||||
static void erase_pack_indices(dv::value& root, const std::string& id) {
|
||||
auto prefix = id + ":";
|
||||
auto blocks = root->list("blocks");
|
||||
for (uint i = 0; i < blocks->size(); i++) {
|
||||
auto name = blocks->str(i);
|
||||
auto& blocks = root["blocks"];
|
||||
for (uint i = 0; i < blocks.size(); i++) {
|
||||
auto name = blocks[i].asString();
|
||||
if (name.find(prefix) != 0) continue;
|
||||
auto value = blocks->getValueWriteable(i);
|
||||
*value = CORE_AIR;
|
||||
blocks[i] = CORE_AIR;
|
||||
}
|
||||
|
||||
auto items = root->list("items");
|
||||
for (uint i = 0; i < items->size(); i++) {
|
||||
auto name = items->str(i);
|
||||
auto& items = root["items"];
|
||||
for (uint i = 0; i < items.size(); i++) {
|
||||
auto& name = items[i].asString();
|
||||
if (name.find(prefix) != 0) continue;
|
||||
auto value = items->getValueWriteable(i);
|
||||
*value = CORE_EMPTY;
|
||||
items[i] = CORE_EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
void WorldFiles::removeIndices(const std::vector<std::string>& packs) {
|
||||
auto root = files::read_json(getIndicesFile());
|
||||
for (const auto& id : packs) {
|
||||
erase_pack_indices(root.get(), id);
|
||||
erase_pack_indices(root, id);
|
||||
}
|
||||
files::write_json(getIndicesFile(), root.get());
|
||||
files::write_json(getIndicesFile(), root);
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getFolder() const {
|
||||
|
||||
Reference in New Issue
Block a user