From ea336214de6f4139f69aa3a66391180255aae684 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Tue, 13 Mar 2012 14:04:19 +0100 Subject: [PATCH] more inventory sanity checks --- apps/openmw/mwworld/containerstore.cpp | 5 +++++ apps/openmw/mwworld/containerstore.hpp | 2 ++ apps/openmw/mwworld/inventorystore.cpp | 18 +++++++++++++++++- apps/openmw/mwworld/inventorystore.hpp | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 0edc8ca141..c498cabced 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -371,6 +371,11 @@ int MWWorld::ContainerStoreIterator::getType() const return mType; } +const MWWorld::ContainerStore *MWWorld::ContainerStoreIterator::getContainerStore() const +{ + return mContainer; +} + bool MWWorld::operator== (const ContainerStoreIterator& left, const ContainerStoreIterator& right) { return left.isEqual (right); diff --git a/apps/openmw/mwworld/containerstore.hpp b/apps/openmw/mwworld/containerstore.hpp index 89f65bc5b3..69e5ba4466 100644 --- a/apps/openmw/mwworld/containerstore.hpp +++ b/apps/openmw/mwworld/containerstore.hpp @@ -142,6 +142,8 @@ namespace MWWorld int getType() const; + const ContainerStore *getContainerStore() const; + friend class ContainerStore; }; diff --git a/apps/openmw/mwworld/inventorystore.cpp b/apps/openmw/mwworld/inventorystore.cpp index 363781294d..264be7bb33 100644 --- a/apps/openmw/mwworld/inventorystore.cpp +++ b/apps/openmw/mwworld/inventorystore.cpp @@ -2,6 +2,9 @@ #include "inventorystore.hpp" #include +#include + +#include "class.hpp" void MWWorld::InventoryStore::copySlots (const InventoryStore& store) { @@ -46,7 +49,20 @@ void MWWorld::InventoryStore::equip (int slot, const ContainerStoreIterator& ite if (slot<0 || slot>=static_cast (mSlots.size())) throw std::runtime_error ("slot number out of range"); - /// \todo verify slot + if (iterator.getContainerStore()!=this) + throw std::runtime_error ("attempt to equip an item that is not in the inventory"); + + if (iterator!=end()) + { + std::pair, bool> slots = Class::get (*iterator).getEquipmentSlots (*iterator); + + if (std::find (slots.first.begin(), slots.first.end(), slot)==slots.first.end()) + throw std::runtime_error ("invalid slot"); + } + + /// \todo restack item previously in this slot (if required) + + /// \todo unstack item pointed to by iterator if required) mSlots[slot] = iterator; } diff --git a/apps/openmw/mwworld/inventorystore.hpp b/apps/openmw/mwworld/inventorystore.hpp index cfde6c6876..c41f9e8e37 100644 --- a/apps/openmw/mwworld/inventorystore.hpp +++ b/apps/openmw/mwworld/inventorystore.hpp @@ -49,6 +49,7 @@ namespace MWWorld InventoryStore& operator= (const InventoryStore& store); void equip (int slot, const ContainerStoreIterator& iterator); + ///< \note \a iteartor can be an end-iterator ContainerStoreIterator getSlot (int slot); };