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.
master
Quentin Bazin 2022-03-04 14:07:19 +01:00
parent 7e52b422ca
commit 3b13b36fd4
3 changed files with 15 additions and 1 deletions

View File

@ -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);

View File

@ -24,9 +24,12 @@
*
* =====================================================================================
*/
#include <gk/core/Debug.hpp>
#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;
}
}

View File

@ -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();