From 9002c009f053da615744b2263585792d476dbdba Mon Sep 17 00:00:00 2001 From: Shauren Date: Wed, 31 Jul 2024 11:43:39 +0200 Subject: Core/PacketIO: Added new packet reading/writing utilities for type casting, optionals and bits (cherry picked from commit f41ab921b9e32bd952ab712f99aa9754753ba43e) --- src/server/game/Server/Packets/PacketUtilities.h | 116 +++++++++++++++++++++++ 1 file changed, 116 insertions(+) (limited to 'src') diff --git a/src/server/game/Server/Packets/PacketUtilities.h b/src/server/game/Server/Packets/PacketUtilities.h index 9810ae932f4..30973bb3f07 100644 --- a/src/server/game/Server/Packets/PacketUtilities.h +++ b/src/server/game/Server/Packets/PacketUtilities.h @@ -20,6 +20,7 @@ #include "ByteBuffer.h" #include "Duration.h" +#include "Optional.h" #include "Tuples.h" #include #include @@ -317,6 +318,121 @@ namespace WorldPackets private: ChronoDuration _value = ChronoDuration::zero(); }; + + template + struct AsWriter + { + T const& Value; + + friend inline ByteBuffer& operator<<(ByteBuffer& data, AsWriter const& opt) + { + data << Underlying(opt.Value); + return data; + } + }; + + template + struct AsReaderWriter : AsWriter + { + friend inline ByteBuffer& operator>>(ByteBuffer& data, AsReaderWriter const& opt) + { + Underlying temp; + data >> temp; + const_cast(opt.Value) = static_cast(temp); + return data; + } + }; + + template + inline AsWriter As(T const& value) { return { value }; } + + template + inline AsReaderWriter As(T& value) { return { value }; } + + template + struct OptionalInitWriter + { + Optional const& Opt; + + friend inline ByteBuffer& operator<<(ByteBuffer& data, OptionalInitWriter const& opt) + { + data.WriteBit(opt.Opt.has_value()); + return data; + } + }; + + template + struct OptionalInitReaderWriter : OptionalInitWriter + { + friend inline ByteBuffer& operator>>(ByteBuffer& data, OptionalInitReaderWriter const& opt) + { + if (data.ReadBit()) + const_cast&>(opt.Opt).emplace(); + return data; + } + }; + + template + inline OptionalInitWriter OptionalInit(Optional const& value) { return { value }; } + + template + inline OptionalInitReaderWriter OptionalInit(Optional& value) { return { value }; } + + template + struct BitsWriter + { + T const& Value; + + friend inline ByteBuffer& operator<<(ByteBuffer& data, BitsWriter const& bits) + { + data.WriteBits(static_cast(bits.Value), BitCount); + return data; + } + }; + + template + struct BitsReaderWriter : BitsWriter + { + friend inline ByteBuffer& operator>>(ByteBuffer& data, BitsReaderWriter const& bits) + { + const_cast(bits.Value) = static_cast(data.ReadBits(BitCount)); + return data; + } + }; + + template + inline BitsWriter Bits(T const& value) { return { value }; } + + template + inline BitsReaderWriter Bits(T& value) { return { value }; } + + template + struct BitsSizeWriter + { + Container const& Value; + + friend inline ByteBuffer& operator<<(ByteBuffer& data, BitsSizeWriter const& bits) + { + data.WriteBits(static_cast(bits.Value.size()), BitCount); + return data; + } + }; + + template + struct BitsSizeReaderWriter : BitsSizeWriter + { + friend inline ByteBuffer& operator>>(ByteBuffer& data, BitsSizeReaderWriter const& bits) + { + const_cast(bits.Value).resize(data.ReadBits(BitCount)); + return data; + } + }; + + template + inline BitsSizeWriter BitsSize(Container const& value) { return { value }; } + + template + inline BitsSizeReaderWriter BitsSize(Container& value) { return { value }; } } #endif // PacketUtilities_h__ -- cgit v1.2.3