feat: saving/loading resource entries
This commit is contained in:
parent
ca8652ffab
commit
45c4da048c
@ -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() {
|
||||
}
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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_
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -46,6 +46,7 @@ public:
|
||||
~WorldFiles();
|
||||
|
||||
fs::path getPlayerFile() const;
|
||||
fs::path getResourcesFile() const;
|
||||
void createDirectories();
|
||||
|
||||
bool readWorldInfo(World* world);
|
||||
|
||||
@ -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<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) {
|
||||
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<Player>(object)) {
|
||||
@ -66,6 +83,8 @@ void World::write(Level* level) {
|
||||
}
|
||||
}
|
||||
files::write_json(wfile->getPlayerFile(), &playerFile);
|
||||
|
||||
writeResources(content);
|
||||
}
|
||||
|
||||
std::unique_ptr<Level> World::create(
|
||||
|
||||
@ -34,6 +34,8 @@ class World : Serializable {
|
||||
std::vector<ContentPack> packs;
|
||||
|
||||
int64_t nextInventoryId = 0;
|
||||
|
||||
void writeResources(const Content* content);
|
||||
public:
|
||||
std::unique_ptr<WorldFiles> wfile;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user