public void TestPersistAndApply([Values(1, 2, 3, 10)] int total, [Values(false, true)] bool groupedJobs)
        {
            PersistencySettings settings = CreateTestSettings();

            settings.ForceUseGroupedJobsInEditor   = groupedJobs;
            settings.ForceUseNonGroupedJobsInBuild = !groupedJobs;
            Assert.True(groupedJobs == settings.UseGroupedJobs());

            // Override the settings fields of both systems
            PersistentSceneSystem persistentSceneSystem = World.GetOrCreateSystem <PersistentSceneSystem>();

            persistentSceneSystem.ReplaceSettings(settings);
            BeginFramePersistencySystem beginFramePersistencySystem = World.GetOrCreateSystem <BeginFramePersistencySystem>();

            beginFramePersistencySystem.ReplaceSettings(settings);

            EndInitializationEntityCommandBufferSystem ecbSystem = World.GetOrCreateSystem <EndInitializationEntityCommandBufferSystem>();

            // Load SubScenes
            for (int i = 0; i < total; i++)
            {
                Unity.Entities.Hash128 sceneGUID = Hash128.Compute(i);
                LoadFakeSubScene(settings, sceneGUID, i + 10);
            }

            // The fake subscene entities don't have any actual test data on them yet, so add some here
            Entities.ForEach((Entity e, ref PersistenceState persistenceState) =>
            {
                // tracking add/remove of bufferdata isn't supported so we add all of them
                DynamicBuffer <DynamicBufferData1> buffer1 = m_Manager.AddBuffer <DynamicBufferData1>(e);
                buffer1.Add(new DynamicBufferData1()
                {
                    Value = persistenceState.ArrayIndex
                });
                DynamicBuffer <DynamicBufferData2> buffer2 = m_Manager.AddBuffer <DynamicBufferData2>(e);
                buffer2.Add(new DynamicBufferData2()
                {
                    Value = persistenceState.ArrayIndex
                });
                DynamicBuffer <DynamicBufferData3> buffer3 = m_Manager.AddBuffer <DynamicBufferData3>(e);
                buffer3.Add(new DynamicBufferData3()
                {
                    Value = (byte)persistenceState.ArrayIndex
                });

                // Add different components
                if (persistenceState.ArrayIndex % 2 == 0)
                {
                    m_Manager.AddComponentData(e, new EcsTestData(persistenceState.ArrayIndex));
                }
                else
                {
                    m_Manager.AddComponent <EmptyEcsTestData>(e);
                }
            });

            persistentSceneSystem.Update();
            beginFramePersistencySystem.Update();
            ecbSystem.Update();

            // Check if some data was written to the container
            for (int i = 0; i < total; i++)
            {
                Unity.Entities.Hash128 sceneGUID = Hash128.Compute(i);
                Assert.True(persistentSceneSystem.PersistentDataStorage.IsInitialized(sceneGUID), "Expected the subscene to have an initialized PersistentDataContainer");
                PersistentDataContainer container = persistentSceneSystem.PersistentDataStorage.GetReadContainerForLatestWriteIndex(sceneGUID, out bool isInitial);
                Assert.True(isInitial);

                bool hasOnlyZero = true;
                foreach (byte dataByte in container.GetRawData())
                {
                    if (dataByte != 0)
                    {
                        hasOnlyZero = false;
                        break;
                    }
                }
                Assert.False(hasOnlyZero, "");
            }

            // Change entities
            Entities.ForEach((Entity e, ref PersistenceState persistenceState) =>
            {
                DynamicBuffer <DynamicBufferData1> buffer1 = m_Manager.GetBuffer <DynamicBufferData1>(e);
                buffer1[0] = default;
                DynamicBuffer <DynamicBufferData2> buffer2 = m_Manager.GetBuffer <DynamicBufferData2>(e);
                buffer2[0] = default;
                DynamicBuffer <DynamicBufferData3> buffer3 = m_Manager.GetBuffer <DynamicBufferData3>(e);
                buffer3[0] = default;

                if (persistenceState.ArrayIndex % 2 == 0)
                {
                    m_Manager.RemoveComponent <EcsTestData>(e);
                    m_Manager.AddComponent <EmptyEcsTestData>(e);
                }
                else
                {
                    m_Manager.AddComponentData(e, new EcsTestData(persistenceState.ArrayIndex));
                    m_Manager.RemoveComponent <EmptyEcsTestData>(e);
                }
            });

            // Revert entities to initial state
            for (int i = 0; i < total; i++)
            {
                Unity.Entities.Hash128 sceneGUID = Hash128.Compute(i);
                Assert.True(persistentSceneSystem.PersistentDataStorage.IsInitialized(sceneGUID),
                            "Expected the subscene to have an initialized PersistentDataContainer");
                PersistentDataContainer container = persistentSceneSystem.PersistentDataStorage.GetReadContainerForLatestWriteIndex(sceneGUID, out bool isInitial);
                Assert.True(isInitial);
                beginFramePersistencySystem.RequestApply(container);
            }
            beginFramePersistencySystem.Update();
            ecbSystem.Update();

            // Test if the revert actually worked on entities that were tracking the components
            Entities.ForEach((Entity e, ref PersistenceState persistenceState) =>
            {
                if (m_Manager.GetName(e).Contains("_B")) // this means it was tracking all buffer components
                {
                    DynamicBuffer <DynamicBufferData1> buffer1 = m_Manager.GetBuffer <DynamicBufferData1>(e);
                    Assert.True(buffer1[0].Value == persistenceState.ArrayIndex, "The entity was expected to be reset and have it's original buffer value (1)");
                    DynamicBuffer <DynamicBufferData2> buffer2 = m_Manager.GetBuffer <DynamicBufferData2>(e);
                    Assert.True(Math.Abs(buffer2[0].Value - persistenceState.ArrayIndex) < 0.001f, "The entity was expected to be reset and have it's original buffer value (2)");
                    DynamicBuffer <DynamicBufferData3> buffer3 = m_Manager.GetBuffer <DynamicBufferData3>(e);
                    Assert.True(buffer3[0].Value == (byte)persistenceState.ArrayIndex, "The entity was expected to be reset and have it's original buffer value (3)");
                }
                else
                {
                    DynamicBuffer <DynamicBufferData1> buffer1 = m_Manager.GetBuffer <DynamicBufferData1>(e);
                    Assert.True(buffer1[0].Value == 0, "The entity was expected to NOT be reset and have a 0 buffer value. (1)");
                    DynamicBuffer <DynamicBufferData2> buffer2 = m_Manager.GetBuffer <DynamicBufferData2>(e);
                    Assert.True(buffer2[0].Value == 0, "The entity was expected to NOT be reset and have a 0 buffer value. (2)");
                    DynamicBuffer <DynamicBufferData3> buffer3 = m_Manager.GetBuffer <DynamicBufferData3>(e);
                    Assert.True(buffer3[0].Value == 0, "The entity was expected to NOT be reset and have a 0 buffer value. (3)");
                }

                if (persistenceState.ArrayIndex % 2 == 0)
                {
                    if (m_Manager.GetName(e).Contains("_C")) // this means it was tracking all standard components
                    {
                        Assert.True(m_Manager.HasComponent <EcsTestData>(e), "The entity was expected to be reset and have its original component back.");
                    }
                    else
                    {
                        Assert.False(m_Manager.HasComponent <EcsTestData>(e), "The entity was expected to NOT be reset and NOT have its original component back.");
                    }

                    if (m_Manager.GetName(e).Contains("_T")) // this means it was tracking all tag components
                    {
                        Assert.False(m_Manager.HasComponent <EmptyEcsTestData>(e), "The entity was expected to be reset and have the new TAG component removed.");
                    }
                    else
                    {
                        Assert.True(m_Manager.HasComponent <EmptyEcsTestData>(e), "The entity was expected to NOT be reset and NOT have the new TAG component removed. ");
                    }
                }
                else
                {
                    if (m_Manager.GetName(e).Contains("_C")) // this means it was tracking all standard components
                    {
                        Assert.False(m_Manager.HasComponent <EcsTestData>(e), "The entity was expected to be reset and have the new component removed.");
                    }
                    else
                    {
                        Assert.True(m_Manager.HasComponent <EcsTestData>(e), "The entity was expected to NOT be reset and NOT have the new component removed.");
                    }

                    if (m_Manager.GetName(e).Contains("_T")) // this means it was tracking all tag components
                    {
                        Assert.True(m_Manager.HasComponent <EmptyEcsTestData>(e), "The entity was expected to be reset and have its original TAG component back.");
                    }
                    else
                    {
                        Assert.False(m_Manager.HasComponent <EmptyEcsTestData>(e), "The entity was expected to NOT be reset and NOT have its original TAG component back.");
                    }
                }
            });
            m_Manager.DestroyEntity(m_Manager.UniversalQuery);
        }
Beispiel #2
0
        public void TestDynamicPoolApplyAndPersist([Values(InstantiationType.Manager, InstantiationType.Command, InstantiationType.ParallelCommand)] InstantiationType instantiationType,
                                                   [Values(1, 2, 3, 444, 5555)] int total, [Values(false, true)] bool groupedJobs)
        {
            // Prepare
            PersistencySettings settings = CreateTestSettings();

            settings.ForceUseGroupedJobsInEditor   = groupedJobs;
            settings.ForceUseNonGroupedJobsInBuild = !groupedJobs;
            Assert.True(groupedJobs == settings.UseGroupedJobs());
            PersistentSceneSystem       persistentSceneSystem       = World.GetOrCreateSystem <PersistentSceneSystem>();
            BeginFramePersistencySystem beginFramePersistencySystem = World.GetExistingSystem <BeginFramePersistencySystem>();

            beginFramePersistencySystem.ReplaceSettings(settings);
            EndFramePersistencySystem endFramePersistencySystem = World.GetOrCreateSystem <EndFramePersistencySystem>();

            endFramePersistencySystem.ReplaceSettings(settings);
            EndInitializationEntityCommandBufferSystem ecbSystem = World.GetOrCreateSystem <EndInitializationEntityCommandBufferSystem>();
            PersistentDataStorage dataStorage = persistentSceneSystem.PersistentDataStorage;
            Hash128 identifier = Hash128.Compute("DynamicPoolTests");

            NativeArray <PersistableTypeHandle> typeHandles = new NativeArray <PersistableTypeHandle>(Archetype.Length, Allocator.Persistent);

            for (int i = 0; i < typeHandles.Length; i++)
            {
                typeHandles[i] = settings.GetPersistableTypeHandleFromFullTypeName(Archetype[i].GetManagedType().FullName); // This is not advised in a game
            }

            // Action
            Entity prefabEntity = m_Manager.CreateEntity(Archetype);

            m_Manager.AddComponent <Prefab>(prefabEntity);
            persistentSceneSystem.InitializePool(prefabEntity, total, identifier, typeHandles);
            int amountSpawned = 0;

            InstantiatePersistent(instantiationType, m_Manager, prefabEntity, ref amountSpawned, total);
            persistentSceneSystem.InstantChangePoolCapacity(identifier, total * 2); // double amount
            InstantiatePersistent(instantiationType, m_Manager, prefabEntity, ref amountSpawned, total);

            m_Manager.RemoveComponent <EmptyEcsTestData>(m_Manager.CreateEntityQuery(typeof(PersistenceState)));
            Entities.ForEach((Entity e, ref EcsTestData testData, DynamicBuffer <DynamicBufferData1> buffer) =>
            {
                testData.Value = 1;
                buffer.Add(new DynamicBufferData1()
                {
                    Value = 1
                });
            });

            dataStorage.ToIndex(0);
            endFramePersistencySystem.RequestPersist(dataStorage.GetWriteContainerForCurrentIndex(identifier));
            endFramePersistencySystem.Update();
            m_Manager.CompleteAllJobs();
            beginFramePersistencySystem.RequestApply(dataStorage.GetInitialStateReadContainer(identifier));
            beginFramePersistencySystem.Update();
            ecbSystem.Update();

            // Test
            int amountSurvivingEntities = m_Manager.CreateEntityQuery(typeof(EcsTestData)).CalculateEntityCount();

            Assert.True(amountSurvivingEntities == total * 2, $"{instantiationType} didn't create the expected amount of entities({amountSurvivingEntities}vs{total*2})! (Or they were destroyed by Apply)");
            Entities.ForEach((Entity e, ref EcsTestData testData, DynamicBuffer <DynamicBufferData1> buffer) =>
            {
                Assert.True(testData.Value == 0, "The apply didn't restore the component value!");
                Assert.True(buffer.IsEmpty, "The apply didn't restore the buffer!");
                Assert.True(m_Manager.HasComponent <EmptyEcsTestData>(e), "The apply didn't restore the component!");
            });

            // Action
            dataStorage.ToIndex(0);
            beginFramePersistencySystem.RequestApply(dataStorage.GetWriteContainerForCurrentIndex(identifier));
            beginFramePersistencySystem.Update();
            ecbSystem.Update();

            // Test
            amountSurvivingEntities = m_Manager.CreateEntityQuery(typeof(EcsTestData)).CalculateEntityCount();
            Assert.True(amountSurvivingEntities == total * 2, $"Some entities were unexpectedly destroyed!");
            Entities.ForEach((Entity e, ref EcsTestData testData, DynamicBuffer <DynamicBufferData1> buffer) =>
            {
                Assert.True(testData.Value == 1, "The second apply didn't restore the component value!");
                Assert.True(buffer[0].Value == 1, "The second apply didn't restore the buffer!");
                Assert.False(m_Manager.HasComponent <EmptyEcsTestData>(e), "The second apply didn't restore the component!");
            });

            // Cleanup
            m_Manager.DestroyEntity(m_Manager.UniversalQuery);
        }