adding/removing packs update

This commit is contained in:
MihailRis
2024-04-13 23:26:21 +03:00
parent 657b09da9e
commit 7c4c511826
5 changed files with 61 additions and 95 deletions
+15 -51
View File
@@ -21,6 +21,7 @@
#include "../items/Inventory.h"
#include "../data/dynamic.h"
#include "../core_defs.h"
#include <cassert>
#include <iostream>
@@ -496,7 +497,9 @@ void WorldFiles::write(const World* world, const Content* content) {
if (world) {
writeWorldInfo(world);
writePacks(world);
if (!fs::exists(getPacksFile())) {
writePacks(world->getPacks());
}
}
if (generatorTestMode) {
return;
@@ -508,13 +511,8 @@ void WorldFiles::write(const World* world, const Content* content) {
writeRegions(storages, inventoriesFolder, REGION_LAYER_INVENTORIES);
}
void WorldFiles::writePacks(const World* world) {
void WorldFiles::writePacks(const std::vector<ContentPack>& packs) {
auto packsFile = getPacksFile();
if (fs::is_regular_file(packsFile)) {
return;
}
const auto& packs = world->getPacks();
std::stringstream ss;
ss << "# autogenerated; do not modify\n";
for (const auto& pack : packs) {
@@ -559,56 +557,15 @@ bool WorldFiles::readWorldInfo(World* world) {
return true;
}
void WorldFiles::addPack(const World* world, const std::string& id) {
fs::path file = getPacksFile();
if (!fs::is_regular_file(file)) {
if (!fs::is_directory(directory)) {
fs::create_directories(directory);
}
writePacks(world);
}
auto packs = files::read_list(file);
packs.push_back(id);
std::stringstream ss;
ss << "# autogenerated; do not modify\n";
for (const auto& pack : packs) {
ss << pack << "\n";
}
files::write_string(file, ss.str());
}
void WorldFiles::removePack(const World* world, const std::string& id) {
fs::path file = getPacksFile();
if (!fs::is_regular_file(file)) {
if (!fs::is_directory(directory)) {
fs::create_directories(directory);
}
writePacks(world);
}
auto packs = files::read_list(file);
auto found = std::find(packs.begin(), packs.end(), id);
if (found != packs.end()) {
packs.erase(found);
}
std::stringstream ss;
ss << "# autogenerated; do not modify\n";
for (const auto& pack : packs) {
ss << pack << "\n";
}
files::write_string(file, ss.str());
// erase invalid indices
static void erase_pack_indices(dynamic::Map* root, const std::string& id) {
auto prefix = id+":";
auto root = files::read_json(getIndicesFile());
auto blocks = root->list("blocks");
for (uint i = 0; i < blocks->size(); i++) {
auto name = blocks->str(i);
if (name.find(prefix) != 0)
continue;
auto value = blocks->getValueWriteable(i);
value->value = "core:air";
value->value = CORE_AIR;
}
auto items = root->list("items");
@@ -617,7 +574,14 @@ void WorldFiles::removePack(const World* world, const std::string& id) {
if (name.find(prefix) != 0)
continue;
auto value = items->getValueWriteable(i);
value->value = "core:empty";
value->value = 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);
}
files::write_json(getIndicesFile(), root.get());
}