From 3b13b36fd4978a0bdc5bc900eb85c42c2699eb4b Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Fri, 4 Mar 2022 14:07:19 +0100 Subject: [PATCH] Fixed a bug preventing loading of incompatible save files. More error checking has to be performed here, along with a prompt for the user to ensure everything loads how he wants to. --- source/common/core/Registry.hpp | 3 +++ source/common/inventory/Inventory.cpp | 10 +++++++++- source/server/world/save/WorldSaveBasicBackend.cpp | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/source/common/core/Registry.hpp b/source/common/core/Registry.hpp index cb733d88..7d0be2e6 100644 --- a/source/common/core/Registry.hpp +++ b/source/common/core/Registry.hpp @@ -116,6 +116,9 @@ class Registry : public gk::ISerializable { const Dimension &getDimension(u16 id) const { return m_dimensions.at(id); } const Key &getKey(u16 id) const; + bool hasBlock(const std::string &stringID) const { return m_blocksID.find(stringID) != m_blocksID.end(); } + bool hasItem(const std::string &stringID) const { return m_itemsID.find(stringID) != m_itemsID.end(); } + const Block &getBlockFromStringID(const std::string &stringID); const Item &getItemFromStringID(const std::string &stringID); const Sky &getSkyFromStringID(const std::string &stringID); diff --git a/source/common/inventory/Inventory.cpp b/source/common/inventory/Inventory.cpp index 8f3e6128..7b9c468d 100644 --- a/source/common/inventory/Inventory.cpp +++ b/source/common/inventory/Inventory.cpp @@ -24,9 +24,12 @@ * * ===================================================================================== */ +#include + #include "EngineConfig.hpp" #include "Inventory.hpp" #include "Network.hpp" +#include "Registry.hpp" void Inventory::setStack(u16 x, u16 y, const std::string &stringID, u16 amount) { m_items.at(x + y * m_width) = ItemStack(stringID, amount); @@ -120,7 +123,12 @@ void Inventory::deserialize(sf::Packet &packet) { u8 x, y; while (i < itemListSize) { packet >> name >> amount >> x >> y; - setStack(x, y, name, amount); + + if (Registry::getInstance().hasItem(name)) + setStack(x, y, name, amount); + else + gkError() << "Inventory::deserialize: Failed to find item in registry:" << name; + ++i; } } diff --git a/source/server/world/save/WorldSaveBasicBackend.cpp b/source/server/world/save/WorldSaveBasicBackend.cpp index 44088e2e..a350fd36 100644 --- a/source/server/world/save/WorldSaveBasicBackend.cpp +++ b/source/server/world/save/WorldSaveBasicBackend.cpp @@ -48,6 +48,9 @@ void WorldSaveBasicBackend::load(const std::string &name) { std::ifstream file("saves/" + name + ".dat", std::ofstream::binary); + // Note: Registry should be saved, otherwise blocks won't match since they're stored using + // their integer ID (not string ID), so we need to add a check for that + if (file.is_open()) { file.seekg(0, file.end); std::size_t length = file.tellg();