[General] Remove redundant code

Remove BaseEvent::worldObjectCount
Remove BaseActor::count
Use foreach loops in packets and processors
Remove redundant "&" in CellController::get().getCell() calls
sol2-server-rewrite
Koncord 7 years ago
parent 585c24cee8
commit f35d35741e

@ -15,7 +15,7 @@ using namespace std;
Cell::Cell(ESM::Cell cell) : cell(cell) Cell::Cell(ESM::Cell cell) : cell(cell)
{ {
cellActorList.count = 0;
} }
Cell::Iterator Cell::begin() const Cell::Iterator Cell::begin() const
@ -109,8 +109,6 @@ void Cell::readActorList(unsigned char packetID, const mwmp::BaseActorList *newA
else else
cellActorList.baseActors.push_back(newActor); cellActorList.baseActors.push_back(newActor);
} }
cellActorList.count = cellActorList.baseActors.size();
} }
bool Cell::containsActor(int refNumIndex, int mpNum) bool Cell::containsActor(int refNumIndex, int mpNum)
@ -155,8 +153,6 @@ void Cell::removeActors(const mwmp::BaseActorList &newActorList)
if (!foundActor) if (!foundActor)
it++; it++;
} }
cellActorList.count = cellActorList.baseActors.size();
} }
RakNet::RakNetGUID *Cell::getAuthority() RakNet::RakNetGUID *Cell::getAuthority()

@ -16,7 +16,7 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
// Send only to players who have the cell loaded // Send only to players who have the cell loaded
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
serverCell->sendToLoaded(&packet, &actorList); serverCell->sendToLoaded(&packet, &actorList);

@ -16,7 +16,7 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
// Send only to players who have the cell loaded // Send only to players who have the cell loaded
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
serverCell->sendToLoaded(&packet, &actorList); serverCell->sendToLoaded(&packet, &actorList);

@ -16,7 +16,7 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
// Send only to players who have the cell loaded // Send only to players who have the cell loaded
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
serverCell->sendToLoaded(&packet, &actorList); serverCell->sendToLoaded(&packet, &actorList);

@ -16,7 +16,7 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
// Send only to players who have the cell loaded // Send only to players who have the cell loaded
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
serverCell->sendToLoaded(&packet, &actorList); serverCell->sendToLoaded(&packet, &actorList);

@ -15,11 +15,11 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
{ {
serverCell->removeActors(&actorList); serverCell->removeActors(actorList);
std::vector<std::shared_ptr<Actor>> actors; std::vector<std::shared_ptr<Actor>> actors;

@ -16,7 +16,7 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
// Send only to players who have the cell loaded // Send only to players who have the cell loaded
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
serverCell->sendToLoaded(&packet, &actorList); serverCell->sendToLoaded(&packet, &actorList);

@ -16,7 +16,7 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
// Send only to players who have the cell loaded // Send only to players who have the cell loaded
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
{ {

@ -16,7 +16,7 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
// Send only to players who have the cell loaded // Send only to players who have the cell loaded
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
serverCell->sendToLoaded(&packet, &actorList); serverCell->sendToLoaded(&packet, &actorList);

@ -16,7 +16,7 @@ namespace mwmp
void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override void Do(ActorPacket &packet, const std::shared_ptr<Player> &player, BaseActorList &actorList) override
{ {
// Send only to players who have the cell loaded // Send only to players who have the cell loaded
Cell *serverCell = CellController::get().getCell(&actorList.cell); Cell *serverCell = CellController::get().getCell(actorList.cell);
if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid) if (serverCell != nullptr && *serverCell->getAuthority() == actorList.guid)
{ {

@ -23,9 +23,9 @@ namespace mwmp
// Until we have a timestamp-based system, send packets pertaining to more // Until we have a timestamp-based system, send packets pertaining to more
// than one container (i.e. replies to server requests for container contents) // than one container (i.e. replies to server requests for container contents)
// only to players who have the container's cell loaded // only to players who have the container's cell loaded
if (event.action == BaseEvent::SET && event.worldObjectCount > 1) if (event.action == BaseEvent::SET && event.worldObjects.size() > 1)
{ {
Cell *serverCell = CellController::get().getCell(&event.cell); Cell *serverCell = CellController::get().getCell(event.cell);
if (serverCell != nullptr) if (serverCell != nullptr)
serverCell->sendToLoaded(&packet, &event); serverCell->sendToLoaded(&packet, &event);

@ -18,10 +18,8 @@ namespace mwmp
{ {
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received %s from %s", strPacketID.c_str(), player->npc.mName.c_str()); LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received %s from %s", strPacketID.c_str(), player->npc.mName.c_str());
for (unsigned int i = 0; i < event.worldObjectCount; i++) for (auto & object : event.worldObjects)
{ object.mpNum = mwmp::Networking::getPtr()->incrementMpNum();
event.worldObjects.at(i).mpNum = mwmp::Networking::getPtr()->incrementMpNum();
}
// Send this packet back to the original sender with the mpNum generation from above, // Send this packet back to the original sender with the mpNum generation from above,
// then send it to the other players // then send it to the other players

@ -18,10 +18,8 @@ namespace mwmp
{ {
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received %s from %s", strPacketID.c_str(), player->npc.mName.c_str()); LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received %s from %s", strPacketID.c_str(), player->npc.mName.c_str());
for (unsigned int i = 0; i < event.worldObjectCount; i++) for (auto & object : event.worldObjects)
{ object.mpNum = mwmp::Networking::getPtr()->incrementMpNum();
event.worldObjects.at(i).mpNum = mwmp::Networking::getPtr()->incrementMpNum();
}
// Send this packet back to the original sender with the mpNum generation from above, // Send this packet back to the original sender with the mpNum generation from above,
// then send it to the other players // then send it to the other players

@ -59,8 +59,6 @@ namespace mwmp
std::vector<std::shared_ptr<BaseActor>> baseActors; std::vector<std::shared_ptr<BaseActor>> baseActors;
unsigned int count;
ESM::Cell cell; ESM::Cell cell;
unsigned char action; // 0 - Clear and set in entirety, 1 - Add item, 2 - Remove item, 3 - Request items unsigned char action; // 0 - Clear and set in entirety, 1 - Add item, 2 - Remove item, 3 - Request items

@ -82,7 +82,6 @@ namespace mwmp
RakNet::RakNetGUID guid; RakNet::RakNetGUID guid;
std::vector<WorldObject> worldObjects; std::vector<WorldObject> worldObjects;
unsigned int worldObjectCount;
ESM::Cell cell; ESM::Cell cell;

@ -25,23 +25,13 @@ void ActorPacket::Packet(RakNet::BitStream *bs, bool send)
if (!PacketHeader(bs, send)) if (!PacketHeader(bs, send))
return; return;
BaseActor *actor; for(auto &actor : actorList->baseActors)
for (unsigned int i = 0; i < actorList->count; i++)
{ {
if (send)
actor = actorList->baseActors.at(i).get();
else
actor = new BaseActor();
RW(actor->refNumIndex, send); RW(actor->refNumIndex, send);
RW(actor->mpNum, send); RW(actor->mpNum, send);
Actor(*actor, send); Actor(*actor, send);
if (!send)
actorList->baseActors.push_back(std::shared_ptr<BaseActor>(actor));
} }
} }
bool ActorPacket::PacketHeader(RakNet::BitStream *bs, bool send) bool ActorPacket::PacketHeader(RakNet::BitStream *bs, bool send)
@ -51,14 +41,19 @@ bool ActorPacket::PacketHeader(RakNet::BitStream *bs, bool send)
RW(actorList->cell.mData, send, true); RW(actorList->cell.mData, send, true);
RW(actorList->cell.mName, send, true); RW(actorList->cell.mName, send, true);
uint32_t actorCount;
if (send) if (send)
actorList->count = (unsigned int)(actorList->baseActors.size()); actorCount = (uint32_t)(actorList->baseActors.size());
else
actorList->baseActors.clear(); RW(actorCount, send);
RW(actorList->count, send); if(!send)
{
actorList->baseActors.clear();
actorList->baseActors.resize(actorCount, std::make_shared<BaseActor>());
}
if (actorList->count > maxActors) if (actorCount > maxActors)
{ {
actorList->isValid = false; actorList->isValid = false;
return false; return false;

@ -15,15 +15,8 @@ void PacketActorList::Packet(RakNet::BitStream *bs, bool send)
RW(actorList->action, send); RW(actorList->action, send);
BaseActor *actor; for(auto &actor : actorList->baseActors)
for (unsigned int i = 0; i < actorList->count; i++)
{ {
if (send)
actor = actorList->baseActors.at(i).get();
else
actor = new BaseActor();
RW(actor->refId, send); RW(actor->refId, send);
RW(actor->refNumIndex, send); RW(actor->refNumIndex, send);
RW(actor->mpNum, send); RW(actor->mpNum, send);
@ -33,8 +26,5 @@ void PacketActorList::Packet(RakNet::BitStream *bs, bool send)
actorList->isValid = false; actorList->isValid = false;
return; return;
} }
if (!send)
actorList->baseActors.push_back(std::shared_ptr<BaseActor>(actor));
} }
} }

@ -36,14 +36,14 @@ bool WorldPacket::PacketHeader(RakNet::BitStream *bs, bool send)
{ {
BasePacket::Packet(bs, send); BasePacket::Packet(bs, send);
uint32_t worldObjectCount;
if (send) if (send)
event->worldObjectCount = (unsigned int)(event->worldObjects.size()); worldObjectCount = (uint32_t) (event->worldObjects.size());
else
event->worldObjects.clear();
RW(event->worldObjectCount, send); RW(worldObjectCount, send);
if (event->worldObjectCount > maxObjects) if (worldObjectCount > maxObjects)
{ {
event->isValid = false; event->isValid = false;
return false; return false;
@ -51,7 +51,8 @@ bool WorldPacket::PacketHeader(RakNet::BitStream *bs, bool send)
if (!send) if (!send)
{ {
event->worldObjects.resize(event->worldObjectCount); event->worldObjects.clear();
event->worldObjects.resize(worldObjectCount);
} }
if (hasCellData) if (hasCellData)

Loading…
Cancel
Save