#include #include #include #include #include #include #include #include #include namespace scwx { namespace wsr88d { namespace rpg { static const std::string logPrefix_ = "[scwx::wsr88d::rpg::packet_factory] "; typedef std::function(std::istream&)> CreateMessageFunction; static const std::unordered_map create_ { {1, TextAndSpecialSymbolPacket::Create}, {2, TextAndSpecialSymbolPacket::Create}, {6, LinkedVectorPacket::Create}, {7, UnlinkedVectorPacket::Create}, {8, TextAndSpecialSymbolPacket::Create}, {9, LinkedVectorPacket::Create}, {10, UnlinkedVectorPacket::Create}, {0x0802, SetColorLevelPacket::Create}, {0x0E03, LinkedContourVectorPacket::Create}, {0x3501, UnlinkedContourVectorPacket::Create}}; std::shared_ptr PacketFactory::Create(std::istream& is) { std::shared_ptr packet = nullptr; bool packetValid = true; uint16_t packetCode; is.read(reinterpret_cast(&packetCode), 2); packetCode = ntohs(packetCode); if (is.eof()) { packetValid = false; } is.seekg(-2, std::ios_base::cur); if (packetValid && create_.find(packetCode) == create_.end()) { BOOST_LOG_TRIVIAL(warning) << logPrefix_ << "Unknown packet code: " << packetCode << " (0x" << std::hex << packetCode << std::dec << ")"; packetValid = false; } if (packetValid) { packet = create_.at(packetCode)(is); } return packet; } } // namespace rpg } // namespace wsr88d } // namespace scwx