diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 2de7829b..6616d54d 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -536,7 +536,7 @@ bool WorldFiles::readWorldInfo(World* world) { } void WorldFiles::writePlayer(Player* player) { - files::write_json(getPlayerFile(), player->write().release()); + files::write_json(getPlayerFile(), player->serialize().release()); } bool WorldFiles::readPlayer(Player* player) { @@ -546,37 +546,7 @@ bool WorldFiles::readPlayer(Player* player) { return false; } - auto root = files::read_json(file); - auto posarr = root->list("position"); - glm::vec3& position = player->hitbox->position; - position.x = posarr->num(0); - position.y = posarr->num(1); - position.z = posarr->num(2); - player->camera->position = position; - - auto rotarr = root->list("rotation"); - player->cam.x = rotarr->num(0); - player->cam.y = rotarr->num(1); - - if (root->has("spawnpoint")) { - auto sparr = root->list("spawnpoint"); - player->setSpawnPoint(glm::vec3( - sparr->num(0), - sparr->num(1), - sparr->num(2) - )); - } else { - player->setSpawnPoint(position); - } - - root->flag("flight", player->flight); - root->flag("noclip", player->noclip); - player->setChosenSlot(root->getInt("chosen-slot", player->getChosenSlot())); - - auto invmap = root->map("inventory"); - if (invmap) { - player->getInventory()->read(invmap); - } + player->deserialize(files::read_json(file).get()); return true; } diff --git a/src/interfaces/Serializable.h b/src/interfaces/Serializable.h new file mode 100644 index 00000000..a1c0d4e2 --- /dev/null +++ b/src/interfaces/Serializable.h @@ -0,0 +1,15 @@ +#ifndef SERIALIZABLE_H +#define SERIALIZABLE_H + +#include +#include "../coders/json.h" + +class Serializable +{ +public: + virtual ~Serializable() { } + virtual std::unique_ptr serialize() const = 0; + virtual void deserialize(dynamic::Map* src) = 0; +}; + +#endif \ No newline at end of file diff --git a/src/items/Inventory.cpp b/src/items/Inventory.cpp index 3a0f7318..1fbf2aa7 100644 --- a/src/items/Inventory.cpp +++ b/src/items/Inventory.cpp @@ -44,7 +44,7 @@ void Inventory::move( } } -void Inventory::read(const dynamic::Map* src) { +void Inventory::deserialize(dynamic::Map* src) { auto slotsarr = src->list("slots"); size_t slotscount = std::min(slotsarr->size(), slots.size()); for (size_t i = 0; i < slotscount; i++) { @@ -56,7 +56,7 @@ void Inventory::read(const dynamic::Map* src) { } } -std::unique_ptr Inventory::write() const { +std::unique_ptr Inventory::serialize() const { auto map = std::make_unique(); auto& slotsarr = map->putList("slots"); for (size_t i = 0; i < slots.size(); i++) { diff --git a/src/items/Inventory.h b/src/items/Inventory.h index 99193cde..4ac0d130 100644 --- a/src/items/Inventory.h +++ b/src/items/Inventory.h @@ -8,12 +8,13 @@ #include "../typedefs.h" #include "../data/dynamic.h" +#include "../interfaces/Serializable.h" class ContentLUT; class ContentIndices; // TODO: items indices fix -class Inventory { +class Inventory : Serializable { std::vector slots; public: Inventory(size_t size); @@ -33,9 +34,9 @@ public: size_t end=-1); /* deserializing inventory */ - void read(const dynamic::Map* src); + void deserialize(dynamic::Map* src) override; /* serializing inventory */ - std::unique_ptr write() const; + std::unique_ptr serialize() const override; static void convert(dynamic::Map* data, const ContentLUT* lut); diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 640ea277..3a5299fa 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -170,7 +170,7 @@ glm::vec3 Player::getSpawnPoint() const { return spawnpoint; } -std::unique_ptr Player::write() const { +std::unique_ptr Player::serialize() const { glm::vec3 position = hitbox->position; auto root = std::make_unique(); auto& posarr = root->putList("position"); @@ -190,10 +190,45 @@ std::unique_ptr Player::write() const { root->put("flight", flight); root->put("noclip", noclip); root->put("chosen-slot", chosenSlot); - root->put("inventory", inventory->write().release()); + root->put("inventory", inventory->serialize().release()); return root; } +void Player::deserialize(dynamic::Map *src) { + + auto posarr = src->list("position"); + glm::vec3& position = hitbox->position; + position.x = posarr->num(0); + position.y = posarr->num(1); + position.z = posarr->num(2); + camera->position = position; + + auto rotarr = src->list("rotation"); + cam.x = rotarr->num(0); + cam.y = rotarr->num(1); + + if (src->has("spawnpoint")) { + auto sparr = src->list("spawnpoint"); + setSpawnPoint(glm::vec3( + sparr->num(0), + sparr->num(1), + sparr->num(2) + )); + } else { + setSpawnPoint(position); + } + + src->flag("flight", flight); + src->flag("noclip", noclip); + setChosenSlot(src->getInt("chosen-slot", getChosenSlot())); + + auto invmap = src->map("inventory"); + if (invmap) { + getInventory()->deserialize(invmap); + } +} + + void Player::convert(dynamic::Map* data, const ContentLUT* lut) { auto inventory = data->map("inventory"); if (inventory) { diff --git a/src/objects/Player.h b/src/objects/Player.h index 70822cd8..390a2147 100644 --- a/src/objects/Player.h +++ b/src/objects/Player.h @@ -7,6 +7,7 @@ #include "../data/dynamic.h" #include "../voxels/voxel.h" #include "../settings.h" +#include "../interfaces/Serializable.h" class Camera; class Hitbox; @@ -31,7 +32,7 @@ struct PlayerInput { bool flight; }; -class Player { +class Player : Serializable { float speed; int chosenSlot; glm::vec3 spawnpoint {}; @@ -65,7 +66,8 @@ public: void setSpawnPoint(glm::vec3 point); glm::vec3 getSpawnPoint() const; - std::unique_ptr write() const; + std::unique_ptr serialize() const override; + void deserialize(dynamic::Map *src) override; static void convert(dynamic::Map* data, const ContentLUT* lut); };