public void EntityDiffer_GetChanges_IgnoresSystemState() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var entityGuid = CreateEntityGuid(); var entity = SrcEntityManager.CreateEntity(); SrcEntityManager.AddComponentData(entity, entityGuid); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) {} SrcEntityManager.AddComponentData(entity, new EcsState1 { Value = 9 }); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsFalse(changes.AnyChanges); } SrcEntityManager.SetComponentData(entity, new EcsState1 { Value = 10 }); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsFalse(changes.AnyChanges); } // NOTE: the system state component being copied to shadow world is not required by the public API. // This is simply defining the expected internal behaviour. Assert.AreEqual(10, differ.ShadowEntityManager.GetComponentData <EcsState1>(entity).Value); } }
public void EntityPatcher_ApplyChanges_RemapEntityReferences() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { // Create extra entity to make sure test doesn't accidentally succeed with no remapping SrcEntityManager.CreateEntity(); var entityGuid0 = CreateEntityGuid(); var entityGuid1 = CreateEntityGuid(); var e0 = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestDataEntity)); var e1 = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestDataEntity)); SrcEntityManager.SetComponentData(e0, entityGuid0); SrcEntityManager.SetComponentData(e1, entityGuid1); SrcEntityManager.SetComponentData(e0, new EcsTestDataEntity { value1 = e1 }); SrcEntityManager.SetComponentData(e1, new EcsTestDataEntity { value1 = e0 }); PushChanges(differ, DstEntityManager); Assert.AreEqual(GetEntity(DstEntityManager, entityGuid1), GetComponentData <EcsTestDataEntity>(DstEntityManager, entityGuid0).value1); Assert.AreEqual(GetEntity(DstEntityManager, entityGuid0), GetComponentData <EcsTestDataEntity>(DstEntityManager, entityGuid1).value1); } }
public void EntityDiffer_GetChanges_CreateEntityAndSetSharedComponentData_IncrementalChanges_ManagedComponents() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestSharedComp), typeof(EcsTestManagedComponent)); SrcEntityManager.SetComponentData(entity, CreateEntityGuid()); SrcEntityManager.SetComponentData(entity, new EcsTestManagedComponent { value = "SomeString" }); SrcEntityManager.SetSharedComponentData(entity, new EcsTestSharedComp { value = 2 }); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); Assert.AreEqual(1, changes.ForwardChangeSet.CreatedEntityCount); Assert.AreEqual(3, changes.ForwardChangeSet.AddComponents.Length); Assert.AreEqual(1, changes.ForwardChangeSet.SetComponents.Length); Assert.AreEqual(1, changes.ForwardChangeSet.SetSharedComponents.Length); Assert.AreEqual(1, changes.ForwardChangeSet.SetManagedComponents.Length); } } }
[StandaloneFixme] // No support for PinGCObject public void EntityPatcher_ApplyChanges_RemapEntityReferencesInManagedComponentCollection() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { // Create extra entity to make sure test doesn't accidentally succeed with no remapping SrcEntityManager.CreateEntity(); var entityGuid0 = CreateEntityGuid(); var entityGuid1 = CreateEntityGuid(); var e0 = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestManagedDataEntityCollection)); var e1 = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestManagedDataEntityCollection)); SrcEntityManager.SetComponentData(e0, entityGuid0); SrcEntityManager.SetComponentData(e1, entityGuid1); SrcEntityManager.SetComponentData(e0, new EcsTestManagedDataEntityCollection(new string[] { e1.ToString() }, new Entity[] { e1, e1, e1 })); SrcEntityManager.SetComponentData(e1, new EcsTestManagedDataEntityCollection(new string[] { e0.ToString() }, new Entity[] { e0, e0, e0 })); PushChanges(differ, DstEntityManager); var c0 = GetManagedComponentData <EcsTestManagedDataEntityCollection>(DstEntityManager, entityGuid0); var c1 = GetManagedComponentData <EcsTestManagedDataEntityCollection>(DstEntityManager, entityGuid1); Assert.AreEqual(GetEntity(DstEntityManager, entityGuid1), c0.value1[0]); Assert.AreEqual(GetEntity(DstEntityManager, entityGuid1), c0.value1[1]); Assert.AreEqual(GetEntity(DstEntityManager, entityGuid1), c0.value1[2]); Assert.AreEqual(GetEntity(DstEntityManager, entityGuid0), c1.value1[0]); Assert.AreEqual(GetEntity(DstEntityManager, entityGuid0), c1.value1[1]); Assert.AreEqual(GetEntity(DstEntityManager, entityGuid0), c1.value1[2]); } }
public void PerformanceTest_EntityDiffer_GetChanges_ForwardChangesOnly_SingleChange(int entityCount) { CreateEntitiesWithMockComponentData(SrcEntityManager, entityCount, typeof(EcsTestData), typeof(EcsTestData2), typeof(EcsTestSharedComp)); var entity = default(Entity); using (var entities = SrcEntityManager.GetAllEntities(Allocator.Temp)) { entity = entities[0]; } using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { // Fast forward the shadow world using (differ.GetChanges(EntityManagerDifferOptions.FastForwardShadowWorld, Allocator.TempJob)) { } var startValue = 99; Measure.Method(() => { SrcEntityManager.SetComponentData(entity, new EcsTestData(startValue++)); // Get changes with all options selected using (differ.GetChanges(EntityManagerDifferOptions.IncludeForwardChangeSet, Allocator.TempJob)) { } }) .Definition("EntityDiffer") .WarmupCount(1) .MeasurementCount(100) .Run(); } }
public void EntityPatcher_ApplyChanges_CreateEntityWithTestData() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestData)); var entityGuid = CreateEntityGuid(); SrcEntityManager.SetComponentData(entity, entityGuid); SrcEntityManager.SetComponentData(entity, new EcsTestData { value = 9 }); PushChanges(differ, DstEntityManager); Assert.AreEqual(1, DstEntityManager.Debug.EntityCount); Assert.AreEqual(9, GetComponentData <EcsTestData>(DstEntityManager, entityGuid).value); // Mutate some component data. SrcEntityManager.SetComponentData(entity, entityGuid); SrcEntityManager.SetComponentData(entity, new EcsTestData { value = 10 }); PushChanges(differ, DstEntityManager); Assert.AreEqual(1, DstEntityManager.Debug.EntityCount); Assert.AreEqual(10, GetComponentData <EcsTestData>(DstEntityManager, entityGuid).value); // Destroy the entity SrcEntityManager.DestroyEntity(entity); PushChanges(differ, DstEntityManager); Assert.AreEqual(0, DstEntityManager.Debug.EntityCount); } }
public unsafe void EntityDiffer_GetChanges_BlobAssets_SetComponent_SharedAsset() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var blobAssetReference = BlobAssetReference <int> .Create(10); try { for (var i = 0; i < 100; i++) { var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestDataBlobAssetRef)); SrcEntityManager.SetComponentData(entity, CreateEntityGuid()); SrcEntityManager.SetComponentData(entity, new EcsTestDataBlobAssetRef { value = blobAssetReference }); } using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); var forward = changes.ForwardChangeSet; Assert.That(forward.CreatedBlobAssets.Length, Is.EqualTo(1)); Assert.That(forward.BlobAssetReferenceChanges.Length, Is.EqualTo(100)); Assert.That(forward.BlobAssetData.Length, Is.EqualTo(sizeof(int))); Assert.That(*(int *)forward.BlobAssetData.GetUnsafePtr(), Is.EqualTo(10)); } } finally { blobAssetReference.Dispose(); } } }
public void EntityDiffer_GetChanges_CreateEntityAndSetComponentData_IncrementalChanges() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var entityGuid = CreateEntityGuid(); var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestData)); SrcEntityManager.SetComponentData(entity, entityGuid); SrcEntityManager.SetComponentData(entity, new EcsTestData { value = 9 }); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.AnyChanges); } // Mutate some component data. SrcEntityManager.SetComponentData(entity, entityGuid); SrcEntityManager.SetComponentData(entity, new EcsTestData { value = 10 }); // The entityGuid value is the same so it should not be picked up during change tracking. // We should only see the one data change. using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { // The ForwardChangeSet will contain a set value 10 Assert.IsTrue(changes.HasForwardChangeSet); Assert.AreEqual(0, changes.ForwardChangeSet.DestroyedEntityCount); Assert.AreEqual(0, changes.ForwardChangeSet.CreatedEntityCount); Assert.AreEqual(1, changes.ForwardChangeSet.SetComponents.Length); Assert.AreEqual(0, changes.ForwardChangeSet.AddComponents.Length); // The ReverseChangeSet will contain a set value 9 Assert.IsTrue(changes.HasReverseChangeSet); Assert.AreEqual(0, changes.ReverseChangeSet.DestroyedEntityCount); Assert.AreEqual(0, changes.ReverseChangeSet.CreatedEntityCount); Assert.AreEqual(1, changes.ReverseChangeSet.SetComponents.Length); Assert.AreEqual(0, changes.ReverseChangeSet.AddComponents.Length); } SrcEntityManager.DestroyEntity(entity); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); Assert.AreEqual(1, changes.ForwardChangeSet.DestroyedEntityCount); Assert.AreEqual(0, changes.ForwardChangeSet.CreatedEntityCount); Assert.AreEqual(0, changes.ForwardChangeSet.SetComponents.Length); Assert.AreEqual(0, changes.ForwardChangeSet.RemoveComponents.Length); // In this case the ReverseChangeSet should describe how to get this entity back in it's entirety Assert.IsTrue(changes.HasReverseChangeSet); Assert.AreEqual(0, changes.ReverseChangeSet.DestroyedEntityCount); Assert.AreEqual(1, changes.ReverseChangeSet.CreatedEntityCount); Assert.AreEqual(2, changes.ReverseChangeSet.AddComponents.Length); Assert.AreEqual(2, changes.ReverseChangeSet.SetComponents.Length); } } }
public unsafe void EntityDiffer_GetChanges_BlobAssets_SetComponent_SameContentHash() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var blobAssetReference0 = BlobAssetReference <int> .Create(10); var blobAssetReference1 = BlobAssetReference <int> .Create(10); var entity0 = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestDataBlobAssetRef)); SrcEntityManager.SetComponentData(entity0, CreateEntityGuid()); SrcEntityManager.SetComponentData(entity0, new EcsTestDataBlobAssetRef { value = blobAssetReference0 }); var entity1 = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestDataBlobAssetRef)); SrcEntityManager.SetComponentData(entity1, CreateEntityGuid()); SrcEntityManager.SetComponentData(entity1, new EcsTestDataBlobAssetRef { value = blobAssetReference1 }); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); var forward = changes.ForwardChangeSet; // Only one blob asset is created Assert.That(forward.CreatedBlobAssets.Length, Is.EqualTo(1)); Assert.That(forward.DestroyedBlobAssets.Length, Is.EqualTo(0)); Assert.That(forward.BlobAssetReferenceChanges.Length, Is.EqualTo(2)); Assert.That(forward.BlobAssetReferenceChanges[0].Value, Is.EqualTo(forward.CreatedBlobAssets[0].Hash)); Assert.That(forward.BlobAssetReferenceChanges[1].Value, Is.EqualTo(forward.CreatedBlobAssets[0].Hash)); Assert.That(forward.BlobAssetData.Length, Is.EqualTo(sizeof(int))); Assert.That(*(int *)forward.BlobAssetData.GetUnsafePtr(), Is.EqualTo(10)); } SrcEntityManager.DestroyEntity(entity1); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.That(changes.ForwardChangeSet.DestroyedBlobAssets.Length, Is.EqualTo(0)); } SrcEntityManager.DestroyEntity(entity0); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.That(changes.ForwardChangeSet.DestroyedBlobAssets.Length, Is.EqualTo(1)); } blobAssetReference0.Dispose(); blobAssetReference1.Dispose(); } }
public void EntityManagerPatcher_ApplyChanges_NewEntityIsReplicatedIntoExistingPrefabInstances([Values(1, 10)] int instanceCount) { using (var differ = new EntityManagerDiffer(SrcWorld, Allocator.TempJob)) using (var patcher = new EntityManagerPatcher(DstWorld, Allocator.TempJob)) { var rootEntityGuid = CreateEntityGuid(); var childEntityGuid = CreateEntityGuid(); var srcRootEntity = SrcEntityManager.CreateEntity(typeof(EcsTestDataEntity), typeof(Prefab), typeof(LinkedEntityGroup)); SrcEntityManager.AddComponentData(srcRootEntity, rootEntityGuid); SrcEntityManager.GetBuffer <LinkedEntityGroup>(srcRootEntity).Add(srcRootEntity); PushChanges(differ, patcher); var dstRootEntity = GetEntity(DstEntityManager, rootEntityGuid); // Instantiate root in dst world var dstRootInstances = new Entity[instanceCount]; for (var i = 0; i != dstRootInstances.Length; i++) { var dstRootInstance = DstEntityManager.Instantiate(dstRootEntity); dstRootInstances[i] = dstRootInstance; Assert.AreEqual(1, DstEntityManager.GetBuffer <LinkedEntityGroup>(dstRootInstance).Length); Assert.AreEqual(dstRootInstance, DstEntityManager.GetBuffer <LinkedEntityGroup>(dstRootInstance)[0].Value); } // Add a new entity into the prefab var srcChildEntity = SrcEntityManager.CreateEntity(typeof(EcsTestDataEntity), typeof(Prefab)); SrcEntityManager.AddComponentData(srcChildEntity, childEntityGuid); SrcEntityManager.GetBuffer <LinkedEntityGroup>(srcRootEntity).Add(srcChildEntity); SrcEntityManager.SetComponentData(srcRootEntity, new EcsTestDataEntity { value1 = srcChildEntity }); SrcEntityManager.SetComponentData(srcChildEntity, new EcsTestDataEntity { value1 = srcRootEntity }); PushChanges(differ, patcher); for (var i = 0; i != dstRootInstances.Length; i++) { var dstRootInstance = dstRootInstances[i]; var dstInstanceGroup = DstEntityManager.GetBuffer <LinkedEntityGroup>(dstRootInstance); Assert.AreEqual(2, dstInstanceGroup.Length); Assert.AreEqual(dstRootInstance, dstInstanceGroup[0].Value); var dstChildInstance = dstInstanceGroup[1].Value; Assert.IsTrue(DstEntityManager.HasComponent <Prefab>(dstRootEntity)); Assert.IsFalse(DstEntityManager.HasComponent <Prefab>(dstRootInstance)); Assert.IsFalse(DstEntityManager.HasComponent <Prefab>(dstChildInstance)); Assert.AreEqual(dstRootInstance, DstEntityManager.GetComponentData <EcsTestDataEntity>(dstChildInstance).value1); Assert.AreEqual(dstChildInstance, DstEntityManager.GetComponentData <EcsTestDataEntity>(dstRootInstance).value1); } } }
public void ReplaceChangedEntities() { CreateTestData(out var entity, out var metaEntity, 5, 7); DstEntityManager.CopyAndReplaceEntitiesFrom(SrcEntityManager); SrcEntityManager.SetComponentData(entity, new EcsTestData(11)); DstEntityManager.CopyAndReplaceEntitiesFrom(SrcEntityManager); TestValues(entity, metaEntity, 11, 7); }
public unsafe void EntityDiffer_GetChanges_BlobAssets_SetComponent_TypeMemoryOrdering() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var blobAssetReferences = new NativeArray <BlobAssetReference <int> >(100, Allocator.Temp); for (var i = 0; i < blobAssetReferences.Length; i++) { // Construct the archetype in an order which will NOT match the memory order. var archetype = SrcEntityManager.CreateArchetype( typeof(EntityGuid), typeof(EcsTestDataBlobAssetRef), typeof(EcsTestData4)); // Validate the assumption that the archetype is created in this way. Assert.That(archetype.Archetype->TypeMemoryOrder[0], Is.EqualTo(0)); Assert.That(archetype.Archetype->TypeMemoryOrder[1], Is.EqualTo(2)); Assert.That(archetype.Archetype->TypeMemoryOrder[2], Is.EqualTo(1)); // Validate the component sizes are different Assert.That(UnsafeUtility.SizeOf <EcsTestDataBlobAssetRef>(), Is.Not.EqualTo(UnsafeUtility.SizeOf <EcsTestData4>())); var entity = SrcEntityManager.CreateEntity(archetype); blobAssetReferences[i] = BlobAssetReference <int> .Create(i); SrcEntityManager.SetComponentData(entity, CreateEntityGuid()); SrcEntityManager.SetComponentData(entity, new EcsTestData4()); SrcEntityManager.SetComponentData(entity, new EcsTestDataBlobAssetRef { value = blobAssetReferences[i] }); } try { using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); var forward = changes.ForwardChangeSet; Assert.That(forward.CreatedBlobAssets.Length, Is.EqualTo(100)); Assert.That(forward.BlobAssetReferenceChanges.Length, Is.EqualTo(100)); Assert.That(forward.BlobAssetData.Length, Is.EqualTo(sizeof(int) * 100)); } } finally { for (var i = 0; i < blobAssetReferences.Length; i++) { blobAssetReferences[i].Dispose(); } blobAssetReferences.Dispose(); } } }
public void EntityDiffer_GetChanges_CreateEntityAndSetComponentData_WithoutFastForward_ManagedComponents() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestData), typeof(EcsTestManagedComponent)); SrcEntityManager.SetComponentData(entity, CreateEntityGuid()); SrcEntityManager.SetComponentData(entity, new EcsTestData { value = 9 }); SrcEntityManager.SetComponentData(entity, new EcsTestManagedComponent { value = "SomeString" }); const EntityManagerDifferOptions options = EntityManagerDifferOptions.IncludeForwardChangeSet | EntityManagerDifferOptions.IncludeReverseChangeSet; using (var changes = differ.GetChanges(options, Allocator.Temp)) { // ForwardChanges defines all operations needed to go from the shadow state to the current state. Assert.IsTrue(changes.HasForwardChangeSet); Assert.AreEqual(0, changes.ForwardChangeSet.DestroyedEntityCount); Assert.AreEqual(1, changes.ForwardChangeSet.CreatedEntityCount); Assert.AreEqual(3, changes.ForwardChangeSet.AddComponents.Length); Assert.AreEqual(2, changes.ForwardChangeSet.SetComponents.Length); Assert.AreEqual(1, changes.ForwardChangeSet.SetManagedComponents.Length); // ReverseChanges defines all operations needed to go from the current state back to the last shadow state. (i.e. Undo) Assert.IsTrue(changes.HasReverseChangeSet); Assert.AreEqual(1, changes.ReverseChangeSet.DestroyedEntityCount); Assert.AreEqual(0, changes.ReverseChangeSet.CreatedEntityCount); Assert.AreEqual(0, changes.ReverseChangeSet.AddComponents.Length); Assert.AreEqual(0, changes.ReverseChangeSet.SetComponents.Length); Assert.AreEqual(0, changes.ReverseChangeSet.SetManagedComponents.Length); } // Since we did not fast forward the inner shadow world. We should be able to generate the exact same changes again. using (var changes = differ.GetChanges(options, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); Assert.AreEqual(0, changes.ForwardChangeSet.DestroyedEntityCount); Assert.AreEqual(1, changes.ForwardChangeSet.CreatedEntityCount); Assert.AreEqual(3, changes.ForwardChangeSet.AddComponents.Length); Assert.AreEqual(2, changes.ForwardChangeSet.SetComponents.Length); Assert.AreEqual(1, changes.ForwardChangeSet.SetManagedComponents.Length); Assert.IsTrue(changes.HasReverseChangeSet); Assert.AreEqual(1, changes.ReverseChangeSet.DestroyedEntityCount); Assert.AreEqual(0, changes.ReverseChangeSet.CreatedEntityCount); Assert.AreEqual(0, changes.ReverseChangeSet.AddComponents.Length); Assert.AreEqual(0, changes.ReverseChangeSet.SetComponents.Length); Assert.AreEqual(0, changes.ReverseChangeSet.SetManagedComponents.Length); } } }
public void EntityDiffer_GetChanges_BlobAssets_SetBuffer_MultipleEntities() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var blobAssetReferences = new NativeArray <BlobAssetReference <int> >(100, Allocator.Temp); for (var i = 0; i < blobAssetReferences.Length; i++) { blobAssetReferences[i] = BlobAssetReference <int> .Create(i); } for (var i = 0; i < blobAssetReferences.Length; i += 4) { var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid)); SrcEntityManager.SetComponentData(entity, CreateEntityGuid()); var buffer = SrcEntityManager.AddBuffer <EcsTestDataBlobAssetElement>(entity); buffer.Add(new EcsTestDataBlobAssetElement { blobElement = blobAssetReferences[i + 0] }); buffer.Add(new EcsTestDataBlobAssetElement { blobElement = blobAssetReferences[i + 1] }); buffer.Add(new EcsTestDataBlobAssetElement { blobElement = blobAssetReferences[i + 2] }); buffer.Add(new EcsTestDataBlobAssetElement { blobElement = blobAssetReferences[i + 3] }); } try { using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); var forward = changes.ForwardChangeSet; Assert.That(forward.CreatedBlobAssets.Length, Is.EqualTo(100)); Assert.That(forward.BlobAssetReferenceChanges.Length, Is.EqualTo(100)); Assert.That(forward.BlobAssetData.Length, Is.EqualTo(sizeof(int) * 100)); } } finally { for (var i = 0; i < blobAssetReferences.Length; i++) { blobAssetReferences[i].Dispose(); } blobAssetReferences.Dispose(); } } }
public void EntityPatcher_ApplyChanges_CreateEntityWithDisabledComponent() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var entityGuid = CreateEntityGuid(); var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(Disabled)); SrcEntityManager.SetComponentData(entity, entityGuid); PushChanges(differ, DstEntityManager); Assert.IsTrue(HasComponent <Disabled>(DstEntityManager, entityGuid)); } }
public void EntityManagerPatcher_ApplyChanges_CreateEntityWithPrefabComponent() { using (var differ = new EntityManagerDiffer(SrcWorld, Allocator.TempJob)) using (var patcher = new EntityManagerPatcher(DstWorld, Allocator.TempJob)) { var entityGuid = CreateEntityGuid(); var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(Prefab)); SrcEntityManager.SetComponentData(entity, entityGuid); PushChanges(differ, patcher); Assert.IsTrue(HasComponent <Prefab>(DstEntityManager, entityGuid)); } }
public void EntityDiffer_GetChanges_CreateEntityAndSetComponentData_WithFastForward_ManagedComponents() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestData), typeof(EcsTestManagedComponent)); var entityGuid = CreateEntityGuid(); SrcEntityManager.SetComponentData(entity, entityGuid); SrcEntityManager.SetComponentData(entity, new EcsTestData { value = 9 }); SrcEntityManager.SetComponentData(entity, new EcsTestManagedComponent { value = "SomeString" }); const EntityManagerDifferOptions options = EntityManagerDifferOptions.IncludeForwardChangeSet | EntityManagerDifferOptions.IncludeReverseChangeSet | EntityManagerDifferOptions.FastForwardShadowWorld; using (var changes = differ.GetChanges(options, Allocator.Temp)) { // Forward changes is all changes needed to go from the shadow state to the current state. Assert.IsTrue(changes.HasForwardChangeSet); Assert.AreEqual(0, changes.ForwardChangeSet.DestroyedEntityCount); Assert.AreEqual(1, changes.ForwardChangeSet.CreatedEntityCount); Assert.AreEqual(3, changes.ForwardChangeSet.AddComponents.Length); Assert.AreEqual(2, changes.ForwardChangeSet.SetComponents.Length); Assert.AreEqual(1, changes.ForwardChangeSet.SetManagedComponents.Length); // Reverse changes is all changes needed to go from the current state back to the last shadow state. (i.e. Undo) Assert.IsTrue(changes.HasReverseChangeSet); Assert.AreEqual(1, changes.ReverseChangeSet.DestroyedEntityCount); Assert.AreEqual(0, changes.ReverseChangeSet.CreatedEntityCount); Assert.AreEqual(0, changes.ReverseChangeSet.AddComponents.Length); Assert.AreEqual(0, changes.ReverseChangeSet.SetComponents.Length); Assert.AreEqual(0, changes.ReverseChangeSet.SetManagedComponents.Length); } // The inner shadow world was updated during the last call which means no new changes should be found. using (var changes = differ.GetChanges(options, Allocator.Temp)) { Assert.IsFalse(changes.AnyChanges); } } }
public void EntityDiffer_GetChanges_BlobAssets_SetComponent_Null() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestDataBlobAssetRef)); SrcEntityManager.SetComponentData(entity, CreateEntityGuid()); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); var forward = changes.ForwardChangeSet; Assert.That(forward.BlobAssetReferenceChanges.Length, Is.EqualTo(1)); Assert.That(forward.BlobAssetReferenceChanges[0].Value, Is.EqualTo(0)); Assert.That(forward.CreatedBlobAssets.Length, Is.EqualTo(0)); Assert.That(forward.BlobAssetData.Length, Is.EqualTo(0)); } } }
public void EntityManagerDiffer_GetChanges_DuplicateEntityGuidThrows() { using (var tracker = new EntityManagerDiffer(SrcWorld, Allocator.TempJob)) { var entityGuid = CreateEntityGuid(); var entity0 = SrcEntityManager.CreateEntity(typeof(EntityGuid)); var entity1 = SrcEntityManager.CreateEntity(typeof(EntityGuid)); SrcEntityManager.SetComponentData(entity0, entityGuid); SrcEntityManager.SetComponentData(entity1, entityGuid); Assert.Throws <DuplicateEntityGuidException>(() => { using (tracker.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { } }); } }
public void EntityPatcher_ApplyChanges_BlobAssets_CreateEntityWithBlobAssetReference() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var blobAssetReference = BlobAssetReference <int> .Create(11); var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestDataBlobAssetRef)); var entityGuid = CreateEntityGuid(); SrcEntityManager.SetComponentData(entity, entityGuid); SrcEntityManager.SetComponentData(entity, new EcsTestDataBlobAssetRef { value = blobAssetReference }); PushChanges(differ, DstEntityManager); Assert.AreEqual(1, DstEntityManager.Debug.EntityCount); Assert.AreEqual(11, GetComponentData <EcsTestDataBlobAssetRef>(DstEntityManager, entityGuid).value.Value); } }
public void EntityDiffer_GetChanges_DuplicateEntityGuidThrows() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { void GetChanges() { using (differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { } } var entityGuid0 = CreateEntityGuid(); var entityGuid1 = CreateEntityGuid(); var entityGuid2 = CreateEntityGuid(); var entityGuid3 = CreateEntityGuid(); var entity0 = SrcEntityManager.CreateEntity(typeof(EntityGuid)); var entity1 = SrcEntityManager.CreateEntity(typeof(EntityGuid)); var entity2 = SrcEntityManager.CreateEntity(typeof(EntityGuid)); var entity3 = SrcEntityManager.CreateEntity(typeof(EntityGuid)); SrcEntityManager.SetComponentData(entity0, entityGuid0); SrcEntityManager.SetComponentData(entity1, entityGuid1); SrcEntityManager.SetComponentData(entity2, entityGuid2); SrcEntityManager.SetComponentData(entity3, entityGuid3); Assert.DoesNotThrow(GetChanges); SrcEntityManager.SetComponentData(entity1, entityGuid0); SrcEntityManager.SetComponentData(entity2, entityGuid0); SrcEntityManager.SetComponentData(entity3, entityGuid0); var dup0 = Assert.Throws <DuplicateEntityGuidException>(GetChanges).DuplicateEntityGuids; Assert.That(dup0, Is.EquivalentTo(new[] { new DuplicateEntityGuid(entityGuid0, 3) })); SrcEntityManager.SetComponentData(entity0, entityGuid1); SrcEntityManager.SetComponentData(entity3, entityGuid1); var dup1 = Assert.Throws <DuplicateEntityGuidException>(GetChanges).DuplicateEntityGuids; Assert.That(dup1, Is.EquivalentTo(new[] { new DuplicateEntityGuid(entityGuid0, 1), new DuplicateEntityGuid(entityGuid1, 1) })); } }
public void EntityManagerPatcher_ApplyChanges_ChangeAppliesToAllPrefabInstances([Values] bool prefabTag) { using (var differ = new EntityManagerDiffer(SrcWorld, Allocator.TempJob)) using (var patcher = new EntityManagerPatcher(DstWorld, Allocator.TempJob)) { // Create a prefab in the source world. var entityGuid = CreateEntityGuid(); var prefab = SrcEntityManager.CreateEntity(); SrcEntityManager.AddComponentData(prefab, entityGuid); SrcEntityManager.AddComponentData(prefab, new EcsTestData()); if (prefabTag) { SrcEntityManager.AddComponentData(prefab, new Prefab()); } // Sync to the dst world. At this point the dst world will have a single entity. PushChanges(differ, patcher); var dstPrefab = GetEntity(DstEntityManager, entityGuid); // Spawn some more instances of this thing in the dst world. var dstInstance0 = DstEntityManager.Instantiate(dstPrefab); var dstInstance1 = DstEntityManager.Instantiate(dstPrefab); // Mutate the original prefab in the src world. SrcEntityManager.SetComponentData(prefab, new EcsTestData(10)); // Sync to the dst world. PushChanges(differ, patcher); // The changes should be propagated to all instances. Assert.AreEqual(10, DstEntityManager.GetComponentData <EcsTestData>(dstPrefab).value); Assert.AreEqual(10, DstEntityManager.GetComponentData <EcsTestData>(dstInstance0).value); Assert.AreEqual(10, DstEntityManager.GetComponentData <EcsTestData>(dstInstance1).value); } }
public unsafe void EntityDiffer_GetChanges_BlobAssets_SetComponent() { using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { var blobAssetReference0 = BlobAssetReference <int> .Create(10); var entity = SrcEntityManager.CreateEntity(typeof(EntityGuid), typeof(EcsTestDataBlobAssetRef)); SrcEntityManager.SetComponentData(entity, CreateEntityGuid()); SrcEntityManager.SetComponentData(entity, new EcsTestDataBlobAssetRef { value = blobAssetReference0 }); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); var forward = changes.ForwardChangeSet; Assert.That(forward.CreatedBlobAssets.Length, Is.EqualTo(1)); Assert.That(forward.DestroyedBlobAssets.Length, Is.EqualTo(0)); Assert.That(forward.BlobAssetReferenceChanges.Length, Is.EqualTo(1)); Assert.That(forward.BlobAssetReferenceChanges[0].Value, Is.EqualTo(forward.CreatedBlobAssets[0].Hash)); Assert.That(forward.BlobAssetData.Length, Is.EqualTo(sizeof(int))); Assert.That(*(int *)forward.BlobAssetData.GetUnsafePtr(), Is.EqualTo(10)); } blobAssetReference0.Release(); var blobAssetReference1 = BlobAssetReference <int> .Create(20); SrcEntityManager.SetComponentData(entity, new EcsTestDataBlobAssetRef { value = blobAssetReference1 }); using (var changes = differ.GetChanges(EntityManagerDifferOptions.Default, Allocator.Temp)) { Assert.IsTrue(changes.HasForwardChangeSet); var forward = changes.ForwardChangeSet; Assert.That(forward.CreatedBlobAssets.Length, Is.EqualTo(1)); Assert.That(forward.BlobAssetReferenceChanges.Length, Is.EqualTo(1)); Assert.That(forward.BlobAssetData.Length, Is.EqualTo(sizeof(int))); Assert.That(*(int *)forward.BlobAssetData.GetUnsafePtr(), Is.EqualTo(20)); Assert.That(forward.BlobAssetReferenceChanges[0].Value, Is.EqualTo(forward.CreatedBlobAssets[0].Hash)); Assert.IsTrue(changes.HasReverseChangeSet); var reverse = changes.ReverseChangeSet; Assert.That(reverse.CreatedBlobAssets.Length, Is.EqualTo(1)); Assert.That(reverse.BlobAssetReferenceChanges.Length, Is.EqualTo(1)); Assert.That(reverse.BlobAssetData.Length, Is.EqualTo(sizeof(int))); Assert.That(*(int *)reverse.BlobAssetData.GetUnsafePtr(), Is.EqualTo(10)); Assert.That(reverse.BlobAssetReferenceChanges[0].Value, Is.EqualTo(reverse.CreatedBlobAssets[0].Hash)); } blobAssetReference1.Release(); } }
public void UndoRedoPrefabInstancesWithRelationship() { const int instanceCount = 10; var srcPrefabRootGuid = CreateEntityGuid(); var srcPrefabLevel0Guid = CreateEntityGuid(); var prefabLevel1Guid = CreateEntityGuid(); const EntityManagerDifferOptions options = EntityManagerDifferOptions.IncludeForwardChangeSet | EntityManagerDifferOptions.FastForwardShadowWorld; using (var differ = new EntityManagerDiffer(SrcEntityManager, Allocator.TempJob)) { // Create Root Prefab entity with a Guid component var srcPrefabRoot = SrcEntityManager.CreateEntity(typeof(HierarchyComponent), typeof(Prefab), typeof(LinkedEntityGroup)); SrcEntityManager.AddComponentData(srcPrefabRoot, srcPrefabRootGuid); SrcEntityManager.GetBuffer <LinkedEntityGroup>(srcPrefabRoot).Add(srcPrefabRoot); PushChanges(differ, DstEntityManager); using (var changes = differ.GetChanges(options, Allocator.TempJob)) { EntityPatcher.ApplyChangeSet(DstEntityManager, changes.ForwardChangeSet); } // Instantiate root prefab in dst world 10 times var dstPrefabRoot = GetEntity(DstEntityManager, srcPrefabRootGuid); var dstInstanceRoots = new Entity[instanceCount]; for (var i = 0; i != dstInstanceRoots.Length; i++) { var dstInstanceRoot = DstEntityManager.Instantiate(dstPrefabRoot); dstInstanceRoots[i] = dstInstanceRoot; Assert.AreEqual(1, DstEntityManager.GetBuffer <LinkedEntityGroup>(dstInstanceRoot).Length); Assert.AreEqual(dstInstanceRoot, DstEntityManager.GetBuffer <LinkedEntityGroup>(dstInstanceRoot)[0].Value); } // Add level 0 entity to the prefab var srcLevel0Prefab = SrcEntityManager.CreateEntity(typeof(HierarchyComponent), typeof(Prefab)); SrcEntityManager.AddComponentData(srcLevel0Prefab, srcPrefabLevel0Guid); SrcEntityManager.GetBuffer <LinkedEntityGroup>(srcPrefabRoot).Add(srcLevel0Prefab); SrcEntityManager.SetComponentData(srcPrefabRoot, new HierarchyComponent { Parent = Entity.Null, Child = srcLevel0Prefab }); SrcEntityManager.SetComponentData(srcLevel0Prefab, new HierarchyComponent { Parent = srcPrefabRoot, Child = Entity.Null }); // Synchronize worlds, we now should have 10 instances in the world along with a level0 for each // and hierarchy matching the relationships create PushChanges(differ, DstEntityManager); for (var i = 0; i != dstInstanceRoots.Length; i++) { var dstInstanceRoot = dstInstanceRoots[i]; var dstInstanceGroup = DstEntityManager.GetBuffer <LinkedEntityGroup>(dstInstanceRoot); var dstInstanceLevel0 = dstInstanceGroup[1].Value; Assert.AreEqual(2, dstInstanceGroup.Length); Assert.AreEqual(dstInstanceRoot, dstInstanceGroup[0].Value); Assert.AreEqual(dstInstanceLevel0, dstInstanceGroup[1].Value); var hierarchyLevel0 = DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel0); var hierarchyRoot = DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceRoot); Assert.AreEqual(dstInstanceRoot, hierarchyLevel0.Parent); Assert.AreEqual(dstInstanceLevel0, hierarchyRoot.Child); } // Add level 1 entity to the prefab var srcLevel1Prefab = SrcEntityManager.CreateEntity(typeof(HierarchyComponent), typeof(Prefab)); SrcEntityManager.AddComponentData(srcLevel1Prefab, prefabLevel1Guid); SrcEntityManager.GetBuffer <LinkedEntityGroup>(srcPrefabRoot).Add(srcLevel1Prefab); SrcEntityManager.SetComponentData(srcLevel0Prefab, new HierarchyComponent { Parent = srcPrefabRoot, Child = srcLevel1Prefab }); SrcEntityManager.SetComponentData(srcLevel1Prefab, new HierarchyComponent { Parent = srcLevel0Prefab, Child = Entity.Null }); // Synchronize worlds, we now should have 10 instances of level 1 linked to their matching level 0 PushChanges(differ, DstEntityManager); for (var i = 0; i != dstInstanceRoots.Length; i++) { var dstRootInstance = dstInstanceRoots[i]; var dstInstanceGroup = DstEntityManager.GetBuffer <LinkedEntityGroup>(dstRootInstance); Assert.AreEqual(3, dstInstanceGroup.Length); Assert.AreEqual(dstRootInstance, dstInstanceGroup[0].Value); var dstInstanceLevel0 = dstInstanceGroup[1].Value; var dstInstanceLevel1 = dstInstanceGroup[2].Value; Assert.AreEqual(dstInstanceLevel0, DstEntityManager.GetComponentData <HierarchyComponent>(dstRootInstance).Child); Assert.AreEqual(dstRootInstance, DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel0).Parent); Assert.AreEqual(dstInstanceLevel1, DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel0).Child); Assert.AreEqual(dstInstanceLevel0, DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel1).Parent); } // Remove level 1 entity from the prefab SrcEntityManager.GetBuffer <LinkedEntityGroup>(srcPrefabRoot).RemoveAt(2); SrcEntityManager.DestroyEntity(srcLevel1Prefab); // Fix the hierarchy of level 0 to remove the link to level 1 SrcEntityManager.SetComponentData(srcLevel0Prefab, new HierarchyComponent { Parent = srcPrefabRoot, Child = Entity.Null }); // Synchronize worlds, destination world should no longer have instances of level 1 PushChanges(differ, DstEntityManager); for (var i = 0; i != dstInstanceRoots.Length; i++) { var dstRootInstance = dstInstanceRoots[i]; var dstInstanceGroup = DstEntityManager.GetBuffer <LinkedEntityGroup>(dstRootInstance); Assert.AreEqual(2, dstInstanceGroup.Length); Assert.AreEqual(dstRootInstance, dstInstanceGroup[0].Value); var dstInstanceLevel1 = dstInstanceGroup[1].Value; Assert.AreEqual(dstInstanceLevel1, DstEntityManager.GetComponentData <HierarchyComponent>(dstRootInstance).Child); Assert.AreEqual(dstRootInstance, DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel1).Parent); Assert.AreEqual(Entity.Null, DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel1).Child); } // Add again level 1 as a Child of level 0 srcLevel1Prefab = SrcEntityManager.CreateEntity(typeof(HierarchyComponent), typeof(Prefab)); SrcEntityManager.AddComponentData(srcLevel1Prefab, prefabLevel1Guid); SrcEntityManager.GetBuffer <LinkedEntityGroup>(srcPrefabRoot).Add(srcLevel1Prefab); SrcEntityManager.SetComponentData(srcLevel0Prefab, new HierarchyComponent { Parent = srcPrefabRoot, Child = srcLevel1Prefab }); SrcEntityManager.SetComponentData(srcLevel1Prefab, new HierarchyComponent { Parent = srcLevel0Prefab, Child = Entity.Null }); PushChanges(differ, DstEntityManager); for (var i = 0; i != dstInstanceRoots.Length; i++) { var dstRootInstance = dstInstanceRoots[i]; var dstInstanceGroup = DstEntityManager.GetBuffer <LinkedEntityGroup>(dstRootInstance); Assert.AreEqual(3, dstInstanceGroup.Length); Assert.AreEqual(dstRootInstance, dstInstanceGroup[0].Value); var dstInstanceLevel0 = dstInstanceGroup[1].Value; var dstInstanceLevel1 = dstInstanceGroup[2].Value; Assert.AreEqual(dstInstanceLevel0, DstEntityManager.GetComponentData <HierarchyComponent>(dstRootInstance).Child); Assert.AreEqual(dstRootInstance, DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel0).Parent); Assert.AreEqual(dstInstanceLevel1, DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel0).Child); Assert.AreEqual(dstInstanceLevel0, DstEntityManager.GetComponentData <HierarchyComponent>(dstInstanceLevel1).Parent); } } }