[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

@ -2,41 +2,41 @@
#define OPENMW_WORLD_HPP
#define WORLDFUNCTIONS \
{"CreateBaseEvent", WorldFunctions::CreateBaseEvent},\
{"CreateBaseEvent", WorldFunctions::CreateBaseEvent},\
\
{"AddWorldObject", WorldFunctions::AddWorldObject},\
{"SetBaseEventCell", WorldFunctions::SetBaseEventCell},\
{"SetContainerChangesAction", WorldFunctions::SetContainerChangesAction},\
{"AddWorldObject", WorldFunctions::AddWorldObject},\
{"SetBaseEventCell", WorldFunctions::SetBaseEventCell},\
{"SetBaseEventAction", WorldFunctions::SetBaseEventAction},\
\
{"SetObjectRefId", WorldFunctions::SetObjectRefId},\
{"SetObjectRefNumIndex", WorldFunctions::SetObjectRefNumIndex},\
{"SetObjectCharge", WorldFunctions::SetObjectCharge},\
{"SetObjectCount", WorldFunctions::SetObjectCount},\
{"SetObjectGoldValue", WorldFunctions::SetObjectGoldValue},\
{"SetObjectScale", WorldFunctions::SetObjectScale},\
{"SetObjectDoorState", WorldFunctions::SetObjectDoorState},\
{"SetObjectLockLevel", WorldFunctions::SetObjectLockLevel},\
{"SetObjectPosition", WorldFunctions::SetObjectPosition},\
{"SetObjectRotation", WorldFunctions::SetObjectRotation},\
{"SetObjectRefId", WorldFunctions::SetObjectRefId},\
{"SetObjectRefNumIndex", WorldFunctions::SetObjectRefNumIndex},\
{"SetObjectCharge", WorldFunctions::SetObjectCharge},\
{"SetObjectCount", WorldFunctions::SetObjectCount},\
{"SetObjectGoldValue", WorldFunctions::SetObjectGoldValue},\
{"SetObjectScale", WorldFunctions::SetObjectScale},\
{"SetObjectDoorState", WorldFunctions::SetObjectDoorState},\
{"SetObjectLockLevel", WorldFunctions::SetObjectLockLevel},\
{"SetObjectPosition", WorldFunctions::SetObjectPosition},\
{"SetObjectRotation", WorldFunctions::SetObjectRotation},\
\
{"GetObjectChangesSize", WorldFunctions::GetObjectChangesSize},\
{"GetObjectChangesSize", WorldFunctions::GetObjectChangesSize},\
\
{"GetObjectRefId", WorldFunctions::GetObjectRefId},\
{"GetObjectRefNumIndex", WorldFunctions::GetObjectRefNumIndex},\
{"GetObjectCharge", WorldFunctions::GetObjectCharge},\
{"GetObjectCount", WorldFunctions::GetObjectCount},\
{"GetObjectGoldValue", WorldFunctions::GetObjectGoldValue},\
{"GetObjectScale", WorldFunctions::GetObjectScale},\
{"GetObjectDoorState", WorldFunctions::GetObjectDoorState},\
{"GetObjectLockLevel", WorldFunctions::GetObjectLockLevel},\
{"GetObjectPosX", WorldFunctions::GetObjectPosX},\
{"GetObjectPosY", WorldFunctions::GetObjectPosY},\
{"GetObjectPosZ", WorldFunctions::GetObjectPosZ},\
{"GetObjectRotX", WorldFunctions::GetObjectRotX},\
{"GetObjectRotY", WorldFunctions::GetObjectRotY},\
{"GetObjectRotZ", WorldFunctions::GetObjectRotZ},\
{"GetObjectRefId", WorldFunctions::GetObjectRefId},\
{"GetObjectRefNumIndex", WorldFunctions::GetObjectRefNumIndex},\
{"GetObjectCharge", WorldFunctions::GetObjectCharge},\
{"GetObjectCount", WorldFunctions::GetObjectCount},\
{"GetObjectGoldValue", WorldFunctions::GetObjectGoldValue},\
{"GetObjectScale", WorldFunctions::GetObjectScale},\
{"GetObjectDoorState", WorldFunctions::GetObjectDoorState},\
{"GetObjectLockLevel", WorldFunctions::GetObjectLockLevel},\
{"GetObjectPosX", WorldFunctions::GetObjectPosX},\
{"GetObjectPosY", WorldFunctions::GetObjectPosY},\
{"GetObjectPosZ", WorldFunctions::GetObjectPosZ},\
{"GetObjectRotX", WorldFunctions::GetObjectRotX},\
{"GetObjectRotY", WorldFunctions::GetObjectRotY},\
{"GetObjectRotZ", WorldFunctions::GetObjectRotZ},\
\
{"SendContainer", WorldFunctions::SendContainer},\
{"SendContainer", WorldFunctions::SendContainer},\
\
{"SendObjectDelete", WorldFunctions::SendObjectDelete},\
{"SendObjectPlace", WorldFunctions::SendObjectPlace},\
@ -57,7 +57,7 @@ public:
static void AddWorldObject() 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 SetObjectRefNumIndex(int refNumIndex) noexcept;

@ -104,11 +104,11 @@ namespace MWGui
// Added by tes3mp
mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent();
event->cell = *mPtr.getCell()->getCell();
event->action = mwmp::BaseEvent::REMOVE;
mwmp::WorldObject worldObject;
worldObject.refId = mPtr.getCellRef().getRefId();
worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex;
event->addObject(worldObject);
MWWorld::Ptr itemPtr = mModel->getItem(mSelectedItem).mBase;
@ -119,8 +119,9 @@ namespace MWGui
containerItem.goldValue = itemPtr.getCellRef().getGoldValue();
containerItem.owner = itemPtr.getCellRef().getOwner();
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);
@ -163,11 +164,11 @@ namespace MWGui
// Added by tes3mp
mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent();
event->cell = *mPtr.getCell()->getCell();
event->action = mwmp::BaseEvent::ADD;
mwmp::WorldObject worldObject;
worldObject.refId = mPtr.getCellRef().getRefId();
worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex;
event->addObject(worldObject);
MWWorld::Ptr itemPtr = mDragAndDrop->mItem.mBase;
@ -180,8 +181,9 @@ namespace MWGui
containerItem.charge = itemPtr.getCellRef().getCharge();
containerItem.goldValue = itemPtr.getCellRef().getGoldValue();
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);
@ -317,12 +319,12 @@ namespace MWGui
// Added by tes3mp
mwmp::WorldEvent *event = mwmp::Main::get().getNetworking()->createWorldEvent();
event->cell = *mPtr.getCell()->getCell();
event->action = mwmp::BaseEvent::SET;
mwmp::WorldObject worldObject;
worldObject.refId = mPtr.getCellRef().getRefId();
worldObject.refNumIndex = mPtr.getCellRef().getRefNum().mIndex;
event->addObject(worldObject);
event->containerChanges.action = mwmp::ContainerChanges::SET;
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");
// 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);
// Otherwise, edit containers based on the information received
else

@ -40,11 +40,6 @@ void WorldEvent::addObject(WorldObject worldObject)
objectChanges.objects.push_back(worldObject);
}
void WorldEvent::addContainerItem(ContainerItem containerItem)
{
containerChanges.items.push_back(containerItem);
}
void WorldEvent::sendContainers(MWWorld::CellStore* cellStore)
{
MWWorld::CellRefList<ESM::Container> *containerList = cellStore->getContainers();
@ -86,21 +81,20 @@ void WorldEvent::editContainers(MWWorld::CellStore* cellStore)
ptrFound.getCellRef().getRefNum());
MWWorld::ContainerStore& containerStore = ptrFound.getClass().getContainerStore(ptrFound);
int action = containerChanges.action;
// If we are setting the entire contents, clear the current ones
if (action == ContainerChanges::SET)
if (action == BaseEvent::SET)
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);
if (ownerPtr.isEmpty())
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
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);
}
else if (action == ContainerChanges::REMOVE)
else if (action == BaseEvent::REMOVE)
{
// We have to find the right item ourselves because ContainerStore has no method
// accounting for charge

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

@ -7,6 +7,28 @@
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
{
std::string refId;
@ -30,22 +52,8 @@ namespace mwmp
int shortVal;
float floatVal;
std::string varName;
};
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;
}
ContainerChanges containerChanges;
};
struct ObjectChanges
@ -54,22 +62,6 @@ namespace mwmp
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
{
public:
@ -84,11 +76,20 @@ namespace mwmp
}
enum WORLD_ACTION
{
SET = 0,
ADD = 1,
REMOVE = 2,
REQUEST = 3
};
RakNet::RakNetGUID guid;
ObjectChanges objectChanges;
ContainerChanges containerChanges;
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);
RW(event->containerChanges.action, send);
RW(event->action, send);
if (!send)
if (send)
{
event->objectChanges.objects.clear();
event->containerChanges.items.clear();
event->objectChanges.count = (unsigned int)(event->objectChanges.objects.size());
}
else
{
event->objectChanges.count = (unsigned int)(event->objectChanges.objects.size());
event->containerChanges.count = (unsigned int)(event->containerChanges.items.size());
event->objectChanges.objects.clear();
}
RW(event->objectChanges.count, send);
RW(event->containerChanges.count, send);
RW(event->cell.mData.mFlags, send);
RW(event->cell.mData.mX, send);
@ -40,37 +37,42 @@ void PacketContainer::Packet(RakNet::BitStream *bs, BaseEvent *event, bool send)
if (send)
{
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.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++)
{
if (send)
for (unsigned int i = 0; i < worldObject.containerChanges.count; i++)
{
containerItem = event->containerChanges.items.at(i);
if (send)
{
containerItem = worldObject.containerChanges.items.at(i);
}
RW(containerItem.refId, send);
RW(containerItem.count, send);
RW(containerItem.charge, send);
RW(containerItem.goldValue, send);
RW(containerItem.owner, send);
RW(containerItem.actionCount, send);
if (!send)
{
worldObject.containerChanges.items.push_back(containerItem);
}
}
RW(containerItem.refId, send);
RW(containerItem.count, send);
RW(containerItem.charge, send);
RW(containerItem.goldValue, send);
RW(containerItem.owner, send);
RW(containerItem.actionCount, send);
if (!send)
{
event->containerChanges.items.push_back(containerItem);
event->objectChanges.objects.push_back(worldObject);
}
}
}

Loading…
Cancel
Save