Skip to content

Commit

Permalink
Fixed a bug preventing loading of incompatible save files.
Browse files Browse the repository at this point in the history
More error checking has to be performed here, along with a prompt for the user to ensure everything loads how he wants to.
  • Loading branch information
Unarelith committed Mar 4, 2022
1 parent 7e52b42 commit 3b13b36
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions source/common/core/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion source/common/inventory/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down
3 changes: 3 additions & 0 deletions source/server/world/save/WorldSaveBasicBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 3b13b36

Please sign in to comment.