public void TestMergeSimpleWithBasePartsAndLinksForChildren() { // Similar to TestMergeSimpleWithBasePartsAndLinks, but perform merge on Entity.Transform.Children instead // Also Check that an asset that was removed is not added and links are actually removed as well // part1: part2: newAsset (BaseParts: part1): newAssetMerged (BaseParts: part2): // ERoot ERoot ERoot1 ERoot1 // EA EA EA1 (base: EA) EA1' (base: EA) // EB EB EB1 (base: EB) EB1' (base: EB) // EC + link: EA EC + link: EA EC1 (base: EC) + link: EA1 EC1' (base: EC) + link: EA1' // ED + link: EB ED1' (base: ED) + link: EB1' // ERoot2 ERoot2 // EA2 (base: EA) EA2' (base: EA) // // EC2 (base: EC) + link: EA2 EC2' (base: EC) + link: EA2' // ED2' (base: ED) + noLink var eRoot = new Entity("Root"); var entityA = new Entity() { Name = "A" }; var entityB = new Entity() { Name = "B" }; var entityC = new Entity() { Name = "C" }; // EC + link: EA entityC.Add(new TestEntityComponent() { EntityLink = entityA }); eRoot.Transform.Children.Add(entityA.Transform); eRoot.Transform.Children.Add(entityB.Transform); eRoot.Transform.Children.Add(entityC.Transform); // part1 Asset var part1 = new PrefabAsset(); part1.Hierarchy.Entities.Add(new EntityDesign(eRoot, new EntityDesignData())); part1.Hierarchy.Entities.Add(new EntityDesign(entityA, new EntityDesignData())); part1.Hierarchy.Entities.Add(new EntityDesign(entityB, new EntityDesignData())); part1.Hierarchy.Entities.Add(new EntityDesign(entityC, new EntityDesignData())); part1.Hierarchy.RootEntities.Add(eRoot.Id); // part2 Asset var part2 = (PrefabAsset)AssetCloner.Clone(part1); var eRootPart2 = part2.Hierarchy.Entities.Where(it => it.Entity.Name == "Root").Select(it => it.Entity).First(); var entityD = new Entity() { Name = "D" }; eRootPart2.Transform.Children.Add(entityD.Transform); // ED + link: EB var entityBFrom2 = part2.Hierarchy.Entities.Where(it => it.Entity.Name == "B").Select(it => it.Entity).First(); entityD.Add(new TestEntityComponent() { EntityLink = entityBFrom2 }); part2.Hierarchy.Entities.Add(new EntityDesign(entityD, new EntityDesignData())); // originalAsset: Add a new instanceId for this part var asset = new PrefabAsset(); // Create derived parts //var eRoot1Asset = (PrefabAsset)part1.CreateChildAsset("part"); //var eRoot2Asset = (PrefabAsset)part1.CreateChildAsset("part"); //asset.AddPart(eRoot1Asset); //asset.AddPart(eRoot2Asset); Guid eRoot1Id; Guid eRoot2Id; var eRoot1Asset = part1.CreatePrefabInstance(asset, "part", out eRoot1Id); var eRoot2Asset = part1.CreatePrefabInstance(asset, "part", out eRoot2Id); asset.Hierarchy.Entities.AddRange(eRoot1Asset.Entities); asset.Hierarchy.Entities.AddRange(eRoot2Asset.Entities); asset.Hierarchy.RootEntities.AddRange(eRoot1Asset.RootEntities); asset.Hierarchy.RootEntities.AddRange(eRoot2Asset.RootEntities); //var eRoot2 = asset.Hierarchy.Entities[eRoot2Asset.Hierarchy.RootEntities[0]]; var eRoot2 = asset.Hierarchy.Entities[eRoot2Asset.RootEntities[0]]; var entityToRemove = eRoot2.Entity.Transform.Children.First(it => it.Entity.Name == "B"); eRoot2.Entity.Transform.Children.Remove(entityToRemove); asset.Hierarchy.Entities.Remove(entityToRemove.Entity.Id); // Merge entities (NOTE: it is important to clone baseAsset/newBaseAsset) var entityMerge = asset.Merge(null, null, new List<AssetBase>() { new AssetBase("part", part2) } ); Assert.False(entityMerge.HasErrors); // EntityD must be now part of the new asset Assert.AreEqual(2, asset.Hierarchy.RootEntities.Count); Assert.AreEqual(9, asset.Hierarchy.Entities.Count); Assert.AreEqual(2, asset.Hierarchy.Entities.Count(it => it.Entity.Name == "D")); foreach (var entity in asset.Hierarchy.Entities.Where(it => it.Entity.Name == "D")) { // Check that we have the correct baesId and basePartInstanceId Assert.True(entity.Design.BasePartInstanceId.HasValue); Assert.True(entity.Design.BaseId.HasValue); Assert.AreEqual(entityD.Id, entity.Design.BaseId.Value); } var entityDesignD1 = asset.Hierarchy.Entities[asset.Hierarchy.Entities[asset.Hierarchy.RootEntities[0]].Entity.Transform.Children.Where(it => it.Entity.Name == "D").Select(it => it.Entity.Id).FirstOrDefault()]; Assert.NotNull(entityDesignD1); //Assert.AreEqual(eRoot1Asset.Id, entityDesignD1.Design.BasePartInstanceId); Assert.AreEqual(eRoot1Id, entityDesignD1.Design.BasePartInstanceId); var testComponentD1 = entityDesignD1.Entity.Get<TestEntityComponent>(); Assert.NotNull(testComponentD1); var entityB1 = asset.Hierarchy.Entities[asset.Hierarchy.RootEntities[0]].Entity.Transform.Children.Where(it => it.Entity.Name == "B").Select(it => it.Entity).First(); Assert.AreEqual(entityB1, testComponentD1.EntityLink); var entityDesignD2 = asset.Hierarchy.Entities[asset.Hierarchy.Entities[asset.Hierarchy.RootEntities[1]].Entity.Transform.Children.Where(it => it.Entity.Name == "D").Select(it => it.Entity.Id).FirstOrDefault()]; Assert.NotNull(entityDesignD2); //Assert.AreEqual(eRoot2Asset.Id, entityDesignD2.Design.BasePartInstanceId); Assert.AreEqual(eRoot2Id, entityDesignD2.Design.BasePartInstanceId); var testComponentD2 = entityDesignD2.Entity.Get<TestEntityComponent>(); Assert.NotNull(testComponentD2); Assert.AreEqual(null, testComponentD2.EntityLink); }
public void TestCascadedInheritance() { // Test with: // a1: an asset with 2 entities // a2: an asset using a1 by composition with 2 instances // a3: an asset based on a2 // // Add one entity to a1. Check that a2 and a3 will get correctly the entities replicated // Before Merge // a1: a2: (baseParts: a1, 2 instances) a3: (base: a2) // | ea | ea1 (base: ea) | ea1' (base: ea1) // | eb | eb1 (base: eb) | eb1' (base: eb1) // | ea2 (base: ea) | ea2' (base: ea2) // | eb2 (base: eb) | eb2' (base: eb2) // After Merge // We add one entity to the base a1 // a1: a2: (baseParts: a1, 2 instances) a3: (base: a2) // | ea | ea1 (base: ea) | ea1' (base: ea1) // | eb | eb1 (base: eb) | eb1' (base: eb1) // | ec | ec1 (base: ec) | ec1' (base: ec1) // | ea2 (base: ea) | ea2' (base: ea2) // | eb2 (base: eb) | eb2' (base: eb2) // | ec2 (base: ec) | ec2' (base: ec2) var a1 = new PrefabAsset(); var ea = new Entity("ea"); var eb = new Entity("eb"); a1.Hierarchy.Entities.Add(ea); a1.Hierarchy.Entities.Add(eb); a1.Hierarchy.RootEntities.Add(ea.Id); a1.Hierarchy.RootEntities.Add(eb.Id); var a2 = new PrefabAsset(); var aPartInstance1 = a1.CreatePrefabInstance(a2, "a1"); var aPartInstance2 = a1.CreatePrefabInstance(a2, "a1"); a2.Hierarchy.Entities.AddRange(aPartInstance1.Entities); a2.Hierarchy.Entities.AddRange(aPartInstance2.Entities); a2.Hierarchy.RootEntities.AddRange(aPartInstance1.RootEntities); a2.Hierarchy.RootEntities.AddRange(aPartInstance2.RootEntities); // Modify a1 to add entity ec var ec = new Entity("ec"); a1.Hierarchy.Entities.Add(ec); a1.Hierarchy.RootEntities.Add(ec.Id); var a3 = (PrefabAsset)a2.CreateChildAsset("a2"); // Merge a2 var result2 = a2.Merge(null, null, new List<AssetBase>() { new AssetBase("a1", (Asset)AssetCloner.Clone(a1)) }); Assert.False(result2.HasErrors); Assert.AreEqual(6, a2.Hierarchy.RootEntities.Count); Assert.True(a2.Hierarchy.Entities.All(it => it.Design.BaseId.HasValue && it.Design.BasePartInstanceId.HasValue)); // Merge a3 var result3 = a3.Merge((Asset)AssetCloner.Clone(a3.Base.Asset), (Asset)AssetCloner.Clone(a2), null); Assert.False(result3.HasErrors); Assert.AreEqual(6, a3.Hierarchy.RootEntities.Count); Assert.True(a3.Hierarchy.Entities.All(it => !it.Design.BasePartInstanceId.HasValue)); Assert.True(a3.Hierarchy.Entities.All(it => it.Design.BaseId.HasValue && a2.Hierarchy.Entities.ContainsKey(it.Design.BaseId.Value))); }
public void TestMergeSimpleWithBasePartsAndLinks() { // part1: part2: newAsset (BaseParts: part1): newAssetMerged (BaseParts: part2): // EA EA EA1 (base: EA) [0] EA1' (base: EA) // EB EB EB1 (base: EB) [1] EB1' (base: EB) // EC + link: EA EC + link: EA EC1 (base: EC) + link: EA1 [2] EC1' (base: EC) + link: EA1' // ED + link: EB EA2 (base: EA) [3] EA2' (base: EA) // EB2 (base: EB) [4] EB2' (base: EB) // EC2 (base: EC) + link: EA2 [5] EC2' (base: EC) + link: EA2' // [6] ED1' (base: ED) + link: EB1' // [7] ED2' (base: ED) + link: EB2' var entityA = new Entity() { Name = "A" }; var entityB = new Entity() { Name = "B" }; var entityC = new Entity() { Name = "C" }; // EC + link: EA entityC.Add(new TestEntityComponent() { EntityLink = entityA }); // part1 Asset var basePart = new PrefabAsset(); basePart.Hierarchy.Entities.Add(new EntityDesign(entityA, new EntityDesignData())); basePart.Hierarchy.Entities.Add(new EntityDesign(entityB, new EntityDesignData())); basePart.Hierarchy.Entities.Add(new EntityDesign(entityC, new EntityDesignData())); basePart.Hierarchy.RootEntities.Add(entityA.Id); basePart.Hierarchy.RootEntities.Add(entityB.Id); basePart.Hierarchy.RootEntities.Add(entityC.Id); // originalAsset: Add a new instanceId for this part var asset = new PrefabAsset(); // Create part1 asset Guid part1InstanceId; Guid part12InstanceId; var part1 = basePart.CreatePrefabInstance(asset, "part", out part1InstanceId); var entityB1 = part1.Entities.First(it => it.Entity.Name == "B").Entity; var part12 = basePart.CreatePrefabInstance(asset, "part", out part12InstanceId); var entityB2 = part12.Entities.First(it => it.Entity.Name == "B").Entity; // create part2 assset var entityD = new Entity() { Name = "D" }; basePart.Hierarchy.Entities.Add(new EntityDesign(entityD, new EntityDesignData())); basePart.Hierarchy.RootEntities.Add(entityD.Id); // ED + link: EB var entityBFrom2 = basePart.Hierarchy.Entities.Where(it => it.Entity.Name == "B").Select(it => it.Entity).First(); entityD.Add(new TestEntityComponent() { EntityLink = entityBFrom2 }); asset.Hierarchy.Entities.AddRange(part1.Entities); asset.Hierarchy.Entities.AddRange(part12.Entities); asset.Hierarchy.RootEntities.AddRange(part1.RootEntities); asset.Hierarchy.RootEntities.AddRange(part12.RootEntities); // Merge entities (NOTE: it is important to clone baseAsset/newBaseAsset) var entityMerge = asset.Merge(null, null, new List<AssetBase>() { new AssetBase("part", (Asset)AssetCloner.Clone(basePart)) } ); Assert.False(entityMerge.HasErrors); // EntityD must be now part of the new asset Assert.AreEqual(8, asset.Hierarchy.RootEntities.Count); Assert.AreEqual(8, asset.Hierarchy.Entities.Count); Assert.AreEqual(2, asset.Hierarchy.Entities.Count(it => it.Entity.Name == "D")); foreach (var entity in asset.Hierarchy.Entities.Where(it => it.Entity.Name == "D")) { // Check that we have the correct baesId and basePartInstanceId Assert.True(entity.Design.BasePartInstanceId.HasValue); Assert.True(entity.Design.BaseId.HasValue); Assert.AreEqual(entityD.Id, entity.Design.BaseId.Value); // Make sure that the entity is in the RootEntities Assert.True(asset.Hierarchy.RootEntities.Contains(entity.Entity.Id)); } var entityDesignD1 = asset.Hierarchy.Entities.FirstOrDefault(it => it.Entity.Name == "D" && it.Design.BasePartInstanceId == part1InstanceId); Assert.NotNull(entityDesignD1); var entityDesignD2 = asset.Hierarchy.Entities.FirstOrDefault(it => it.Entity.Name == "D" && it.Design.BasePartInstanceId == part12InstanceId); Assert.NotNull(entityDesignD2); // Check components var testComponentD1 = entityDesignD1.Entity.Get<TestEntityComponent>(); Assert.NotNull(testComponentD1); Assert.AreEqual(entityB1, testComponentD1.EntityLink); var testComponentD2 = entityDesignD2.Entity.Get<TestEntityComponent>(); Assert.NotNull(testComponentD2); Assert.AreEqual(entityB2, testComponentD2.EntityLink); }