Fixing some dependencies in script canvas and emfx

This commit is contained in:
karlberg
2021-05-11 13:42:44 -07:00
parent 3e13dd52d1
commit 5440d0926d
12 changed files with 1 additions and 1521 deletions
@@ -1,245 +0,0 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <ScriptCanvas/Variable/GraphVariableMarshal.h>
#include <ScriptCanvas/Variable/GraphVariable.h>
#include <ScriptCanvas/Variable/GraphVariableNetBindings.h>
#include <ScriptCanvas/Core/ModifiableDatumView.h>
#include <AzFramework/Network/EntityIdMarshaler.h>
#include <GridMate/Serialize/MathMarshal.h>
#include <GridMate/Serialize/UtilityMarshal.h>
#include <GridMate/Serialize/UuidMarshal.h>
namespace ScriptCanvas
{
void DatumMarshaler::SetNetBindingTable(GraphVariableNetBindingTable* netBindingTable)
{
m_graphVariableNetBindingTable = netBindingTable;
}
void DatumMarshaler::Marshal(GridMate::WriteBuffer& wb, const Datum* const & property) const
{
if (!property)
{
return;
}
GridMate::Marshaler<Data::eType> typeMarshaler;
const Data::eType& datumType = property->GetType().GetType();
typeMarshaler.Marshal(wb, datumType);
VariableId assetVariableId;
AZStd::unordered_map<VariableId, AZStd::pair<GraphVariable*, int>>& variableIdMap = m_graphVariableNetBindingTable->GetVariableIdMap();
for (AZStd::pair<VariableId, AZStd::pair<GraphVariable*, int>>& pair : variableIdMap)
{
AZStd::pair<GraphVariable*, int>& variableIndexPair = pair.second;
if (variableIndexPair.first->GetDatum() == property)
{
assetVariableId = m_graphVariableNetBindingTable->FindAssetVariableIdByRuntimeVariableId(pair.first);
break;
}
}
if (!assetVariableId.IsValid())
{
return;
}
GridMate::Marshaler<AZ::Uuid> uuidMarshaler;
uuidMarshaler.Marshal(wb, assetVariableId.GetDatumId());
AZStd::string uuidString = assetVariableId.m_id.ToString<AZStd::string>();
switch (datumType)
{
case Data::eType::AABB:
MarshalType<Data::AABBType>(wb, property);
break;
case Data::eType::Boolean:
MarshalType<Data::BooleanType>(wb, property);
break;
case Data::eType::Color:
MarshalType<Data::ColorType>(wb, property);
break;
case Data::eType::CRC:
MarshalType<Data::CRCType>(wb, property);
break;
case Data::eType::EntityID:
MarshalType<Data::EntityIDType>(wb, property);
break;
case Data::eType::Matrix3x3:
MarshalType<Data::Matrix3x3Type>(wb, property);
break;
case Data::eType::Matrix4x4:
MarshalType<Data::Matrix4x4Type>(wb, property);
break;
case Data::eType::NamedEntityID:
MarshalType<Data::NamedEntityIDType>(wb, property);
break;
case Data::eType::Number:
MarshalType<Data::NumberType>(wb, property);
break;
case Data::eType::OBB:
MarshalType<Data::OBBType>(wb, property);
break;
case Data::eType::Plane:
MarshalType<Data::PlaneType>(wb, property);
break;
case Data::eType::Quaternion:
MarshalType<Data::QuaternionType>(wb, property);
break;
case Data::eType::String:
MarshalType<Data::StringType>(wb, property);
break;
case Data::eType::Transform:
MarshalType<Data::TransformType>(wb, property);
break;
case Data::eType::Vector2:
MarshalType<Data::Vector2Type>(wb, property);
break;
case Data::eType::Vector3:
MarshalType<Data::Vector3Type>(wb, property);
break;
case Data::eType::Vector4:
MarshalType<Data::Vector4Type>(wb, property);
break;
default:
AZ_Warning("ScriptCanvasNetworking", false, "Marshal unsupported data type");
break;
}
}
bool DatumMarshaler::UnmarshalToPointer(const Datum*& target, GridMate::ReadBuffer& rb)
{
// :SCTODO: for some reason, this UnmarshalToPointer can get called before SetNetworkBinding is called
// (which is where we set m_graphVariableNetBindingTable). So we check for nullptr here just in case.
if (!m_graphVariableNetBindingTable)
{
return false;
}
ScriptCanvas::Data::eType datumType = Data::eType::Invalid;
GridMate::Marshaler<Data::eType> typeMarshaler;
typeMarshaler.Unmarshal(datumType, rb);
AZ::Uuid uuid;
GridMate::Marshaler<AZ::Uuid> uuidMarshaler;
uuidMarshaler.Unmarshal(uuid, rb);
VariableId runtimeVariableId = m_graphVariableNetBindingTable->FindRuntimeVariableIdByAssetVariableId(VariableId(uuid));
if (!runtimeVariableId.IsValid())
{
return false;
}
AZStd::string uuidString = runtimeVariableId.m_id.ToString<AZStd::string>();
AZStd::unordered_map<VariableId, AZStd::pair<GraphVariable*, int>>& m_variableIdMap = m_graphVariableNetBindingTable->GetVariableIdMap();
AZStd::pair<GraphVariable*, int>& variableIndexPair = m_variableIdMap[runtimeVariableId];
GraphVariable* graphVariable = variableIndexPair.first;
switch (datumType)
{
case Data::eType::AABB:
return UnmarshalType<Data::AABBType>(target, rb, graphVariable);
case Data::eType::Boolean:
return UnmarshalType<Data::BooleanType>(target, rb, graphVariable);
case Data::eType::Color:
return UnmarshalType<Data::ColorType>(target, rb, graphVariable);
case Data::eType::CRC:
return UnmarshalType<Data::CRCType>(target, rb, graphVariable);
case Data::eType::EntityID:
return UnmarshalType<Data::EntityIDType>(target, rb, graphVariable);
case Data::eType::Matrix3x3:
return UnmarshalType<Data::Matrix3x3Type>(target, rb, graphVariable);
case Data::eType::Matrix4x4:
return UnmarshalType<Data::Matrix4x4Type>(target, rb, graphVariable);
case Data::eType::NamedEntityID:
return UnmarshalType<Data::NamedEntityIDType>(target, rb, graphVariable);
case Data::eType::Number:
return UnmarshalType<Data::NumberType>(target, rb, graphVariable);
case Data::eType::OBB:
return UnmarshalType<Data::OBBType>(target, rb, graphVariable);
case Data::eType::Plane:
return UnmarshalType<Data::PlaneType>(target, rb, graphVariable);
case Data::eType::Quaternion:
return UnmarshalType<Data::QuaternionType>(target, rb, graphVariable);
case Data::eType::String:
return UnmarshalType<Data::StringType>(target, rb, graphVariable);
case Data::eType::Transform:
return UnmarshalType<Data::TransformType>(target, rb, graphVariable);
case Data::eType::Vector2:
return UnmarshalType<Data::Vector2Type>(target, rb, graphVariable);
case Data::eType::Vector3:
return UnmarshalType<Data::Vector3Type>(target, rb, graphVariable);
case Data::eType::Vector4:
return UnmarshalType<Data::Vector4Type>(target, rb, graphVariable);
default:
AZ_Warning("ScriptCanvasNetworking", false, "Unmarshal unsupported data type");
break;
}
return false;
}
void DatumThrottler::SignalDirty()
{
m_isDirty = true;
}
bool DatumThrottler::WithinThreshold(const Datum* newValue) const
{
return (newValue == nullptr || !m_isDirty);
}
void DatumThrottler::UpdateBaseline([[maybe_unused]] const Datum* baseline)
{
m_isDirty = false;
}
}
@@ -1,86 +0,0 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <GridMate/Serialize/ContainerMarshal.h>
#include <ScriptCanvas/Core/Datum.h>
#include <ScriptCanvas/Core/ModifiableDatumView.h>
#include <ScriptCanvas/Variable/GraphVariable.h>
namespace ScriptCanvas
{
class GraphVariableNetBindingTable;
class DatumMarshaler
{
public:
void SetNetBindingTable(GraphVariableNetBindingTable* netBindingTable);
void Marshal(GridMate::WriteBuffer& wb, const Datum* const & cont) const;
bool UnmarshalToPointer(const Datum*& target, GridMate::ReadBuffer& rb);
private:
template <typename T>
void MarshalType(GridMate::WriteBuffer& wb, const Datum* const & property) const
{
GridMate::Marshaler<T> marshaler;
const T* value = property->GetAs<T>();
marshaler.Marshal(wb, *value);
}
template <typename T>
bool UnmarshalType(const Datum*& target, GridMate::ReadBuffer& rb, GraphVariable* graphVariable)
{
bool valueChanged = false;
ModifiableDatumView datumView;
if (graphVariable)
{
graphVariable->ConfigureDatumView(datumView);
if (datumView.IsValid())
{
GridMate::Marshaler<T> marshaler;
T value;
marshaler.Unmarshal(value, rb);
datumView.SetAs(value);
target = graphVariable->GetDatum();
valueChanged = true;
}
}
return valueChanged;
}
private:
//! The network binding table is needed to determine which Datum to update
//! when unmarshaling data.
// :SCTODO: synced Datums should be tracked via ID
//! and that ID should be used to lookup Datums (right now we can assume
//! which Datum should be updated, since only one Datum is supported).
GraphVariableNetBindingTable* m_graphVariableNetBindingTable = nullptr;
};
//! Simple throttler that simple operates via dirty flag.
class DatumThrottler
{
public:
DatumThrottler() = default;
void SignalDirty();
bool WithinThreshold(const Datum* newValue) const;
void UpdateBaseline(const Datum* baseline);
private:
bool m_isDirty = false;
};
}
@@ -1,181 +0,0 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <ScriptCanvas/Execution/RuntimeBus.h>
#include <ScriptCanvas/Variable/GraphVariable.h>
#include <ScriptCanvas/Variable/GraphVariableNetBindings.h>
#include <ScriptCanvas/Core/Datum.h>
#include <GridMate/Replica/DataSet.h>
#include <GridMate/Replica/ReplicaFunctions.h>
#include <AzFramework/Network/NetworkContext.h>
namespace ScriptCanvas
{
const char* DatumDataSet::GetDataSetName()
{
static size_t s_chunkIndex = 0;
static const char* s_nameArray[] = {
"DataSet1","DataSet2","DataSet3","DataSet4","DataSet5",
"DataSet6","DataSet7","DataSet8","DataSet9","DataSet10",
"DataSet11","DataSet12","DataSet13","DataSet14","DataSet15",
"DataSet16","DataSet17","DataSet18","DataSet19","DataSet20",
"DataSet21","DataSet22","DataSet23","DataSet24","DataSet25",
"DataSet26","DataSet27","DataSet28","DataSet29","DataSet30",
"DataSet31","DataSet32"
};
if (s_chunkIndex > AZ_ARRAY_SIZE(s_nameArray) && AZ_ARRAY_SIZE(s_nameArray) >= 0)
{
s_chunkIndex = s_chunkIndex % AZ_ARRAY_SIZE(s_nameArray);
}
return s_nameArray[s_chunkIndex++];
}
DatumDataSet::DatumDataSet()
: DatumDataSetType(DatumDataSet::GetDataSetName())
{
}
//////////////////////////
// GraphVariableReplicaChunk
//////////////////////////
const char* GraphVariableReplicaChunk::GetChunkName()
{
return "GraphVariableReplicaChunk";
}
bool GraphVariableReplicaChunk::IsReplicaMigratable()
{
return true;
}
//////////////////////////
// GraphVariableNetBindingTable
//////////////////////////
void GraphVariableNetBindingTable::Reflect([[maybe_unused]] AZ::ReflectContext* reflect)
{
GridMate::ReplicaChunkDescriptorTable& descriptorTable = GridMate::ReplicaChunkDescriptorTable::Get();
AZ::Crc32 hash = GridMate::ReplicaChunkClassId(GraphVariableReplicaChunk::GetChunkName());
if (!descriptorTable.FindReplicaChunkDescriptor(hash))
{
descriptorTable.RegisterChunkType<GraphVariableReplicaChunk>();
}
}
GridMate::ReplicaChunkPtr GraphVariableNetBindingTable::GetNetworkBinding()
{
if (!m_replicaChunk)
{
m_replicaChunk = GridMate::CreateReplicaChunk<GraphVariableReplicaChunk>();
m_replicaChunk->SetHandler(this);
SetGraphNetBindingTable();
}
return m_replicaChunk;
}
void GraphVariableNetBindingTable::SetNetworkBinding(GridMate::ReplicaChunkPtr chunk)
{
m_replicaChunk = chunk;
m_replicaChunk->SetHandler(this);
SetGraphNetBindingTable();
}
void GraphVariableNetBindingTable::UnbindFromNetwork()
{
if (m_replicaChunk)
{
m_replicaChunk->SetHandler(nullptr);
m_replicaChunk = nullptr;
}
}
void GraphVariableNetBindingTable::OnPropertyUpdate([[maybe_unused]] const Datum* const & scriptProperty, [[maybe_unused]] const GridMate::TimeContext& tc)
{
}
void GraphVariableNetBindingTable::AddDatum(GraphVariable* variable)
{
size_t index = m_variableIdMap.size();
m_variableIdMap[variable->GetVariableId()] = AZStd::make_pair(variable, static_cast<int>(index));
}
void GraphVariableNetBindingTable::OnDatumChanged(GraphVariable& variable)
{
if (m_replicaChunk && m_replicaChunk->IsMaster())
{
GraphVariableReplicaChunk* graphVarChunk = static_cast<GraphVariableReplicaChunk*>(m_replicaChunk.get());
auto iter = m_variableIdMap.find(variable.GetVariableId());
if (iter == m_variableIdMap.end())
{
AZ_TracePrintf("ScriptCanvasNetworking", "GraphVariableNetBindingTable::OnDatumChanged: variable not found");
return;
}
const AZStd::pair<GraphVariable*, int>& pair = iter->second;
DatumDataSet& datumDataSet = graphVarChunk->m_properties[pair.second];
datumDataSet.GetThrottler().SignalDirty();
datumDataSet.Set(variable.GetDatum());
}
}
void GraphVariableNetBindingTable::SetVariableMappings(const AZStd::unordered_map<VariableId, VariableId>& assetToRuntimeVariableMap, const AZStd::unordered_map<VariableId, VariableId>& runtimeToAssetVariableMap)
{
m_assetToRuntimeVariableMap = assetToRuntimeVariableMap;
m_runtimeToAssetVariableMap = runtimeToAssetVariableMap;
}
VariableId GraphVariableNetBindingTable::FindAssetVariableIdByRuntimeVariableId(VariableId runtimeVariableId)
{
auto iter = m_runtimeToAssetVariableMap.find(runtimeVariableId);
if (iter != m_runtimeToAssetVariableMap.end())
{
return iter->second;
}
return VariableId();
}
VariableId GraphVariableNetBindingTable::FindRuntimeVariableIdByAssetVariableId(VariableId assetVariableId)
{
auto iter = m_assetToRuntimeVariableMap.find(assetVariableId);
if (iter != m_assetToRuntimeVariableMap.end())
{
return iter->second;
}
return VariableId();
}
AZStd::unordered_map<VariableId, AZStd::pair<GraphVariable*, int>>& GraphVariableNetBindingTable::GetVariableIdMap()
{
return m_variableIdMap;
}
void GraphVariableNetBindingTable::SetGraphNetBindingTable()
{
GraphVariableReplicaChunk* graphVariableChunk = static_cast<GraphVariableReplicaChunk*>(m_replicaChunk.get());
for (DatumDataSet& dataSet : graphVariableChunk->m_properties)
{
dataSet.GetMarshaler().SetNetBindingTable(this);
}
}
}
@@ -1,101 +0,0 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#include <AzCore/RTTI/ReflectContext.h>
#include <GridMate/Replica/DataSet.h>
#include <GridMate/Replica/ReplicaChunkInterface.h>
#include <GridMate/Replica/ReplicaCommon.h>
#include <ScriptCanvas/Variable/GraphVariableMarshal.h>
namespace ScriptCanvas
{
class GraphVariable;
class GraphVariableReplicaChunk;
//! Core functionality for managing replicated Datums in a script canvas and the
//! corresponding GridMate callbacks and data structs (DataSets).
class GraphVariableNetBindingTable
: public GridMate::ReplicaChunkInterface
{
public:
AZ_CLASS_ALLOCATOR(GraphVariableNetBindingTable, AZ::SystemAllocator, 0);
static void Reflect(AZ::ReflectContext* reflect);
GraphVariableNetBindingTable() = default;
~GraphVariableNetBindingTable() = default;
GridMate::ReplicaChunkPtr GetNetworkBinding();
void SetNetworkBinding(GridMate::ReplicaChunkPtr chunk);
void UnbindFromNetwork();
//! Gets called when the given Datum object is updated with a new value
//! that was received over the network.
void OnPropertyUpdate(const Datum* const & scriptProperty, const GridMate::TimeContext& tc);
//! Adds the given Datum to the list of "synced datums" for this instance.
void AddDatum(GraphVariable* variable);
//! Called when local data changes for a Datum whose values should be replicated
//! over the network.
void OnDatumChanged(GraphVariable& variable);
void SetVariableMappings(const AZStd::unordered_map<VariableId, VariableId>& assetToRuntimeVariableMap, const AZStd::unordered_map<VariableId, VariableId>& runtimeToAssetVariableMap);
VariableId FindAssetVariableIdByRuntimeVariableId(VariableId runtimeVariableId);
VariableId FindRuntimeVariableIdByAssetVariableId(VariableId assetVariableId);
AZStd::unordered_map<VariableId, AZStd::pair<GraphVariable*, int>>& GetVariableIdMap();
private:
void SetGraphNetBindingTable();
private:
AZStd::unordered_map<VariableId, VariableId> m_assetToRuntimeVariableMap;
AZStd::unordered_map<VariableId, VariableId> m_runtimeToAssetVariableMap;
//! Replica chunk used for GridMate networking binding. See GraphVariableReplicaChunk.
GridMate::ReplicaChunkPtr m_replicaChunk;
//! Contains pointers to all replicated variables contained within the runtime component
//! of the canvas this net binding is associated with.
AZStd::unordered_map<VariableId, AZStd::pair<GraphVariable*, int>> m_variableIdMap;
};
typedef GridMate::DataSet<const Datum*, DatumMarshaler, DatumThrottler>::BindInterface<GraphVariableNetBindingTable, &GraphVariableNetBindingTable::OnPropertyUpdate> DatumDataSetType;
class DatumDataSet
: public DatumDataSetType
{
public:
DatumDataSet();
~DatumDataSet() = default;
private:
const char* GetDataSetName();
};
class GraphVariableReplicaChunk
: public GridMate::ReplicaChunkBase
{
public:
AZ_CLASS_ALLOCATOR(GraphVariableReplicaChunk, AZ::SystemAllocator, 0);
static const char* GetChunkName();
GraphVariableReplicaChunk() = default;
~GraphVariableReplicaChunk() = default;
bool IsReplicaMigratable() override;
DatumDataSet m_properties[GM_MAX_DATASETS_IN_CHUNK];
};
}