Skip to content

Commit

Permalink
CopyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
ZorPastaman committed Nov 19, 2020
1 parent 09d0369 commit b1507af
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
37 changes: 37 additions & 0 deletions Runtime/Core/Blackboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,43 @@ public void Clear()
Profiler.EndSample();
}

/// <summary>
/// Copies its properties to <paramref name="blackboard"/>.
/// </summary>
/// <param name="blackboard">Destination.</param>
public void CopyTo([NotNull] Blackboard blackboard)
{
Profiler.BeginSample("Blackboard.CopyTo");

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

Dictionary<Type, IBlackboardTable>.Enumerator tableEnumerator = m_tables.GetEnumerator();
while (tableEnumerator.MoveNext())
{
KeyValuePair<Type, IBlackboardTable> current = tableEnumerator.Current;
IBlackboardTable tableToCopy = current.Value;

if (tableToCopy.count == 0)
{
continue;
}

IBlackboardTable tableToCopyTo = blackboard.GetOrCreateTable(current.Key);
tableToCopy.CopyTo(tableToCopyTo);
}
tableEnumerator.Dispose();

Dictionary<BlackboardPropertyName, Type>.Enumerator propertyEnumerator = m_propertyTypes.GetEnumerator();
while (propertyEnumerator.MoveNext())
{
KeyValuePair<BlackboardPropertyName, Type> current = propertyEnumerator.Current;
blackboard.m_propertyTypes[current.Key] = current.Value;
}
propertyEnumerator.Dispose();

Profiler.EndSample();
}

[Pure]
public override string ToString()
{
Expand Down
14 changes: 14 additions & 0 deletions Runtime/Core/BlackboardTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ public void Clear()
m_table.Clear();
}

/// <inheritdoc/>
public void CopyTo(IBlackboardTable table)
{
var typedTable = (BlackboardTable<T>)table;

Dictionary<BlackboardPropertyName, T>.Enumerator enumerator = m_table.GetEnumerator();
while (enumerator.MoveNext())
{
KeyValuePair<BlackboardPropertyName, T> current = enumerator.Current;
typedTable.SetValue(current.Key, current.Value);
}
enumerator.Dispose();
}

public override string ToString()
{
var builder = new StringBuilder();
Expand Down
6 changes: 6 additions & 0 deletions Runtime/Core/IBlackboardTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,11 @@ void GetPropertiesAs<TAs>([NotNull] List<KeyValuePair<BlackboardPropertyName, TA
/// Clears of all properties.
/// </summary>
void Clear();

/// <summary>
/// Copies its properties to <paramref name="table"/>.
/// </summary>
/// <param name="table">Destination. Must be the same type.</param>
void CopyTo([NotNull] IBlackboardTable table);
}
}
61 changes: 60 additions & 1 deletion Tests/Runtime/BlackboardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,44 @@ public static void PropertiesCountTests()
new GameObject(), new GameObject(),
new List<object>(), new List<int>(),
new GameObject().AddComponent<Rigidbody>(), new GameObject().transform,
1, 230f
});
blackboard.Clear();
PropertiesCountTest(blackboard, new object[]
{
new int[1], new float[1],
new Dictionary<int, int>(), new List()
new Dictionary<int, int>(), new List(),
12, 230.87f
});
}

[Test]
public static void CopyToTests()
{
var fromBlackboard = new Blackboard();
var toBlackboard = new Blackboard();

CopyToTest(fromBlackboard, toBlackboard, new object[]
{
new GameObject(), new GameObject(),
new List<object>(), new List<int>(),
new GameObject().AddComponent<Rigidbody>(), new GameObject().transform,
1, 230f
});

CopyToTest(fromBlackboard, toBlackboard, new object[]
{
new int[1], new float[1],
new Dictionary<int, int>(), new List(),
12, 230.87f
});

CopyToTest(fromBlackboard, toBlackboard, new object[]
{
new GameObject(), new GameObject(),
new List<object>(), new List<int>(),
new GameObject().AddComponent<Rigidbody>(), new GameObject().transform,
1, 230f
});
}

Expand Down Expand Up @@ -1287,6 +1319,33 @@ private static void PropertiesCountTest([NotNull] Blackboard blackboard, [NotNul
Assert.AreEqual(propertyNames.Length, blackboard.propertiesCount,
"Blackboard has wrong number of properties");
}

private static void CopyToTest([NotNull] Blackboard fromBlackboard, [NotNull] Blackboard toBlackboard,
[NotNull] object[] values)
{
int count = values.Length;
var propertyNames = new BlackboardPropertyName[count];

for (int i = 0; i < count; ++i)
{
propertyNames[i] = new BlackboardPropertyName(values[i].GetType().FullName + " " + i.ToString());
}

for (int i = 0; i < count; ++i)
{
fromBlackboard.SetClassValue(propertyNames[i], values[i]);
}

fromBlackboard.CopyTo(toBlackboard);

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

0 comments on commit b1507af

Please sign in to comment.