From 0cf09adb44eedbb701187086ea838cc8015aa49d Mon Sep 17 00:00:00 2001 From: Koncord Date: Fri, 30 Jun 2017 20:09:05 +0800 Subject: [PATCH] [General] Move AddProcessor to BasePacketProcessor --- apps/openmw-mp/ActorProcessor.cpp | 14 ++------------ apps/openmw-mp/ActorProcessor.hpp | 9 +-------- apps/openmw-mp/PlayerProcessor.cpp | 15 ++------------- apps/openmw-mp/PlayerProcessor.hpp | 9 +-------- apps/openmw-mp/WorldProcessor.cpp | 14 ++------------ apps/openmw-mp/WorldProcessor.hpp | 9 +-------- apps/openmw/mwmp/processors/ActorProcessor.cpp | 16 +++------------- apps/openmw/mwmp/processors/ActorProcessor.hpp | 9 +-------- .../processors/BaseClientPacketProcessor.hpp | 2 +- apps/openmw/mwmp/processors/PlayerProcessor.cpp | 14 ++------------ apps/openmw/mwmp/processors/PlayerProcessor.hpp | 8 +------- apps/openmw/mwmp/processors/WorldProcessor.cpp | 14 ++------------ apps/openmw/mwmp/processors/WorldProcessor.hpp | 9 +-------- .../openmw-mp/Base/BasePacketProcessor.hpp | 16 +++++++++++++++- 14 files changed, 35 insertions(+), 123 deletions(-) diff --git a/apps/openmw-mp/ActorProcessor.cpp b/apps/openmw-mp/ActorProcessor.cpp index 1acbd5d38..b7a68d832 100644 --- a/apps/openmw-mp/ActorProcessor.cpp +++ b/apps/openmw-mp/ActorProcessor.cpp @@ -3,24 +3,14 @@ using namespace mwmp; -ActorProcessor::processors_t ActorProcessor::processors; +template +typename BasePacketProcessor::processors_t BasePacketProcessor::processors; void ActorProcessor::Do(ActorPacket &packet, Player &player, BaseActorList &actorList) { packet.Send(true); } -void ActorProcessor::AddProcessor(mwmp::ActorProcessor *processor) noexcept -{ - for (auto &p : processors) - { - if (processor->packetID == p.first) - throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " + - processor->className + " and " + p.second->className); - } - processors.insert(processors_t::value_type(processor->GetPacketID(), processor)); -} - bool ActorProcessor::Process(RakNet::Packet &packet, BaseActorList &actorList) noexcept { // Clear our BaseActorList before loading new data in it diff --git a/apps/openmw-mp/ActorProcessor.hpp b/apps/openmw-mp/ActorProcessor.hpp index 502b65d4a..03e6e7d09 100644 --- a/apps/openmw-mp/ActorProcessor.hpp +++ b/apps/openmw-mp/ActorProcessor.hpp @@ -6,25 +6,18 @@ #include #include #include -#include -#include #include "Script/Script.hpp" #include "Player.hpp" namespace mwmp { - class ActorProcessor : public BasePacketProcessor + class ActorProcessor : public BasePacketProcessor { public: virtual void Do(ActorPacket &packet, Player &player, BaseActorList &actorList); static bool Process(RakNet::Packet &packet, BaseActorList &actorList) noexcept; - static void AddProcessor(ActorProcessor *processor) noexcept; - - typedef std::unordered_map > processors_t; - private: - static processors_t processors; }; } diff --git a/apps/openmw-mp/PlayerProcessor.cpp b/apps/openmw-mp/PlayerProcessor.cpp index bcf1b451c..90b8658a5 100644 --- a/apps/openmw-mp/PlayerProcessor.cpp +++ b/apps/openmw-mp/PlayerProcessor.cpp @@ -4,22 +4,11 @@ #include "PlayerProcessor.hpp" #include "Networking.hpp" -//#include using namespace mwmp; -PlayerProcessor::processors_t PlayerProcessor::processors; - -void PlayerProcessor::AddProcessor(PlayerProcessor *processor) noexcept -{ - for (auto &p : processors) - { - if (processor->packetID == p.first) - throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " + - processor->className + " and " + p.second->className); - } - processors.insert(processors_t::value_type(processor->GetPacketID(), processor)); -} +template +typename BasePacketProcessor::processors_t BasePacketProcessor::processors; bool PlayerProcessor::Process(RakNet::Packet &packet) noexcept { diff --git a/apps/openmw-mp/PlayerProcessor.hpp b/apps/openmw-mp/PlayerProcessor.hpp index e97e034d1..812aef5e2 100644 --- a/apps/openmw-mp/PlayerProcessor.hpp +++ b/apps/openmw-mp/PlayerProcessor.hpp @@ -8,24 +8,17 @@ #include #include #include -#include -#include #include "Player.hpp" namespace mwmp { - class PlayerProcessor : public BasePacketProcessor + class PlayerProcessor : public BasePacketProcessor { public: virtual void Do(PlayerPacket &packet, Player &player) = 0; static bool Process(RakNet::Packet &packet) noexcept; - static void AddProcessor(PlayerProcessor *processor) noexcept; - - typedef std::unordered_map > processors_t; - private: - static processors_t processors; }; } diff --git a/apps/openmw-mp/WorldProcessor.cpp b/apps/openmw-mp/WorldProcessor.cpp index e31748a7b..1dbebb5cd 100644 --- a/apps/openmw-mp/WorldProcessor.cpp +++ b/apps/openmw-mp/WorldProcessor.cpp @@ -7,24 +7,14 @@ using namespace mwmp; -WorldProcessor::processors_t WorldProcessor::processors; +template +typename BasePacketProcessor::processors_t BasePacketProcessor::processors; void WorldProcessor::Do(WorldPacket &packet, Player &player, BaseEvent &event) { packet.Send(true); } -void WorldProcessor::AddProcessor(mwmp::WorldProcessor *processor) noexcept -{ - for (auto &p : processors) - { - if (processor->packetID == p.first) - throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " + - processor->className + " and " + p.second->className); - } - processors.insert(processors_t::value_type(processor->GetPacketID(), processor)); -} - bool WorldProcessor::Process(RakNet::Packet &packet, BaseEvent &event) noexcept { // Clear our BaseEvent before loading new data in it diff --git a/apps/openmw-mp/WorldProcessor.hpp b/apps/openmw-mp/WorldProcessor.hpp index bf96f7014..b4a70632b 100644 --- a/apps/openmw-mp/WorldProcessor.hpp +++ b/apps/openmw-mp/WorldProcessor.hpp @@ -10,25 +10,18 @@ #include #include #include -#include -#include #include "Script/Script.hpp" #include "Player.hpp" namespace mwmp { - class WorldProcessor : public BasePacketProcessor + class WorldProcessor : public BasePacketProcessor { public: virtual void Do(WorldPacket &packet, Player &player, BaseEvent &event); static bool Process(RakNet::Packet &packet, BaseEvent &event) noexcept; - static void AddProcessor(WorldProcessor *processor) noexcept; - - typedef std::unordered_map > processors_t; - private: - static processors_t processors; }; } diff --git a/apps/openmw/mwmp/processors/ActorProcessor.cpp b/apps/openmw/mwmp/processors/ActorProcessor.cpp index a1908b35c..6f61d6766 100644 --- a/apps/openmw/mwmp/processors/ActorProcessor.cpp +++ b/apps/openmw/mwmp/processors/ActorProcessor.cpp @@ -8,7 +8,8 @@ using namespace mwmp; -ActorProcessor::processors_t ActorProcessor::processors; +template +typename BasePacketProcessor::processors_t BasePacketProcessor::processors; bool ActorProcessor::Process(RakNet::Packet &packet, ActorList &actorList) { @@ -43,15 +44,4 @@ bool ActorProcessor::Process(RakNet::Packet &packet, ActorList &actorList) } } return false; -} - -void ActorProcessor::AddProcessor(mwmp::ActorProcessor *processor) -{ - for (auto &p : processors) - { - if (processor->packetID == p.first) - throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " + - processor->className + " and " + p.second->className); - } - processors.insert(processors_t::value_type(processor->GetPacketID(), processor)); -} +} \ No newline at end of file diff --git a/apps/openmw/mwmp/processors/ActorProcessor.hpp b/apps/openmw/mwmp/processors/ActorProcessor.hpp index bd4e4527e..a63e4d9eb 100644 --- a/apps/openmw/mwmp/processors/ActorProcessor.hpp +++ b/apps/openmw/mwmp/processors/ActorProcessor.hpp @@ -8,25 +8,18 @@ #include #include #include -#include #include "../WorldEvent.hpp" #include "../ActorList.hpp" #include "BaseClientPacketProcessor.hpp" namespace mwmp { - class ActorProcessor : public BaseClientPacketProcessor + class ActorProcessor : public BasePacketProcessor, public BaseClientPacketProcessor { public: virtual void Do(ActorPacket &packet, ActorList &actorList) = 0; static bool Process(RakNet::Packet &packet, ActorList &actorList); - static void AddProcessor(ActorProcessor *processor); - - typedef std::unordered_map > processors_t; - - private: - static processors_t processors; }; } diff --git a/apps/openmw/mwmp/processors/BaseClientPacketProcessor.hpp b/apps/openmw/mwmp/processors/BaseClientPacketProcessor.hpp index 5e39e4381..9a18bcf08 100644 --- a/apps/openmw/mwmp/processors/BaseClientPacketProcessor.hpp +++ b/apps/openmw/mwmp/processors/BaseClientPacketProcessor.hpp @@ -11,7 +11,7 @@ namespace mwmp { - class BaseClientPacketProcessor : public BasePacketProcessor + class BaseClientPacketProcessor { public: static void SetServerAddr(RakNet::SystemAddress addr) diff --git a/apps/openmw/mwmp/processors/PlayerProcessor.cpp b/apps/openmw/mwmp/processors/PlayerProcessor.cpp index f03a1b43b..2ef955871 100644 --- a/apps/openmw/mwmp/processors/PlayerProcessor.cpp +++ b/apps/openmw/mwmp/processors/PlayerProcessor.cpp @@ -8,18 +8,8 @@ using namespace mwmp; -PlayerProcessor::processors_t PlayerProcessor::processors; - -void PlayerProcessor::AddProcessor(PlayerProcessor *processor) -{ - for (auto &p : processors) - { - if (processor->packetID == p.first) - throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " + - processor->className + " and " + p.second->className); - } - processors.insert(processors_t::value_type(processor->GetPacketID(), processor)); -} +template +typename BasePacketProcessor::processors_t BasePacketProcessor::processors; bool PlayerProcessor::Process(RakNet::Packet &packet) { diff --git a/apps/openmw/mwmp/processors/PlayerProcessor.hpp b/apps/openmw/mwmp/processors/PlayerProcessor.hpp index 2ae9bf7ee..dbf4581b7 100644 --- a/apps/openmw/mwmp/processors/PlayerProcessor.hpp +++ b/apps/openmw/mwmp/processors/PlayerProcessor.hpp @@ -8,7 +8,6 @@ #include #include #include -#include #include "../LocalPlayer.hpp" #include "../DedicatedPlayer.hpp" #include "../PlayerList.hpp" @@ -16,17 +15,12 @@ namespace mwmp { - class PlayerProcessor : public BaseClientPacketProcessor + class PlayerProcessor : public BasePacketProcessor, public BaseClientPacketProcessor { public: virtual void Do(PlayerPacket &packet, BasePlayer *player) = 0; static bool Process(RakNet::Packet &packet); - static void AddProcessor(PlayerProcessor *processor); - - typedef std::unordered_map > processors_t; - private: - static processors_t processors; }; } diff --git a/apps/openmw/mwmp/processors/WorldProcessor.cpp b/apps/openmw/mwmp/processors/WorldProcessor.cpp index 5ffd32c75..a1fc95bb5 100644 --- a/apps/openmw/mwmp/processors/WorldProcessor.cpp +++ b/apps/openmw/mwmp/processors/WorldProcessor.cpp @@ -8,7 +8,8 @@ using namespace mwmp; -WorldProcessor::processors_t WorldProcessor::processors; +template +typename BasePacketProcessor::processors_t BasePacketProcessor::processors; bool WorldProcessor::Process(RakNet::Packet &packet, WorldEvent &event) { @@ -42,14 +43,3 @@ bool WorldProcessor::Process(RakNet::Packet &packet, WorldEvent &event) } return false; } - -void WorldProcessor::AddProcessor(mwmp::WorldProcessor *processor) -{ - for (auto &p : processors) - { - if (processor->packetID == p.first) - throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " + - processor->className + " and " + p.second->className); - } - processors.insert(processors_t::value_type(processor->GetPacketID(), processor)); -} diff --git a/apps/openmw/mwmp/processors/WorldProcessor.hpp b/apps/openmw/mwmp/processors/WorldProcessor.hpp index 7b8038afd..0e74ac70e 100644 --- a/apps/openmw/mwmp/processors/WorldProcessor.hpp +++ b/apps/openmw/mwmp/processors/WorldProcessor.hpp @@ -8,7 +8,6 @@ #include #include #include -#include #include "../WorldEvent.hpp" #include "../LocalPlayer.hpp" #include "../DedicatedPlayer.hpp" @@ -16,18 +15,12 @@ namespace mwmp { - class WorldProcessor : public BaseClientPacketProcessor + class WorldProcessor : public BasePacketProcessor, public BaseClientPacketProcessor { public: virtual void Do(WorldPacket &packet, WorldEvent &event) = 0; static bool Process(RakNet::Packet &packet, WorldEvent &event); - static void AddProcessor(WorldProcessor *processor); - - typedef std::unordered_map > processors_t; - - private: - static processors_t processors; }; } diff --git a/components/openmw-mp/Base/BasePacketProcessor.hpp b/components/openmw-mp/Base/BasePacketProcessor.hpp index 8941ef32a..6fd23002d 100644 --- a/components/openmw-mp/Base/BasePacketProcessor.hpp +++ b/components/openmw-mp/Base/BasePacketProcessor.hpp @@ -6,12 +6,16 @@ #define OPENMW_BASEPACKETPROCESSOR_HPP #include +#include +#include #define BPP_INIT(packet_id) packetID = packet_id; strPacketID = #packet_id; className = typeid(this).name(); avoidReading = false; +template class BasePacketProcessor { public: + typedef std::unordered_map> processors_t; unsigned char GetPacketID() { return packetID; @@ -26,12 +30,22 @@ public: return className; } + static void AddProcessor(Proccessor *processor) + { + for (auto &p : processors) + { + if (processor->packetID == p.first) + throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " + + processor->className + " and " + p.second->className); + } + processors.insert(typename processors_t::value_type(processor->GetPacketID(), processor)); + } protected: unsigned char packetID; std::string strPacketID; std::string className; bool avoidReading; + static processors_t processors; }; - #endif //OPENMW_BASEPACKETPROCESSOR_HPP