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

chore: Change AMF to use references #1479

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions dCommon/AMFDeserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ AMFBaseValue* AMFDeserialize::ReadAmfArray(RakNet::BitStream& inStream) {
auto arrayValue = new AMFArrayValue();

// Read size of dense array
auto sizeOfDenseArray = (ReadU29(inStream) >> 1);
const auto sizeOfDenseArray = (ReadU29(inStream) >> 1);
// Then read associative portion
while (true) {
auto key = ReadString(inStream);
const auto key = ReadString(inStream);
// No more associative values when we encounter an empty string key
if (key.size() == 0) break;
arrayValue->Insert(key, Read(inStream));
Expand Down
2 changes: 1 addition & 1 deletion dCommon/AMFDeserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AMFDeserialize {
* @param inStream bitstream to read data from
* @return The number as an unsigned 29 bit integer
*/
uint32_t ReadU29(RakNet::BitStream& inStream);
static uint32_t ReadU29(RakNet::BitStream& inStream);

/**
* @brief Reads a string from a bitstream
Expand Down
1 change: 1 addition & 0 deletions dCommon/Amf3.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AMFValue : public AMFBaseValue {
public:
AMFValue() = default;
AMFValue(const ValueType value) : m_Data{ value } {}

virtual ~AMFValue() override = default;

[[nodiscard]] constexpr eAmf GetValueType() const noexcept override;
Expand Down
2 changes: 1 addition & 1 deletion dGame/dComponents/ModelComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ModelComponent final : public Component {
* @param args the arguments of the message to be deserialized
*/
template<typename Msg>
void HandleControlBehaviorsMsg(AMFArrayValue* args) {
void HandleControlBehaviorsMsg(const AMFArrayValue& args) {
static_assert(std::is_base_of_v<BehaviorMessageBase, Msg>, "Msg must be a BehaviorMessageBase");
Msg msg(args);
for (auto& behavior : m_Behaviors) {
Expand Down
9 changes: 5 additions & 4 deletions dGame/dGameMessages/GameMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2489,23 +2489,24 @@ void GameMessages::SendUnSmash(Entity* entity, LWOOBJID builderID, float duratio

void GameMessages::HandleControlBehaviors(RakNet::BitStream& inStream, Entity* entity, const SystemAddress& sysAddr) {
AMFDeserialize reader;
std::unique_ptr<AMFBaseValue> amfArguments(reader.Read(inStream));
std::unique_ptr<AMFArrayValue> amfArguments{ static_cast<AMFArrayValue*>(reader.Read(inStream)) };
if (amfArguments->GetValueType() != eAmf::Array) return;

uint32_t commandLength{};
inStream.Read(commandLength);

std::string command;
for (uint32_t i = 0; i < commandLength; i++) {
command.reserve(commandLength);
for (uint32_t i = 0; i < commandLength; ++i) {
unsigned char character;
inStream.Read(character);
command.push_back(character);
}

auto owner = PropertyManagementComponent::Instance()->GetOwner();
auto* const owner = PropertyManagementComponent::Instance()->GetOwner();
if (!owner) return;

ControlBehaviors::Instance().ProcessCommand(entity, static_cast<AMFArrayValue*>(amfArguments.get()), command, owner);
ControlBehaviors::Instance().ProcessCommand(entity, *amfArguments, command, owner);
}

void GameMessages::HandleBBBSaveRequest(RakNet::BitStream& inStream, Entity* entity, const SystemAddress& sysAddr) {
Expand Down
4 changes: 2 additions & 2 deletions dGame/dPropertyBehaviors/ControlBehaviorMessages/Action.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Action.h"
#include "Amf3.h"

Action::Action(const AMFArrayValue* arguments) {
for (const auto& [paramName, paramValue] : arguments->GetAssociative()) {
Action::Action(const AMFArrayValue& arguments) {
for (const auto& [paramName, paramValue] : arguments.GetAssociative()) {
if (paramName == "Type") {
if (paramValue->GetValueType() != eAmf::String) continue;
m_Type = static_cast<AMFStringValue*>(paramValue)->GetValue();
Expand Down
2 changes: 1 addition & 1 deletion dGame/dPropertyBehaviors/ControlBehaviorMessages/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AMFArrayValue;
class Action {
public:
Action() = default;
Action(const AMFArrayValue* arguments);
Action(const AMFArrayValue& arguments);
[[nodiscard]] const std::string& GetType() const { return m_Type; };
[[nodiscard]] const std::string& GetValueParameterName() const { return m_ValueParameterName; };
[[nodiscard]] const std::string& GetValueParameterString() const { return m_ValueParameterString; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

#include "Amf3.h"

ActionContext::ActionContext(const AMFArrayValue* arguments, const std::string& customStateKey, const std::string& customStripKey)
ActionContext::ActionContext(const AMFArrayValue& arguments, const std::string& customStateKey, const std::string& customStripKey)
: m_StripId{ GetStripIdFromArgument(arguments, customStripKey) }
, m_StateId{ GetBehaviorStateFromArgument(arguments, customStateKey) } {
}

BehaviorState ActionContext::GetBehaviorStateFromArgument(const AMFArrayValue* arguments, const std::string& key) const {
const auto* const stateIDValue = arguments->Get<double>(key);
BehaviorState ActionContext::GetBehaviorStateFromArgument(const AMFArrayValue& arguments, const std::string& key) const {
const auto* const stateIDValue = arguments.Get<double>(key);
if (!stateIDValue) throw std::invalid_argument("Unable to find behavior state from argument \"" + key + "\"");

return static_cast<BehaviorState>(stateIDValue->GetValue());
}

StripId ActionContext::GetStripIdFromArgument(const AMFArrayValue* arguments, const std::string& key) const {
const auto* const stripIdValue = arguments->Get<double>(key);
StripId ActionContext::GetStripIdFromArgument(const AMFArrayValue& arguments, const std::string& key) const {
const auto* const stripIdValue = arguments.Get<double>(key);
if (!stripIdValue) throw std::invalid_argument("Unable to find strip ID from argument \"" + key + "\"");

return static_cast<StripId>(stripIdValue->GetValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class AMFArrayValue;
class ActionContext {
public:
ActionContext() noexcept = default;
ActionContext(const AMFArrayValue* arguments, const std::string& customStateKey = "stateID", const std::string& customStripKey = "stripID");
ActionContext(const AMFArrayValue& arguments, const std::string& customStateKey = "stateID", const std::string& customStripKey = "stripID");
[[nodiscard]] StripId GetStripId() const noexcept { return m_StripId; };
[[nodiscard]] BehaviorState GetStateId() const noexcept { return m_StateId; };

private:
[[nodiscard]] BehaviorState GetBehaviorStateFromArgument(const AMFArrayValue* arguments, const std::string& key) const;
[[nodiscard]] StripId GetStripIdFromArgument(const AMFArrayValue* arguments, const std::string& key) const;
[[nodiscard]] BehaviorState GetBehaviorStateFromArgument(const AMFArrayValue& arguments, const std::string& key) const;
[[nodiscard]] StripId GetStripIdFromArgument(const AMFArrayValue& arguments, const std::string& key) const;
StripId m_StripId{ 0 };
BehaviorState m_StateId{ BehaviorState::HOME_STATE };
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "AddActionMessage.h"

AddActionMessage::AddActionMessage(const AMFArrayValue* arguments)
AddActionMessage::AddActionMessage(const AMFArrayValue& arguments)
: BehaviorMessageBase{ arguments }
, m_ActionIndex{ GetActionIndexFromArgument(arguments) }
, m_ActionContext{ arguments } {

const auto* const actionValue = arguments->GetArray("action");
const auto* const actionValue = arguments.GetArray("action");
if (!actionValue) return;

m_Action = Action{ actionValue };
m_Action = Action{ *actionValue };

LOG_DEBUG("actionIndex %i stripId %i stateId %i type %s valueParameterName %s valueParameterString %s valueParameterDouble %f m_BehaviorId %i", m_ActionIndex, m_ActionContext.GetStripId(), m_ActionContext.GetStateId(), m_Action.GetType().c_str(), m_Action.GetValueParameterName().c_str(), m_Action.GetValueParameterString().c_str(), m_Action.GetValueParameterDouble(), m_BehaviorId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AMFArrayValue;
*/
class AddActionMessage : public BehaviorMessageBase {
public:
AddActionMessage(const AMFArrayValue* arguments);
AddActionMessage(const AMFArrayValue& arguments);

[[nodiscard]] int32_t GetActionIndex() const noexcept { return m_ActionIndex; };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "AddMessage.h"

AddMessage::AddMessage(const AMFArrayValue* arguments) : BehaviorMessageBase{ arguments } {
const auto* const behaviorIndexValue = arguments->Get<double>("BehaviorIndex");
AddMessage::AddMessage(const AMFArrayValue& arguments) : BehaviorMessageBase{ arguments } {
const auto* const behaviorIndexValue = arguments.Get<double>("BehaviorIndex");
if (!behaviorIndexValue) return;

m_BehaviorIndex = static_cast<uint32_t>(behaviorIndexValue->GetValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class AddMessage : public BehaviorMessageBase {
public:
AddMessage(const AMFArrayValue* arguments);
AddMessage(const AMFArrayValue& arguments);
[[nodiscard]] uint32_t GetBehaviorIndex() const noexcept { return m_BehaviorIndex; };

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

#include "Action.h"

AddStripMessage::AddStripMessage(const AMFArrayValue* arguments)
AddStripMessage::AddStripMessage(const AMFArrayValue& arguments)
: BehaviorMessageBase{ arguments }
, m_Position{ arguments }
, m_ActionContext{ arguments } {

const auto* const strip = arguments->GetArray("strip");
const auto* const strip = arguments.GetArray("strip");
if (!strip) return;

const auto* const actions = strip->GetArray("actions");
if (!actions) return;

for (uint32_t actionNumber = 0; actionNumber < actions->GetDense().size(); actionNumber++) {
for (size_t actionNumber = 0; actionNumber < actions->GetDense().size(); ++actionNumber) {
const auto* const actionValue = actions->GetArray(actionNumber);
if (!actionValue) continue;

m_ActionsToAdd.emplace_back(actionValue);
m_ActionsToAdd.emplace_back(*actionValue);

LOG_DEBUG("xPosition %f yPosition %f stripId %i stateId %i behaviorId %i t %s valueParameterName %s valueParameterString %s valueParameterDouble %f", m_Position.GetX(), m_Position.GetY(), m_ActionContext.GetStripId(), m_ActionContext.GetStateId(), m_BehaviorId, m_ActionsToAdd.back().GetType().c_str(), m_ActionsToAdd.back().GetValueParameterName().c_str(), m_ActionsToAdd.back().GetValueParameterString().c_str(), m_ActionsToAdd.back().GetValueParameterDouble());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AMFArrayValue;
*/
class AddStripMessage : public BehaviorMessageBase {
public:
AddStripMessage(const AMFArrayValue* arguments);
AddStripMessage(const AMFArrayValue& arguments);

[[nodiscard]] const StripUiPosition& GetPosition() const noexcept { return m_Position; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
#include "BehaviorStates.h"
#include "dCommonVars.h"

int32_t BehaviorMessageBase::GetBehaviorIdFromArgument(const AMFArrayValue* arguments) {
int32_t BehaviorMessageBase::GetBehaviorIdFromArgument(const AMFArrayValue& arguments) {
static constexpr const char* key = "BehaviorID";
const auto* const behaviorIDValue = arguments->Get<std::string>(key);
const auto* const behaviorIDValue = arguments.Get<std::string>(key);
int32_t behaviorId = DefaultBehaviorId;

if (behaviorIDValue && behaviorIDValue->GetValueType() == eAmf::String) {
behaviorId =
GeneralUtils::TryParse<int32_t>(behaviorIDValue->GetValue()).value_or(behaviorId);
} else if (arguments->Get(key) && arguments->Get(key)->GetValueType() != eAmf::Undefined) {
} else if (arguments.Get(key) && arguments.Get(key)->GetValueType() != eAmf::Undefined) {
throw std::invalid_argument("Unable to find behavior ID");
}

return behaviorId;
}

int32_t BehaviorMessageBase::GetActionIndexFromArgument(const AMFArrayValue* arguments, const std::string& keyName) const {
const auto* const actionIndexAmf = arguments->Get<double>(keyName);
int32_t BehaviorMessageBase::GetActionIndexFromArgument(const AMFArrayValue& arguments, const std::string& keyName) const {
const auto* const actionIndexAmf = arguments.Get<double>(keyName);
if (!actionIndexAmf) throw std::invalid_argument("Unable to find actionIndex");

return static_cast<int32_t>(actionIndexAmf->GetValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ enum class BehaviorState : uint32_t;
class BehaviorMessageBase {
public:
static constexpr int32_t DefaultBehaviorId{ -1 };
BehaviorMessageBase(const AMFArrayValue* arguments) : m_BehaviorId{ GetBehaviorIdFromArgument(arguments) } {}
BehaviorMessageBase(const AMFArrayValue& arguments) : m_BehaviorId{ GetBehaviorIdFromArgument(arguments) } {}
[[nodiscard]] int32_t GetBehaviorId() const noexcept { return m_BehaviorId; }
[[nodiscard]] bool IsDefaultBehaviorId() const noexcept { return m_BehaviorId == DefaultBehaviorId; }

protected:
[[nodiscard]] int32_t GetBehaviorIdFromArgument(const AMFArrayValue* arguments);
[[nodiscard]] int32_t GetActionIndexFromArgument(const AMFArrayValue* arguments, const std::string& keyName = "actionIndex") const;
[[nodiscard]] int32_t GetBehaviorIdFromArgument(const AMFArrayValue& arguments);
[[nodiscard]] int32_t GetActionIndexFromArgument(const AMFArrayValue& arguments, const std::string& keyName = "actionIndex") const;
int32_t m_BehaviorId{ DefaultBehaviorId };
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "MergeStripsMessage.h"

MergeStripsMessage::MergeStripsMessage(const AMFArrayValue* arguments)
MergeStripsMessage::MergeStripsMessage(const AMFArrayValue& arguments)
: BehaviorMessageBase{ arguments }
, m_DstActionIndex{ GetActionIndexFromArgument(arguments, "dstActionIndex") }
, m_SourceActionContext{ arguments, "srcStateID", "srcStripID" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AMFArrayValue;
*/
class MergeStripsMessage : public BehaviorMessageBase {
public:
MergeStripsMessage(const AMFArrayValue* arguments);
MergeStripsMessage(const AMFArrayValue& arguments);

[[nodiscard]] int32_t GetDstActionIndex() const noexcept { return m_DstActionIndex; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "MigrateActionsMessage.h"

MigrateActionsMessage::MigrateActionsMessage(const AMFArrayValue* arguments)
MigrateActionsMessage::MigrateActionsMessage(const AMFArrayValue& arguments)
: BehaviorMessageBase{ arguments }
, m_SrcActionIndex{ GetActionIndexFromArgument(arguments, "srcActionIndex") }
, m_DstActionIndex{ GetActionIndexFromArgument(arguments, "dstActionIndex") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AMFArrayValue;
*/
class MigrateActionsMessage : public BehaviorMessageBase {
public:
MigrateActionsMessage(const AMFArrayValue* arguments);
MigrateActionsMessage(const AMFArrayValue& arguments);

[[nodiscard]] int32_t GetSrcActionIndex() const noexcept { return m_SrcActionIndex; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "MoveToInventoryMessage.h"

MoveToInventoryMessage::MoveToInventoryMessage(const AMFArrayValue* arguments) : BehaviorMessageBase{ arguments } {
const auto* const behaviorIndexValue = arguments->Get<double>("BehaviorIndex");
MoveToInventoryMessage::MoveToInventoryMessage(const AMFArrayValue& arguments) : BehaviorMessageBase{ arguments } {
const auto* const behaviorIndexValue = arguments.Get<double>("BehaviorIndex");
if (!behaviorIndexValue) return;

m_BehaviorIndex = static_cast<uint32_t>(behaviorIndexValue->GetValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AMFArrayValue;
#pragma warning("This Control Behavior Message does not have a test yet. Non-developers can ignore this warning.")
class MoveToInventoryMessage : public BehaviorMessageBase {
public:
MoveToInventoryMessage(const AMFArrayValue* arguments);
MoveToInventoryMessage(const AMFArrayValue& arguments);
[[nodiscard]] uint32_t GetBehaviorIndex() const noexcept { return m_BehaviorIndex; };

private:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "RearrangeStripMessage.h"

RearrangeStripMessage::RearrangeStripMessage(const AMFArrayValue* arguments)
RearrangeStripMessage::RearrangeStripMessage(const AMFArrayValue& arguments)
: BehaviorMessageBase{ arguments }
, m_SrcActionIndex{ GetActionIndexFromArgument(arguments, "srcActionIndex") }
, m_DstActionIndex{ GetActionIndexFromArgument(arguments, "dstActionIndex") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class RearrangeStripMessage : public BehaviorMessageBase {
public:
RearrangeStripMessage(const AMFArrayValue* arguments);
RearrangeStripMessage(const AMFArrayValue& arguments);

[[nodiscard]] int32_t GetSrcActionIndex() const noexcept { return m_SrcActionIndex; }
[[nodiscard]] int32_t GetDstActionIndex() const noexcept { return m_DstActionIndex; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "RemoveActionsMessage.h"

RemoveActionsMessage::RemoveActionsMessage(const AMFArrayValue* arguments)
RemoveActionsMessage::RemoveActionsMessage(const AMFArrayValue& arguments)
: BehaviorMessageBase{ arguments }
, m_ActionIndex{ GetActionIndexFromArgument(arguments) }
, m_ActionContext{ arguments } {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AMFArrayValue;
*/
class RemoveActionsMessage : public BehaviorMessageBase {
public:
RemoveActionsMessage(const AMFArrayValue* arguments);
RemoveActionsMessage(const AMFArrayValue& arguments);

[[nodiscard]] int32_t GetActionIndex() const noexcept { return m_ActionIndex; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "RemoveStripMessage.h"

RemoveStripMessage::RemoveStripMessage(const AMFArrayValue* arguments)
RemoveStripMessage::RemoveStripMessage(const AMFArrayValue& arguments)
: BehaviorMessageBase{ arguments }
, m_ActionContext{ arguments } {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class RemoveStripMessage : public BehaviorMessageBase {
public:
RemoveStripMessage(const AMFArrayValue* arguments);
RemoveStripMessage(const AMFArrayValue& arguments);

const ActionContext& GetActionContext() const noexcept { return m_ActionContext; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "RenameMessage.h"

RenameMessage::RenameMessage(const AMFArrayValue* arguments) : BehaviorMessageBase{ arguments } {
const auto* const nameAmf = arguments->Get<std::string>("Name");
RenameMessage::RenameMessage(const AMFArrayValue& arguments) : BehaviorMessageBase{ arguments } {
const auto* const nameAmf = arguments.Get<std::string>("Name");
if (!nameAmf) return;

m_Name = nameAmf->GetValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AMFArrayValue;
*/
class RenameMessage : public BehaviorMessageBase {
public:
RenameMessage(const AMFArrayValue* arguments);
RenameMessage(const AMFArrayValue& arguments);
[[nodiscard]] const std::string& GetName() const { return m_Name; };

private:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "SplitStripMessage.h"

SplitStripMessage::SplitStripMessage(const AMFArrayValue* arguments)
SplitStripMessage::SplitStripMessage(const AMFArrayValue& arguments)
: BehaviorMessageBase{ arguments }
, m_SrcActionIndex{ GetActionIndexFromArgument(arguments, "srcActionIndex") }
, m_SourceActionContext{ arguments, "srcStateID", "srcStripID" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AMFArrayValue;
*/
class SplitStripMessage : public BehaviorMessageBase {
public:
SplitStripMessage(const AMFArrayValue* arguments);
SplitStripMessage(const AMFArrayValue& arguments);

[[nodiscard]] int32_t GetSrcActionIndex() const noexcept { return m_SrcActionIndex; }

Expand Down
Loading
Loading