Exemplo n.º 1
0
        public void TestDynamicPoolInitialState([Values(1, 22, 333, 4444)] int total)
        {
            // Prepare
            PersistencySettings         settings = CreateTestSettings();
            PersistentSceneSystem       persistentSceneSystem       = World.GetOrCreateSystem <PersistentSceneSystem>();
            BeginFramePersistencySystem beginFramePersistencySystem = World.GetExistingSystem <BeginFramePersistencySystem>();

            beginFramePersistencySystem.ReplaceSettings(settings);
            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.SetComponentData(prefabEntity, new EcsTestData(1));
            m_Manager.AddComponent <Prefab>(prefabEntity);
            persistentSceneSystem.InitializePool(prefabEntity, total, identifier, typeHandles);

            // Verify
            {
                Assert.True(dataStorage.IsInitialized(identifier), "PoolContainer is not initialized!");
                PersistentDataContainer container = dataStorage.GetInitialStateReadContainer(identifier);
                int sizePerEntity = container.GetDataLayoutAtIndex(0).SizePerEntity;
                Assert.AreEqual(sizePerEntity * total, container.GetRawData().Length, "The data container was not of the expected size!");
                int amountNonZero = container.GetRawData().Count(dataByte => dataByte != 0);
                Assert.NotZero(amountNonZero, "The datacontainer didn't have valid initial state.");
            }

            // Action 2
            total += 31;
            persistentSceneSystem.InstantChangePoolCapacity(identifier, total);

            // Verify
            {
                Assert.True(dataStorage.IsInitialized(identifier), "PoolContainer is not initialized!");
                dataStorage.ToIndex(0);
                PersistentDataContainer container = dataStorage.GetReadContainerForCurrentIndex(identifier);
                int sizePerEntity = container.GetDataLayoutAtIndex(0).SizePerEntity;
                Assert.AreEqual(sizePerEntity * total, container.GetRawData().Length, "The data container was not of the expected size after an InstantResize!");
                int amountNonZero = container.GetRawData().Count(dataByte => dataByte != 0);
                Assert.NotZero(amountNonZero, "The datacontainer grew but didn't have valid initial state.");
            }

            // Cleanup
            m_Manager.DestroyEntity(m_Manager.UniversalQuery);
        }
Exemplo n.º 2
0
        public void TestSceneLoad([Values(1, 2, 3, 10)] int total)
        {
            PersistencySettings settings = CreateTestSettings();

            // override the settings field
            PersistentSceneSystem persistentSceneSystem = World.GetOrCreateSystem <PersistentSceneSystem>();

            persistentSceneSystem.ReplaceSettings(settings);

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

            persistentSceneSystem.Update();

            for (int i = 0; i < total; i++)
            {
                Unity.Entities.Hash128 sceneGUID = Hash128.Compute(i);
                // Since BeginFramePersistencySystem hasn't run, this data will be uninitialized.
                // We're only interested if it's the container exists & if it's the right size
                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);
                Assert.True(container.DataIdentifier == sceneGUID,
                            $"Expected the container to have the sceneGUID {sceneGUID} as data identifier, but it was {container.DataIdentifier}.");
                Assert.True(container.DataLayoutCount == 6,
                            $"LoadFakeSubScene creates 6 different types of persistable entities so we expect 6 different data layouts in the container, but it reports {container.DataLayoutCount}");

                int entitiesInScene     = (i + 10) * 6;
                int entitiesInContainer = container.CalculateEntityCapacity();
                Assert.True(entitiesInContainer == entitiesInScene,
                            $"LoadFakeSubScene created {entitiesInScene} entities, but the container reports {entitiesInContainer}.");
            }
            m_Manager.DestroyEntity(m_Manager.UniversalQuery);
        }
Exemplo n.º 3
0
        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);
        }