mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-04 17:54:01 +00:00
Refactor: Amf3 implementation (#998)
* Update AMFDeserializeTests.cpp Redo Amf3 functionality Overhaul the whole thing due to it being outdated and clunky to use Sometimes you want to keep the value Update AMFDeserializeTests.cpp * Fix enum and constructors Correct enum to a class and simplify names. Add a proper default constructor * Update MasterServer.cpp * Fix bugs and add more tests * Refactor: AMF with templates in mind - Remove hard coded bodge - Use templates and generics to allow for much looser typing and strengthened implementation - Move code into header only implementation for portability Refactor: Convert AMF implementation to templates - Rip out previous implementation - Remove all extraneous terminology - Add proper overloads for all types of inserts - Fix up tests and codebase * Fix compiler errors * Check for null first * Add specialization for const char* * Update tests for new template specialization * Switch BitStream to use references * Rename files * Check enum bounds on deserialize I did this on a phone
This commit is contained in:
@@ -12,19 +12,19 @@ Action::Action(AMFArrayValue* arguments) {
|
||||
valueParameterName = "";
|
||||
valueParameterString = "";
|
||||
valueParameterDouble = 0.0;
|
||||
for (auto& typeValueMap : arguments->GetAssociativeMap()) {
|
||||
for (auto& typeValueMap : arguments->GetAssociative()) {
|
||||
if (typeValueMap.first == "Type") {
|
||||
if (typeValueMap.second->GetValueType() != AMFValueType::AMFString) continue;
|
||||
type = static_cast<AMFStringValue*>(typeValueMap.second)->GetStringValue();
|
||||
if (typeValueMap.second->GetValueType() != eAmf::String) continue;
|
||||
type = static_cast<AMFStringValue*>(typeValueMap.second)->GetValue();
|
||||
} else {
|
||||
valueParameterName = typeValueMap.first;
|
||||
// Message is the only known string parameter
|
||||
if (valueParameterName == "Message") {
|
||||
if (typeValueMap.second->GetValueType() != AMFValueType::AMFString) continue;
|
||||
valueParameterString = static_cast<AMFStringValue*>(typeValueMap.second)->GetStringValue();
|
||||
if (typeValueMap.second->GetValueType() != eAmf::String) continue;
|
||||
valueParameterString = static_cast<AMFStringValue*>(typeValueMap.second)->GetValue();
|
||||
} else {
|
||||
if (typeValueMap.second->GetValueType() != AMFValueType::AMFDouble) continue;
|
||||
valueParameterDouble = static_cast<AMFDoubleValue*>(typeValueMap.second)->GetDoubleValue();
|
||||
if (typeValueMap.second->GetValueType() != eAmf::Double) continue;
|
||||
valueParameterDouble = static_cast<AMFDoubleValue*>(typeValueMap.second)->GetValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "AMFFormat.h"
|
||||
#include "Amf3.h"
|
||||
|
||||
ActionContext::ActionContext() {
|
||||
stripId = 0;
|
||||
@@ -17,15 +17,15 @@ ActionContext::ActionContext(AMFArrayValue* arguments, std::string customStateKe
|
||||
}
|
||||
|
||||
BehaviorState ActionContext::GetBehaviorStateFromArgument(AMFArrayValue* arguments, const std::string& key) {
|
||||
auto* stateIDValue = arguments->FindValue<AMFDoubleValue>(key);
|
||||
auto* stateIDValue = arguments->Get<double>(key);
|
||||
if (!stateIDValue) throw std::invalid_argument("Unable to find behavior state from argument \"" + key + "\"");
|
||||
|
||||
return static_cast<BehaviorState>(stateIDValue->GetDoubleValue());
|
||||
return static_cast<BehaviorState>(stateIDValue->GetValue());
|
||||
}
|
||||
|
||||
StripId ActionContext::GetStripIdFromArgument(AMFArrayValue* arguments, const std::string& key) {
|
||||
auto* stripIdValue = arguments->FindValue<AMFDoubleValue>(key);
|
||||
auto* stripIdValue = arguments->Get<double>(key);
|
||||
if (!stripIdValue) throw std::invalid_argument("Unable to find strip ID from argument \"" + key + "\"");
|
||||
|
||||
return static_cast<StripId>(stripIdValue->GetDoubleValue());
|
||||
return static_cast<StripId>(stripIdValue->GetValue());
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ AddActionMessage::AddActionMessage(AMFArrayValue* arguments) : BehaviorMessageBa
|
||||
actionContext = ActionContext(arguments);
|
||||
actionIndex = GetActionIndexFromArgument(arguments);
|
||||
|
||||
auto* actionValue = arguments->FindValue<AMFArrayValue>("action");
|
||||
auto* actionValue = arguments->GetArray("action");
|
||||
if (!actionValue) return;
|
||||
|
||||
action = Action(actionValue);
|
||||
|
@@ -2,10 +2,10 @@
|
||||
|
||||
AddMessage::AddMessage(AMFArrayValue* arguments) : BehaviorMessageBase(arguments) {
|
||||
behaviorIndex = 0;
|
||||
auto* behaviorIndexValue = arguments->FindValue<AMFDoubleValue>("BehaviorIndex");
|
||||
auto* behaviorIndexValue = arguments->Get<double>("BehaviorIndex");
|
||||
|
||||
if (!behaviorIndexValue) return;
|
||||
|
||||
behaviorIndex = static_cast<uint32_t>(behaviorIndexValue->GetDoubleValue());
|
||||
behaviorIndex = static_cast<uint32_t>(behaviorIndexValue->GetValue());
|
||||
Game::logger->LogDebug("AddMessage", "behaviorId %i index %i", behaviorId, behaviorIndex);
|
||||
}
|
||||
|
@@ -4,17 +4,16 @@
|
||||
|
||||
AddStripMessage::AddStripMessage(AMFArrayValue* arguments) : BehaviorMessageBase(arguments) {
|
||||
actionContext = ActionContext(arguments);
|
||||
|
||||
position = StripUiPosition(arguments);
|
||||
|
||||
auto* strip = arguments->FindValue<AMFArrayValue>("strip");
|
||||
auto* strip = arguments->GetArray("strip");
|
||||
if (!strip) return;
|
||||
|
||||
auto* actions = strip->FindValue<AMFArrayValue>("actions");
|
||||
auto* actions = strip->GetArray("actions");
|
||||
if (!actions) return;
|
||||
|
||||
for (uint32_t actionNumber = 0; actionNumber < actions->GetDenseValueSize(); actionNumber++) {
|
||||
auto* actionValue = actions->GetValueAt<AMFArrayValue>(actionNumber);
|
||||
for (uint32_t actionNumber = 0; actionNumber < actions->GetDense().size(); actionNumber++) {
|
||||
auto* actionValue = actions->GetArray(actionNumber);
|
||||
if (!actionValue) continue;
|
||||
|
||||
actionsToAdd.push_back(Action(actionValue));
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#include "BehaviorMessageBase.h"
|
||||
|
||||
#include "AMFFormat.h"
|
||||
#include "Amf3.h"
|
||||
#include "BehaviorStates.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
@@ -11,12 +11,12 @@ BehaviorMessageBase::BehaviorMessageBase(AMFArrayValue* arguments) {
|
||||
|
||||
int32_t BehaviorMessageBase::GetBehaviorIdFromArgument(AMFArrayValue* arguments) {
|
||||
const auto* key = "BehaviorID";
|
||||
auto* behaviorIDValue = arguments->FindValue<AMFStringValue>(key);
|
||||
auto* behaviorIDValue = arguments->Get<std::string>(key);
|
||||
int32_t behaviorID = -1;
|
||||
|
||||
if (behaviorIDValue) {
|
||||
behaviorID = std::stoul(behaviorIDValue->GetStringValue());
|
||||
} else if (!arguments->FindValue<AMFUndefinedValue>(key)) {
|
||||
if (behaviorIDValue && behaviorIDValue->GetValueType() == eAmf::String) {
|
||||
behaviorID = std::stoul(behaviorIDValue->GetValue());
|
||||
} else if (arguments->Get(key)->GetValueType() != eAmf::Undefined) {
|
||||
throw std::invalid_argument("Unable to find behavior ID");
|
||||
}
|
||||
|
||||
@@ -24,10 +24,10 @@ int32_t BehaviorMessageBase::GetBehaviorIdFromArgument(AMFArrayValue* arguments)
|
||||
}
|
||||
|
||||
uint32_t BehaviorMessageBase::GetActionIndexFromArgument(AMFArrayValue* arguments, const std::string& keyName) {
|
||||
auto* actionIndexAmf = arguments->FindValue<AMFDoubleValue>(keyName);
|
||||
auto* actionIndexAmf = arguments->Get<double>(keyName);
|
||||
if (!actionIndexAmf) {
|
||||
throw std::invalid_argument("Unable to find actionIndex");
|
||||
}
|
||||
|
||||
return static_cast<uint32_t>(actionIndexAmf->GetDoubleValue());
|
||||
return static_cast<uint32_t>(actionIndexAmf->GetValue());
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "AMFFormat.h"
|
||||
#include "Amf3.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
#include "Game.h"
|
||||
|
@@ -1,9 +1,9 @@
|
||||
#include "MoveToInventoryMessage.h"
|
||||
|
||||
MoveToInventoryMessage::MoveToInventoryMessage(AMFArrayValue* arguments) : BehaviorMessageBase(arguments) {
|
||||
auto* behaviorIndexValue = arguments->FindValue<AMFDoubleValue>("BehaviorIndex");
|
||||
auto* behaviorIndexValue = arguments->Get<double>("BehaviorIndex");
|
||||
if (!behaviorIndexValue) return;
|
||||
|
||||
behaviorIndex = static_cast<uint32_t>(behaviorIndexValue->GetDoubleValue());
|
||||
behaviorIndex = static_cast<uint32_t>(behaviorIndexValue->GetValue());
|
||||
Game::logger->LogDebug("MoveToInventoryMessage", "behaviorId %i behaviorIndex %i", behaviorId, behaviorIndex);
|
||||
}
|
||||
|
@@ -1,9 +1,9 @@
|
||||
#include "RenameMessage.h"
|
||||
|
||||
RenameMessage::RenameMessage(AMFArrayValue* arguments) : BehaviorMessageBase(arguments) {
|
||||
auto* nameAmf = arguments->FindValue<AMFStringValue>("Name");
|
||||
auto* nameAmf = arguments->Get<std::string>("Name");
|
||||
if (!nameAmf) return;
|
||||
|
||||
name = nameAmf->GetStringValue();
|
||||
name = nameAmf->GetValue();
|
||||
Game::logger->LogDebug("RenameMessage", "behaviorId %i n %s", behaviorId, name.c_str());
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#include "StripUiPosition.h"
|
||||
|
||||
#include "AMFFormat.h"
|
||||
#include "Amf3.h"
|
||||
|
||||
StripUiPosition::StripUiPosition() {
|
||||
xPosition = 0.0;
|
||||
@@ -10,13 +10,13 @@ StripUiPosition::StripUiPosition() {
|
||||
StripUiPosition::StripUiPosition(AMFArrayValue* arguments, std::string uiKeyName) {
|
||||
xPosition = 0.0;
|
||||
yPosition = 0.0;
|
||||
auto* uiArray = arguments->FindValue<AMFArrayValue>(uiKeyName);
|
||||
auto* uiArray = arguments->GetArray(uiKeyName);
|
||||
if (!uiArray) return;
|
||||
|
||||
auto* xPositionValue = uiArray->FindValue<AMFDoubleValue>("x");
|
||||
auto* yPositionValue = uiArray->FindValue<AMFDoubleValue>("y");
|
||||
auto* xPositionValue = uiArray->Get<double>("x");
|
||||
auto* yPositionValue = uiArray->Get<double>("y");
|
||||
if (!xPositionValue || !yPositionValue) return;
|
||||
|
||||
yPosition = yPositionValue->GetDoubleValue();
|
||||
xPosition = xPositionValue->GetDoubleValue();
|
||||
yPosition = yPositionValue->GetValue();
|
||||
xPosition = xPositionValue->GetValue();
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@
|
||||
UpdateActionMessage::UpdateActionMessage(AMFArrayValue* arguments) : BehaviorMessageBase(arguments) {
|
||||
actionContext = ActionContext(arguments);
|
||||
|
||||
auto* actionValue = arguments->FindValue<AMFArrayValue>("action");
|
||||
auto* actionValue = arguments->GetArray("action");
|
||||
if (!actionValue) return;
|
||||
|
||||
action = Action(actionValue);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#include "ControlBehaviors.h"
|
||||
|
||||
#include "AMFFormat.h"
|
||||
#include "Amf3.h"
|
||||
#include "Entity.h"
|
||||
#include "Game.h"
|
||||
#include "GameMessages.h"
|
||||
@@ -43,11 +43,11 @@ void ControlBehaviors::RequestUpdatedID(int32_t behaviorID, ModelComponent* mode
|
||||
// AMFArrayValue args;
|
||||
|
||||
// AMFStringValue* behaviorIDString = new AMFStringValue();
|
||||
// behaviorIDString->SetStringValue(std::to_string(persistentId));
|
||||
// behaviorIDString->SetValue(std::to_string(persistentId));
|
||||
// args.InsertValue("behaviorID", behaviorIDString);
|
||||
|
||||
// AMFStringValue* objectIDAsString = new AMFStringValue();
|
||||
// objectIDAsString->SetStringValue(std::to_string(modelComponent->GetParent()->GetObjectID()));
|
||||
// objectIDAsString->SetValue(std::to_string(modelComponent->GetParent()->GetObjectID()));
|
||||
// args.InsertValue("objectID", objectIDAsString);
|
||||
|
||||
// GameMessages::SendUIMessageServerToSingleClient(modelOwner, sysAddr, "UpdateBehaviorID", &args);
|
||||
@@ -63,8 +63,6 @@ void ControlBehaviors::SendBehaviorListToClient(Entity* modelEntity, const Syste
|
||||
|
||||
AMFArrayValue behaviorsToSerialize;
|
||||
|
||||
AMFArrayValue* behaviors = new AMFArrayValue(); // Empty for now
|
||||
|
||||
/**
|
||||
* The behaviors AMFArray will have up to 5 elements in the dense portion.
|
||||
* Each element in the dense portion will be made up of another AMFArray
|
||||
@@ -75,20 +73,17 @@ void ControlBehaviors::SendBehaviorListToClient(Entity* modelEntity, const Syste
|
||||
* "name": The name of the behavior formatted as an AMFString
|
||||
*/
|
||||
|
||||
behaviorsToSerialize.InsertValue("behaviors", behaviors);
|
||||
behaviorsToSerialize.Insert("behaviors");
|
||||
behaviorsToSerialize.Insert("objectID", std::to_string(modelComponent->GetParent()->GetObjectID()));
|
||||
|
||||
AMFStringValue* amfStringValueForObjectID = new AMFStringValue();
|
||||
amfStringValueForObjectID->SetStringValue(std::to_string(modelComponent->GetParent()->GetObjectID()));
|
||||
|
||||
behaviorsToSerialize.InsertValue("objectID", amfStringValueForObjectID);
|
||||
GameMessages::SendUIMessageServerToSingleClient(modelOwner, sysAddr, "UpdateBehaviorList", &behaviorsToSerialize);
|
||||
GameMessages::SendUIMessageServerToSingleClient(modelOwner, sysAddr, "UpdateBehaviorList", behaviorsToSerialize);
|
||||
}
|
||||
|
||||
void ControlBehaviors::ModelTypeChanged(AMFArrayValue* arguments, ModelComponent* ModelComponent) {
|
||||
auto* modelTypeAmf = arguments->FindValue<AMFDoubleValue>("ModelType");
|
||||
auto* modelTypeAmf = arguments->Get<double>("ModelType");
|
||||
if (!modelTypeAmf) return;
|
||||
|
||||
uint32_t modelType = static_cast<uint32_t>(modelTypeAmf->GetDoubleValue());
|
||||
uint32_t modelType = static_cast<uint32_t>(modelTypeAmf->GetValue());
|
||||
|
||||
//TODO Update the model type here
|
||||
}
|
||||
@@ -179,7 +174,7 @@ void ControlBehaviors::SendBehaviorBlocksToClient(ModelComponent* modelComponent
|
||||
// AMFArrayValue* state = new AMFArrayValue();
|
||||
|
||||
// AMFDoubleValue* stateAsDouble = new AMFDoubleValue();
|
||||
// stateAsDouble->SetDoubleValue(it->first);
|
||||
// stateAsDouble->SetValue(it->first);
|
||||
// state->InsertValue("id", stateAsDouble);
|
||||
|
||||
// AMFArrayValue* strips = new AMFArrayValue();
|
||||
@@ -189,16 +184,16 @@ void ControlBehaviors::SendBehaviorBlocksToClient(ModelComponent* modelComponent
|
||||
// AMFArrayValue* thisStrip = new AMFArrayValue();
|
||||
|
||||
// AMFDoubleValue* stripID = new AMFDoubleValue();
|
||||
// stripID->SetDoubleValue(strip->first);
|
||||
// stripID->SetValue(strip->first);
|
||||
// thisStrip->InsertValue("id", stripID);
|
||||
|
||||
// AMFArrayValue* uiArray = new AMFArrayValue();
|
||||
// AMFDoubleValue* yPosition = new AMFDoubleValue();
|
||||
// yPosition->SetDoubleValue(strip->second->GetYPosition());
|
||||
// yPosition->SetValue(strip->second->GetYPosition());
|
||||
// uiArray->InsertValue("y", yPosition);
|
||||
|
||||
// AMFDoubleValue* xPosition = new AMFDoubleValue();
|
||||
// xPosition->SetDoubleValue(strip->second->GetXPosition());
|
||||
// xPosition->SetValue(strip->second->GetXPosition());
|
||||
// uiArray->InsertValue("x", xPosition);
|
||||
|
||||
// thisStrip->InsertValue("ui", uiArray);
|
||||
@@ -211,19 +206,19 @@ void ControlBehaviors::SendBehaviorBlocksToClient(ModelComponent* modelComponent
|
||||
// AMFArrayValue* thisAction = new AMFArrayValue();
|
||||
|
||||
// AMFStringValue* actionName = new AMFStringValue();
|
||||
// actionName->SetStringValue(behaviorAction->actionName);
|
||||
// actionName->SetValue(behaviorAction->actionName);
|
||||
// thisAction->InsertValue("Type", actionName);
|
||||
|
||||
// if (behaviorAction->parameterValueString != "")
|
||||
// {
|
||||
// AMFStringValue* valueAsString = new AMFStringValue();
|
||||
// valueAsString->SetStringValue(behaviorAction->parameterValueString);
|
||||
// valueAsString->SetValue(behaviorAction->parameterValueString);
|
||||
// thisAction->InsertValue(behaviorAction->parameterName, valueAsString);
|
||||
// }
|
||||
// else if (behaviorAction->parameterValueDouble != 0.0)
|
||||
// {
|
||||
// AMFDoubleValue* valueAsDouble = new AMFDoubleValue();
|
||||
// valueAsDouble->SetDoubleValue(behaviorAction->parameterValueDouble);
|
||||
// valueAsDouble->SetValue(behaviorAction->parameterValueDouble);
|
||||
// thisAction->InsertValue(behaviorAction->parameterName, valueAsDouble);
|
||||
// }
|
||||
// stripSerialize->PushBackValue(thisAction);
|
||||
@@ -237,11 +232,11 @@ void ControlBehaviors::SendBehaviorBlocksToClient(ModelComponent* modelComponent
|
||||
// behaviorInfo.InsertValue("states", stateSerialize);
|
||||
|
||||
// AMFStringValue* objectidAsString = new AMFStringValue();
|
||||
// objectidAsString->SetStringValue(std::to_string(targetObjectID));
|
||||
// objectidAsString->SetValue(std::to_string(targetObjectID));
|
||||
// behaviorInfo.InsertValue("objectID", objectidAsString);
|
||||
|
||||
// AMFStringValue* behaviorIDAsString = new AMFStringValue();
|
||||
// behaviorIDAsString->SetStringValue(std::to_string(behaviorID));
|
||||
// behaviorIDAsString->SetValue(std::to_string(behaviorID));
|
||||
// behaviorInfo.InsertValue("BehaviorID", behaviorIDAsString);
|
||||
|
||||
// GameMessages::SendUIMessageServerToSingleClient(modelOwner, sysAddr, "UpdateBehaviorBlocks", &behaviorInfo);
|
||||
@@ -275,10 +270,9 @@ void ControlBehaviors::MoveToInventory(ModelComponent* modelComponent, const Sys
|
||||
// This closes the UI menu should it be open while the player is removing behaviors
|
||||
AMFArrayValue args;
|
||||
|
||||
AMFFalseValue* stateToPop = new AMFFalseValue();
|
||||
args.InsertValue("visible", stateToPop);
|
||||
args.Insert("visible", false);
|
||||
|
||||
GameMessages::SendUIMessageServerToSingleClient(modelOwner, modelOwner->GetParentUser()->GetSystemAddress(), "ToggleBehaviorEditor", &args);
|
||||
GameMessages::SendUIMessageServerToSingleClient(modelOwner, modelOwner->GetParentUser()->GetSystemAddress(), "ToggleBehaviorEditor", args);
|
||||
|
||||
MoveToInventoryMessage moveToInventoryMessage(arguments);
|
||||
|
||||
|
Reference in New Issue
Block a user