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
This commit is contained in:
jadebenn
2024-11-18 20:45:24 -06:00
committed by GitHub
parent 83f8646936
commit 53877a0bc3
25 changed files with 200 additions and 181 deletions

View File

@@ -7,16 +7,16 @@ 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();
m_Type = static_cast<AMFStringValue*>(paramValue.get())->GetValue();
} else {
m_ValueParameterName = paramName;
// Message is the only known string parameter
if (m_ValueParameterName == "Message") {
if (paramValue->GetValueType() != eAmf::String) continue;
m_ValueParameterString = static_cast<AMFStringValue*>(paramValue)->GetValue();
m_ValueParameterString = static_cast<AMFStringValue*>(paramValue.get())->GetValue();
} else {
if (paramValue->GetValueType() != eAmf::Double) continue;
m_ValueParameterDouble = static_cast<AMFDoubleValue*>(paramValue)->GetValue();
m_ValueParameterDouble = static_cast<AMFDoubleValue*>(paramValue.get())->GetValue();
}
}
}

View File

@@ -17,9 +17,9 @@ class Action {
public:
Action() = default;
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; };
[[nodiscard]] std::string_view GetType() const { return m_Type; };
[[nodiscard]] std::string_view GetValueParameterName() const { return m_ValueParameterName; };
[[nodiscard]] std::string_view GetValueParameterString() const { return m_ValueParameterString; };
[[nodiscard]] double GetValueParameterDouble() const noexcept { return m_ValueParameterDouble; };
void SendBehaviorBlocksToClient(AMFArrayValue& args) const;

View File

@@ -4,21 +4,21 @@
#include "Amf3.h"
ActionContext::ActionContext(const AMFArrayValue& arguments, const std::string& customStateKey, const std::string& customStripKey)
ActionContext::ActionContext(const AMFArrayValue& arguments, const std::string_view customStateKey, const std::string_view customStripKey)
: m_StripId{ GetStripIdFromArgument(arguments, customStripKey) }
, m_StateId{ GetBehaviorStateFromArgument(arguments, customStateKey) } {
}
BehaviorState ActionContext::GetBehaviorStateFromArgument(const AMFArrayValue& arguments, const std::string& key) const {
BehaviorState ActionContext::GetBehaviorStateFromArgument(const AMFArrayValue& arguments, const std::string_view key) const {
const auto* const stateIDValue = arguments.Get<double>(key);
if (!stateIDValue) throw std::invalid_argument("Unable to find behavior state from argument \"" + key + "\"");
if (!stateIDValue) throw std::invalid_argument("Unable to find behavior state from argument \"" + std::string{ key } + "\"");
return static_cast<BehaviorState>(stateIDValue->GetValue());
}
StripId ActionContext::GetStripIdFromArgument(const AMFArrayValue& arguments, const std::string& key) const {
StripId ActionContext::GetStripIdFromArgument(const AMFArrayValue& arguments, const std::string_view key) const {
const auto* const stripIdValue = arguments.Get<double>(key);
if (!stripIdValue) throw std::invalid_argument("Unable to find strip ID from argument \"" + key + "\"");
if (!stripIdValue) throw std::invalid_argument("Unable to find strip ID from argument \"" + std::string{ key } + "\"");
return static_cast<StripId>(stripIdValue->GetValue());
}

View File

@@ -4,6 +4,8 @@
#include "BehaviorStates.h"
#include "dCommonVars.h"
#include <string_view>
class AMFArrayValue;
/**
@@ -13,13 +15,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_view customStateKey = "stateID", const std::string_view 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_view key) const;
[[nodiscard]] StripId GetStripIdFromArgument(const AMFArrayValue& arguments, const std::string_view key) const;
StripId m_StripId{ 0 };
BehaviorState m_StateId{ BehaviorState::HOME_STATE };
};

View File

@@ -10,5 +10,5 @@ AddActionMessage::AddActionMessage(const AMFArrayValue& arguments)
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);
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().data(), m_Action.GetValueParameterName().data(), m_Action.GetValueParameterString().data(), m_Action.GetValueParameterDouble(), m_BehaviorId);
}

View File

@@ -19,7 +19,7 @@ AddStripMessage::AddStripMessage(const AMFArrayValue& arguments)
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());
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().data(), m_ActionsToAdd.back().GetValueParameterName().data(), m_ActionsToAdd.back().GetValueParameterString().data(), m_ActionsToAdd.back().GetValueParameterDouble());
}
LOG_DEBUG("number of actions %i", m_ActionsToAdd.size());
}

View File

@@ -5,7 +5,7 @@
#include "dCommonVars.h"
int32_t BehaviorMessageBase::GetBehaviorIdFromArgument(const AMFArrayValue& arguments) {
static constexpr const char* key = "BehaviorID";
static constexpr std::string_view key = "BehaviorID";
const auto* const behaviorIDValue = arguments.Get<std::string>(key);
int32_t behaviorId = DefaultBehaviorId;
@@ -19,7 +19,7 @@ int32_t BehaviorMessageBase::GetBehaviorIdFromArgument(const AMFArrayValue& argu
return behaviorId;
}
int32_t BehaviorMessageBase::GetActionIndexFromArgument(const AMFArrayValue& arguments, const std::string& keyName) const {
int32_t BehaviorMessageBase::GetActionIndexFromArgument(const AMFArrayValue& arguments, const std::string_view keyName) const {
const auto* const actionIndexAmf = arguments.Get<double>(keyName);
if (!actionIndexAmf) throw std::invalid_argument("Unable to find actionIndex");

View File

@@ -22,7 +22,7 @@ public:
protected:
[[nodiscard]] int32_t GetBehaviorIdFromArgument(const AMFArrayValue& arguments);
[[nodiscard]] int32_t GetActionIndexFromArgument(const AMFArrayValue& arguments, const std::string& keyName = "actionIndex") const;
[[nodiscard]] int32_t GetActionIndexFromArgument(const AMFArrayValue& arguments, const std::string_view keyName = "actionIndex") const;
int32_t m_BehaviorId{ DefaultBehaviorId };
};

View File

@@ -11,7 +11,7 @@ class AMFArrayValue;
class RenameMessage : public BehaviorMessageBase {
public:
RenameMessage(const AMFArrayValue& arguments);
[[nodiscard]] const std::string& GetName() const { return m_Name; };
[[nodiscard]] std::string_view GetName() const { return m_Name; };
private:
std::string m_Name;

View File

@@ -3,7 +3,7 @@
#include "Amf3.h"
#include "tinyxml2.h"
StripUiPosition::StripUiPosition(const AMFArrayValue& arguments, const std::string& uiKeyName) {
StripUiPosition::StripUiPosition(const AMFArrayValue& arguments, const std::string_view uiKeyName) {
const auto* const uiArray = arguments.GetArray(uiKeyName);
if (!uiArray) return;

View File

@@ -14,7 +14,7 @@ namespace tinyxml2 {
class StripUiPosition {
public:
StripUiPosition() noexcept = default;
StripUiPosition(const AMFArrayValue& arguments, const std::string& uiKeyName = "ui");
StripUiPosition(const AMFArrayValue& arguments, const std::string_view uiKeyName = "ui");
void SendBehaviorBlocksToClient(AMFArrayValue& args) const;
[[nodiscard]] double GetX() const noexcept { return m_XPosition; }
[[nodiscard]] double GetY() const noexcept { return m_YPosition; }

View File

@@ -12,5 +12,5 @@ UpdateActionMessage::UpdateActionMessage(const AMFArrayValue& arguments)
m_Action = Action{ *actionValue };
LOG_DEBUG("type %s valueParameterName %s valueParameterString %s valueParameterDouble %f behaviorId %i actionIndex %i stripId %i stateId %i", m_Action.GetType().c_str(), m_Action.GetValueParameterName().c_str(), m_Action.GetValueParameterString().c_str(), m_Action.GetValueParameterDouble(), m_BehaviorId, m_ActionIndex, m_ActionContext.GetStripId(), m_ActionContext.GetStateId());
LOG_DEBUG("type %s valueParameterName %s valueParameterString %s valueParameterDouble %f behaviorId %i actionIndex %i stripId %i stateId %i", m_Action.GetType().data(), m_Action.GetValueParameterName().data(), m_Action.GetValueParameterString().data(), m_Action.GetValueParameterDouble(), m_BehaviorId, m_ActionIndex, m_ActionContext.GetStripId(), m_ActionContext.GetStateId());
}