more use of DeclareAllPacket

This commit is contained in:
2024-07-19 20:22:12 +02:00
parent 7abb31c2e1
commit f9f5f60049
6 changed files with 108 additions and 98 deletions

View File

@@ -9,29 +9,18 @@ namespace PacketFactory {
using PacketCreator = std::function<std::unique_ptr<Packet>()>;
#define DeclarePacket(PacketName, ...) std::make_unique<packets::PacketName>(),
static const std::array<std::unique_ptr<Packet>, static_cast<std::size_t>(PacketType::PACKET_COUNT)> Packets = {
std::make_unique<packets::PlayerLogin>(),
std::make_unique<packets::UpdateHealth>(),
std::make_unique<packets::LoggingSuccess>(),
std::make_unique<packets::PlayerDeath>(),
std::make_unique<packets::PlayerJoin>(),
std::make_unique<packets::PlayerLeave>(),
std::make_unique<packets::PlayerList>(),
std::make_unique<packets::PlayerStats>(),
std::make_unique<packets::ServerConfig>(),
std::make_unique<packets::ServerTps>(),
std::make_unique<packets::UpdateGameState>(),
std::make_unique<packets::KeepAlive>(),
std::make_unique<packets::Disconnect>(),
std::make_unique<packets::ChatMessage>(),
std::make_unique<packets::PlayerPositionAndRotation>(),
std::make_unique<packets::PlayerShoot>(),
DeclareAllPacket()
};
const std::unique_ptr<Packet>& CreateReadOnlyPacket(PacketType a_Type) {
return Packets[static_cast<std::size_t>(a_Type)];
return Packets[static_cast<std::size_t>(a_Type)];
}
} // namespace PacketFactory
} // namespace PacketFactory
} // namespace protocol
} // namespace blitz

View File

@@ -11,29 +11,16 @@ namespace protocol {
namespace PacketSerializer {
#define VisitSerialize(ClassName) \
void Visit(const ClassName& a_Packet) override { \
#define DeclarePacket(PacketName, ...) \
void Visit(const packets::PacketName& a_Packet) override { \
const auto& packetData = a_Packet.m_Data; \
SerializePacketData(packetData); \
} \
\
void SerializePacketData(const ClassName::PacketDataType& a_Packet)
#define VisitDeserialize(ClassName) \
void Visit(const ClassName& a_Packet) override { \
auto packetPtr = PacketFactory::CreatePacket<ClassName>(); \
auto& packetData = packetPtr->m_Data; \
\
DeserializePacketData(packetData); \
\
m_Packet = std::move(packetPtr); \
} \
\
void DeserializePacketData(ClassName::PacketDataType& a_Packet)
void SerializePacketData(const packets::PacketName::PacketDataType& a_Packet);
#define DeclarePacket(PacketName, ...) \
VisitSerialize(packets::PacketName)
class Serializer : public PacketVisitor {
private:
@@ -51,8 +38,24 @@ class Serializer : public PacketVisitor {
};
#undef DeclarePacket
#define DeclarePacket(PacketName, ...) \
VisitDeserialize(packets::PacketName)
#define DeclarePacket(PacketName, ...) \
void Visit(const packets::PacketName& a_Packet) override { \
auto packetPtr = PacketFactory::CreatePacket<packets::PacketName>(); \
auto& packetData = packetPtr->m_Data; \
\
DeserializePacketData(packetData); \
\
m_Packet = std::move(packetPtr); \
} \
\
void DeserializePacketData(packets::PacketName::PacketDataType& a_Packet);
class Deserializer : public PacketVisitor {
private: