diff options
author | Shauren <shauren.trinity@gmail.com> | 2024-07-31 11:43:39 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2024-07-31 11:43:39 +0200 |
commit | f41ab921b9e32bd952ab712f99aa9754753ba43e (patch) | |
tree | 204400de55246e837f89ed224b3f2b250f9f141e | |
parent | 068d1526e0a3e5489a27341a831faa046ddf32d3 (diff) |
Core/PacketIO: Added new packet reading/writing utilities for type casting, optionals and bits
-rw-r--r-- | src/server/game/Server/Packets/PacketUtilities.h | 116 |
1 files changed, 116 insertions, 0 deletions
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 <short_alloc/short_alloc.h> #include <string_view> @@ -317,6 +318,121 @@ namespace WorldPackets private: ChronoDuration _value = ChronoDuration::zero(); }; + + template<typename Underlying, typename T> + struct AsWriter + { + T const& Value; + + friend inline ByteBuffer& operator<<(ByteBuffer& data, AsWriter const& opt) + { + data << Underlying(opt.Value); + return data; + } + }; + + template<typename Underlying, typename T> + struct AsReaderWriter : AsWriter<Underlying, T> + { + friend inline ByteBuffer& operator>>(ByteBuffer& data, AsReaderWriter const& opt) + { + Underlying temp; + data >> temp; + const_cast<T&>(opt.Value) = static_cast<T>(temp); + return data; + } + }; + + template<typename Underlying, typename T> + inline AsWriter<Underlying, T> As(T const& value) { return { value }; } + + template<typename Underlying, typename T> + inline AsReaderWriter<Underlying, T> As(T& value) { return { value }; } + + template<typename T> + struct OptionalInitWriter + { + Optional<T> const& Opt; + + friend inline ByteBuffer& operator<<(ByteBuffer& data, OptionalInitWriter const& opt) + { + data.WriteBit(opt.Opt.has_value()); + return data; + } + }; + + template<typename T> + struct OptionalInitReaderWriter : OptionalInitWriter<T> + { + friend inline ByteBuffer& operator>>(ByteBuffer& data, OptionalInitReaderWriter const& opt) + { + if (data.ReadBit()) + const_cast<Optional<T>&>(opt.Opt).emplace(); + return data; + } + }; + + template<typename T> + inline OptionalInitWriter<T> OptionalInit(Optional<T> const& value) { return { value }; } + + template<typename T> + inline OptionalInitReaderWriter<T> OptionalInit(Optional<T>& value) { return { value }; } + + template<uint32 BitCount, typename T> + struct BitsWriter + { + T const& Value; + + friend inline ByteBuffer& operator<<(ByteBuffer& data, BitsWriter const& bits) + { + data.WriteBits(static_cast<uint32>(bits.Value), BitCount); + return data; + } + }; + + template<uint32 BitCount, typename T> + struct BitsReaderWriter : BitsWriter<BitCount, T> + { + friend inline ByteBuffer& operator>>(ByteBuffer& data, BitsReaderWriter const& bits) + { + const_cast<T&>(bits.Value) = static_cast<T>(data.ReadBits(BitCount)); + return data; + } + }; + + template<uint32 BitCount, typename T> + inline BitsWriter<BitCount, T> Bits(T const& value) { return { value }; } + + template<uint32 BitCount, typename T> + inline BitsReaderWriter<BitCount, T> Bits(T& value) { return { value }; } + + template<uint32 BitCount, typename Container> + struct BitsSizeWriter + { + Container const& Value; + + friend inline ByteBuffer& operator<<(ByteBuffer& data, BitsSizeWriter const& bits) + { + data.WriteBits(static_cast<uint32>(bits.Value.size()), BitCount); + return data; + } + }; + + template<uint32 BitCount, typename Container> + struct BitsSizeReaderWriter : BitsSizeWriter<BitCount, Container> + { + friend inline ByteBuffer& operator>>(ByteBuffer& data, BitsSizeReaderWriter const& bits) + { + const_cast<Container&>(bits.Value).resize(data.ReadBits(BitCount)); + return data; + } + }; + + template<uint32 BitCount, typename Container> + inline BitsSizeWriter<BitCount, Container> BitsSize(Container const& value) { return { value }; } + + template<uint32 BitCount, typename Container> + inline BitsSizeReaderWriter<BitCount, Container> BitsSize(Container& value) { return { value }; } } #endif // PacketUtilities_h__ |