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:
David Markowitz
2023-05-13 15:22:00 -07:00
committed by GitHub
parent 9d105a287d
commit 4fe335cc66
46 changed files with 1081 additions and 1420 deletions

View File

@@ -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();
}
}
}

View File

@@ -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());
}

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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));

View File

@@ -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());
}

View File

@@ -4,7 +4,7 @@
#include <stdexcept>
#include <string>
#include "AMFFormat.h"
#include "Amf3.h"
#include "dCommonVars.h"
#include "Game.h"

View File

@@ -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);
}

View File

@@ -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());
}

View File

@@ -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();
}

View File

@@ -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);