Skip to content

Commit

Permalink
CopyTo more
Browse files Browse the repository at this point in the history
  • Loading branch information
ZorPastaman committed Nov 20, 2020
1 parent b1507af commit 667deb9
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Serialization of local components in Blackboard Container.
- Blackboard.GetValueType() method that returns a type of a property by its property name.
- Blackboard.GetPropertyNames() method that returns all property names of properties contained in Blackboard.
- Blackboard.CopyTo() methods that copy properties to another blackboard.

## [1.1.0] - 2020-09-28

Expand Down
80 changes: 78 additions & 2 deletions Runtime/Core/Blackboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,9 @@ public void Clear()
/// <param name="blackboard">Destination.</param>
public void CopyTo([NotNull] Blackboard blackboard)
{
Profiler.BeginSample("Blackboard.CopyTo");
Profiler.BeginSample("Blackboard.CopyTo(Blackboard)");

BlackboardDebug.LogDetails($"[Blackboard] CopyTo");
BlackboardDebug.LogDetails($"[Blackboard] CopyTo(Blackboard)");

Dictionary<Type, IBlackboardTable>.Enumerator tableEnumerator = m_tables.GetEnumerator();
while (tableEnumerator.MoveNext())
Expand Down Expand Up @@ -985,6 +985,82 @@ public void CopyTo([NotNull] Blackboard blackboard)
Profiler.EndSample();
}

/// <summary>
/// Copies a property of the property name <paramref name="propertyName"/> to <paramref name="blackboard"/>.
/// </summary>
/// <param name="blackboard">Destination.</param>
/// <param name="propertyName">Property to copy.</param>
public void CopyTo([NotNull] Blackboard blackboard, BlackboardPropertyName propertyName)
{
Profiler.BeginSample("Blackboard.CopyTo(Blackboard, BlackboardPropertyName)");

BlackboardDebug.LogDetails($"[Blackboard] CopyTo(Blackboard, BlackboardPropertyName)");

if (m_propertyTypes.TryGetValue(propertyName, out Type propertyType))
{
IBlackboardTable tableToCopy = m_tables[propertyType];
IBlackboardTable tableToCopyTo = blackboard.GetOrCreateTable(propertyType);
tableToCopy.CopyTo(tableToCopyTo);
blackboard.m_propertyTypes[propertyName] = propertyType;
}

Profiler.EndSample();
}

/// <summary>
/// Copies properties of the property names <paramref name="propertyNames"/> to <paramref name="blackboard"/>.
/// </summary>
/// <param name="blackboard">Destination.</param>
/// <param name="propertyNames">Properties to copy.</param>
public void CopyTo([NotNull] Blackboard blackboard, BlackboardPropertyName[] propertyNames)
{
Profiler.BeginSample("Blackboard.CopyTo(Blackboard, BlackboardPropertyName[])");

BlackboardDebug.LogDetails($"[Blackboard] CopyTo(Blackboard, BlackboardPropertyName[])");

for (int i = 0, count = propertyNames.Length; i < count; ++i)
{
BlackboardPropertyName propertyName = propertyNames[i];

if (m_propertyTypes.TryGetValue(propertyName, out Type propertyType))
{
IBlackboardTable tableToCopy = m_tables[propertyType];
IBlackboardTable tableToCopyTo = blackboard.GetOrCreateTable(propertyType);
tableToCopy.CopyTo(tableToCopyTo);
blackboard.m_propertyTypes[propertyName] = propertyType;
}
}

Profiler.EndSample();
}

/// <summary>
/// Copies properties of the property names <paramref name="propertyNames"/> to <paramref name="blackboard"/>.
/// </summary>
/// <param name="blackboard">Destination.</param>
/// <param name="propertyNames">Properties to copy.</param>
public void CopyTo<T>([NotNull] Blackboard blackboard, T propertyNames) where T : IList<BlackboardPropertyName>
{
Profiler.BeginSample("Blackboard.CopyTo(Blackboard, BlackboardPropertyName[])");

BlackboardDebug.LogDetails($"[Blackboard] CopyTo(Blackboard, BlackboardPropertyName[])");

for (int i = 0, count = propertyNames.Count; i < count; ++i)
{
BlackboardPropertyName propertyName = propertyNames[i];

if (m_propertyTypes.TryGetValue(propertyName, out Type propertyType))
{
IBlackboardTable tableToCopy = m_tables[propertyType];
IBlackboardTable tableToCopyTo = blackboard.GetOrCreateTable(propertyType);
tableToCopy.CopyTo(tableToCopyTo);
blackboard.m_propertyTypes[propertyName] = propertyType;
}
}

Profiler.EndSample();
}

[Pure]
public override string ToString()
{
Expand Down
38 changes: 38 additions & 0 deletions Tests/Runtime/BlackboardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,10 @@ public static void CopyToTests()
new GameObject().AddComponent<Rigidbody>(), new GameObject().transform,
1, 230f
});

CopyToTest(fromBlackboard, toBlackboard, new GameObject());
CopyToTest(fromBlackboard, toBlackboard, 1);
CopyToTest(fromBlackboard, toBlackboard, new List<object>());
}

private static void SetObjectTryGetObjectTest([NotNull] Blackboard blackboard, [NotNull] object[] values)
Expand Down Expand Up @@ -1345,6 +1349,40 @@ private static void CopyToTest([NotNull] Blackboard fromBlackboard, [NotNull] Bl
&& value.Equals(values[i]),
$"Blackboard has a wrong property of name {propertyName.ToString()}");
}

toBlackboard.Clear();
fromBlackboard.CopyTo(toBlackboard, propertyNames);

for (int i = 0; i < count; ++i)
{
BlackboardPropertyName propertyName = propertyNames[i];
Assert.IsTrue(toBlackboard.TryGetObjectValue(propertyName, out object value)
&& value.Equals(values[i]),
$"Blackboard has a wrong property of name {propertyName.ToString()}");
}

toBlackboard.Clear();
var list = new List<BlackboardPropertyName>(propertyNames);
fromBlackboard.CopyTo(toBlackboard, list);

for (int i = 0; i < count; ++i)
{
BlackboardPropertyName propertyName = propertyNames[i];
Assert.IsTrue(toBlackboard.TryGetObjectValue(propertyName, out object value)
&& value.Equals(values[i]),
$"Blackboard has a wrong property of name {propertyName.ToString()}");
}
}

private static void CopyToTest([NotNull] Blackboard fromBlackboard, [NotNull] Blackboard toBlackboard,
[NotNull] object value)
{
var propertyName = new BlackboardPropertyName(value.GetType().FullName + "");
fromBlackboard.SetClassValue(propertyName, value);
fromBlackboard.CopyTo(toBlackboard, propertyName);
Assert.IsTrue(toBlackboard.TryGetClassValue(propertyName, out object containedValue)
&& value.Equals(containedValue),
$"Blackboard has a wrong property of name {propertyName.ToString()}");
}
}
}
Expand Down

0 comments on commit 667deb9

Please sign in to comment.