Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Rewrite AMF and property behavior logic to use smart pointers, references, and string_views over raw pointers and std::string& #1452

Merged
merged 29 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fca5623
Rewrite AMF and behavior logic to use smart pointers, references, and…
jadebenn Feb 11, 2024
acd601f
fix m_BehaviorID initialization
jadebenn Feb 11, 2024
c75cef2
Fix BlockDefinition member naming
jadebenn Feb 11, 2024
07b5135
remove redundant reset()s
jadebenn Feb 11, 2024
7e036a3
Replace UB forward template declarations with header include
jadebenn Feb 11, 2024
08608c7
Merge remote-tracking branch 'upstream/main' into AMFUniquePtrs
jadebenn Feb 11, 2024
b8498c3
remove unneeded comment
jadebenn Feb 11, 2024
1bb82b8
remove non-const ref getters
jadebenn Feb 11, 2024
b89488b
simplify default behavior id initialization
jadebenn Feb 11, 2024
0f1922a
Fix invalidated use of Getter to set a value
jadebenn Feb 11, 2024
ab5ed7d
Update AddStripMessage.cpp - change push_back to emplace_back
jadebenn Feb 12, 2024
188462e
fix pointer to ref conversion mistake (should not have directly grabb…
jadebenn Feb 12, 2024
797eeb5
deref
jadebenn Feb 14, 2024
e381ef7
VERY experimental testing of forward declaration of templates - proba…
jadebenn Feb 18, 2024
b2b21a8
Revert changes (as expected)
jadebenn Feb 18, 2024
2798ee2
Merge remote-tracking branch 'upstream/main' into AMFUniquePtrs
jadebenn Feb 19, 2024
cee38f5
Update BlockDefinition.h - remove extraneous semicolons
jadebenn Feb 19, 2024
c9f2b31
Update BlockDefinition.h - remove linebreak
jadebenn Feb 19, 2024
f5e9bd8
Merge remote-tracking branch 'upstream/main' into AMFUniquePtrs
jadebenn Feb 25, 2024
2d7165a
Update Amf3.h member naming scheme
jadebenn Feb 25, 2024
9bb5089
Merge remote-tracking branch 'upstream/AMFClassNaming' into AMFUnique…
jadebenn Feb 25, 2024
7cfc10c
Merge remote-tracking branch 'upstream/main' into AMFUniquePtrs
jadebenn Feb 25, 2024
da613ad
fix duplicated code
jadebenn Feb 25, 2024
939eb61
const iterators
jadebenn Feb 25, 2024
9f8cff8
const pointers
jadebenn Feb 25, 2024
cf569f7
Merge remote-tracking branch 'upstream/main' into AMFUniquePtrs
jadebenn Feb 27, 2024
ec7d0bf
Merge branch 'main' into AMFUniquePtrs
jadebenn Nov 18, 2024
42d3c73
reviving this branch
jadebenn Nov 18, 2024
2f97e8b
update read switch cases
jadebenn Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ cpp_space_around_assignment_operator=insert
cpp_space_pointer_reference_alignment=left
cpp_space_around_ternary_operator=insert
cpp_wrap_preserve_blocks=one_liners
cpp_indent_comment=fasle
cpp_indent_comment=false
89 changes: 35 additions & 54 deletions dCommon/AMFDeserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,54 @@
* AMF3 Deserializer written by EmosewaMC
*/

AMFBaseValue* AMFDeserialize::Read(RakNet::BitStream& inStream) {
AMFBaseValue* returnValue = nullptr;
std::unique_ptr<AMFBaseValue> AMFDeserialize::Read(RakNet::BitStream& inStream) {
// Read in the value type from the bitStream
eAmf marker;
inStream.Read(marker);
// Based on the typing, create the value associated with that and return the base value class
switch (marker) {
case eAmf::Undefined: {
returnValue = new AMFBaseValue();
break;
}

case eAmf::Null: {
returnValue = new AMFNullValue();
break;
}

case eAmf::False: {
returnValue = new AMFBoolValue(false);
break;
}

case eAmf::True: {
returnValue = new AMFBoolValue(true);
break;
}

case eAmf::Integer: {
returnValue = ReadAmfInteger(inStream);
break;
}

case eAmf::Double: {
returnValue = ReadAmfDouble(inStream);
break;
}

case eAmf::String: {
returnValue = ReadAmfString(inStream);
break;
}

case eAmf::Array: {
returnValue = ReadAmfArray(inStream);
break;
}
case eAmf::Undefined:
return std::make_unique<AMFBaseValue>();
case eAmf::Null:
return std::make_unique<AMFNullValue>();
case eAmf::False:
return std::make_unique<AMFBoolValue>(false);
case eAmf::True:
return std::make_unique<AMFBoolValue>(true);
case eAmf::Integer:
return ReadAmfInteger(inStream);
case eAmf::Double:
return ReadAmfDouble(inStream);
case eAmf::String:
return ReadAmfString(inStream);
case eAmf::Array:
return ReadAmfArray(inStream);

// 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:
[[fallthrough]];
case eAmf::Date:
[[fallthrough]];
case eAmf::Object:
[[fallthrough]];
case eAmf::XML:
[[fallthrough]];
case eAmf::ByteArray:
[[fallthrough]];
case eAmf::VectorInt:
[[fallthrough]];
case eAmf::VectorUInt:
[[fallthrough]];
case eAmf::VectorDouble:
[[fallthrough]];
case eAmf::VectorObject:
case eAmf::Dictionary: {
[[fallthrough]];
case eAmf::Dictionary:
throw marker;
break;
}
default:
throw std::invalid_argument("Invalid AMF3 marker" + std::to_string(static_cast<int32_t>(marker)));
break;
}
return returnValue;
}

uint32_t AMFDeserialize::ReadU29(RakNet::BitStream& inStream) {
Expand Down Expand Up @@ -118,14 +99,14 @@ const std::string AMFDeserialize::ReadString(RakNet::BitStream& inStream) {
}
}

AMFBaseValue* AMFDeserialize::ReadAmfDouble(RakNet::BitStream& inStream) {
std::unique_ptr<AMFDoubleValue> AMFDeserialize::ReadAmfDouble(RakNet::BitStream& inStream) {
double value;
inStream.Read<double>(value);
return new AMFDoubleValue(value);
return std::make_unique<AMFDoubleValue>(value);
}

AMFBaseValue* AMFDeserialize::ReadAmfArray(RakNet::BitStream& inStream) {
auto arrayValue = new AMFArrayValue();
std::unique_ptr<AMFArrayValue> AMFDeserialize::ReadAmfArray(RakNet::BitStream& inStream) {
auto arrayValue = std::make_unique<AMFArrayValue>();

// Read size of dense array
const auto sizeOfDenseArray = (ReadU29(inStream) >> 1);
Expand All @@ -143,10 +124,10 @@ AMFBaseValue* AMFDeserialize::ReadAmfArray(RakNet::BitStream& inStream) {
return arrayValue;
}

AMFBaseValue* AMFDeserialize::ReadAmfString(RakNet::BitStream& inStream) {
return new AMFStringValue(ReadString(inStream));
std::unique_ptr<AMFStringValue> AMFDeserialize::ReadAmfString(RakNet::BitStream& inStream) {
return std::make_unique<AMFStringValue>(ReadString(inStream));
}

AMFBaseValue* AMFDeserialize::ReadAmfInteger(RakNet::BitStream& inStream) {
return new AMFIntValue(ReadU29(inStream));
std::unique_ptr<AMFIntValue> AMFDeserialize::ReadAmfInteger(RakNet::BitStream& inStream) {
return std::make_unique<AMFIntValue>(ReadU29(inStream)); // NOTE: NARROWING CONVERSION FROM UINT TO INT. IS THIS INTENDED?
}
14 changes: 7 additions & 7 deletions dCommon/AMFDeserialize.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "Amf3.h"
#include "BitStream.h"

#include <memory>
#include <vector>
#include <string>

class AMFBaseValue;

class AMFDeserialize {
public:
/**
Expand All @@ -15,7 +15,7 @@ class AMFDeserialize {
* @param inStream inStream to read value from.
* @return Returns an AMFValue with all the information from the bitStream in it.
*/
AMFBaseValue* Read(RakNet::BitStream& inStream);
std::unique_ptr<AMFBaseValue> Read(RakNet::BitStream& inStream);
private:
/**
* @brief Private method to read a U29 integer from a bitstream
Expand All @@ -39,31 +39,31 @@ class AMFDeserialize {
* @param inStream bitStream to read data from
* @return Double value represented as an AMFValue
*/
AMFBaseValue* ReadAmfDouble(RakNet::BitStream& inStream);
static std::unique_ptr<AMFDoubleValue> ReadAmfDouble(RakNet::BitStream& inStream);

/**
* @brief Read an AMFArray from a bitStream
*
* @param inStream bitStream to read data from
* @return Array value represented as an AMFValue
*/
AMFBaseValue* ReadAmfArray(RakNet::BitStream& inStream);
std::unique_ptr<AMFArrayValue> ReadAmfArray(RakNet::BitStream& inStream);

/**
* @brief Read an AMFString from a bitStream
*
* @param inStream bitStream to read data from
* @return String value represented as an AMFValue
*/
AMFBaseValue* ReadAmfString(RakNet::BitStream& inStream);
std::unique_ptr<AMFStringValue> ReadAmfString(RakNet::BitStream& inStream);

/**
* @brief Read an AMFInteger from a bitStream
*
* @param inStream bitStream to read data from
* @return Integer value represented as an AMFValue
*/
AMFBaseValue* ReadAmfInteger(RakNet::BitStream& inStream);
static std::unique_ptr<AMFIntValue> ReadAmfInteger(RakNet::BitStream& inStream);

/**
* List of strings read so far saved to be read by reference.
Expand Down
Loading
Loading