2023-05-02 22:19:20 +00:00
|
|
|
#ifndef __LDFFORMAT__H__
|
|
|
|
#define __LDFFORMAT__H__
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// Custom Classes
|
|
|
|
#include "dCommonVars.h"
|
|
|
|
#include "GeneralUtils.h"
|
|
|
|
|
|
|
|
// C++
|
|
|
|
#include <string>
|
2023-05-02 22:19:20 +00:00
|
|
|
#include <string_view>
|
2021-12-05 17:54:36 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
// RakNet
|
2023-05-02 22:19:20 +00:00
|
|
|
#include "BitStream.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
enum eLDFType {
|
2022-07-28 13:39:57 +00:00
|
|
|
LDF_TYPE_UNKNOWN = -1, //!< Unknown data type
|
|
|
|
LDF_TYPE_UTF_16 = 0, //!< UTF-16 wstring data type
|
|
|
|
LDF_TYPE_S32 = 1, //!< Signed 32-bit data type
|
|
|
|
LDF_TYPE_FLOAT = 3, //!< Float data type
|
|
|
|
LDF_TYPE_DOUBLE = 4, //!< Double data type
|
|
|
|
LDF_TYPE_U32 = 5, //!< Unsigned 32-bit data type
|
|
|
|
LDF_TYPE_BOOLEAN = 7, //!< Boolean data type
|
|
|
|
LDF_TYPE_U64 = 8, //!< Unsigned 64-bit data type (originally signed, templates won't work with both S64 & OBJID
|
|
|
|
LDF_TYPE_OBJID = 9, //!< Signed 64-bit data type (reserved for object IDs)
|
|
|
|
LDF_TYPE_UTF_8 = 13, //!< UTF-8 string data type
|
2021-12-05 17:54:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class LDFBaseData {
|
|
|
|
public:
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-05-02 22:19:20 +00:00
|
|
|
virtual ~LDFBaseData() {}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
virtual void WriteToPacket(RakNet::BitStream& packet) const = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
virtual const std::u16string& GetKey() const = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
virtual eLDFType GetValueType() const = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-05-02 22:19:20 +00:00
|
|
|
/** Gets a string from the key/value pair
|
|
|
|
* @param includeKey Whether or not to include the key in the data
|
|
|
|
* @param includeTypeId Whether or not to include the type id in the data
|
|
|
|
* @return The string representation of the data
|
2022-07-28 13:39:57 +00:00
|
|
|
*/
|
2024-05-23 00:06:52 +00:00
|
|
|
virtual std::string GetString(bool includeKey = true, bool includeTypeId = true) const = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
virtual std::string GetValueAsString() const = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
virtual LDFBaseData* Copy() const = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-05-02 22:19:20 +00:00
|
|
|
/**
|
|
|
|
* Given an input string, return the data as a LDF key.
|
2022-07-28 13:39:57 +00:00
|
|
|
*/
|
2023-05-02 22:19:20 +00:00
|
|
|
static LDFBaseData* DataFromString(const std::string_view& format);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2023-05-02 22:19:20 +00:00
|
|
|
class LDFData: public LDFBaseData {
|
2021-12-05 17:54:36 +00:00
|
|
|
private:
|
2022-07-28 13:39:57 +00:00
|
|
|
std::u16string key;
|
|
|
|
T value;
|
|
|
|
|
|
|
|
//! Writes the key to the packet
|
2024-05-23 00:06:52 +00:00
|
|
|
void WriteKey(RakNet::BitStream& packet) const {
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint8_t>(this->key.length() * sizeof(uint16_t));
|
2022-07-28 13:39:57 +00:00
|
|
|
for (uint32_t i = 0; i < this->key.length(); ++i) {
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint16_t>(this->key[i]);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Writes the value to the packet
|
2024-05-23 00:06:52 +00:00
|
|
|
void WriteValue(RakNet::BitStream& packet) const {
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint8_t>(this->GetValueType());
|
|
|
|
packet.Write(this->value);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
public:
|
2022-07-28 13:39:57 +00:00
|
|
|
|
|
|
|
//! Initializer
|
|
|
|
LDFData(const std::u16string& key, const T& value) {
|
|
|
|
this->key = key;
|
|
|
|
this->value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Destructor
|
|
|
|
~LDFData(void) override {}
|
|
|
|
|
|
|
|
//! Gets the value
|
|
|
|
/*!
|
|
|
|
\return The value
|
|
|
|
*/
|
2024-05-23 00:06:52 +00:00
|
|
|
const T& GetValue(void) const { return this->value; }
|
2022-07-28 13:39:57 +00:00
|
|
|
|
|
|
|
//! Sets the value
|
|
|
|
/*!
|
|
|
|
\param value The value to set to
|
|
|
|
*/
|
|
|
|
void SetValue(T value) { this->value = value; };
|
|
|
|
|
|
|
|
//! Gets the value string
|
|
|
|
/*!
|
|
|
|
\return The value string
|
|
|
|
*/
|
2024-05-23 00:06:52 +00:00
|
|
|
std::string GetValueString(void) const { return ""; }
|
2022-07-28 13:39:57 +00:00
|
|
|
|
|
|
|
//! Writes the data to a packet
|
|
|
|
/*!
|
|
|
|
\param packet The packet
|
|
|
|
*/
|
2024-05-23 00:06:52 +00:00
|
|
|
void WriteToPacket(RakNet::BitStream& packet) const override {
|
2022-07-28 13:39:57 +00:00
|
|
|
this->WriteKey(packet);
|
|
|
|
this->WriteValue(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Gets the key
|
|
|
|
/*!
|
|
|
|
\return The key
|
|
|
|
*/
|
2024-05-23 00:06:52 +00:00
|
|
|
const std::u16string& GetKey(void) const override { return this->key; }
|
2022-07-28 13:39:57 +00:00
|
|
|
|
|
|
|
//! Gets the LDF Type
|
|
|
|
/*!
|
|
|
|
\return The LDF value type
|
|
|
|
*/
|
2024-05-23 00:06:52 +00:00
|
|
|
eLDFType GetValueType(void) const override { return LDF_TYPE_UNKNOWN; }
|
2022-07-28 13:39:57 +00:00
|
|
|
|
|
|
|
//! Gets the string data
|
|
|
|
/*!
|
|
|
|
\param includeKey Whether or not to include the key in the data
|
|
|
|
\param includeTypeId Whether or not to include the type id in the data
|
|
|
|
\return The string representation of the data
|
|
|
|
*/
|
2024-05-23 00:06:52 +00:00
|
|
|
std::string GetString(const bool includeKey = true, const bool includeTypeId = true) const override {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (GetValueType() == -1) {
|
|
|
|
return GeneralUtils::UTF16ToWTF8(this->key) + "=-1:<server variable>";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream stream;
|
|
|
|
|
|
|
|
if (includeKey) {
|
|
|
|
const std::string& sKey = GeneralUtils::UTF16ToWTF8(this->key, this->key.size());
|
2023-05-02 22:19:20 +00:00
|
|
|
stream << sKey << '=';
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeTypeId) {
|
2023-05-02 22:19:20 +00:00
|
|
|
stream << this->GetValueType() << ':';
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& sData = this->GetValueString();
|
|
|
|
|
|
|
|
stream << sData;
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
std::string GetValueAsString() const override {
|
2022-07-28 13:39:57 +00:00
|
|
|
return this->GetValueString();
|
|
|
|
}
|
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
LDFBaseData* Copy() const override {
|
2022-07-28 13:39:57 +00:00
|
|
|
return new LDFData<T>(key, value);
|
|
|
|
}
|
|
|
|
|
2024-02-10 19:44:40 +00:00
|
|
|
inline static const T Default = {};
|
2021-12-05 17:54:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// LDF Types
|
2024-05-23 00:06:52 +00:00
|
|
|
template<> inline eLDFType LDFData<std::u16string>::GetValueType(void) const { return LDF_TYPE_UTF_16; };
|
|
|
|
template<> inline eLDFType LDFData<int32_t>::GetValueType(void) const { return LDF_TYPE_S32; };
|
|
|
|
template<> inline eLDFType LDFData<float>::GetValueType(void) const { return LDF_TYPE_FLOAT; };
|
|
|
|
template<> inline eLDFType LDFData<double>::GetValueType(void) const { return LDF_TYPE_DOUBLE; };
|
|
|
|
template<> inline eLDFType LDFData<uint32_t>::GetValueType(void) const { return LDF_TYPE_U32; };
|
|
|
|
template<> inline eLDFType LDFData<bool>::GetValueType(void) const { return LDF_TYPE_BOOLEAN; };
|
|
|
|
template<> inline eLDFType LDFData<uint64_t>::GetValueType(void) const { return LDF_TYPE_U64; };
|
|
|
|
template<> inline eLDFType LDFData<LWOOBJID>::GetValueType(void) const { return LDF_TYPE_OBJID; };
|
|
|
|
template<> inline eLDFType LDFData<std::string>::GetValueType(void) const { return LDF_TYPE_UTF_8; };
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
// The specialized version for std::u16string (UTF-16)
|
|
|
|
template<>
|
2024-05-23 00:06:52 +00:00
|
|
|
inline void LDFData<std::u16string>::WriteValue(RakNet::BitStream& packet) const {
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint8_t>(this->GetValueType());
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint32_t>(this->value.length());
|
2022-07-28 13:39:57 +00:00
|
|
|
for (uint32_t i = 0; i < this->value.length(); ++i) {
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint16_t>(this->value[i]);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The specialized version for bool
|
|
|
|
template<>
|
2024-05-23 00:06:52 +00:00
|
|
|
inline void LDFData<bool>::WriteValue(RakNet::BitStream& packet) const {
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint8_t>(this->GetValueType());
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint8_t>(this->value);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The specialized version for std::string (UTF-8)
|
|
|
|
template<>
|
2024-05-23 00:06:52 +00:00
|
|
|
inline void LDFData<std::string>::WriteValue(RakNet::BitStream& packet) const {
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint8_t>(this->GetValueType());
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint32_t>(this->value.length());
|
2022-07-28 13:39:57 +00:00
|
|
|
for (uint32_t i = 0; i < this->value.length(); ++i) {
|
2024-02-26 14:15:29 +00:00
|
|
|
packet.Write<uint8_t>(this->value[i]);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
template<> inline std::string LDFData<std::u16string>::GetValueString() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return GeneralUtils::UTF16ToWTF8(this->value, this->value.size());
|
|
|
|
}
|
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
template<> inline std::string LDFData<int32_t>::GetValueString() const { return std::to_string(this->value); }
|
|
|
|
template<> inline std::string LDFData<float>::GetValueString() const { return std::to_string(this->value); }
|
|
|
|
template<> inline std::string LDFData<double>::GetValueString() const { return std::to_string(this->value); }
|
|
|
|
template<> inline std::string LDFData<uint32_t>::GetValueString() const { return std::to_string(this->value); }
|
|
|
|
template<> inline std::string LDFData<bool>::GetValueString() const { return std::to_string(this->value); }
|
|
|
|
template<> inline std::string LDFData<uint64_t>::GetValueString() const { return std::to_string(this->value); }
|
|
|
|
template<> inline std::string LDFData<LWOOBJID>::GetValueString() const { return std::to_string(this->value); }
|
2023-05-02 22:19:20 +00:00
|
|
|
|
2024-05-23 00:06:52 +00:00
|
|
|
template<> inline std::string LDFData<std::string>::GetValueString() const { return this->value; }
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-05-02 22:19:20 +00:00
|
|
|
#endif //!__LDFFORMAT__H__
|