[General] Restructure ID_CONTAINER to allow multiple containers at once

coverity_scan^2
David Cernat 8 years ago
parent 88f891b5bd
commit 3b604a432e

@ -67,9 +67,9 @@ void WorldFunctions::SetBaseEventCell(const char* cellDescription) noexcept
} }
} }
void WorldFunctions::SetContainerChangesAction(int action) noexcept void WorldFunctions::SetBaseEventAction(int action) noexcept
{ {
baseEvent->containerChanges.action = action; baseEvent->action = action;
} }
void WorldFunctions::SetObjectRefId(const char* refId) noexcept void WorldFunctions::SetObjectRefId(const char* refId) noexcept

@ -6,7 +6,7 @@
\ \
{"AddWorldObject", WorldFunctions::AddWorldObject},\ {"AddWorldObject", WorldFunctions::AddWorldObject},\
{"SetBaseEventCell", WorldFunctions::SetBaseEventCell},\ {"SetBaseEventCell", WorldFunctions::SetBaseEventCell},\
{"SetContainerChangesAction", WorldFunctions::SetContainerChangesAction},\ {"SetBaseEventAction", WorldFunctions::SetBaseEventAction},\
\ \
{"SetObjectRefId", WorldFunctions::SetObjectRefId},\ {"SetObjectRefId", WorldFunctions::SetObjectRefId},\
{"SetObjectRefNumIndex", WorldFunctions::SetObjectRefNumIndex},\ {"SetObjectRefNumIndex", WorldFunctions::SetObjectRefNumIndex},\
@ -57,7 +57,7 @@ public:
static void AddWorldObject() noexcept; static void AddWorldObject() noexcept;
static void SetBaseEventCell(const char* cellDescription) noexcept; static void SetBaseEventCell(const char* cellDescription) noexcept;
static void SetContainerChangesAction(int action) noexcept; static void SetBaseEventAction(int action) noexcept;
static void SetObjectRefId(const char* refId) noexcept; static void SetObjectRefId(const char* refId) noexcept;
static void SetObjectRefNumIndex(int refNumIndex) noexcept; static void SetObjectRefNumIndex(int refNumIndex) noexcept;

@ -104,11 +104,11 @@ namespace MWGui
// Added by tes3mp // Added by tes3mp
mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent(); mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent();
event->cell = *mPtr.getCell()->getCell(); event->cell = *mPtr.getCell()->getCell();
event->action = mwmp::BaseEvent::REMOVE;
mwmp::WorldObject worldObject; mwmp::WorldObject worldObject;
worldObject.refId = mPtr.getCellRef().getRefId(); worldObject.refId = mPtr.getCellRef().getRefId();
worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex; worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex;
event->addObject(worldObject);
MWWorld::Ptr itemPtr = mModel->getItem(mSelectedItem).mBase; MWWorld::Ptr itemPtr = mModel->getItem(mSelectedItem).mBase;
@ -119,8 +119,9 @@ namespace MWGui
containerItem.goldValue = itemPtr.getCellRef().getGoldValue(); containerItem.goldValue = itemPtr.getCellRef().getGoldValue();
containerItem.owner = itemPtr.getCellRef().getOwner(); containerItem.owner = itemPtr.getCellRef().getOwner();
containerItem.actionCount = count; containerItem.actionCount = count;
event->addContainerItem(containerItem);
event->containerChanges.action = mwmp::ContainerChanges::REMOVE; worldObject.containerChanges.items.push_back(containerItem);
event->addObject(worldObject);
mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event); mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event);
@ -163,11 +164,11 @@ namespace MWGui
// Added by tes3mp // Added by tes3mp
mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent(); mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent();
event->cell = *mPtr.getCell()->getCell(); event->cell = *mPtr.getCell()->getCell();
event->action = mwmp::BaseEvent::ADD;
mwmp::WorldObject worldObject; mwmp::WorldObject worldObject;
worldObject.refId = mPtr.getCellRef().getRefId(); worldObject.refId = mPtr.getCellRef().getRefId();
worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex; worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex;
event->addObject(worldObject);
MWWorld::Ptr itemPtr = mDragAndDrop->mItem.mBase; MWWorld::Ptr itemPtr = mDragAndDrop->mItem.mBase;
@ -180,8 +181,9 @@ namespace MWGui
containerItem.charge = itemPtr.getCellRef().getCharge(); containerItem.charge = itemPtr.getCellRef().getCharge();
containerItem.goldValue = itemPtr.getCellRef().getGoldValue(); containerItem.goldValue = itemPtr.getCellRef().getGoldValue();
containerItem.owner = itemPtr.getCellRef().getOwner(); containerItem.owner = itemPtr.getCellRef().getOwner();
event->addContainerItem(containerItem);
event->containerChanges.action = mwmp::ContainerChanges::ADD; worldObject.containerChanges.items.push_back(containerItem);
event->addObject(worldObject);
mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event); mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event);
@ -317,12 +319,12 @@ namespace MWGui
// Added by tes3mp // Added by tes3mp
mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent(); mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent();
event->cell = *mPtr.getCell()->getCell(); event->cell = *mPtr.getCell()->getCell();
event->action = mwmp::BaseEvent::SET;
mwmp::WorldObject worldObject; mwmp::WorldObject worldObject;
worldObject.refId = mPtr.getCellRef().getRefId(); worldObject.refId = mPtr.getCellRef().getRefId();
worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex; worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex;
event->addObject(worldObject); event->addObject(worldObject);
event->containerChanges.action = mwmp::ContainerChanges::SET;
mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event); mwmp::Main::get().getNetworking()->getWorldPacket(ID_CONTAINER)->Send(event);

@ -749,7 +749,7 @@ void Networking::processWorldPacket(RakNet::Packet *packet)
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_CONTAINER"); LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_CONTAINER");
// If we've received a request for information, comply with it // If we've received a request for information, comply with it
if (event->containerChanges.action == mwmp::ContainerChanges::REQUEST) if (event->action == mwmp::BaseEvent::REQUEST)
event->sendContainers(ptrCellStore); event->sendContainers(ptrCellStore);
// Otherwise, edit containers based on the information received // Otherwise, edit containers based on the information received
else else

@ -40,11 +40,6 @@ void WorldEvent::addObject(WorldObject worldObject)
objectChanges.objects.push_back(worldObject); objectChanges.objects.push_back(worldObject);
} }
void WorldEvent::addContainerItem(ContainerItem containerItem)
{
containerChanges.items.push_back(containerItem);
}
void WorldEvent::sendContainers(MWWorld::CellStore* cellStore) void WorldEvent::sendContainers(MWWorld::CellStore* cellStore)
{ {
MWWorld::CellRefList<ESM::Container> *containerList = cellStore->getContainers(); MWWorld::CellRefList<ESM::Container> *containerList = cellStore->getContainers();
@ -86,21 +81,20 @@ void WorldEvent::editContainers(MWWorld::CellStore* cellStore)
ptrFound.getCellRef().getRefNum()); ptrFound.getCellRef().getRefNum());
MWWorld::ContainerStore& containerStore = ptrFound.getClass().getContainerStore(ptrFound); MWWorld::ContainerStore& containerStore = ptrFound.getClass().getContainerStore(ptrFound);
int action = containerChanges.action;
// If we are setting the entire contents, clear the current ones // If we are setting the entire contents, clear the current ones
if (action == ContainerChanges::SET) if (action == BaseEvent::SET)
containerStore.clear(); containerStore.clear();
for (unsigned int i = 0; i < containerChanges.count; i++) for (unsigned int i = 0; i < worldObject.containerChanges.count; i++)
{ {
ContainerItem containerItem = containerChanges.items.at(i); ContainerItem containerItem = worldObject.containerChanges.items.at(i);
MWWorld::Ptr ownerPtr = MWBase::Environment::get().getWorld()->searchPtr(containerItem.owner, false); MWWorld::Ptr ownerPtr = MWBase::Environment::get().getWorld()->searchPtr(containerItem.owner, false);
if (ownerPtr.isEmpty()) if (ownerPtr.isEmpty())
ownerPtr = MWBase::Environment::get().getWorld()->getPlayerPtr(); ownerPtr = MWBase::Environment::get().getWorld()->getPlayerPtr();
if (action == ContainerChanges::ADD || action == ContainerChanges::SET) if (action == BaseEvent::ADD || action == BaseEvent::SET)
{ {
// Create a ManualRef to be able to set item charge // Create a ManualRef to be able to set item charge
MWWorld::ManualRef ref(MWBase::Environment::get().getWorld()->getStore(), containerItem.refId, 1); MWWorld::ManualRef ref(MWBase::Environment::get().getWorld()->getStore(), containerItem.refId, 1);
@ -116,7 +110,7 @@ void WorldEvent::editContainers(MWWorld::CellStore* cellStore)
containerStore.add(newPtr, containerItem.count, ownerPtr, true); containerStore.add(newPtr, containerItem.count, ownerPtr, true);
} }
else if (action == ContainerChanges::REMOVE) else if (action == BaseEvent::REMOVE)
{ {
// We have to find the right item ourselves because ContainerStore has no method // We have to find the right item ourselves because ContainerStore has no method
// accounting for charge // accounting for charge

@ -16,7 +16,6 @@ namespace mwmp
virtual ~WorldEvent(); virtual ~WorldEvent();
void addObject(WorldObject worldObject); void addObject(WorldObject worldObject);
void addContainerItem(ContainerItem containerItem);
void sendContainers(MWWorld::CellStore* cellStore); void sendContainers(MWWorld::CellStore* cellStore);

@ -7,6 +7,28 @@
namespace mwmp namespace mwmp
{ {
struct ContainerItem
{
std::string refId;
int count;
int charge;
int goldValue;
std::string owner;
int actionCount;
inline bool operator==(const ContainerItem& rhs)
{
return refId == rhs.refId && count == rhs.count && charge == rhs.charge && goldValue && rhs.goldValue;
}
};
struct ContainerChanges
{
std::vector<ContainerItem> items;
unsigned int count;
};
struct WorldObject struct WorldObject
{ {
std::string refId; std::string refId;
@ -30,22 +52,8 @@ namespace mwmp
int shortVal; int shortVal;
float floatVal; float floatVal;
std::string varName; std::string varName;
};
struct ContainerItem
{
std::string refId;
int count;
int charge;
int goldValue;
std::string owner; ContainerChanges containerChanges;
int actionCount;
inline bool operator==(const ContainerItem& rhs)
{
return refId == rhs.refId && count == rhs.count && charge == rhs.charge && goldValue && rhs.goldValue;
}
}; };
struct ObjectChanges struct ObjectChanges
@ -54,22 +62,6 @@ namespace mwmp
unsigned int count; unsigned int count;
}; };
struct ContainerChanges
{
std::vector<ContainerItem> items;
unsigned int count;
enum CONTAINER_ACTION
{
SET = 0,
ADD = 1,
REMOVE = 2,
REQUEST = 3
};
int action; // 0 - Clear and set in entirety, 1 - Add item, 2 - Remove item, 3 - Request items
};
class BaseEvent class BaseEvent
{ {
public: public:
@ -84,11 +76,20 @@ namespace mwmp
} }
enum WORLD_ACTION
{
SET = 0,
ADD = 1,
REMOVE = 2,
REQUEST = 3
};
RakNet::RakNetGUID guid; RakNet::RakNetGUID guid;
ObjectChanges objectChanges; ObjectChanges objectChanges;
ContainerChanges containerChanges;
ESM::Cell cell; ESM::Cell cell;
int action; // 0 - Clear and set in entirety, 1 - Add item, 2 - Remove item, 3 - Request items
}; };
} }

@ -12,21 +12,18 @@ void PacketContainer::Packet(RakNet::BitStream *bs, BaseEvent *event, bool send)
{ {
WorldPacket::Packet(bs, event, send); WorldPacket::Packet(bs, event, send);
RW(event->containerChanges.action, send); RW(event->action, send);
if (!send) if (send)
{ {
event->objectChanges.objects.clear(); event->objectChanges.count = (unsigned int)(event->objectChanges.objects.size());
event->containerChanges.items.clear();
} }
else else
{ {
event->objectChanges.count = (unsigned int)(event->objectChanges.objects.size()); event->objectChanges.objects.clear();
event->containerChanges.count = (unsigned int)(event->containerChanges.items.size());
} }
RW(event->objectChanges.count, send); RW(event->objectChanges.count, send);
RW(event->containerChanges.count, send);
RW(event->cell.mData.mFlags, send); RW(event->cell.mData.mFlags, send);
RW(event->cell.mData.mX, send); RW(event->cell.mData.mX, send);
@ -40,24 +37,24 @@ void PacketContainer::Packet(RakNet::BitStream *bs, BaseEvent *event, bool send)
if (send) if (send)
{ {
worldObject = event->objectChanges.objects.at(i); worldObject = event->objectChanges.objects.at(i);
worldObject.containerChanges.count = (unsigned int)(worldObject.containerChanges.items.size());
}
else
{
worldObject.containerChanges.items.clear();
} }
RW(worldObject.refId, send); RW(worldObject.refId, send);
RW(worldObject.refNumIndex, send); RW(worldObject.refNumIndex, send);
RW(worldObject.containerChanges.count, send);
if (!send)
{
event->objectChanges.objects.push_back(worldObject);
}
}
ContainerItem containerItem; ContainerItem containerItem;
for (unsigned int i = 0; i < event->containerChanges.count; i++) for (unsigned int i = 0; i < worldObject.containerChanges.count; i++)
{ {
if (send) if (send)
{ {
containerItem = event->containerChanges.items.at(i); containerItem = worldObject.containerChanges.items.at(i);
} }
RW(containerItem.refId, send); RW(containerItem.refId, send);
@ -69,8 +66,13 @@ void PacketContainer::Packet(RakNet::BitStream *bs, BaseEvent *event, bool send)
if (!send) if (!send)
{ {
event->containerChanges.items.push_back(containerItem); worldObject.containerChanges.items.push_back(containerItem);
} }
} }
if (!send)
{
event->objectChanges.objects.push_back(worldObject);
}
}
} }

Loading…
Cancel
Save