feat: world files resources file loading
This commit is contained in:
parent
a013e7eefd
commit
9623f35862
@ -117,7 +117,7 @@ public:
|
||||
: savedData(std::make_unique<std::vector<dynamic::Map_sptr>>()){
|
||||
}
|
||||
|
||||
static constexpr size_t MISSING = -1;
|
||||
static constexpr size_t MISSING = SIZE_MAX;
|
||||
|
||||
void add(std::string name, dynamic::Map_sptr map) {
|
||||
indices[name] = names.size();
|
||||
@ -131,7 +131,7 @@ public:
|
||||
|
||||
size_t indexOf(const std::string& name) const {
|
||||
const auto& found = indices.find(name);
|
||||
if (found == indices.end()) {
|
||||
if (found != indices.end()) {
|
||||
return found->second;
|
||||
}
|
||||
return MISSING;
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include "../content/Content.hpp"
|
||||
#include "../core_defs.hpp"
|
||||
#include "../data/dynamic.hpp"
|
||||
#include "../debug/Logger.hpp"
|
||||
#include "../items/Inventory.hpp"
|
||||
#include "../items/ItemDef.hpp"
|
||||
#include "../lighting/Lightmap.hpp"
|
||||
@ -32,6 +33,8 @@
|
||||
|
||||
#define WORLD_FORMAT_MAGIC ".VOXWLD"
|
||||
|
||||
static debug::Logger logger("world-files");
|
||||
|
||||
WorldFiles::WorldFiles(const fs::path& directory) : directory(directory), regions(directory) {
|
||||
}
|
||||
|
||||
@ -120,15 +123,52 @@ void WorldFiles::writeWorldInfo(const World* world) {
|
||||
bool WorldFiles::readWorldInfo(World* world) {
|
||||
fs::path file = getWorldFile();
|
||||
if (!fs::is_regular_file(file)) {
|
||||
std::cerr << "warning: world.json does not exists" << std::endl;
|
||||
logger.warning() << "world.json does not exists";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto root = files::read_json(file);
|
||||
world->deserialize(root.get());
|
||||
return true;
|
||||
}
|
||||
|
||||
static void read_resources_data(
|
||||
const Content* content,
|
||||
const dynamic::List_sptr& 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);
|
||||
size_t index = indices.indexOf(name);
|
||||
if (index == ResourceIndices::MISSING) {
|
||||
logger.warning() << "discard " << name;
|
||||
} else {
|
||||
indices.saveData(index, map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool WorldFiles::readResourcesData(const Content* content) {
|
||||
fs::path file = getResourcesFile();
|
||||
if (!fs::is_regular_file(file)) {
|
||||
logger.warning() << "resources.json does not exists";
|
||||
return false;
|
||||
}
|
||||
auto root = files::read_json(file);
|
||||
for (const auto& [key, _] : root->values) {
|
||||
if (auto resType = ResourceType_from(key)) {
|
||||
if (auto arr = root->list(key)) {
|
||||
read_resources_data(content, arr, *resType);
|
||||
}
|
||||
} else {
|
||||
logger.warning() << "unknown resource type: " << key;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void erase_pack_indices(dynamic::Map* root, const std::string& id) {
|
||||
auto prefix = id+":";
|
||||
auto blocks = root->list("blocks");
|
||||
|
||||
@ -50,6 +50,7 @@ public:
|
||||
void createDirectories();
|
||||
|
||||
bool readWorldInfo(World* world);
|
||||
bool readResourcesData(const Content* content);
|
||||
|
||||
/// @brief Write all unsaved data to world files
|
||||
/// @param world target world
|
||||
|
||||
@ -59,8 +59,7 @@ Level::Level(
|
||||
auto camera = std::make_shared<Camera>();
|
||||
if (auto map = cameraIndices.getSavedData(i)) {
|
||||
dynamic::get_vec(map, "pos", camera->position);
|
||||
dynamic::get_vec(map, "front", camera->front);
|
||||
dynamic::get_vec(map, "up", camera->up);
|
||||
dynamic::get_mat(map, "rot", camera->rotation);
|
||||
map->flag("perspective", camera->perspective);
|
||||
map->flag("flipped", camera->flipped);
|
||||
map->num("zoom", camera->zoom);
|
||||
@ -99,8 +98,7 @@ void Level::onSave() {
|
||||
auto& camera = *cameras.at(i);
|
||||
auto map = dynamic::create_map();
|
||||
map->put("pos", dynamic::to_value(camera.position));
|
||||
map->put("front", dynamic::to_value(camera.front));
|
||||
map->put("up", dynamic::to_value(camera.up));
|
||||
map->put("rot", dynamic::to_value(camera.rotation));
|
||||
map->put("perspective", camera.perspective);
|
||||
map->put("flipped", camera.flipped);
|
||||
map->put("zoom", camera.zoom);
|
||||
|
||||
@ -116,6 +116,7 @@ std::unique_ptr<Level> World::load(
|
||||
if (!wfile->readWorldInfo(world.get())) {
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
wfile->readResourcesData(content);
|
||||
|
||||
auto level = std::make_unique<Level>(std::move(world), content, settings);
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user