2023-05-13 22:22:00 +00:00
|
|
|
#include "AmfSerialize.h"
|
|
|
|
|
|
|
|
#include "Game.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2023-05-13 22:22:00 +00:00
|
|
|
|
|
|
|
// 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: {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Encountered unwritable AMFType %i!", type);
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
2024-02-27 05:11:56 +00:00
|
|
|
void WriteUInt29(RakNet::BitStream& bs, uint32_t v) {
|
2023-12-28 04:18:20 +00:00
|
|
|
unsigned char b4 = static_cast<unsigned char>(v);
|
2023-05-13 22:22:00 +00:00
|
|
|
if (v < 0x00200000) {
|
|
|
|
b4 = b4 & 0x7F;
|
|
|
|
if (v > 0x7F) {
|
|
|
|
unsigned char b3;
|
|
|
|
v = v >> 7;
|
2023-12-28 04:18:20 +00:00
|
|
|
b3 = static_cast<unsigned char>(v) | 0x80;
|
2023-05-13 22:22:00 +00:00
|
|
|
if (v > 0x7F) {
|
|
|
|
unsigned char b2;
|
|
|
|
v = v >> 7;
|
2023-12-28 04:18:20 +00:00
|
|
|
b2 = static_cast<unsigned char>(v) | 0x80;
|
2024-02-27 05:11:56 +00:00
|
|
|
bs.Write(b2);
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 05:11:56 +00:00
|
|
|
bs.Write(b3);
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned char b1;
|
|
|
|
unsigned char b2;
|
|
|
|
unsigned char b3;
|
|
|
|
|
|
|
|
v = v >> 8;
|
2023-12-28 04:18:20 +00:00
|
|
|
b3 = static_cast<unsigned char>(v) | 0x80;
|
2023-05-13 22:22:00 +00:00
|
|
|
v = v >> 7;
|
2023-12-28 04:18:20 +00:00
|
|
|
b2 = static_cast<unsigned char>(v) | 0x80;
|
2023-05-13 22:22:00 +00:00
|
|
|
v = v >> 7;
|
2023-12-28 04:18:20 +00:00
|
|
|
b1 = static_cast<unsigned char>(v) | 0x80;
|
2023-05-13 22:22:00 +00:00
|
|
|
|
2024-02-27 05:11:56 +00:00
|
|
|
bs.Write(b1);
|
|
|
|
bs.Write(b2);
|
|
|
|
bs.Write(b3);
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 05:11:56 +00:00
|
|
|
bs.Write(b4);
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a flag number to a RakNet::BitStream
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2024-02-27 05:11:56 +00:00
|
|
|
void WriteFlagNumber(RakNet::BitStream& bs, uint32_t v) {
|
2023-05-13 22:22:00 +00:00
|
|
|
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.
|
|
|
|
*/
|
2024-02-27 05:11:56 +00:00
|
|
|
void WriteAMFString(RakNet::BitStream& bs, const std::string& str) {
|
2023-12-28 04:18:20 +00:00
|
|
|
WriteFlagNumber(bs, static_cast<uint32_t>(str.size()));
|
2024-02-27 05:11:56 +00:00
|
|
|
bs.Write(str.c_str(), static_cast<uint32_t>(str.size()));
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes an U16 to a bitstream
|
|
|
|
*
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2024-02-27 05:11:56 +00:00
|
|
|
void WriteAMFU16(RakNet::BitStream& bs, uint16_t value) {
|
|
|
|
bs.Write(value);
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes an U32 to a bitstream
|
|
|
|
*
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2024-02-27 05:11:56 +00:00
|
|
|
void WriteAMFU32(RakNet::BitStream& bs, uint32_t value) {
|
|
|
|
bs.Write(value);
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes an U64 to a bitstream
|
|
|
|
*
|
|
|
|
* RakNet writes in the correct byte order - do not reverse this.
|
|
|
|
*/
|
2024-02-27 05:11:56 +00:00
|
|
|
void WriteAMFU64(RakNet::BitStream& bs, uint64_t value) {
|
|
|
|
bs.Write(value);
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFIntegerValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFIntValue&>(AMFIntValue& value) {
|
2024-02-27 05:11:56 +00:00
|
|
|
WriteUInt29(*this, value.GetValue());
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFDoubleValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFDoubleValue&>(AMFDoubleValue& value) {
|
|
|
|
double d = value.GetValue();
|
2024-02-27 05:11:56 +00:00
|
|
|
WriteAMFU64(*this, *reinterpret_cast<uint64_t*>(&d));
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFStringValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFStringValue&>(AMFStringValue& value) {
|
2024-02-27 05:11:56 +00:00
|
|
|
WriteAMFString(*this, value.GetValue());
|
2023-05-13 22:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes an AMFArrayValue to BitStream
|
|
|
|
template<>
|
|
|
|
void RakNet::BitStream::Write<AMFArrayValue&>(AMFArrayValue& value) {
|
|
|
|
uint32_t denseSize = value.GetDense().size();
|
2024-02-27 05:11:56 +00:00
|
|
|
WriteFlagNumber(*this, denseSize);
|
2023-05-13 22:22:00 +00:00
|
|
|
|
|
|
|
auto it = value.GetAssociative().begin();
|
|
|
|
auto end = value.GetAssociative().end();
|
|
|
|
|
|
|
|
while (it != end) {
|
2024-02-27 05:11:56 +00:00
|
|
|
WriteAMFString(*this, it->first);
|
2023-05-13 22:22:00 +00:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|