mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 13:30:06 +00:00
Load packets using a packet factory
This commit is contained in:
parent
0ef21fd609
commit
ae7baf0980
4 changed files with 155 additions and 14 deletions
73
wxdata/source/scwx/wsr88d/rpg/packet_factory.cpp
Normal file
73
wxdata/source/scwx/wsr88d/rpg/packet_factory.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#include <scwx/wsr88d/rpg/packet_factory.hpp>
|
||||
|
||||
#include <scwx/wsr88d/rpg/linked_contour_vector_packet.hpp>
|
||||
#include <scwx/wsr88d/rpg/linked_vector_packet.hpp>
|
||||
#include <scwx/wsr88d/rpg/set_color_level_packet.hpp>
|
||||
#include <scwx/wsr88d/rpg/text_and_special_symbol_packet.hpp>
|
||||
#include <scwx/wsr88d/rpg/unlinked_contour_vector_packet.hpp>
|
||||
#include <scwx/wsr88d/rpg/unlinked_vector_packet.hpp>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace wsr88d
|
||||
{
|
||||
namespace rpg
|
||||
{
|
||||
|
||||
static const std::string logPrefix_ = "[scwx::wsr88d::rpg::packet_factory] ";
|
||||
|
||||
typedef std::function<std::shared_ptr<Packet>(std::istream&)>
|
||||
CreateMessageFunction;
|
||||
|
||||
static const std::unordered_map<uint16_t, CreateMessageFunction> 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<Packet> PacketFactory::Create(std::istream& is)
|
||||
{
|
||||
std::shared_ptr<Packet> packet = nullptr;
|
||||
bool packetValid = true;
|
||||
|
||||
uint16_t packetCode;
|
||||
|
||||
is.read(reinterpret_cast<char*>(&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
|
||||
Loading…
Add table
Add a link
Reference in a new issue