Skip to content

Commit 53877a0

Browse files
authored
refactor: Rewrite AMF and property behavior logic to use smart pointers, references, and string_views over raw pointers and std::string& (#1452)
* Rewrite AMF and behavior logic to use smart pointers, references, and string_views over raw pointers and std::string& * fix m_BehaviorID initialization * Fix BlockDefinition member naming * remove redundant reset()s * Replace UB forward template declarations with header include * remove unneeded comment * remove non-const ref getters * simplify default behavior id initialization * Fix invalidated use of Getter to set a value * Update AddStripMessage.cpp - change push_back to emplace_back * fix pointer to ref conversion mistake (should not have directly grabbed from the other branch commit) * deref * VERY experimental testing of forward declaration of templates - probably will revert * Revert changes (as expected) * Update BlockDefinition.h - remove extraneous semicolons * Update BlockDefinition.h - remove linebreak * Update Amf3.h member naming scheme * fix duplicated code * const iterators * const pointers * reviving this branch * update read switch cases
1 parent 83f8646 commit 53877a0

25 files changed

+200
-181
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ cpp_space_around_assignment_operator=insert
7373
cpp_space_pointer_reference_alignment=left
7474
cpp_space_around_ternary_operator=insert
7575
cpp_wrap_preserve_blocks=one_liners
76-
cpp_indent_comment=fasle
76+
cpp_indent_comment=false

dCommon/AMFDeserialize.cpp

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,54 @@
99
* AMF3 Deserializer written by EmosewaMC
1010
*/
1111

12-
AMFBaseValue* AMFDeserialize::Read(RakNet::BitStream& inStream) {
13-
AMFBaseValue* returnValue = nullptr;
12+
std::unique_ptr<AMFBaseValue> AMFDeserialize::Read(RakNet::BitStream& inStream) {
1413
// Read in the value type from the bitStream
1514
eAmf marker;
1615
inStream.Read(marker);
1716
// Based on the typing, create the value associated with that and return the base value class
1817
switch (marker) {
19-
case eAmf::Undefined: {
20-
returnValue = new AMFBaseValue();
21-
break;
22-
}
23-
24-
case eAmf::Null: {
25-
returnValue = new AMFNullValue();
26-
break;
27-
}
28-
29-
case eAmf::False: {
30-
returnValue = new AMFBoolValue(false);
31-
break;
32-
}
33-
34-
case eAmf::True: {
35-
returnValue = new AMFBoolValue(true);
36-
break;
37-
}
38-
39-
case eAmf::Integer: {
40-
returnValue = ReadAmfInteger(inStream);
41-
break;
42-
}
43-
44-
case eAmf::Double: {
45-
returnValue = ReadAmfDouble(inStream);
46-
break;
47-
}
48-
49-
case eAmf::String: {
50-
returnValue = ReadAmfString(inStream);
51-
break;
52-
}
53-
54-
case eAmf::Array: {
55-
returnValue = ReadAmfArray(inStream);
56-
break;
57-
}
18+
case eAmf::Undefined:
19+
return std::make_unique<AMFBaseValue>();
20+
case eAmf::Null:
21+
return std::make_unique<AMFNullValue>();
22+
case eAmf::False:
23+
return std::make_unique<AMFBoolValue>(false);
24+
case eAmf::True:
25+
return std::make_unique<AMFBoolValue>(true);
26+
case eAmf::Integer:
27+
return ReadAmfInteger(inStream);
28+
case eAmf::Double:
29+
return ReadAmfDouble(inStream);
30+
case eAmf::String:
31+
return ReadAmfString(inStream);
32+
case eAmf::Array:
33+
return ReadAmfArray(inStream);
5834

5935
// These values are unimplemented in the live client and will remain unimplemented
6036
// unless someone modifies the client to allow serializing of these values.
6137
case eAmf::XMLDoc:
38+
[[fallthrough]];
6239
case eAmf::Date:
40+
[[fallthrough]];
6341
case eAmf::Object:
42+
[[fallthrough]];
6443
case eAmf::XML:
44+
[[fallthrough]];
6545
case eAmf::ByteArray:
46+
[[fallthrough]];
6647
case eAmf::VectorInt:
48+
[[fallthrough]];
6749
case eAmf::VectorUInt:
50+
[[fallthrough]];
6851
case eAmf::VectorDouble:
52+
[[fallthrough]];
6953
case eAmf::VectorObject:
70-
case eAmf::Dictionary: {
54+
[[fallthrough]];
55+
case eAmf::Dictionary:
7156
throw marker;
72-
break;
73-
}
7457
default:
7558
throw std::invalid_argument("Invalid AMF3 marker" + std::to_string(static_cast<int32_t>(marker)));
76-
break;
7759
}
78-
return returnValue;
7960
}
8061

8162
uint32_t AMFDeserialize::ReadU29(RakNet::BitStream& inStream) {
@@ -118,14 +99,14 @@ const std::string AMFDeserialize::ReadString(RakNet::BitStream& inStream) {
11899
}
119100
}
120101

121-
AMFBaseValue* AMFDeserialize::ReadAmfDouble(RakNet::BitStream& inStream) {
102+
std::unique_ptr<AMFDoubleValue> AMFDeserialize::ReadAmfDouble(RakNet::BitStream& inStream) {
122103
double value;
123104
inStream.Read<double>(value);
124-
return new AMFDoubleValue(value);
105+
return std::make_unique<AMFDoubleValue>(value);
125106
}
126107

127-
AMFBaseValue* AMFDeserialize::ReadAmfArray(RakNet::BitStream& inStream) {
128-
auto arrayValue = new AMFArrayValue();
108+
std::unique_ptr<AMFArrayValue> AMFDeserialize::ReadAmfArray(RakNet::BitStream& inStream) {
109+
auto arrayValue = std::make_unique<AMFArrayValue>();
129110

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

146-
AMFBaseValue* AMFDeserialize::ReadAmfString(RakNet::BitStream& inStream) {
147-
return new AMFStringValue(ReadString(inStream));
127+
std::unique_ptr<AMFStringValue> AMFDeserialize::ReadAmfString(RakNet::BitStream& inStream) {
128+
return std::make_unique<AMFStringValue>(ReadString(inStream));
148129
}
149130

150-
AMFBaseValue* AMFDeserialize::ReadAmfInteger(RakNet::BitStream& inStream) {
151-
return new AMFIntValue(ReadU29(inStream));
131+
std::unique_ptr<AMFIntValue> AMFDeserialize::ReadAmfInteger(RakNet::BitStream& inStream) {
132+
return std::make_unique<AMFIntValue>(ReadU29(inStream)); // NOTE: NARROWING CONVERSION FROM UINT TO INT. IS THIS INTENDED?
152133
}

dCommon/AMFDeserialize.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#pragma once
22

3+
#include "Amf3.h"
34
#include "BitStream.h"
45

6+
#include <memory>
57
#include <vector>
68
#include <string>
79

8-
class AMFBaseValue;
9-
1010
class AMFDeserialize {
1111
public:
1212
/**
@@ -15,7 +15,7 @@ class AMFDeserialize {
1515
* @param inStream inStream to read value from.
1616
* @return Returns an AMFValue with all the information from the bitStream in it.
1717
*/
18-
AMFBaseValue* Read(RakNet::BitStream& inStream);
18+
std::unique_ptr<AMFBaseValue> Read(RakNet::BitStream& inStream);
1919
private:
2020
/**
2121
* @brief Private method to read a U29 integer from a bitstream
@@ -39,31 +39,31 @@ class AMFDeserialize {
3939
* @param inStream bitStream to read data from
4040
* @return Double value represented as an AMFValue
4141
*/
42-
AMFBaseValue* ReadAmfDouble(RakNet::BitStream& inStream);
42+
static std::unique_ptr<AMFDoubleValue> ReadAmfDouble(RakNet::BitStream& inStream);
4343

4444
/**
4545
* @brief Read an AMFArray from a bitStream
4646
*
4747
* @param inStream bitStream to read data from
4848
* @return Array value represented as an AMFValue
4949
*/
50-
AMFBaseValue* ReadAmfArray(RakNet::BitStream& inStream);
50+
std::unique_ptr<AMFArrayValue> ReadAmfArray(RakNet::BitStream& inStream);
5151

5252
/**
5353
* @brief Read an AMFString from a bitStream
5454
*
5555
* @param inStream bitStream to read data from
5656
* @return String value represented as an AMFValue
5757
*/
58-
AMFBaseValue* ReadAmfString(RakNet::BitStream& inStream);
58+
std::unique_ptr<AMFStringValue> ReadAmfString(RakNet::BitStream& inStream);
5959

6060
/**
6161
* @brief Read an AMFInteger from a bitStream
6262
*
6363
* @param inStream bitStream to read data from
6464
* @return Integer value represented as an AMFValue
6565
*/
66-
AMFBaseValue* ReadAmfInteger(RakNet::BitStream& inStream);
66+
static std::unique_ptr<AMFIntValue> ReadAmfInteger(RakNet::BitStream& inStream);
6767

6868
/**
6969
* List of strings read so far saved to be read by reference.

0 commit comments

Comments
 (0)