mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-05 18:24:12 +00:00
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:
@@ -66,8 +66,8 @@ public:
|
||||
template<typename Msg>
|
||||
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) {
|
||||
Msg msg{ args };
|
||||
for (auto&& behavior : m_Behaviors) {
|
||||
if (behavior.GetBehaviorId() == msg.GetBehaviorId()) {
|
||||
behavior.HandleMsg(msg);
|
||||
return;
|
||||
|
@@ -2492,7 +2492,7 @@ 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<AMFArrayValue> amfArguments{ static_cast<AMFArrayValue*>(reader.Read(inStream)) };
|
||||
std::unique_ptr<AMFArrayValue> amfArguments{ static_cast<AMFArrayValue*>(reader.Read(inStream).release()) };
|
||||
if (amfArguments->GetValueType() != eAmf::Array) return;
|
||||
|
||||
uint32_t commandLength{};
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
BlockDefinition BlockDefinition::blockDefinitionDefault{};
|
||||
|
||||
BlockDefinition::BlockDefinition(const std::string& defaultValue, const float minimumValue, const float maximumValue)
|
||||
BlockDefinition::BlockDefinition(const std::string_view defaultValue, const float minimumValue, const float maximumValue)
|
||||
: m_DefaultValue{ defaultValue }
|
||||
, m_MinimumValue{ minimumValue }
|
||||
, m_MaximumValue{ maximumValue } {
|
||||
|
@@ -7,13 +7,13 @@ class AMFArrayValue;
|
||||
|
||||
class BlockDefinition {
|
||||
public:
|
||||
BlockDefinition(const std::string& defaultValue = "", const float minimumValue = 0.0f, const float maximumValue = 0.0f);
|
||||
BlockDefinition(const std::string_view defaultValue = "", const float minimumValue = 0.0f, const float maximumValue = 0.0f);
|
||||
static BlockDefinition blockDefinitionDefault;
|
||||
|
||||
[[nodiscard]] const std::string& GetDefaultValue() const { return m_DefaultValue; }
|
||||
[[nodiscard]] std::string_view 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 SetDefaultValue(const std::string_view value) { m_DefaultValue = std::string{ value }; }
|
||||
void SetMinimumValue(const float value) noexcept { m_MinimumValue = value; }
|
||||
void SetMaximumValue(const float value) noexcept { m_MaximumValue = value; }
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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());
|
||||
}
|
||||
|
@@ -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 };
|
||||
};
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -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());
|
||||
}
|
||||
|
@@ -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");
|
||||
|
||||
|
@@ -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 };
|
||||
};
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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; }
|
||||
|
@@ -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());
|
||||
}
|
||||
|
@@ -76,26 +76,26 @@ void ControlBehaviors::UpdateAction(const AMFArrayValue& arguments) {
|
||||
auto blockDefinition = GetBlockInfo(updateActionMessage.GetAction().GetType());
|
||||
|
||||
if (!blockDefinition) {
|
||||
LOG("Received undefined block type %s. Ignoring.", updateActionMessage.GetAction().GetType().c_str());
|
||||
LOG("Received undefined block type %s. Ignoring.", updateActionMessage.GetAction().GetType().data());
|
||||
return;
|
||||
}
|
||||
|
||||
if (updateActionMessage.GetAction().GetValueParameterString().size() > 0) {
|
||||
if (updateActionMessage.GetAction().GetValueParameterString().size() < blockDefinition->GetMinimumValue() ||
|
||||
updateActionMessage.GetAction().GetValueParameterString().size() > blockDefinition->GetMaximumValue()) {
|
||||
LOG("Updated block %s is out of range. Ignoring update", updateActionMessage.GetAction().GetType().c_str());
|
||||
LOG("Updated block %s is out of range. Ignoring update", updateActionMessage.GetAction().GetType().data());
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (updateActionMessage.GetAction().GetValueParameterDouble() < blockDefinition->GetMinimumValue() ||
|
||||
updateActionMessage.GetAction().GetValueParameterDouble() > blockDefinition->GetMaximumValue()) {
|
||||
LOG("Updated block %s is out of range. Ignoring update", updateActionMessage.GetAction().GetType().c_str());
|
||||
LOG("Updated block %s is out of range. Ignoring update", updateActionMessage.GetAction().GetType().data());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ControlBehaviors::ProcessCommand(Entity* const modelEntity, const AMFArrayValue& arguments, const std::string& command, Entity* const modelOwner) {
|
||||
void ControlBehaviors::ProcessCommand(Entity* const modelEntity, const AMFArrayValue& arguments, const std::string_view command, Entity* const modelOwner) {
|
||||
if (!isInitialized || !modelEntity || !modelOwner) return;
|
||||
auto* const modelComponent = modelEntity->GetComponent<ModelComponent>();
|
||||
|
||||
@@ -157,7 +157,7 @@ void ControlBehaviors::ProcessCommand(Entity* const modelEntity, const AMFArrayV
|
||||
} else if (command == "updateAction") {
|
||||
context.modelComponent->HandleControlBehaviorsMsg<UpdateActionMessage>(arguments);
|
||||
} else {
|
||||
LOG("Unknown behavior command (%s)", command.c_str());
|
||||
LOG("Unknown behavior command (%s)", command.data());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,11 +279,11 @@ ControlBehaviors::ControlBehaviors() {
|
||||
isInitialized = true;
|
||||
LOG_DEBUG("Created all base block classes");
|
||||
for (auto& [name, block] : blockTypes) {
|
||||
LOG_DEBUG("block name is %s default %s min %f max %f", name.c_str(), block.GetDefaultValue().c_str(), block.GetMinimumValue(), block.GetMaximumValue());
|
||||
LOG_DEBUG("block name is %s default %s min %f max %f", name.data(), block.GetDefaultValue().data(), block.GetMinimumValue(), block.GetMaximumValue());
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<BlockDefinition> ControlBehaviors::GetBlockInfo(const std::string& blockName) {
|
||||
std::optional<BlockDefinition> ControlBehaviors::GetBlockInfo(const std::string_view blockName) {
|
||||
auto blockDefinition = blockTypes.find(blockName);
|
||||
return blockDefinition != blockTypes.end() ? std::optional(blockDefinition->second) : std::nullopt;
|
||||
}
|
||||
|
@@ -43,7 +43,7 @@ public:
|
||||
* @param command The command to perform
|
||||
* @param modelOwner The owner of the model which sent this command
|
||||
*/
|
||||
void ProcessCommand(Entity* const modelEntity, const AMFArrayValue& arguments, const std::string& command, Entity* const modelOwner);
|
||||
void ProcessCommand(Entity* const modelEntity, const AMFArrayValue& arguments, const std::string_view command, Entity* const modelOwner);
|
||||
|
||||
/**
|
||||
* @brief Gets a blocks parameter values by the name
|
||||
@@ -53,7 +53,7 @@ public:
|
||||
*
|
||||
* @return A pair of the block parameter name to its typing
|
||||
*/
|
||||
[[nodiscard]] std::optional<BlockDefinition> GetBlockInfo(const std::string& blockName);
|
||||
[[nodiscard]] std::optional<BlockDefinition> GetBlockInfo(const std::string_view blockName);
|
||||
private:
|
||||
void RequestUpdatedID(ControlBehaviorContext& context);
|
||||
void SendBehaviorListToClient(const ControlBehaviorContext& context);
|
||||
|
Reference in New Issue
Block a user