diff --git a/src/content/Content.cpp b/src/content/Content.cpp index 0756129f..f0a7c718 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -30,7 +30,8 @@ Content::Content( ContentUnitDefs entities, UptrsMap packs, UptrsMap blockMaterials, - UptrsMap skeletons + UptrsMap skeletons, + const ResourceIndicesSet& resourceIndices ) : indices(std::move(indices)), packs(std::move(packs)), blockMaterials(std::move(blockMaterials)), @@ -39,7 +40,11 @@ Content::Content( items(std::move(items)), entities(std::move(entities)), drawGroups(std::move(drawGroups)) -{} +{ + for (size_t i = 0; i < RESOURCE_TYPES_COUNT; i++) { + this->resourceIndices[i] = resourceIndices[i]; + } +} Content::~Content() { } diff --git a/src/content/Content.hpp b/src/content/Content.hpp index 121b8e16..87a3255c 100644 --- a/src/content/Content.hpp +++ b/src/content/Content.hpp @@ -3,6 +3,8 @@ #include "content_fwd.hpp" +#include "../data/dynamic_fwd.hpp" + #include #include #include @@ -109,14 +111,20 @@ public: class ResourceIndices { std::vector names; std::unordered_map indices; + std::vector savedData; public: ResourceIndices() {} static constexpr size_t MISSING = -1; - void add(std::string name) { + void add(std::string name, dynamic::Map_sptr map) { indices[name] = names.size(); names.push_back(name); + savedData.push_back(map); + } + + const std::string& getName(size_t index) const { + return names.at(index); } size_t indexOf(const std::string& name) const { @@ -126,6 +134,18 @@ public: } return MISSING; } + + dynamic::Map_sptr getSavedData(size_t index) const { + return savedData.at(index); + } + + void saveData(size_t index, dynamic::Map_sptr map) { + savedData.at(index) = map; + } + + size_t size() const { + return names.size(); + } }; constexpr const char* to_string(ResourceType type) { @@ -142,7 +162,7 @@ inline std::optional ResourceType_from(std::string_view str) { return std::nullopt; } -using ResourceIndicesSet = ResourceIndices[static_cast(ResourceType::LAST)+1]; +using ResourceIndicesSet = ResourceIndices[RESOURCE_TYPES_COUNT]; /// @brief Content is a definitions repository class Content { @@ -165,7 +185,8 @@ public: ContentUnitDefs entities, UptrsMap packs, UptrsMap blockMaterials, - UptrsMap skeletons + UptrsMap skeletons, + const ResourceIndicesSet& resourceIndices ); ~Content(); diff --git a/src/content/ContentBuilder.cpp b/src/content/ContentBuilder.cpp index a5392679..6e55151d 100644 --- a/src/content/ContentBuilder.cpp +++ b/src/content/ContentBuilder.cpp @@ -75,7 +75,8 @@ std::unique_ptr ContentBuilder::build() { entities.build(), std::move(packs), std::move(blockMaterials), - std::move(skeletons) + std::move(skeletons), + resourceIndices ); // Now, it's time to resolve foreign keys diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 9ff3463b..72592c1c 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -500,6 +500,6 @@ void ContentLoader::load() { void ContentLoader::loadResources(ResourceType type, dynamic::List* list) { for (size_t i = 0; i < list->size(); i++) { - builder.resourceIndices[static_cast(type)].add(list->str(i)); + builder.resourceIndices[static_cast(type)].add(list->str(i), nullptr); } } diff --git a/src/content/content_fwd.hpp b/src/content/content_fwd.hpp index ea4b3459..52bedab8 100644 --- a/src/content/content_fwd.hpp +++ b/src/content/content_fwd.hpp @@ -15,4 +15,6 @@ enum class ResourceType : size_t { LAST=CAMERA }; +inline constexpr auto RESOURCE_TYPES_COUNT = static_cast(ResourceType::LAST)+1; + #endif // CONTENT_CONTENT_FWD_HPP_ diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index b497b69a..4b43ff51 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -56,6 +56,10 @@ fs::path WorldFiles::getPlayerFile() const { return directory/fs::path("player.json"); } +fs::path WorldFiles::getResourcesFile() const { + return directory/fs::path("resources.json"); +} + fs::path WorldFiles::getWorldFile() const { return directory/fs::path(WORLD_FILE); } diff --git a/src/files/WorldFiles.hpp b/src/files/WorldFiles.hpp index 67d942ea..67e230b4 100644 --- a/src/files/WorldFiles.hpp +++ b/src/files/WorldFiles.hpp @@ -46,6 +46,7 @@ public: ~WorldFiles(); fs::path getPlayerFile() const; + fs::path getResourcesFile() const; void createDirectories(); bool readWorldInfo(World* world); diff --git a/src/world/World.cpp b/src/world/World.cpp index 580dde3e..b27c3f2e 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -52,13 +52,30 @@ void World::updateTimers(float delta) { totalTime += delta; } +void World::writeResources(const Content* content) { + auto root = dynamic::Map(); + for (size_t typeIndex = 0; typeIndex < RESOURCE_TYPES_COUNT; typeIndex++) { + auto typeName = to_string(static_cast(typeIndex)); + auto& list = root.putList(typeName); + auto& indices = content->resourceIndices[typeIndex]; + for (size_t i = 0; i < indices.size(); i++) { + auto& map = list.putMap(); + map.put("name", indices.getName(i)); + if (auto data = indices.getSavedData(i)) { + map.put("saved", data); + } + } + } + files::write_json(wfile->getResourcesFile(), &root); +} + void World::write(Level* level) { const Content* content = level->content; level->chunks->saveAll(); nextEntityId = level->entities->peekNextID(); wfile->write(this, content); + auto playerFile = dynamic::Map(); - auto& players = playerFile.putList("players"); for (const auto& object : level->objects) { if (auto player = std::dynamic_pointer_cast(object)) { @@ -66,6 +83,8 @@ void World::write(Level* level) { } } files::write_json(wfile->getPlayerFile(), &playerFile); + + writeResources(content); } std::unique_ptr World::create( diff --git a/src/world/World.hpp b/src/world/World.hpp index 0e1c8fca..f3e14513 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -34,6 +34,8 @@ class World : Serializable { std::vector packs; int64_t nextInventoryId = 0; + + void writeResources(const Content* content); public: std::unique_ptr wfile;