Skip to content

Commit

Permalink
refactor: Rewrite BehaviorMessage classes to use member initializatio…
Browse files Browse the repository at this point in the history
…n, preferred member naming conventions, and const-ref getters (#1456)

* Split out BehaviorMessage class changes from PR #1452

* remove <string_view> inclusion in ActionContext.h

* add the arguments nullptr check back in

* remove redundant std::string constructor calls

* Update AddStripMessage.cpp - change push_back to emplace_back
  • Loading branch information
jadebenn committed Feb 18, 2024
1 parent c7b3d9e commit b6af92e
Show file tree
Hide file tree
Showing 46 changed files with 361 additions and 340 deletions.
8 changes: 4 additions & 4 deletions dCommon/Amf3.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ class AMFArrayValue : public AMFBaseValue {
/**
* Returns the Associative portion of the object
*/
[[nodiscard]] inline AMFAssociative& GetAssociative() noexcept { return this->associative; }
[[nodiscard]] inline const AMFAssociative& GetAssociative() const noexcept { return this->associative; }

/**
* Returns the dense portion of the object
*/
[[nodiscard]] inline AMFDense& GetDense() noexcept { return this->dense; }
[[nodiscard]] inline const AMFDense& GetDense() const noexcept { return this->dense; }

/**
* Inserts an AMFValue into the associative portion with the given key.
Expand Down Expand Up @@ -297,15 +297,15 @@ class AMFArrayValue : public AMFBaseValue {
if (!this->dense.empty()) Remove(this->dense.size() - 1);
}

[[nodiscard]] AMFArrayValue* GetArray(const std::string& key) {
[[nodiscard]] AMFArrayValue* GetArray(const std::string& key) const {
AMFAssociative::const_iterator it = this->associative.find(key);
if (it != this->associative.end()) {
return dynamic_cast<AMFArrayValue*>(it->second);
}
return nullptr;
}

[[nodiscard]] AMFArrayValue* GetArray(const size_t index) {
[[nodiscard]] AMFArrayValue* GetArray(const size_t index) const {
return index >= this->dense.size() ? nullptr : dynamic_cast<AMFArrayValue*>(this->dense.at(index));
}

Expand Down
2 changes: 1 addition & 1 deletion dGame/dGameMessages/GameMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2504,7 +2504,7 @@ void GameMessages::HandleControlBehaviors(RakNet::BitStream* inStream, Entity* e
auto owner = PropertyManagementComponent::Instance()->GetOwner();
if (!owner) return;

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

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

BlockDefinition BlockDefinition::blockDefinitionDefault{};

BlockDefinition::BlockDefinition(std::string defaultValue, float minimumValue, float maximumValue) {
this->defaultValue = defaultValue;
this->minimumValue = minimumValue;
this->maximumValue = maximumValue;
BlockDefinition::BlockDefinition(const std::string& defaultValue, const float minimumValue, const float maximumValue)
: m_DefaultValue{ defaultValue }
, m_MinimumValue{ minimumValue }
, m_MaximumValue{ maximumValue } {
}
21 changes: 11 additions & 10 deletions dGame/dPropertyBehaviors/BlockDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ class AMFArrayValue;

class BlockDefinition {
public:
BlockDefinition(std::string defaultValue = "", float minimumValue = 0.0f, float maximumValue = 0.0f);
BlockDefinition(const std::string& defaultValue = "", const float minimumValue = 0.0f, const float maximumValue = 0.0f);
static BlockDefinition blockDefinitionDefault;

std::string& GetDefaultValue() { return defaultValue; };
float GetMinimumValue() { return minimumValue; };
float GetMaximumValue() { return maximumValue; };
void SetDefaultValue(std::string value) { defaultValue = value; };
void SetMinimumValue(float value) { minimumValue = value; };
void SetMaximumValue(float value) { maximumValue = value; };
[[nodiscard]] const std::string& GetDefaultValue() const { return m_DefaultValue; }
[[nodiscard]] float GetMinimumValue() const noexcept { return m_MinimumValue; }
[[nodiscard]] float GetMaximumValue() const noexcept { return m_MaximumValue; }
void SetDefaultValue(const std::string& value) { m_DefaultValue = value; }
void SetMinimumValue(const float value) noexcept { m_MinimumValue = value; }
void SetMaximumValue(const float value) noexcept { m_MaximumValue = value; }

private:
std::string defaultValue;
float minimumValue;
float maximumValue;
std::string m_DefaultValue;
float m_MinimumValue;
float m_MaximumValue;
};

#endif //!__BLOCKDEFINITION__H__
38 changes: 13 additions & 25 deletions dGame/dPropertyBehaviors/ControlBehaviorMessages/Action.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
#include "Action.h"
#include "Amf3.h"

Action::Action() {
type = "";
valueParameterName = "";
valueParameterString = "";
valueParameterDouble = 0.0;
}

Action::Action(AMFArrayValue* arguments) {
type = "";
valueParameterName = "";
valueParameterString = "";
valueParameterDouble = 0.0;
for (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;
type = static_cast<AMFStringValue*>(paramValue)->GetValue();
m_Type = static_cast<AMFStringValue*>(paramValue)->GetValue();
} else {
valueParameterName = paramName;
m_ValueParameterName = paramName;
// Message is the only known string parameter
if (valueParameterName == "Message") {
if (m_ValueParameterName == "Message") {
if (paramValue->GetValueType() != eAmf::String) continue;
valueParameterString = static_cast<AMFStringValue*>(paramValue)->GetValue();
m_ValueParameterString = static_cast<AMFStringValue*>(paramValue)->GetValue();
} else {
if (paramValue->GetValueType() != eAmf::Double) continue;
valueParameterDouble = static_cast<AMFDoubleValue*>(paramValue)->GetValue();
m_ValueParameterDouble = static_cast<AMFDoubleValue*>(paramValue)->GetValue();
}
}
}
}

void Action::SendBehaviorBlocksToClient(AMFArrayValue& args) const {
auto* actionArgs = args.PushArray();
actionArgs->Insert("Type", type);
auto* const actionArgs = args.PushArray();
actionArgs->Insert("Type", m_Type);

auto valueParameterName = GetValueParameterName();
if (valueParameterName.empty()) return;
if (m_ValueParameterName.empty()) return;

if (valueParameterName == "Message") {
actionArgs->Insert(valueParameterName, valueParameterString);
if (m_ValueParameterName == "Message") {
actionArgs->Insert(m_ValueParameterName, m_ValueParameterString);
} else {
actionArgs->Insert(valueParameterName, valueParameterDouble);
actionArgs->Insert(m_ValueParameterName, m_ValueParameterDouble);
}
}
21 changes: 11 additions & 10 deletions dGame/dPropertyBehaviors/ControlBehaviorMessages/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ class AMFArrayValue;
*/
class Action {
public:
Action();
Action(AMFArrayValue* arguments);
const std::string& GetType() const { return type; };
const std::string& GetValueParameterName() const { return valueParameterName; };
const std::string& GetValueParameterString() const { return valueParameterString; };
const double GetValueParameterDouble() const { return valueParameterDouble; };
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]] double GetValueParameterDouble() const noexcept { return m_ValueParameterDouble; };

void SendBehaviorBlocksToClient(AMFArrayValue& args) const;

private:
std::string type;
std::string valueParameterName;
std::string valueParameterString;
double valueParameterDouble;
double m_ValueParameterDouble{ 0.0 };
std::string m_Type{ "" };
std::string m_ValueParameterName{ "" };
std::string m_ValueParameterString{ "" };
};

#endif //!__ACTION__H__
21 changes: 7 additions & 14 deletions dGame/dPropertyBehaviors/ControlBehaviorMessages/ActionContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,20 @@

#include "Amf3.h"

ActionContext::ActionContext() {
stripId = 0;
stateId = BehaviorState::HOME_STATE;
ActionContext::ActionContext(const AMFArrayValue* arguments, const std::string& customStateKey, const std::string& customStripKey)
: m_StripId{ GetStripIdFromArgument(arguments, customStripKey) }
, m_StateId{ GetBehaviorStateFromArgument(arguments, customStateKey) } {
}

ActionContext::ActionContext(AMFArrayValue* arguments, std::string customStateKey, std::string customStripKey) {
stripId = 0;
stateId = BehaviorState::HOME_STATE;
stripId = GetStripIdFromArgument(arguments, customStripKey);
stateId = GetBehaviorStateFromArgument(arguments, customStateKey);
}

BehaviorState ActionContext::GetBehaviorStateFromArgument(AMFArrayValue* arguments, const std::string& key) {
auto* 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(AMFArrayValue* arguments, const std::string& key) {
auto* 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
17 changes: 9 additions & 8 deletions dGame/dPropertyBehaviors/ControlBehaviorMessages/ActionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class AMFArrayValue;
*/
class ActionContext {
public:
ActionContext();
ActionContext(AMFArrayValue* arguments, std::string customStateKey = "stateID", std::string customStripKey = "stripID");
const StripId GetStripId() const { return stripId; };
const BehaviorState GetStateId() const { return stateId; };
ActionContext() noexcept = default;
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:
BehaviorState GetBehaviorStateFromArgument(AMFArrayValue* arguments, const std::string& key);
StripId GetStripIdFromArgument(AMFArrayValue* arguments, const std::string& key);
StripId stripId;
BehaviorState stateId;
[[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 };
};

#endif //!__ACTIONCONTEXT__H__
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "AddActionMessage.h"

AddActionMessage::AddActionMessage(AMFArrayValue* arguments) : BehaviorMessageBase(arguments) {
actionContext = ActionContext(arguments);
actionIndex = GetActionIndexFromArgument(arguments);
AddActionMessage::AddActionMessage(const AMFArrayValue* arguments)
: BehaviorMessageBase{ arguments }
, m_ActionIndex{ GetActionIndexFromArgument(arguments) }
, m_ActionContext{ arguments } {

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

action = Action(actionValue);
m_Action = Action{ actionValue };

LOG_DEBUG("actionIndex %i stripId %i stateId %i type %s valueParameterName %s valueParameterString %s valueParameterDouble %f behaviorId %i", actionIndex, actionContext.GetStripId(), actionContext.GetStateId(), action.GetType().c_str(), action.GetValueParameterName().c_str(), action.GetValueParameterString().c_str(), action.GetValueParameterDouble(), 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().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,14 +13,18 @@ class AMFArrayValue;
*/
class AddActionMessage : public BehaviorMessageBase {
public:
AddActionMessage(AMFArrayValue* arguments);
int32_t GetActionIndex() const { return actionIndex; };
Action GetAction() const { return action; };
ActionContext GetActionContext() const { return actionContext; };
AddActionMessage(const AMFArrayValue* arguments);

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

[[nodiscard]] const Action& GetAction() const noexcept { return m_Action; };

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

private:
int32_t actionIndex = -1;
ActionContext actionContext;
Action action;
int32_t m_ActionIndex{ -1 };
ActionContext m_ActionContext;
Action m_Action;
};

#endif //!__ADDACTIONMESSAGE__H__
10 changes: 4 additions & 6 deletions dGame/dPropertyBehaviors/ControlBehaviorMessages/AddMessage.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include "AddMessage.h"

AddMessage::AddMessage(AMFArrayValue* arguments) : BehaviorMessageBase(arguments) {
behaviorIndex = 0;
auto* behaviorIndexValue = arguments->Get<double>("BehaviorIndex");

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

behaviorIndex = static_cast<uint32_t>(behaviorIndexValue->GetValue());
LOG_DEBUG("behaviorId %i index %i", behaviorId, behaviorIndex);
m_BehaviorIndex = static_cast<uint32_t>(behaviorIndexValue->GetValue());
LOG_DEBUG("behaviorId %i index %i", m_BehaviorId, m_BehaviorIndex);
}
7 changes: 4 additions & 3 deletions dGame/dPropertyBehaviors/ControlBehaviorMessages/AddMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
*/
class AddMessage : public BehaviorMessageBase {
public:
AddMessage(AMFArrayValue* arguments);
const uint32_t GetBehaviorIndex() const { return behaviorIndex; };
AddMessage(const AMFArrayValue* arguments);
[[nodiscard]] uint32_t GetBehaviorIndex() const noexcept { return m_BehaviorIndex; };

private:
uint32_t behaviorIndex;
uint32_t m_BehaviorIndex{ 0 };
};

#endif //!__ADDMESSAGE__H__
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@

#include "Action.h"

AddStripMessage::AddStripMessage(AMFArrayValue* const arguments) : BehaviorMessageBase{ arguments } {
actionContext = ActionContext(arguments);
position = StripUiPosition(arguments);
AddStripMessage::AddStripMessage(const AMFArrayValue* arguments)
: BehaviorMessageBase{ arguments }
, m_Position{ arguments }
, m_ActionContext{ arguments } {

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

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

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

actionsToAdd.push_back(Action(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", position.GetX(), position.GetY(), actionContext.GetStripId(), actionContext.GetStateId(), behaviorId, actionsToAdd.back().GetType().c_str(), actionsToAdd.back().GetValueParameterName().c_str(), actionsToAdd.back().GetValueParameterString().c_str(), 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().c_str(), m_ActionsToAdd.back().GetValueParameterName().c_str(), m_ActionsToAdd.back().GetValueParameterString().c_str(), m_ActionsToAdd.back().GetValueParameterDouble());
}
LOG_DEBUG("number of actions %i", actionsToAdd.size());
}

std::vector<Action> AddStripMessage::GetActionsToAdd() const {
return actionsToAdd;
LOG_DEBUG("number of actions %i", m_ActionsToAdd.size());
}
18 changes: 11 additions & 7 deletions dGame/dPropertyBehaviors/ControlBehaviorMessages/AddStripMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ class AMFArrayValue;
*/
class AddStripMessage : public BehaviorMessageBase {
public:
AddStripMessage(AMFArrayValue* const arguments);
StripUiPosition GetPosition() const { return position; };
ActionContext GetActionContext() const { return actionContext; };
std::vector<Action> GetActionsToAdd() const;
AddStripMessage(const AMFArrayValue* arguments);

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

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

[[nodiscard]] const std::vector<Action>& GetActionsToAdd() const noexcept { return m_ActionsToAdd; }

private:
StripUiPosition position;
ActionContext actionContext;
std::vector<Action> actionsToAdd;
StripUiPosition m_Position;
ActionContext m_ActionContext;
std::vector<Action> m_ActionsToAdd;
};

#endif //!__ADDSTRIPMESSAGE__H__
Loading

0 comments on commit b6af92e

Please sign in to comment.