DarkflameServer/dNet/BitStreamUtils.h
jadebenn ef6f2f133e
chore: Eradicate C-style casts and further clean up some code (#1361)
* cast and code cleanup

* cast cleanup

* bug fixes and improvements

* no getBoolField method exists

* fixes

* unbroke sg cannon scoring

* removing comments

* Remove the c-style cast warning I added from CMakeLists now that they're gone (it triggers on 3rd party dependencies and slows down compilation)

* (Hopefully) fix MacOS compilation error

* partially-implemented feedback

* more updates to account for feedback

* change bool default

---------

Co-authored-by: jadebenn <jonahebenn@yahoo.com>
2023-12-27 22:18:20 -06:00

105 lines
2.9 KiB
C++

#ifndef __BITSTREAMUTILS__H__
#define __BITSTREAMUTILS__H__
#include "GeneralUtils.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include <string>
enum class eConnectionType : uint16_t;
struct LUString {
std::string string;
uint32_t size;
LUString(uint32_t size) {
this->size = size;
};
LUString(std::string string, uint32_t size = 33) {
this->string = string;
this->size = size;
};
std::u16string GetAsU16String() const {
return GeneralUtils::ASCIIToUTF16(this->string);
};
};
struct LUWString {
std::u16string string;
uint32_t size;
LUWString(uint32_t size) {
this->size = size;
};
LUWString(std::u16string string, uint32_t size = 33) {
this->string = string;
this->size = size;
};
LUWString(std::string string, uint32_t size = 33) {
this->string = GeneralUtils::ASCIIToUTF16(string);
this->size = size;
};
std::string GetAsString() const {
return GeneralUtils::UTF16ToWTF8(this->string);
};
};
namespace BitStreamUtils {
template<typename T>
void WriteHeader(RakNet::BitStream& bitStream, eConnectionType connectionType, T internalPacketID) {
bitStream.Write<MessageID>(ID_USER_PACKET_ENUM);
bitStream.Write<eConnectionType>(connectionType);
bitStream.Write(static_cast<uint32_t>(internalPacketID));
bitStream.Write<uint8_t>(0);
}
}
namespace RakNet {
#ifndef __BITSTREAM_NATIVE_END
#error No definition for big endian reading of LUString
#endif
template <>
inline bool RakNet::BitStream::Read<LUString>(LUString& value) {
value.string.resize(value.size);
bool res = ReadBits(reinterpret_cast<unsigned char*>(value.string.data()), BYTES_TO_BITS(value.string.size()), true);
if (!res) return false;
value.string.erase(std::find(value.string.begin(), value.string.end(), '\0'), value.string.end());
return res;
}
template <>
inline bool RakNet::BitStream::Read<LUWString>(LUWString& value) {
value.string.resize(value.size);
bool res = ReadBits(reinterpret_cast<unsigned char*>(value.string.data()), BYTES_TO_BITS(value.string.size()) * sizeof(std::u16string::value_type), true);
if (!res) return false;
value.string.erase(std::find(value.string.begin(), value.string.end(), u'\0'), value.string.end());
return res;
}
template <>
inline void RakNet::BitStream::Write<std::string>(std::string value) {
this->WriteBits(reinterpret_cast<const unsigned char*>(value.data()), BYTES_TO_BITS(value.size()));
}
template <>
inline void RakNet::BitStream::Write<std::u16string>(std::u16string value) {
this->WriteBits(reinterpret_cast<const unsigned char*>(value.data()), BYTES_TO_BITS(value.size()) * sizeof(std::u16string::value_type));
}
template <>
inline void RakNet::BitStream::Write<LUString>(LUString value) {
value.string.resize(value.size);
this->Write(value.string);
}
template <>
inline void RakNet::BitStream::Write<LUWString>(LUWString value) {
value.string.resize(value.size);
this->Write(value.string);
}
};
#endif //!__BITSTREAMUTILS__H__