mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-04 09:44:10 +00:00
Refactor: Amf3 implementation (#998)
* Update AMFDeserializeTests.cpp Redo Amf3 functionality Overhaul the whole thing due to it being outdated and clunky to use Sometimes you want to keep the value Update AMFDeserializeTests.cpp * Fix enum and constructors Correct enum to a class and simplify names. Add a proper default constructor * Update MasterServer.cpp * Fix bugs and add more tests * Refactor: AMF with templates in mind - Remove hard coded bodge - Use templates and generics to allow for much looser typing and strengthened implementation - Move code into header only implementation for portability Refactor: Convert AMF implementation to templates - Rip out previous implementation - Remove all extraneous terminology - Add proper overloads for all types of inserts - Fix up tests and codebase * Fix compiler errors * Check for null first * Add specialization for const char* * Update tests for new template specialization * Switch BitStream to use references * Rename files * Check enum bounds on deserialize I did this on a phone
This commit is contained in:
@@ -1,77 +1,81 @@
|
||||
#include "AMFDeserialize.h"
|
||||
|
||||
#include "AMFFormat.h"
|
||||
#include <stdexcept>
|
||||
|
||||
#include "Amf3.h"
|
||||
|
||||
/**
|
||||
* AMF3 Reference document https://rtmp.veriskope.com/pdf/amf3-file-format-spec.pdf
|
||||
* AMF3 Deserializer written by EmosewaMC
|
||||
*/
|
||||
|
||||
AMFValue* AMFDeserialize::Read(RakNet::BitStream* inStream) {
|
||||
AMFBaseValue* AMFDeserialize::Read(RakNet::BitStream* inStream) {
|
||||
if (!inStream) return nullptr;
|
||||
AMFValue* returnValue = nullptr;
|
||||
AMFBaseValue* returnValue = nullptr;
|
||||
// Read in the value type from the bitStream
|
||||
int8_t marker;
|
||||
inStream->Read(marker);
|
||||
uint8_t i;
|
||||
inStream->Read(i);
|
||||
if (i > static_cast<uint8_t>(eAmf::Dictionary)) return nullptr;
|
||||
eAmf marker = static_cast<eAmf>(i);
|
||||
// Based on the typing, create the value associated with that and return the base value class
|
||||
switch (marker) {
|
||||
case AMFValueType::AMFUndefined: {
|
||||
returnValue = new AMFUndefinedValue();
|
||||
case eAmf::Undefined: {
|
||||
returnValue = new AMFBaseValue();
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFValueType::AMFNull: {
|
||||
case eAmf::Null: {
|
||||
returnValue = new AMFNullValue();
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFValueType::AMFFalse: {
|
||||
returnValue = new AMFFalseValue();
|
||||
case eAmf::False: {
|
||||
returnValue = new AMFBoolValue(false);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFValueType::AMFTrue: {
|
||||
returnValue = new AMFTrueValue();
|
||||
case eAmf::True: {
|
||||
returnValue = new AMFBoolValue(true);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFValueType::AMFInteger: {
|
||||
case eAmf::Integer: {
|
||||
returnValue = ReadAmfInteger(inStream);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFValueType::AMFDouble: {
|
||||
case eAmf::Double: {
|
||||
returnValue = ReadAmfDouble(inStream);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFValueType::AMFString: {
|
||||
case eAmf::String: {
|
||||
returnValue = ReadAmfString(inStream);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFValueType::AMFArray: {
|
||||
case eAmf::Array: {
|
||||
returnValue = ReadAmfArray(inStream);
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO We do not need these values, but if someone wants to implement them
|
||||
// then please do so and add the corresponding unit tests.
|
||||
case AMFValueType::AMFXMLDoc:
|
||||
case AMFValueType::AMFDate:
|
||||
case AMFValueType::AMFObject:
|
||||
case AMFValueType::AMFXML:
|
||||
case AMFValueType::AMFByteArray:
|
||||
case AMFValueType::AMFVectorInt:
|
||||
case AMFValueType::AMFVectorUInt:
|
||||
case AMFValueType::AMFVectorDouble:
|
||||
case AMFValueType::AMFVectorObject:
|
||||
case AMFValueType::AMFDictionary: {
|
||||
throw static_cast<AMFValueType>(marker);
|
||||
// These values are unimplemented in the live client and will remain unimplemented
|
||||
// unless someone modifies the client to allow serializing of these values.
|
||||
case eAmf::XMLDoc:
|
||||
case eAmf::Date:
|
||||
case eAmf::Object:
|
||||
case eAmf::XML:
|
||||
case eAmf::ByteArray:
|
||||
case eAmf::VectorInt:
|
||||
case eAmf::VectorUInt:
|
||||
case eAmf::VectorDouble:
|
||||
case eAmf::VectorObject:
|
||||
case eAmf::Dictionary: {
|
||||
throw marker;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw static_cast<AMFValueType>(marker);
|
||||
throw std::invalid_argument("Invalid AMF3 marker" + std::to_string(static_cast<int32_t>(marker)));
|
||||
break;
|
||||
}
|
||||
return returnValue;
|
||||
@@ -99,7 +103,7 @@ uint32_t AMFDeserialize::ReadU29(RakNet::BitStream* inStream) {
|
||||
return actualNumber;
|
||||
}
|
||||
|
||||
std::string AMFDeserialize::ReadString(RakNet::BitStream* inStream) {
|
||||
const std::string AMFDeserialize::ReadString(RakNet::BitStream* inStream) {
|
||||
auto length = ReadU29(inStream);
|
||||
// Check if this is a reference
|
||||
bool isReference = length % 2 == 1;
|
||||
@@ -113,48 +117,39 @@ std::string AMFDeserialize::ReadString(RakNet::BitStream* inStream) {
|
||||
return value;
|
||||
} else {
|
||||
// Length is a reference to a previous index - use that as the read in value
|
||||
return accessedElements[length];
|
||||
return accessedElements.at(length);
|
||||
}
|
||||
}
|
||||
|
||||
AMFValue* AMFDeserialize::ReadAmfDouble(RakNet::BitStream* inStream) {
|
||||
auto doubleValue = new AMFDoubleValue();
|
||||
AMFBaseValue* AMFDeserialize::ReadAmfDouble(RakNet::BitStream* inStream) {
|
||||
double value;
|
||||
inStream->Read<double>(value);
|
||||
doubleValue->SetDoubleValue(value);
|
||||
return doubleValue;
|
||||
return new AMFDoubleValue(value);
|
||||
}
|
||||
|
||||
AMFValue* AMFDeserialize::ReadAmfArray(RakNet::BitStream* inStream) {
|
||||
AMFBaseValue* AMFDeserialize::ReadAmfArray(RakNet::BitStream* inStream) {
|
||||
auto arrayValue = new AMFArrayValue();
|
||||
|
||||
// Read size of dense array
|
||||
auto sizeOfDenseArray = (ReadU29(inStream) >> 1);
|
||||
|
||||
// Then read Key'd portion
|
||||
// Then read associative portion
|
||||
while (true) {
|
||||
auto key = ReadString(inStream);
|
||||
// No more values when we encounter an empty string
|
||||
// No more associative values when we encounter an empty string key
|
||||
if (key.size() == 0) break;
|
||||
arrayValue->InsertValue(key, Read(inStream));
|
||||
arrayValue->Insert(key, Read(inStream));
|
||||
}
|
||||
|
||||
// Finally read dense portion
|
||||
for (uint32_t i = 0; i < sizeOfDenseArray; i++) {
|
||||
arrayValue->PushBackValue(Read(inStream));
|
||||
arrayValue->Insert(i, Read(inStream));
|
||||
}
|
||||
|
||||
return arrayValue;
|
||||
}
|
||||
|
||||
AMFValue* AMFDeserialize::ReadAmfString(RakNet::BitStream* inStream) {
|
||||
auto stringValue = new AMFStringValue();
|
||||
stringValue->SetStringValue(ReadString(inStream));
|
||||
return stringValue;
|
||||
AMFBaseValue* AMFDeserialize::ReadAmfString(RakNet::BitStream* inStream) {
|
||||
return new AMFStringValue(ReadString(inStream));
|
||||
}
|
||||
|
||||
AMFValue* AMFDeserialize::ReadAmfInteger(RakNet::BitStream* inStream) {
|
||||
auto integerValue = new AMFIntegerValue();
|
||||
integerValue->SetIntegerValue(ReadU29(inStream));
|
||||
return integerValue;
|
||||
AMFBaseValue* AMFDeserialize::ReadAmfInteger(RakNet::BitStream* inStream) {
|
||||
return new AMFIntValue(ReadU29(inStream));
|
||||
}
|
||||
|
@@ -5,7 +5,8 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class AMFValue;
|
||||
class AMFBaseValue;
|
||||
|
||||
class AMFDeserialize {
|
||||
public:
|
||||
/**
|
||||
@@ -14,7 +15,7 @@ public:
|
||||
* @param inStream inStream to read value from.
|
||||
* @return Returns an AMFValue with all the information from the bitStream in it.
|
||||
*/
|
||||
AMFValue* Read(RakNet::BitStream* inStream);
|
||||
AMFBaseValue* Read(RakNet::BitStream* inStream);
|
||||
private:
|
||||
/**
|
||||
* @brief Private method to read a U29 integer from a bitstream
|
||||
@@ -30,7 +31,7 @@ private:
|
||||
* @param inStream bitStream to read data from
|
||||
* @return The read string
|
||||
*/
|
||||
std::string ReadString(RakNet::BitStream* inStream);
|
||||
const std::string ReadString(RakNet::BitStream* inStream);
|
||||
|
||||
/**
|
||||
* @brief Read an AMFDouble value from a bitStream
|
||||
@@ -38,7 +39,7 @@ private:
|
||||
* @param inStream bitStream to read data from
|
||||
* @return Double value represented as an AMFValue
|
||||
*/
|
||||
AMFValue* ReadAmfDouble(RakNet::BitStream* inStream);
|
||||
AMFBaseValue* ReadAmfDouble(RakNet::BitStream* inStream);
|
||||
|
||||
/**
|
||||
* @brief Read an AMFArray from a bitStream
|
||||
@@ -46,7 +47,7 @@ private:
|
||||
* @param inStream bitStream to read data from
|
||||
* @return Array value represented as an AMFValue
|
||||
*/
|
||||
AMFValue* ReadAmfArray(RakNet::BitStream* inStream);
|
||||
AMFBaseValue* ReadAmfArray(RakNet::BitStream* inStream);
|
||||
|
||||
/**
|
||||
* @brief Read an AMFString from a bitStream
|
||||
@@ -54,7 +55,7 @@ private:
|
||||
* @param inStream bitStream to read data from
|
||||
* @return String value represented as an AMFValue
|
||||
*/
|
||||
AMFValue* ReadAmfString(RakNet::BitStream* inStream);
|
||||
AMFBaseValue* ReadAmfString(RakNet::BitStream* inStream);
|
||||
|
||||
/**
|
||||
* @brief Read an AMFInteger from a bitStream
|
||||
@@ -62,7 +63,7 @@ private:
|
||||
* @param inStream bitStream to read data from
|
||||
* @return Integer value represented as an AMFValue
|
||||
*/
|
||||
AMFValue* ReadAmfInteger(RakNet::BitStream* inStream);
|
||||
AMFBaseValue* ReadAmfInteger(RakNet::BitStream* inStream);
|
||||
|
||||
/**
|
||||
* List of strings read so far saved to be read by reference.
|
||||
|
@@ -1,156 +0,0 @@
|
||||
#include "AMFFormat.h"
|
||||
|
||||
// AMFInteger
|
||||
void AMFIntegerValue::SetIntegerValue(uint32_t value) {
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
uint32_t AMFIntegerValue::GetIntegerValue() {
|
||||
return this->value;
|
||||
}
|
||||
|
||||
// AMFDouble
|
||||
void AMFDoubleValue::SetDoubleValue(double value) {
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
double AMFDoubleValue::GetDoubleValue() {
|
||||
return this->value;
|
||||
}
|
||||
|
||||
// AMFString
|
||||
void AMFStringValue::SetStringValue(const std::string& value) {
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
std::string AMFStringValue::GetStringValue() {
|
||||
return this->value;
|
||||
}
|
||||
|
||||
// AMFXMLDoc
|
||||
void AMFXMLDocValue::SetXMLDocValue(const std::string& value) {
|
||||
this->xmlData = value;
|
||||
}
|
||||
|
||||
std::string AMFXMLDocValue::GetXMLDocValue() {
|
||||
return this->xmlData;
|
||||
}
|
||||
|
||||
// AMFDate
|
||||
void AMFDateValue::SetDateValue(uint64_t value) {
|
||||
this->millisecondTimestamp = value;
|
||||
}
|
||||
|
||||
uint64_t AMFDateValue::GetDateValue() {
|
||||
return this->millisecondTimestamp;
|
||||
}
|
||||
|
||||
// AMFArray Insert Value
|
||||
void AMFArrayValue::InsertValue(const std::string& key, AMFValue* value) {
|
||||
this->associative.insert(std::make_pair(key, value));
|
||||
}
|
||||
|
||||
// AMFArray Remove Value
|
||||
void AMFArrayValue::RemoveValue(const std::string& key) {
|
||||
_AMFArrayMap_::iterator it = this->associative.find(key);
|
||||
if (it != this->associative.end()) {
|
||||
this->associative.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
// AMFArray Get Associative Iterator Begin
|
||||
_AMFArrayMap_::iterator AMFArrayValue::GetAssociativeIteratorValueBegin() {
|
||||
return this->associative.begin();
|
||||
}
|
||||
|
||||
// AMFArray Get Associative Iterator End
|
||||
_AMFArrayMap_::iterator AMFArrayValue::GetAssociativeIteratorValueEnd() {
|
||||
return this->associative.end();
|
||||
}
|
||||
|
||||
// AMFArray Push Back Value
|
||||
void AMFArrayValue::PushBackValue(AMFValue* value) {
|
||||
this->dense.push_back(value);
|
||||
}
|
||||
|
||||
// AMFArray Pop Back Value
|
||||
void AMFArrayValue::PopBackValue() {
|
||||
this->dense.pop_back();
|
||||
}
|
||||
|
||||
// AMFArray Get Dense List Size
|
||||
uint32_t AMFArrayValue::GetDenseValueSize() {
|
||||
return (uint32_t)this->dense.size();
|
||||
}
|
||||
|
||||
// AMFArray Get Dense Iterator Begin
|
||||
_AMFArrayList_::iterator AMFArrayValue::GetDenseIteratorBegin() {
|
||||
return this->dense.begin();
|
||||
}
|
||||
|
||||
// AMFArray Get Dense Iterator End
|
||||
_AMFArrayList_::iterator AMFArrayValue::GetDenseIteratorEnd() {
|
||||
return this->dense.end();
|
||||
}
|
||||
|
||||
AMFArrayValue::~AMFArrayValue() {
|
||||
for (auto valueToDelete : GetDenseArray()) {
|
||||
if (valueToDelete) delete valueToDelete;
|
||||
}
|
||||
for (auto valueToDelete : GetAssociativeMap()) {
|
||||
if (valueToDelete.second) delete valueToDelete.second;
|
||||
}
|
||||
}
|
||||
|
||||
// AMFObject Constructor
|
||||
AMFObjectValue::AMFObjectValue(std::vector<std::pair<std::string, AMFValueType>> traits) {
|
||||
this->traits.reserve(traits.size());
|
||||
std::vector<std::pair<std::string, AMFValueType>>::iterator it = traits.begin();
|
||||
while (it != traits.end()) {
|
||||
this->traits.insert(std::make_pair(it->first, std::make_pair(it->second, new AMFNullValue())));
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
// AMFObject Set Value
|
||||
void AMFObjectValue::SetTraitValue(const std::string& trait, AMFValue* value) {
|
||||
if (value) {
|
||||
_AMFObjectTraits_::iterator it = this->traits.find(trait);
|
||||
if (it != this->traits.end()) {
|
||||
if (it->second.first == value->GetValueType()) {
|
||||
it->second.second = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AMFObject Get Value
|
||||
AMFValue* AMFObjectValue::GetTraitValue(const std::string& trait) {
|
||||
_AMFObjectTraits_::iterator it = this->traits.find(trait);
|
||||
if (it != this->traits.end()) {
|
||||
return it->second.second;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// AMFObject Get Trait Iterator Begin
|
||||
_AMFObjectTraits_::iterator AMFObjectValue::GetTraitsIteratorBegin() {
|
||||
return this->traits.begin();
|
||||
}
|
||||
|
||||
// AMFObject Get Trait Iterator End
|
||||
_AMFObjectTraits_::iterator AMFObjectValue::GetTraitsIteratorEnd() {
|
||||
return this->traits.end();
|
||||
}
|
||||
|
||||
// AMFObject Get Trait Size
|
||||
uint32_t AMFObjectValue::GetTraitArrayCount() {
|
||||
return (uint32_t)this->traits.size();
|
||||
}
|
||||
|
||||
AMFObjectValue::~AMFObjectValue() {
|
||||
for (auto valueToDelete = GetTraitsIteratorBegin(); valueToDelete != GetTraitsIteratorEnd(); valueToDelete++) {
|
||||
if (valueToDelete->second.second) delete valueToDelete->second.second;
|
||||
}
|
||||
}
|
@@ -1,413 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// Custom Classes
|
||||
#include "dCommonVars.h"
|
||||
|
||||
// C++
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
/*!
|
||||
\file AMFFormat.hpp
|
||||
\brief A class for managing AMF values
|
||||
*/
|
||||
|
||||
class AMFValue; // Forward declaration
|
||||
|
||||
// Definitions
|
||||
#define _AMFArrayMap_ std::unordered_map<std::string, AMFValue*>
|
||||
#define _AMFArrayList_ std::vector<AMFValue*>
|
||||
|
||||
#define _AMFObjectTraits_ std::unordered_map<std::string, std::pair<AMFValueType, AMFValue*>>
|
||||
#define _AMFObjectDynamicTraits_ std::unordered_map<std::string, AMFValue*>
|
||||
|
||||
//! An enum for each AMF value type
|
||||
enum AMFValueType : unsigned char {
|
||||
AMFUndefined = 0x00, //!< An undefined AMF Value
|
||||
AMFNull = 0x01, //!< A null AMF value
|
||||
AMFFalse = 0x02, //!< A false AMF value
|
||||
AMFTrue = 0x03, //!< A true AMF value
|
||||
AMFInteger = 0x04, //!< An integer AMF value
|
||||
AMFDouble = 0x05, //!< A double AMF value
|
||||
AMFString = 0x06, //!< A string AMF value
|
||||
AMFXMLDoc = 0x07, //!< An XML Doc AMF value
|
||||
AMFDate = 0x08, //!< A date AMF value
|
||||
AMFArray = 0x09, //!< An array AMF value
|
||||
AMFObject = 0x0A, //!< An object AMF value
|
||||
AMFXML = 0x0B, //!< An XML AMF value
|
||||
AMFByteArray = 0x0C, //!< A byte array AMF value
|
||||
AMFVectorInt = 0x0D, //!< An integer vector AMF value
|
||||
AMFVectorUInt = 0x0E, //!< An unsigned integer AMF value
|
||||
AMFVectorDouble = 0x0F, //!< A double vector AMF value
|
||||
AMFVectorObject = 0x10, //!< An object vector AMF value
|
||||
AMFDictionary = 0x11 //!< A dictionary AMF value
|
||||
};
|
||||
|
||||
//! An enum for the object value types
|
||||
enum AMFObjectValueType : unsigned char {
|
||||
AMFObjectAnonymous = 0x01,
|
||||
AMFObjectTyped = 0x02,
|
||||
AMFObjectDynamic = 0x03,
|
||||
AMFObjectExternalizable = 0x04
|
||||
};
|
||||
|
||||
//! The base AMF value class
|
||||
class AMFValue {
|
||||
public:
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
virtual AMFValueType GetValueType() = 0;
|
||||
virtual ~AMFValue() {};
|
||||
};
|
||||
|
||||
//! A typedef for a pointer to an AMF value
|
||||
typedef AMFValue* NDGFxValue;
|
||||
|
||||
|
||||
// The various AMF value types
|
||||
|
||||
//! The undefined value AMF type
|
||||
class AMFUndefinedValue : public AMFValue {
|
||||
private:
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFUndefined;
|
||||
};
|
||||
|
||||
//! The null value AMF type
|
||||
class AMFNullValue : public AMFValue {
|
||||
private:
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFNull;
|
||||
};
|
||||
|
||||
//! The false value AMF type
|
||||
class AMFFalseValue : public AMFValue {
|
||||
private:
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFFalse;
|
||||
};
|
||||
|
||||
//! The true value AMF type
|
||||
class AMFTrueValue : public AMFValue {
|
||||
private:
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFTrue;
|
||||
};
|
||||
|
||||
//! The integer value AMF type
|
||||
class AMFIntegerValue : public AMFValue {
|
||||
private:
|
||||
uint32_t value; //!< The value of the AMF type
|
||||
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFInteger;
|
||||
//! Sets the integer value
|
||||
/*!
|
||||
\param value The value to set
|
||||
*/
|
||||
void SetIntegerValue(uint32_t value);
|
||||
|
||||
//! Gets the integer value
|
||||
/*!
|
||||
\return The integer value
|
||||
*/
|
||||
uint32_t GetIntegerValue();
|
||||
};
|
||||
|
||||
//! The double value AMF type
|
||||
class AMFDoubleValue : public AMFValue {
|
||||
private:
|
||||
double value; //!< The value of the AMF type
|
||||
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFDouble;
|
||||
//! Sets the double value
|
||||
/*!
|
||||
\param value The value to set to
|
||||
*/
|
||||
void SetDoubleValue(double value);
|
||||
|
||||
//! Gets the double value
|
||||
/*!
|
||||
\return The double value
|
||||
*/
|
||||
double GetDoubleValue();
|
||||
};
|
||||
|
||||
//! The string value AMF type
|
||||
class AMFStringValue : public AMFValue {
|
||||
private:
|
||||
std::string value; //!< The value of the AMF type
|
||||
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFString;
|
||||
//! Sets the string value
|
||||
/*!
|
||||
\param value The string value to set to
|
||||
*/
|
||||
void SetStringValue(const std::string& value);
|
||||
|
||||
//! Gets the string value
|
||||
/*!
|
||||
\return The string value
|
||||
*/
|
||||
std::string GetStringValue();
|
||||
};
|
||||
|
||||
//! The XML doc value AMF type
|
||||
class AMFXMLDocValue : public AMFValue {
|
||||
private:
|
||||
std::string xmlData; //!< The value of the AMF type
|
||||
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFXMLDoc;
|
||||
//! Sets the XML Doc value
|
||||
/*!
|
||||
\param value The value to set to
|
||||
*/
|
||||
void SetXMLDocValue(const std::string& value);
|
||||
|
||||
//! Gets the XML Doc value
|
||||
/*!
|
||||
\return The XML Doc value
|
||||
*/
|
||||
std::string GetXMLDocValue();
|
||||
};
|
||||
|
||||
//! The date value AMF type
|
||||
class AMFDateValue : public AMFValue {
|
||||
private:
|
||||
uint64_t millisecondTimestamp; //!< The time in milliseconds since the ephoch
|
||||
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() { return ValueType; }
|
||||
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFDate;
|
||||
//! Sets the date time
|
||||
/*!
|
||||
\param value The value to set to
|
||||
*/
|
||||
void SetDateValue(uint64_t value);
|
||||
|
||||
//! Gets the date value
|
||||
/*!
|
||||
\return The date value in milliseconds since the epoch
|
||||
*/
|
||||
uint64_t GetDateValue();
|
||||
};
|
||||
|
||||
//! The array value AMF type
|
||||
// This object will manage it's own memory map and list. Do not delete its values.
|
||||
class AMFArrayValue : public AMFValue {
|
||||
private:
|
||||
_AMFArrayMap_ associative; //!< The array map (associative part)
|
||||
_AMFArrayList_ dense; //!< The array list (dense part)
|
||||
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() override { return ValueType; }
|
||||
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFArray;
|
||||
|
||||
~AMFArrayValue() override;
|
||||
//! Inserts an item into the array map for a specific key
|
||||
/*!
|
||||
\param key The key to set
|
||||
\param value The value to add
|
||||
*/
|
||||
void InsertValue(const std::string& key, AMFValue* value);
|
||||
|
||||
//! Removes an item for a specific key
|
||||
/*!
|
||||
\param key The key to remove
|
||||
*/
|
||||
void RemoveValue(const std::string& key);
|
||||
|
||||
//! Finds an AMF value
|
||||
/*!
|
||||
\return The AMF value if found, nullptr otherwise
|
||||
*/
|
||||
template <typename T>
|
||||
T* FindValue(const std::string& key) const {
|
||||
_AMFArrayMap_::const_iterator it = this->associative.find(key);
|
||||
if (it != this->associative.end() && T::ValueType == it->second->GetValueType()) {
|
||||
return dynamic_cast<T*>(it->second);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
};
|
||||
|
||||
//! Returns where the associative iterator begins
|
||||
/*!
|
||||
\return Where the array map iterator begins
|
||||
*/
|
||||
_AMFArrayMap_::iterator GetAssociativeIteratorValueBegin();
|
||||
|
||||
//! Returns where the associative iterator ends
|
||||
/*!
|
||||
\return Where the array map iterator ends
|
||||
*/
|
||||
_AMFArrayMap_::iterator GetAssociativeIteratorValueEnd();
|
||||
|
||||
//! Pushes back a value into the array list
|
||||
/*!
|
||||
\param value The value to push back
|
||||
*/
|
||||
void PushBackValue(AMFValue* value);
|
||||
|
||||
//! Pops back the last value in the array list
|
||||
void PopBackValue();
|
||||
|
||||
//! Gets the count of the dense list
|
||||
/*!
|
||||
\return The dense list size
|
||||
*/
|
||||
uint32_t GetDenseValueSize();
|
||||
|
||||
//! Gets a specific value from the list for the specified index
|
||||
/*!
|
||||
\param index The index to get
|
||||
*/
|
||||
template <typename T>
|
||||
T* GetValueAt(uint32_t index) {
|
||||
if (index >= this->dense.size()) return nullptr;
|
||||
AMFValue* foundValue = this->dense.at(index);
|
||||
return T::ValueType == foundValue->GetValueType() ? dynamic_cast<T*>(foundValue) : nullptr;
|
||||
};
|
||||
|
||||
//! Returns where the dense iterator begins
|
||||
/*!
|
||||
\return Where the iterator begins
|
||||
*/
|
||||
_AMFArrayList_::iterator GetDenseIteratorBegin();
|
||||
|
||||
//! Returns where the dense iterator ends
|
||||
/*!
|
||||
\return Where the iterator ends
|
||||
*/
|
||||
_AMFArrayList_::iterator GetDenseIteratorEnd();
|
||||
|
||||
//! Returns the associative map
|
||||
/*!
|
||||
\return The associative map
|
||||
*/
|
||||
_AMFArrayMap_ GetAssociativeMap() { return this->associative; };
|
||||
|
||||
//! Returns the dense array
|
||||
/*!
|
||||
\return The dense array
|
||||
*/
|
||||
_AMFArrayList_ GetDenseArray() { return this->dense; };
|
||||
};
|
||||
|
||||
//! The anonymous object value AMF type
|
||||
class AMFObjectValue : public AMFValue {
|
||||
private:
|
||||
_AMFObjectTraits_ traits; //!< The object traits
|
||||
|
||||
//! Returns the AMF value type
|
||||
/*!
|
||||
\return The AMF value type
|
||||
*/
|
||||
AMFValueType GetValueType() override { return ValueType; }
|
||||
~AMFObjectValue() override;
|
||||
|
||||
public:
|
||||
static const AMFValueType ValueType = AMFObject;
|
||||
//! Constructor
|
||||
/*!
|
||||
\param traits The traits to set
|
||||
*/
|
||||
AMFObjectValue(std::vector<std::pair<std::string, AMFValueType>> traits);
|
||||
|
||||
//! Gets the object value type
|
||||
/*!
|
||||
\return The object value type
|
||||
*/
|
||||
virtual AMFObjectValueType GetObjectValueType() { return AMFObjectAnonymous; }
|
||||
|
||||
//! Sets the value of a trait
|
||||
/*!
|
||||
\param trait The trait to set the value for
|
||||
\param value The AMF value to set
|
||||
*/
|
||||
void SetTraitValue(const std::string& trait, AMFValue* value);
|
||||
|
||||
//! Gets a trait value
|
||||
/*!
|
||||
\param trait The trait to get the value for
|
||||
\return The trait value
|
||||
*/
|
||||
AMFValue* GetTraitValue(const std::string& trait);
|
||||
|
||||
//! Gets the beginning of the object traits iterator
|
||||
/*!
|
||||
\return The AMF trait array iterator begin
|
||||
*/
|
||||
_AMFObjectTraits_::iterator GetTraitsIteratorBegin();
|
||||
|
||||
//! Gets the end of the object traits iterator
|
||||
/*!
|
||||
\return The AMF trait array iterator begin
|
||||
*/
|
||||
_AMFObjectTraits_::iterator GetTraitsIteratorEnd();
|
||||
|
||||
//! Gets the amount of traits
|
||||
/*!
|
||||
\return The amount of traits
|
||||
*/
|
||||
uint32_t GetTraitArrayCount();
|
||||
};
|
@@ -1,259 +0,0 @@
|
||||
#include "AMFFormat_BitStream.h"
|
||||
|
||||
// Writes an AMFValue pointer to a RakNet::BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFValue*>(AMFValue* value) {
|
||||
if (value != nullptr) {
|
||||
AMFValueType type = value->GetValueType();
|
||||
|
||||
switch (type) {
|
||||
case AMFUndefined: {
|
||||
AMFUndefinedValue* v = (AMFUndefinedValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFNull: {
|
||||
AMFNullValue* v = (AMFNullValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFFalse: {
|
||||
AMFFalseValue* v = (AMFFalseValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFTrue: {
|
||||
AMFTrueValue* v = (AMFTrueValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFInteger: {
|
||||
AMFIntegerValue* v = (AMFIntegerValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFDouble: {
|
||||
AMFDoubleValue* v = (AMFDoubleValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFString: {
|
||||
AMFStringValue* v = (AMFStringValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFXMLDoc: {
|
||||
AMFXMLDocValue* v = (AMFXMLDocValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFDate: {
|
||||
AMFDateValue* v = (AMFDateValue*)value;
|
||||
this->Write(*v);
|
||||
break;
|
||||
}
|
||||
|
||||
case AMFArray: {
|
||||
this->Write((AMFArrayValue*)value);
|
||||
break;
|
||||
}
|
||||
case AMFObject:
|
||||
case AMFXML:
|
||||
case AMFByteArray:
|
||||
case AMFVectorInt:
|
||||
case AMFVectorUInt:
|
||||
case AMFVectorDouble:
|
||||
case AMFVectorObject:
|
||||
case AMFDictionary:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A private function to write an value to a RakNet::BitStream
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteUInt29(RakNet::BitStream* bs, uint32_t v) {
|
||||
unsigned char b4 = (unsigned char)v;
|
||||
if (v < 0x00200000) {
|
||||
b4 = b4 & 0x7F;
|
||||
if (v > 0x7F) {
|
||||
unsigned char b3;
|
||||
v = v >> 7;
|
||||
b3 = ((unsigned char)(v)) | 0x80;
|
||||
if (v > 0x7F) {
|
||||
unsigned char b2;
|
||||
v = v >> 7;
|
||||
b2 = ((unsigned char)(v)) | 0x80;
|
||||
bs->Write(b2);
|
||||
}
|
||||
|
||||
bs->Write(b3);
|
||||
}
|
||||
} else {
|
||||
unsigned char b1;
|
||||
unsigned char b2;
|
||||
unsigned char b3;
|
||||
|
||||
v = v >> 8;
|
||||
b3 = ((unsigned char)(v)) | 0x80;
|
||||
v = v >> 7;
|
||||
b2 = ((unsigned char)(v)) | 0x80;
|
||||
v = v >> 7;
|
||||
b1 = ((unsigned char)(v)) | 0x80;
|
||||
|
||||
bs->Write(b1);
|
||||
bs->Write(b2);
|
||||
bs->Write(b3);
|
||||
}
|
||||
|
||||
bs->Write(b4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a flag number to a RakNet::BitStream
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteFlagNumber(RakNet::BitStream* bs, uint32_t v) {
|
||||
v = (v << 1) | 0x01;
|
||||
WriteUInt29(bs, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an AMFString to a RakNet::BitStream
|
||||
*
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteAMFString(RakNet::BitStream* bs, const std::string& str) {
|
||||
WriteFlagNumber(bs, (uint32_t)str.size());
|
||||
bs->Write(str.c_str(), (uint32_t)str.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an U16 to a bitstream
|
||||
*
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteAMFU16(RakNet::BitStream* bs, uint16_t value) {
|
||||
bs->Write(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an U32 to a bitstream
|
||||
*
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteAMFU32(RakNet::BitStream* bs, uint32_t value) {
|
||||
bs->Write(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an U64 to a bitstream
|
||||
*
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteAMFU64(RakNet::BitStream* bs, uint64_t value) {
|
||||
bs->Write(value);
|
||||
}
|
||||
|
||||
|
||||
// Writes an AMFUndefinedValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFUndefinedValue>(AMFUndefinedValue value) {
|
||||
this->Write(AMFUndefined);
|
||||
}
|
||||
|
||||
// Writes an AMFNullValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFNullValue>(AMFNullValue value) {
|
||||
this->Write(AMFNull);
|
||||
}
|
||||
|
||||
// Writes an AMFFalseValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFFalseValue>(AMFFalseValue value) {
|
||||
this->Write(AMFFalse);
|
||||
}
|
||||
|
||||
// Writes an AMFTrueValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFTrueValue>(AMFTrueValue value) {
|
||||
this->Write(AMFTrue);
|
||||
}
|
||||
|
||||
// Writes an AMFIntegerValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFIntegerValue>(AMFIntegerValue value) {
|
||||
this->Write(AMFInteger);
|
||||
WriteUInt29(this, value.GetIntegerValue());
|
||||
}
|
||||
|
||||
// Writes an AMFDoubleValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFDoubleValue>(AMFDoubleValue value) {
|
||||
this->Write(AMFDouble);
|
||||
double d = value.GetDoubleValue();
|
||||
WriteAMFU64(this, *((unsigned long long*) & d));
|
||||
}
|
||||
|
||||
// Writes an AMFStringValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFStringValue>(AMFStringValue value) {
|
||||
this->Write(AMFString);
|
||||
std::string v = value.GetStringValue();
|
||||
WriteAMFString(this, v);
|
||||
}
|
||||
|
||||
// Writes an AMFXMLDocValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFXMLDocValue>(AMFXMLDocValue value) {
|
||||
this->Write(AMFXMLDoc);
|
||||
std::string v = value.GetXMLDocValue();
|
||||
WriteAMFString(this, v);
|
||||
}
|
||||
|
||||
// Writes an AMFDateValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFDateValue>(AMFDateValue value) {
|
||||
this->Write(AMFDate);
|
||||
uint64_t date = value.GetDateValue();
|
||||
WriteAMFU64(this, date);
|
||||
}
|
||||
|
||||
// Writes an AMFArrayValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFArrayValue*>(AMFArrayValue* value) {
|
||||
this->Write(AMFArray);
|
||||
uint32_t denseSize = value->GetDenseValueSize();
|
||||
WriteFlagNumber(this, denseSize);
|
||||
|
||||
_AMFArrayMap_::iterator it = value->GetAssociativeIteratorValueBegin();
|
||||
_AMFArrayMap_::iterator end = value->GetAssociativeIteratorValueEnd();
|
||||
|
||||
while (it != end) {
|
||||
WriteAMFString(this, it->first);
|
||||
this->Write(it->second);
|
||||
it++;
|
||||
}
|
||||
|
||||
this->Write(AMFNull);
|
||||
|
||||
if (denseSize > 0) {
|
||||
_AMFArrayList_::iterator it2 = value->GetDenseIteratorBegin();
|
||||
_AMFArrayList_::iterator end2 = value->GetDenseIteratorEnd();
|
||||
|
||||
while (it2 != end2) {
|
||||
this->Write(*it2);
|
||||
it2++;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,92 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// Custom Classes
|
||||
#include "AMFFormat.h"
|
||||
|
||||
// RakNet
|
||||
#include <BitStream.h>
|
||||
|
||||
/*!
|
||||
\file AMFFormat_BitStream.h
|
||||
\brief A class that implements native writing of AMF values to RakNet::BitStream
|
||||
*/
|
||||
|
||||
// We are using the RakNet namespace
|
||||
namespace RakNet {
|
||||
//! Writes an AMFValue pointer to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFValue*>(AMFValue* value);
|
||||
|
||||
//! Writes an AMFUndefinedValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFUndefinedValue>(AMFUndefinedValue value);
|
||||
|
||||
//! Writes an AMFNullValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFNullValue>(AMFNullValue value);
|
||||
|
||||
//! Writes an AMFFalseValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFFalseValue>(AMFFalseValue value);
|
||||
|
||||
//! Writes an AMFTrueValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFTrueValue>(AMFTrueValue value);
|
||||
|
||||
//! Writes an AMFIntegerValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFIntegerValue>(AMFIntegerValue value);
|
||||
|
||||
//! Writes an AMFDoubleValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFDoubleValue>(AMFDoubleValue value);
|
||||
|
||||
//! Writes an AMFStringValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFStringValue>(AMFStringValue value);
|
||||
|
||||
//! Writes an AMFXMLDocValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFXMLDocValue>(AMFXMLDocValue value);
|
||||
|
||||
//! Writes an AMFDateValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFDateValue>(AMFDateValue value);
|
||||
|
||||
//! Writes an AMFArrayValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFArrayValue*>(AMFArrayValue* value);
|
||||
} // namespace RakNet
|
368
dCommon/Amf3.h
Normal file
368
dCommon/Amf3.h
Normal file
@@ -0,0 +1,368 @@
|
||||
#ifndef __AMF3__H__
|
||||
#define __AMF3__H__
|
||||
|
||||
#include "dCommonVars.h"
|
||||
#include "dLogger.h"
|
||||
#include "Game.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
enum class eAmf : uint8_t {
|
||||
Undefined = 0x00, // An undefined AMF Value
|
||||
Null = 0x01, // A null AMF value
|
||||
False = 0x02, // A false AMF value
|
||||
True = 0x03, // A true AMF value
|
||||
Integer = 0x04, // An integer AMF value
|
||||
Double = 0x05, // A double AMF value
|
||||
String = 0x06, // A string AMF value
|
||||
XMLDoc = 0x07, // Unused in the live client and cannot be serialized without modification. An XML Doc AMF value
|
||||
Date = 0x08, // Unused in the live client and cannot be serialized without modification. A date AMF value
|
||||
Array = 0x09, // An array AMF value
|
||||
Object = 0x0A, // Unused in the live client and cannot be serialized without modification. An object AMF value
|
||||
XML = 0x0B, // Unused in the live client and cannot be serialized without modification. An XML AMF value
|
||||
ByteArray = 0x0C, // Unused in the live client and cannot be serialized without modification. A byte array AMF value
|
||||
VectorInt = 0x0D, // Unused in the live client and cannot be serialized without modification. An integer vector AMF value
|
||||
VectorUInt = 0x0E, // Unused in the live client and cannot be serialized without modification. An unsigned integer AMF value
|
||||
VectorDouble = 0x0F, // Unused in the live client and cannot be serialized without modification. A double vector AMF value
|
||||
VectorObject = 0x10, // Unused in the live client and cannot be serialized without modification. An object vector AMF value
|
||||
Dictionary = 0x11 // Unused in the live client and cannot be serialized without modification. A dictionary AMF value
|
||||
};
|
||||
|
||||
class AMFBaseValue {
|
||||
public:
|
||||
virtual eAmf GetValueType() { return eAmf::Undefined; };
|
||||
AMFBaseValue() {};
|
||||
virtual ~AMFBaseValue() {};
|
||||
};
|
||||
|
||||
template<typename ValueType>
|
||||
class AMFValue : public AMFBaseValue {
|
||||
public:
|
||||
AMFValue() {};
|
||||
AMFValue(ValueType value) { SetValue(value); };
|
||||
virtual ~AMFValue() override {};
|
||||
|
||||
eAmf GetValueType() override { return eAmf::Undefined; };
|
||||
|
||||
const ValueType& GetValue() { return data; };
|
||||
void SetValue(ValueType value) { data = value; };
|
||||
protected:
|
||||
ValueType data;
|
||||
};
|
||||
|
||||
// As a string this is much easier to write and read from a BitStream.
|
||||
template<>
|
||||
class AMFValue<const char*> : public AMFBaseValue {
|
||||
public:
|
||||
AMFValue() {};
|
||||
AMFValue(const char* value) { SetValue(std::string(value)); };
|
||||
virtual ~AMFValue() override {};
|
||||
|
||||
eAmf GetValueType() override { return eAmf::String; };
|
||||
|
||||
const std::string& GetValue() { return data; };
|
||||
void SetValue(std::string value) { data = value; };
|
||||
protected:
|
||||
std::string data;
|
||||
};
|
||||
|
||||
typedef AMFValue<std::nullptr_t> AMFNullValue;
|
||||
typedef AMFValue<bool> AMFBoolValue;
|
||||
typedef AMFValue<int32_t> AMFIntValue;
|
||||
typedef AMFValue<std::string> AMFStringValue;
|
||||
typedef AMFValue<double> AMFDoubleValue;
|
||||
|
||||
template<> inline eAmf AMFValue<std::nullptr_t>::GetValueType() { return eAmf::Null; };
|
||||
template<> inline eAmf AMFValue<bool>::GetValueType() { return this->data ? eAmf::True : eAmf::False; };
|
||||
template<> inline eAmf AMFValue<int32_t>::GetValueType() { return eAmf::Integer; };
|
||||
template<> inline eAmf AMFValue<uint32_t>::GetValueType() { return eAmf::Integer; };
|
||||
template<> inline eAmf AMFValue<std::string>::GetValueType() { return eAmf::String; };
|
||||
template<> inline eAmf AMFValue<double>::GetValueType() { return eAmf::Double; };
|
||||
|
||||
/**
|
||||
* The AMFArrayValue object holds 2 types of lists:
|
||||
* An associative list where a key maps to a value
|
||||
* A Dense list where elements are stored back to back
|
||||
*
|
||||
* Objects that are Registered are owned by this object
|
||||
* and are not to be deleted by a caller.
|
||||
*/
|
||||
class AMFArrayValue : public AMFBaseValue {
|
||||
|
||||
typedef std::unordered_map<std::string, AMFBaseValue*> AMFAssociative;
|
||||
typedef std::vector<AMFBaseValue*> AMFDense;
|
||||
|
||||
public:
|
||||
eAmf GetValueType() override { return eAmf::Array; };
|
||||
|
||||
~AMFArrayValue() override {
|
||||
for (auto valueToDelete : GetDense()) {
|
||||
if (valueToDelete) {
|
||||
delete valueToDelete;
|
||||
valueToDelete = nullptr;
|
||||
}
|
||||
}
|
||||
for (auto valueToDelete : GetAssociative()) {
|
||||
if (valueToDelete.second) {
|
||||
delete valueToDelete.second;
|
||||
valueToDelete.second = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the Associative portion of the object
|
||||
*/
|
||||
inline AMFAssociative& GetAssociative() { return this->associative; };
|
||||
|
||||
/**
|
||||
* Returns the dense portion of the object
|
||||
*/
|
||||
inline AMFDense& GetDense() { return this->dense; };
|
||||
|
||||
/**
|
||||
* Inserts an AMFValue into the associative portion with the given key.
|
||||
* If a duplicate is attempted to be inserted, it is ignored and the
|
||||
* first value with that key is kept in the map.
|
||||
*
|
||||
* These objects are not to be deleted by the caller as they are owned by
|
||||
* the AMFArray object which manages its own memory.
|
||||
*
|
||||
* @param key The key to associate with the value
|
||||
* @param value The value to insert
|
||||
*
|
||||
* @return The inserted element if the type matched,
|
||||
* or nullptr if a key existed and was not the same type
|
||||
*/
|
||||
template<typename ValueType>
|
||||
std::pair<AMFValue<ValueType>*, bool> Insert(const std::string& key, ValueType value) {
|
||||
auto element = associative.find(key);
|
||||
AMFValue<ValueType>* val = nullptr;
|
||||
bool found = true;
|
||||
if (element == associative.end()) {
|
||||
val = new AMFValue<ValueType>(value);
|
||||
associative.insert(std::make_pair(key, val));
|
||||
} else {
|
||||
val = dynamic_cast<AMFValue<ValueType>*>(element->second);
|
||||
found = false;
|
||||
}
|
||||
return std::make_pair(val, found);
|
||||
};
|
||||
|
||||
// Associates an array with a string key
|
||||
std::pair<AMFBaseValue*, bool> Insert(const std::string& key) {
|
||||
auto element = associative.find(key);
|
||||
AMFArrayValue* val = nullptr;
|
||||
bool found = true;
|
||||
if (element == associative.end()) {
|
||||
val = new AMFArrayValue();
|
||||
associative.insert(std::make_pair(key, val));
|
||||
} else {
|
||||
val = dynamic_cast<AMFArrayValue*>(element->second);
|
||||
found = false;
|
||||
}
|
||||
return std::make_pair(val, found);
|
||||
};
|
||||
|
||||
// Associates an array with an integer key
|
||||
std::pair<AMFBaseValue*, bool> Insert(const uint32_t& index) {
|
||||
AMFArrayValue* val = nullptr;
|
||||
bool inserted = false;
|
||||
if (index >= dense.size()) {
|
||||
dense.resize(index + 1);
|
||||
val = new AMFArrayValue();
|
||||
dense.at(index) = val;
|
||||
inserted = true;
|
||||
}
|
||||
return std::make_pair(dynamic_cast<AMFArrayValue*>(dense.at(index)), inserted);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Inserts an AMFValue into the AMFArray key'd by index.
|
||||
* Attempting to insert the same key to the same value twice overwrites
|
||||
* the previous value with the new one.
|
||||
*
|
||||
* @param index The index to associate with the value
|
||||
* @param value The value to insert
|
||||
* @return The inserted element, or nullptr if the type did not match
|
||||
* what was at the index.
|
||||
*/
|
||||
template<typename ValueType>
|
||||
std::pair<AMFValue<ValueType>*, bool> Insert(const uint32_t& index, ValueType value) {
|
||||
AMFValue<ValueType>* val = nullptr;
|
||||
bool inserted = false;
|
||||
if (index >= this->dense.size()) {
|
||||
this->dense.resize(index + 1);
|
||||
val = new AMFValue<ValueType>(value);
|
||||
this->dense.at(index) = val;
|
||||
inserted = true;
|
||||
}
|
||||
return std::make_pair(dynamic_cast<AMFValue<ValueType>*>(this->dense.at(index)), inserted);
|
||||
};
|
||||
|
||||
/**
|
||||
* Inserts an AMFValue into the associative portion with the given key.
|
||||
* If a duplicate is attempted to be inserted, it replaces the original
|
||||
*
|
||||
* The inserted element is now owned by this object and is not to be deleted
|
||||
*
|
||||
* @param key The key to associate with the value
|
||||
* @param value The value to insert
|
||||
*/
|
||||
void Insert(const std::string& key, AMFBaseValue* value) {
|
||||
auto element = associative.find(key);
|
||||
if (element != associative.end() && element->second) {
|
||||
delete element->second;
|
||||
element->second = value;
|
||||
} else {
|
||||
associative.insert(std::make_pair(key, value));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Inserts an AMFValue into the associative portion with the given index.
|
||||
* If a duplicate is attempted to be inserted, it replaces the original
|
||||
*
|
||||
* The inserted element is now owned by this object and is not to be deleted
|
||||
*
|
||||
* @param key The key to associate with the value
|
||||
* @param value The value to insert
|
||||
*/
|
||||
void Insert(const uint32_t index, AMFBaseValue* value) {
|
||||
if (index < dense.size()) {
|
||||
AMFDense::iterator itr = dense.begin() + index;
|
||||
if (*itr) delete dense.at(index);
|
||||
} else {
|
||||
dense.resize(index + 1);
|
||||
}
|
||||
dense.at(index) = value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pushes an AMFValue into the back of the dense portion.
|
||||
*
|
||||
* These objects are not to be deleted by the caller as they are owned by
|
||||
* the AMFArray object which manages its own memory.
|
||||
*
|
||||
* @param value The value to insert
|
||||
*
|
||||
* @return The inserted pointer, or nullptr should the key already be in use.
|
||||
*/
|
||||
template<typename ValueType>
|
||||
inline AMFValue<ValueType>* Push(ValueType value) {
|
||||
return Insert(this->dense.size(), value).first;
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes the key from the associative portion
|
||||
*
|
||||
* The pointer removed is now no longer managed by this container
|
||||
*
|
||||
* @param key The key to remove from the associative portion
|
||||
*/
|
||||
void Remove(const std::string& key, bool deleteValue = true) {
|
||||
AMFAssociative::iterator it = this->associative.find(key);
|
||||
if (it != this->associative.end()) {
|
||||
if (deleteValue) delete it->second;
|
||||
this->associative.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops the last element in the dense portion, deleting it in the process.
|
||||
*/
|
||||
void Remove(const uint32_t index) {
|
||||
if (!this->dense.empty() && index < this->dense.size()) {
|
||||
auto itr = this->dense.begin() + index;
|
||||
if (*itr) delete (*itr);
|
||||
this->dense.erase(itr);
|
||||
}
|
||||
}
|
||||
|
||||
void Pop() {
|
||||
if (!this->dense.empty()) Remove(this->dense.size() - 1);
|
||||
}
|
||||
|
||||
AMFArrayValue* GetArray(const std::string& key) {
|
||||
AMFAssociative::const_iterator it = this->associative.find(key);
|
||||
if (it != this->associative.end()) {
|
||||
return dynamic_cast<AMFArrayValue*>(it->second);
|
||||
}
|
||||
return nullptr;
|
||||
};
|
||||
|
||||
AMFArrayValue* GetArray(const uint32_t index) {
|
||||
return index >= this->dense.size() ? nullptr : dynamic_cast<AMFArrayValue*>(this->dense.at(index));
|
||||
};
|
||||
|
||||
inline AMFArrayValue* InsertArray(const std::string& key) {
|
||||
return static_cast<AMFArrayValue*>(Insert(key).first);
|
||||
};
|
||||
|
||||
inline AMFArrayValue* InsertArray(const uint32_t index) {
|
||||
return static_cast<AMFArrayValue*>(Insert(index).first);
|
||||
};
|
||||
|
||||
inline AMFArrayValue* PushArray() {
|
||||
return static_cast<AMFArrayValue*>(Insert(this->dense.size()).first);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets an AMFValue by the key from the associative portion and converts it
|
||||
* to the AmfValue template type. If the key did not exist, it is inserted.
|
||||
*
|
||||
* @tparam The target object type
|
||||
* @param key The key to lookup
|
||||
*
|
||||
* @return The AMFValue
|
||||
*/
|
||||
template <typename AmfType>
|
||||
AMFValue<AmfType>* Get(const std::string& key) const {
|
||||
AMFAssociative::const_iterator it = this->associative.find(key);
|
||||
return it != this->associative.end() ?
|
||||
dynamic_cast<AMFValue<AmfType>*>(it->second) :
|
||||
nullptr;
|
||||
};
|
||||
|
||||
// Get from the array but dont cast it
|
||||
AMFBaseValue* Get(const std::string& key) const {
|
||||
AMFAssociative::const_iterator it = this->associative.find(key);
|
||||
return it != this->associative.end() ? it->second : nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Get an AMFValue object at a position in the dense portion.
|
||||
* Gets an AMFValue by the index from the dense portion and converts it
|
||||
* to the AmfValue template type. If the index did not exist, it is inserted.
|
||||
*
|
||||
* @tparam The target object type
|
||||
* @param index The index to get
|
||||
* @return The casted object, or nullptr.
|
||||
*/
|
||||
template <typename AmfType>
|
||||
AMFValue<AmfType>* Get(uint32_t index) const {
|
||||
std::cout << (index < this->dense.size()) << std::endl;
|
||||
return index < this->dense.size() ?
|
||||
dynamic_cast<AMFValue<AmfType>*>(this->dense.at(index)) :
|
||||
nullptr;
|
||||
};
|
||||
|
||||
// Get from the dense but dont cast it
|
||||
AMFBaseValue* Get(const uint32_t index) const {
|
||||
return index < this->dense.size() ? this->dense.at(index) : nullptr;
|
||||
};
|
||||
private:
|
||||
/**
|
||||
* The associative portion. These values are key'd with strings to an AMFValue.
|
||||
*/
|
||||
AMFAssociative associative;
|
||||
|
||||
/**
|
||||
* The dense portion. These AMFValue's are stored one after
|
||||
* another with the most recent addition being at the back.
|
||||
*/
|
||||
AMFDense dense;
|
||||
};
|
||||
|
||||
#endif //!__AMF3__H__
|
184
dCommon/AmfSerialize.cpp
Normal file
184
dCommon/AmfSerialize.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
#include "AmfSerialize.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
|
||||
// Writes an AMFValue pointer to a RakNet::BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFBaseValue&>(AMFBaseValue& value) {
|
||||
eAmf type = value.GetValueType();
|
||||
this->Write(type);
|
||||
switch (type) {
|
||||
case eAmf::Integer: {
|
||||
this->Write<AMFIntValue&>(*static_cast<AMFIntValue*>(&value));
|
||||
break;
|
||||
}
|
||||
|
||||
case eAmf::Double: {
|
||||
this->Write<AMFDoubleValue&>(*static_cast<AMFDoubleValue*>(&value));
|
||||
break;
|
||||
}
|
||||
|
||||
case eAmf::String: {
|
||||
this->Write<AMFStringValue&>(*static_cast<AMFStringValue*>(&value));
|
||||
break;
|
||||
}
|
||||
|
||||
case eAmf::Array: {
|
||||
this->Write<AMFArrayValue&>(*static_cast<AMFArrayValue*>(&value));
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
Game::logger->Log("AmfSerialize", "Encountered unwritable AMFType %i!", type);
|
||||
}
|
||||
case eAmf::Undefined:
|
||||
case eAmf::Null:
|
||||
case eAmf::False:
|
||||
case eAmf::True:
|
||||
case eAmf::Date:
|
||||
case eAmf::Object:
|
||||
case eAmf::XML:
|
||||
case eAmf::XMLDoc:
|
||||
case eAmf::ByteArray:
|
||||
case eAmf::VectorInt:
|
||||
case eAmf::VectorUInt:
|
||||
case eAmf::VectorDouble:
|
||||
case eAmf::VectorObject:
|
||||
case eAmf::Dictionary:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A private function to write an value to a RakNet::BitStream
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteUInt29(RakNet::BitStream* bs, uint32_t v) {
|
||||
unsigned char b4 = (unsigned char)v;
|
||||
if (v < 0x00200000) {
|
||||
b4 = b4 & 0x7F;
|
||||
if (v > 0x7F) {
|
||||
unsigned char b3;
|
||||
v = v >> 7;
|
||||
b3 = ((unsigned char)(v)) | 0x80;
|
||||
if (v > 0x7F) {
|
||||
unsigned char b2;
|
||||
v = v >> 7;
|
||||
b2 = ((unsigned char)(v)) | 0x80;
|
||||
bs->Write(b2);
|
||||
}
|
||||
|
||||
bs->Write(b3);
|
||||
}
|
||||
} else {
|
||||
unsigned char b1;
|
||||
unsigned char b2;
|
||||
unsigned char b3;
|
||||
|
||||
v = v >> 8;
|
||||
b3 = ((unsigned char)(v)) | 0x80;
|
||||
v = v >> 7;
|
||||
b2 = ((unsigned char)(v)) | 0x80;
|
||||
v = v >> 7;
|
||||
b1 = ((unsigned char)(v)) | 0x80;
|
||||
|
||||
bs->Write(b1);
|
||||
bs->Write(b2);
|
||||
bs->Write(b3);
|
||||
}
|
||||
|
||||
bs->Write(b4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a flag number to a RakNet::BitStream
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteFlagNumber(RakNet::BitStream* bs, uint32_t v) {
|
||||
v = (v << 1) | 0x01;
|
||||
WriteUInt29(bs, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an AMFString to a RakNet::BitStream
|
||||
*
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteAMFString(RakNet::BitStream* bs, const std::string& str) {
|
||||
WriteFlagNumber(bs, (uint32_t)str.size());
|
||||
bs->Write(str.c_str(), (uint32_t)str.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an U16 to a bitstream
|
||||
*
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteAMFU16(RakNet::BitStream* bs, uint16_t value) {
|
||||
bs->Write(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an U32 to a bitstream
|
||||
*
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteAMFU32(RakNet::BitStream* bs, uint32_t value) {
|
||||
bs->Write(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an U64 to a bitstream
|
||||
*
|
||||
* RakNet writes in the correct byte order - do not reverse this.
|
||||
*/
|
||||
void WriteAMFU64(RakNet::BitStream* bs, uint64_t value) {
|
||||
bs->Write(value);
|
||||
}
|
||||
|
||||
// Writes an AMFIntegerValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFIntValue&>(AMFIntValue& value) {
|
||||
WriteUInt29(this, value.GetValue());
|
||||
}
|
||||
|
||||
// Writes an AMFDoubleValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFDoubleValue&>(AMFDoubleValue& value) {
|
||||
double d = value.GetValue();
|
||||
WriteAMFU64(this, *reinterpret_cast<uint64_t*>(&d));
|
||||
}
|
||||
|
||||
// Writes an AMFStringValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFStringValue&>(AMFStringValue& value) {
|
||||
WriteAMFString(this, value.GetValue());
|
||||
}
|
||||
|
||||
// Writes an AMFArrayValue to BitStream
|
||||
template<>
|
||||
void RakNet::BitStream::Write<AMFArrayValue&>(AMFArrayValue& value) {
|
||||
uint32_t denseSize = value.GetDense().size();
|
||||
WriteFlagNumber(this, denseSize);
|
||||
|
||||
auto it = value.GetAssociative().begin();
|
||||
auto end = value.GetAssociative().end();
|
||||
|
||||
while (it != end) {
|
||||
WriteAMFString(this, it->first);
|
||||
this->Write<AMFBaseValue&>(*it->second);
|
||||
it++;
|
||||
}
|
||||
|
||||
this->Write(eAmf::Null);
|
||||
|
||||
if (denseSize > 0) {
|
||||
auto it2 = value.GetDense().begin();
|
||||
auto end2 = value.GetDense().end();
|
||||
|
||||
while (it2 != end2) {
|
||||
this->Write<AMFBaseValue&>(**it2);
|
||||
it2++;
|
||||
}
|
||||
}
|
||||
}
|
50
dCommon/AmfSerialize.h
Normal file
50
dCommon/AmfSerialize.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
// Custom Classes
|
||||
#include "Amf3.h"
|
||||
|
||||
// RakNet
|
||||
#include <BitStream.h>
|
||||
|
||||
/*!
|
||||
\file AmfSerialize.h
|
||||
\brief A class that implements native writing of AMF values to RakNet::BitStream
|
||||
*/
|
||||
|
||||
// We are using the RakNet namespace
|
||||
namespace RakNet {
|
||||
//! Writes an AMFValue pointer to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFBaseValue&>(AMFBaseValue& value);
|
||||
|
||||
//! Writes an AMFIntegerValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFIntValue&>(AMFIntValue& value);
|
||||
|
||||
//! Writes an AMFDoubleValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFDoubleValue&>(AMFDoubleValue& value);
|
||||
|
||||
//! Writes an AMFStringValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFStringValue&>(AMFStringValue& value);
|
||||
|
||||
//! Writes an AMFArrayValue to a RakNet::BitStream
|
||||
/*!
|
||||
\param value The value to write
|
||||
*/
|
||||
template <>
|
||||
void RakNet::BitStream::Write<AMFArrayValue&>(AMFArrayValue& value);
|
||||
} // namespace RakNet
|
@@ -1,6 +1,6 @@
|
||||
set(DCOMMON_SOURCES "AMFFormat.cpp"
|
||||
set(DCOMMON_SOURCES
|
||||
"AMFDeserialize.cpp"
|
||||
"AMFFormat_BitStream.cpp"
|
||||
"AmfSerialize.cpp"
|
||||
"BinaryIO.cpp"
|
||||
"dConfig.cpp"
|
||||
"Diagnostics.cpp"
|
||||
|
Reference in New Issue
Block a user