feat: saving/loading resource entries

This commit is contained in:
MihailRis
2024-07-11 21:56:27 +03:00
parent ca8652ffab
commit 45c4da048c
9 changed files with 63 additions and 8 deletions
+7 -2
View File
@@ -30,7 +30,8 @@ Content::Content(
ContentUnitDefs<EntityDef> entities,
UptrsMap<std::string, ContentPackRuntime> packs,
UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::SkeletonConfig> skeletons
UptrsMap<std::string, rigging::SkeletonConfig> 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() {
}
+24 -3
View File
@@ -3,6 +3,8 @@
#include "content_fwd.hpp"
#include "../data/dynamic_fwd.hpp"
#include <string>
#include <vector>
#include <memory>
@@ -109,14 +111,20 @@ public:
class ResourceIndices {
std::vector<std::string> names;
std::unordered_map<std::string, size_t> indices;
std::vector<dynamic::Map_sptr> 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> ResourceType_from(std::string_view str) {
return std::nullopt;
}
using ResourceIndicesSet = ResourceIndices[static_cast<size_t>(ResourceType::LAST)+1];
using ResourceIndicesSet = ResourceIndices[RESOURCE_TYPES_COUNT];
/// @brief Content is a definitions repository
class Content {
@@ -165,7 +185,8 @@ public:
ContentUnitDefs<EntityDef> entities,
UptrsMap<std::string, ContentPackRuntime> packs,
UptrsMap<std::string, BlockMaterial> blockMaterials,
UptrsMap<std::string, rigging::SkeletonConfig> skeletons
UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
const ResourceIndicesSet& resourceIndices
);
~Content();
+2 -1
View File
@@ -75,7 +75,8 @@ std::unique_ptr<Content> 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
+1 -1
View File
@@ -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<size_t>(type)].add(list->str(i));
builder.resourceIndices[static_cast<size_t>(type)].add(list->str(i), nullptr);
}
}
+2
View File
@@ -15,4 +15,6 @@ enum class ResourceType : size_t {
LAST=CAMERA
};
inline constexpr auto RESOURCE_TYPES_COUNT = static_cast<size_t>(ResourceType::LAST)+1;
#endif // CONTENT_CONTENT_FWD_HPP_