From ff8b5061b42b20761240ec6ba2f6fc35b4cfbc60 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sat, 9 Dec 2017 11:08:53 +0800 Subject: [PATCH] [Server] Modernize CellController singleton --- apps/openmw-mp/Actors.cpp | 6 ++--- apps/openmw-mp/CellController.cpp | 26 +++---------------- apps/openmw-mp/CellController.hpp | 14 +++++----- apps/openmw-mp/Networking.cpp | 7 +---- apps/openmw-mp/Player.cpp | 4 +-- .../processors/actor/ProcessorActorAI.hpp | 2 +- .../actor/ProcessorActorAnimFlags.hpp | 2 +- .../actor/ProcessorActorAnimPlay.hpp | 2 +- .../processors/actor/ProcessorActorAttack.hpp | 2 +- .../actor/ProcessorActorCellChange.hpp | 2 +- .../processors/actor/ProcessorActorDeath.hpp | 2 +- .../actor/ProcessorActorPosition.hpp | 2 +- .../processors/actor/ProcessorActorSpeech.hpp | 2 +- .../actor/ProcessorActorStatsDynamic.hpp | 2 +- .../player/ProcessorPlayerCellState.hpp | 2 +- .../processors/world/ProcessorContainer.hpp | 2 +- 16 files changed, 27 insertions(+), 52 deletions(-) diff --git a/apps/openmw-mp/Actors.cpp b/apps/openmw-mp/Actors.cpp index 0c42a4319..911633405 100644 --- a/apps/openmw-mp/Actors.cpp +++ b/apps/openmw-mp/Actors.cpp @@ -175,7 +175,7 @@ void ActorController::sendActors(std::shared_ptr player, std::vectorgetCell(&actorList.cell); + serverCell = CellController::get().getCell(&actorList.cell); if (positionChanged) { @@ -250,7 +250,7 @@ void ActorController::sendList(std::shared_ptr player, std::vectorsetActorList(&actorList); packet->Send(actorList.guid); if (sendToAll) - CellController::get()->getCell(&actorList.cell)->sendToLoaded(packet, &actorList); + CellController::get().getCell(&actorList.cell)->sendToLoaded(packet, &actorList); } void ActorController::requestList(std::shared_ptr player, const ESM::Cell &cell) @@ -266,7 +266,7 @@ void ActorController::requestList(std::shared_ptr player, const ESM::Cel std::vector> ActorController::getActors(std::shared_ptr player, const ESM::Cell &cell) { - Cell *serverCell = CellController::get()->getCell(&player->cell); + Cell *serverCell = CellController::get().getCell(&player->cell); std::vector> actorList; diff --git a/apps/openmw-mp/CellController.cpp b/apps/openmw-mp/CellController.cpp index 1889c3f19..1062e1e8e 100644 --- a/apps/openmw-mp/CellController.cpp +++ b/apps/openmw-mp/CellController.cpp @@ -9,36 +9,16 @@ using namespace std; -CellController::CellController() -{ - -} - CellController::~CellController() { for (auto cell : cells) delete cell; } -CellController *CellController::sThis = nullptr; - -void CellController::create() -{ - assert(!sThis); - sThis = new CellController; -} - -void CellController::destroy() -{ - assert(sThis); - delete sThis; - sThis = nullptr; -} - -CellController *CellController::get() +CellController &CellController::get() { - assert(sThis); - return sThis; + static CellController cellCtrl; + return cellCtrl; } Cell *CellController::getCell(ESM::Cell *esmCell) diff --git a/apps/openmw-mp/CellController.hpp b/apps/openmw-mp/CellController.hpp index b71064156..faa9af402 100644 --- a/apps/openmw-mp/CellController.hpp +++ b/apps/openmw-mp/CellController.hpp @@ -15,14 +15,15 @@ class Cell; class CellController { private: - CellController(); + CellController() = default; ~CellController(); - - CellController(CellController&); // not used public: - static void create(); - static void destroy(); - static CellController *get(); + CellController(const CellController&) = delete; + CellController(CellController&&) = delete; + CellController& operator=(const CellController&) = delete; + CellController& operator=(CellController&&) = delete; + + static CellController &get(); public: typedef std::deque TContainer; typedef TContainer::iterator TIter; @@ -40,7 +41,6 @@ public: void update(Player *player); private: - static CellController *sThis; TContainer cells; }; diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 05adda1c3..7b192e6ce 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -40,8 +40,6 @@ Networking::Networking(RakNet::RakPeerInterface *peer) : mclient(nullptr) sThis = this; this->peer = peer; - CellController::create(); - playerPacketController = new PlayerPacketController(peer); actorPacketController = new ActorPacketController(peer); worldPacketController = new WorldPacketController(peer); @@ -63,10 +61,7 @@ Networking::Networking(RakNet::RakPeerInterface *peer) : mclient(nullptr) Networking::~Networking() { luaState.getEventCtrl().Call(false); - - CellController::destroy(); - - sThis = nullptr; + delete playerPacketController; delete actorPacketController; delete worldPacketController; diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 83dd73d8f..9ee2314aa 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -124,7 +124,7 @@ Player::Player(RakNet::RakNetGUID guid) : BasePlayer(guid), NetActor(), changedM Player::~Player() { printf("Player::~Player()\n"); - CellController::get()->deletePlayer(this); + CellController::get().deletePlayer(this); } void Player::update() @@ -796,7 +796,7 @@ void Player::setAuthority() writeActorList.cell = cell; writeActorList.guid = guid; - Cell *serverCell = CellController::get()->getCell(&cell); + Cell *serverCell = CellController::get().getCell(&cell); if (serverCell != nullptr) { serverCell->setAuthority(guid); diff --git a/apps/openmw-mp/processors/actor/ProcessorActorAI.hpp b/apps/openmw-mp/processors/actor/ProcessorActorAI.hpp index bed23243a..1929f1693 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorAI.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorAI.hpp @@ -16,7 +16,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &player, BaseActorList &actorList) override { // 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) serverCell->sendToLoaded(&packet, &actorList); diff --git a/apps/openmw-mp/processors/actor/ProcessorActorAnimFlags.hpp b/apps/openmw-mp/processors/actor/ProcessorActorAnimFlags.hpp index 1d064fbea..9c22c2426 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorAnimFlags.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorAnimFlags.hpp @@ -16,7 +16,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &player, BaseActorList &actorList) override { // 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) serverCell->sendToLoaded(&packet, &actorList); diff --git a/apps/openmw-mp/processors/actor/ProcessorActorAnimPlay.hpp b/apps/openmw-mp/processors/actor/ProcessorActorAnimPlay.hpp index b8b3462e5..233e86cb7 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorAnimPlay.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorAnimPlay.hpp @@ -16,7 +16,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &player, BaseActorList &actorList) override { // 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) serverCell->sendToLoaded(&packet, &actorList); diff --git a/apps/openmw-mp/processors/actor/ProcessorActorAttack.hpp b/apps/openmw-mp/processors/actor/ProcessorActorAttack.hpp index 0c0f488b2..ad2610fb1 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorAttack.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorAttack.hpp @@ -16,7 +16,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &player, BaseActorList &actorList) override { // 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) serverCell->sendToLoaded(&packet, &actorList); diff --git a/apps/openmw-mp/processors/actor/ProcessorActorCellChange.hpp b/apps/openmw-mp/processors/actor/ProcessorActorCellChange.hpp index 26ead25df..d57004ef0 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorCellChange.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorCellChange.hpp @@ -15,7 +15,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &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) { diff --git a/apps/openmw-mp/processors/actor/ProcessorActorDeath.hpp b/apps/openmw-mp/processors/actor/ProcessorActorDeath.hpp index 2b3baebec..a5248e6a8 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorDeath.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorDeath.hpp @@ -16,7 +16,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &player, BaseActorList &actorList) override { // 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) serverCell->sendToLoaded(&packet, &actorList); diff --git a/apps/openmw-mp/processors/actor/ProcessorActorPosition.hpp b/apps/openmw-mp/processors/actor/ProcessorActorPosition.hpp index 1850dfd07..676e87877 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorPosition.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorPosition.hpp @@ -16,7 +16,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &player, BaseActorList &actorList) override { // 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) { diff --git a/apps/openmw-mp/processors/actor/ProcessorActorSpeech.hpp b/apps/openmw-mp/processors/actor/ProcessorActorSpeech.hpp index 9b496ed8f..2d878a798 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorSpeech.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorSpeech.hpp @@ -16,7 +16,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &player, BaseActorList &actorList) override { // 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) serverCell->sendToLoaded(&packet, &actorList); diff --git a/apps/openmw-mp/processors/actor/ProcessorActorStatsDynamic.hpp b/apps/openmw-mp/processors/actor/ProcessorActorStatsDynamic.hpp index 8a2b3f67c..b4ac19b06 100644 --- a/apps/openmw-mp/processors/actor/ProcessorActorStatsDynamic.hpp +++ b/apps/openmw-mp/processors/actor/ProcessorActorStatsDynamic.hpp @@ -16,7 +16,7 @@ namespace mwmp void Do(ActorPacket &packet, const std::shared_ptr &player, BaseActorList &actorList) override { // 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) { diff --git a/apps/openmw-mp/processors/player/ProcessorPlayerCellState.hpp b/apps/openmw-mp/processors/player/ProcessorPlayerCellState.hpp index 28bc94c1b..7e22b0a47 100644 --- a/apps/openmw-mp/processors/player/ProcessorPlayerCellState.hpp +++ b/apps/openmw-mp/processors/player/ProcessorPlayerCellState.hpp @@ -21,7 +21,7 @@ namespace mwmp { LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received %s from %s", strPacketID.c_str(), player->npc.mName.c_str()); - CellController::get()->update(player.get()); + CellController::get().update(player.get()); } }; } diff --git a/apps/openmw-mp/processors/world/ProcessorContainer.hpp b/apps/openmw-mp/processors/world/ProcessorContainer.hpp index e7d773f0f..e0fb8f2fc 100644 --- a/apps/openmw-mp/processors/world/ProcessorContainer.hpp +++ b/apps/openmw-mp/processors/world/ProcessorContainer.hpp @@ -25,7 +25,7 @@ namespace mwmp // only to players who have the container's cell loaded if (event.action == BaseEvent::SET && event.worldObjectCount > 1) { - Cell *serverCell = CellController::get()->getCell(&event.cell); + Cell *serverCell = CellController::get().getCell(&event.cell); if (serverCell != nullptr) serverCell->sendToLoaded(&packet, &event);