feat: saving/loading resource entries
This commit is contained in:
parent
ca8652ffab
commit
45c4da048c
@ -30,7 +30,8 @@ Content::Content(
|
|||||||
ContentUnitDefs<EntityDef> entities,
|
ContentUnitDefs<EntityDef> entities,
|
||||||
UptrsMap<std::string, ContentPackRuntime> packs,
|
UptrsMap<std::string, ContentPackRuntime> packs,
|
||||||
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
||||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons
|
UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
|
||||||
|
const ResourceIndicesSet& resourceIndices
|
||||||
) : indices(std::move(indices)),
|
) : indices(std::move(indices)),
|
||||||
packs(std::move(packs)),
|
packs(std::move(packs)),
|
||||||
blockMaterials(std::move(blockMaterials)),
|
blockMaterials(std::move(blockMaterials)),
|
||||||
@ -39,7 +40,11 @@ Content::Content(
|
|||||||
items(std::move(items)),
|
items(std::move(items)),
|
||||||
entities(std::move(entities)),
|
entities(std::move(entities)),
|
||||||
drawGroups(std::move(drawGroups))
|
drawGroups(std::move(drawGroups))
|
||||||
{}
|
{
|
||||||
|
for (size_t i = 0; i < RESOURCE_TYPES_COUNT; i++) {
|
||||||
|
this->resourceIndices[i] = resourceIndices[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Content::~Content() {
|
Content::~Content() {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "content_fwd.hpp"
|
#include "content_fwd.hpp"
|
||||||
|
|
||||||
|
#include "../data/dynamic_fwd.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -109,14 +111,20 @@ public:
|
|||||||
class ResourceIndices {
|
class ResourceIndices {
|
||||||
std::vector<std::string> names;
|
std::vector<std::string> names;
|
||||||
std::unordered_map<std::string, size_t> indices;
|
std::unordered_map<std::string, size_t> indices;
|
||||||
|
std::vector<dynamic::Map_sptr> savedData;
|
||||||
public:
|
public:
|
||||||
ResourceIndices() {}
|
ResourceIndices() {}
|
||||||
|
|
||||||
static constexpr size_t MISSING = -1;
|
static constexpr size_t MISSING = -1;
|
||||||
|
|
||||||
void add(std::string name) {
|
void add(std::string name, dynamic::Map_sptr map) {
|
||||||
indices[name] = names.size();
|
indices[name] = names.size();
|
||||||
names.push_back(name);
|
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 {
|
size_t indexOf(const std::string& name) const {
|
||||||
@ -126,6 +134,18 @@ public:
|
|||||||
}
|
}
|
||||||
return MISSING;
|
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) {
|
constexpr const char* to_string(ResourceType type) {
|
||||||
@ -142,7 +162,7 @@ inline std::optional<ResourceType> ResourceType_from(std::string_view str) {
|
|||||||
return std::nullopt;
|
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
|
/// @brief Content is a definitions repository
|
||||||
class Content {
|
class Content {
|
||||||
@ -165,7 +185,8 @@ public:
|
|||||||
ContentUnitDefs<EntityDef> entities,
|
ContentUnitDefs<EntityDef> entities,
|
||||||
UptrsMap<std::string, ContentPackRuntime> packs,
|
UptrsMap<std::string, ContentPackRuntime> packs,
|
||||||
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
||||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons
|
UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
|
||||||
|
const ResourceIndicesSet& resourceIndices
|
||||||
);
|
);
|
||||||
~Content();
|
~Content();
|
||||||
|
|
||||||
|
|||||||
@ -75,7 +75,8 @@ std::unique_ptr<Content> ContentBuilder::build() {
|
|||||||
entities.build(),
|
entities.build(),
|
||||||
std::move(packs),
|
std::move(packs),
|
||||||
std::move(blockMaterials),
|
std::move(blockMaterials),
|
||||||
std::move(skeletons)
|
std::move(skeletons),
|
||||||
|
resourceIndices
|
||||||
);
|
);
|
||||||
|
|
||||||
// Now, it's time to resolve foreign keys
|
// Now, it's time to resolve foreign keys
|
||||||
|
|||||||
@ -500,6 +500,6 @@ void ContentLoader::load() {
|
|||||||
|
|
||||||
void ContentLoader::loadResources(ResourceType type, dynamic::List* list) {
|
void ContentLoader::loadResources(ResourceType type, dynamic::List* list) {
|
||||||
for (size_t i = 0; i < list->size(); i++) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,4 +15,6 @@ enum class ResourceType : size_t {
|
|||||||
LAST=CAMERA
|
LAST=CAMERA
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline constexpr auto RESOURCE_TYPES_COUNT = static_cast<size_t>(ResourceType::LAST)+1;
|
||||||
|
|
||||||
#endif // CONTENT_CONTENT_FWD_HPP_
|
#endif // CONTENT_CONTENT_FWD_HPP_
|
||||||
|
|||||||
@ -56,6 +56,10 @@ fs::path WorldFiles::getPlayerFile() const {
|
|||||||
return directory/fs::path("player.json");
|
return directory/fs::path("player.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fs::path WorldFiles::getResourcesFile() const {
|
||||||
|
return directory/fs::path("resources.json");
|
||||||
|
}
|
||||||
|
|
||||||
fs::path WorldFiles::getWorldFile() const {
|
fs::path WorldFiles::getWorldFile() const {
|
||||||
return directory/fs::path(WORLD_FILE);
|
return directory/fs::path(WORLD_FILE);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,6 +46,7 @@ public:
|
|||||||
~WorldFiles();
|
~WorldFiles();
|
||||||
|
|
||||||
fs::path getPlayerFile() const;
|
fs::path getPlayerFile() const;
|
||||||
|
fs::path getResourcesFile() const;
|
||||||
void createDirectories();
|
void createDirectories();
|
||||||
|
|
||||||
bool readWorldInfo(World* world);
|
bool readWorldInfo(World* world);
|
||||||
|
|||||||
@ -52,13 +52,30 @@ void World::updateTimers(float delta) {
|
|||||||
totalTime += 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<ResourceType>(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) {
|
void World::write(Level* level) {
|
||||||
const Content* content = level->content;
|
const Content* content = level->content;
|
||||||
level->chunks->saveAll();
|
level->chunks->saveAll();
|
||||||
nextEntityId = level->entities->peekNextID();
|
nextEntityId = level->entities->peekNextID();
|
||||||
wfile->write(this, content);
|
wfile->write(this, content);
|
||||||
auto playerFile = dynamic::Map();
|
|
||||||
|
|
||||||
|
auto playerFile = dynamic::Map();
|
||||||
auto& players = playerFile.putList("players");
|
auto& players = playerFile.putList("players");
|
||||||
for (const auto& object : level->objects) {
|
for (const auto& object : level->objects) {
|
||||||
if (auto player = std::dynamic_pointer_cast<Player>(object)) {
|
if (auto player = std::dynamic_pointer_cast<Player>(object)) {
|
||||||
@ -66,6 +83,8 @@ void World::write(Level* level) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
files::write_json(wfile->getPlayerFile(), &playerFile);
|
files::write_json(wfile->getPlayerFile(), &playerFile);
|
||||||
|
|
||||||
|
writeResources(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Level> World::create(
|
std::unique_ptr<Level> World::create(
|
||||||
|
|||||||
@ -34,6 +34,8 @@ class World : Serializable {
|
|||||||
std::vector<ContentPack> packs;
|
std::vector<ContentPack> packs;
|
||||||
|
|
||||||
int64_t nextInventoryId = 0;
|
int64_t nextInventoryId = 0;
|
||||||
|
|
||||||
|
void writeResources(const Content* content);
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<WorldFiles> wfile;
|
std::unique_ptr<WorldFiles> wfile;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user