From 667deb9e3714d1909f879c89e11b29a4274dc393 Mon Sep 17 00:00:00 2001 From: Vladimir Popov Date: Fri, 20 Nov 2020 10:29:49 +0300 Subject: [PATCH] CopyTo more --- CHANGELOG.md | 1 + Runtime/Core/Blackboard.cs | 80 +++++++++++++++++++++++++++++++- Tests/Runtime/BlackboardTests.cs | 38 +++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49b1470..47e7420 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Runtime/Core/Blackboard.cs b/Runtime/Core/Blackboard.cs index 8b38f05..7c8c556 100644 --- a/Runtime/Core/Blackboard.cs +++ b/Runtime/Core/Blackboard.cs @@ -954,9 +954,9 @@ public void Clear() /// Destination. 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.Enumerator tableEnumerator = m_tables.GetEnumerator(); while (tableEnumerator.MoveNext()) @@ -985,6 +985,82 @@ public void CopyTo([NotNull] Blackboard blackboard) Profiler.EndSample(); } + /// + /// Copies a property of the property name to . + /// + /// Destination. + /// Property to copy. + 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(); + } + + /// + /// Copies properties of the property names to . + /// + /// Destination. + /// Properties to copy. + 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(); + } + + /// + /// Copies properties of the property names to . + /// + /// Destination. + /// Properties to copy. + public void CopyTo([NotNull] Blackboard blackboard, T propertyNames) where T : IList + { + 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() { diff --git a/Tests/Runtime/BlackboardTests.cs b/Tests/Runtime/BlackboardTests.cs index 5080322..736d731 100644 --- a/Tests/Runtime/BlackboardTests.cs +++ b/Tests/Runtime/BlackboardTests.cs @@ -647,6 +647,10 @@ public static void CopyToTests() new GameObject().AddComponent(), new GameObject().transform, 1, 230f }); + + CopyToTest(fromBlackboard, toBlackboard, new GameObject()); + CopyToTest(fromBlackboard, toBlackboard, 1); + CopyToTest(fromBlackboard, toBlackboard, new List()); } private static void SetObjectTryGetObjectTest([NotNull] Blackboard blackboard, [NotNull] object[] values) @@ -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(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()}"); } } }