public void ChangeEntityCapacity(int capacity)
            {
                Debug.Assert(IsPool);
                Debug.Assert(Archetypes.Length == 1); // Only pools with 1 PersistenceArchetype are supported
                Debug.Assert(capacity != EntityCapacity);

                // Grow InitialSceneState & fill with default
                InitialSceneState.ResizeByInitializingWithFirstEntry(capacity);

                // for each in ringbuffer
                // Copy that array & copy existing smaller data into it
                // Dispose of old array
                for (var i = 0; i < RingBuffer.Count; i++)
                {
                    PersistentDataContainer persistentDataContainer = RingBuffer[i];
                    persistentDataContainer.ResizeByInitializingFromOther(capacity, InitialSceneState);
                    RingBuffer[i] = persistentDataContainer;
                }

                // Change ArchetypeDataLayout
                PersistencyArchetypeDataLayout dataLayout = Archetypes[0];

                dataLayout.Amount = capacity;
                Archetypes[0]     = dataLayout;

                ref BlobArray <PersistencyArchetypeDataLayout.TypeInfo> typeInfoArray = ref dataLayout.PersistedTypeInfoArrayRef.Value;
        private JobHandle SchedulePersistJobs(JobHandle inputDeps, PersistentDataContainer dataContainer, ComponentTypeHandle <PersistenceState> persistenceStateTypeHandle)
        {
            var returnJobHandle = inputDeps;

            for (int persistenceArchetypeIndex = 0; persistenceArchetypeIndex < dataContainer.DataLayoutCount; persistenceArchetypeIndex++)
            {
                PersistencyArchetypeDataLayout dataLayout = dataContainer.GetDataLayoutAtIndex(persistenceArchetypeIndex);
                ref BlobArray <PersistencyArchetypeDataLayout.TypeInfo> typeInfoArray = ref dataLayout.PersistedTypeInfoArrayRef.Value;

                PersistencyContainerTag containerTag = new PersistencyContainerTag {
                    DataIdentifier = dataContainer.DataIdentifier
                };
                PersistableTypeCombinationHash persistableTypeCombinationHash = new PersistableTypeCombinationHash {
                    Value = dataLayout.PersistableTypeHandleCombinationHash
                };

                for (int typeInfoIndex = 0; typeInfoIndex < typeInfoArray.Length; typeInfoIndex++)
                {
                    // type info
                    PersistencyArchetypeDataLayout.TypeInfo typeInfo = typeInfoArray[typeInfoIndex];
                    ComponentType runtimeType = ComponentType.ReadOnly(PersistencySettings.GetTypeIndex(typeInfo.PersistableTypeHandle));
                    int           stride      = typeInfo.ElementSize * typeInfo.MaxElements + PersistenceMetaData.SizeOfStruct;
                    int           byteSize    = dataLayout.Amount * stride;

                    // query
                    var query = PersistableEntitiesQuery;
                    query.SetSharedComponentFilter(containerTag, persistableTypeCombinationHash);

                    JobHandle jobHandle;
                    if (typeInfo.IsBuffer)
                    {
                        jobHandle = new CopyBufferElementsToByteArray
                        {
                            BufferTypeHandle     = GetDynamicComponentTypeHandle(runtimeType),
                            MaxElements          = typeInfo.MaxElements,
                            PersistenceStateType = persistenceStateTypeHandle,
                            RawContainerData     = dataContainer.GetRawData(),
                            SubArrayOffset       = dataLayout.Offset + typeInfo.Offset,
                            SubArrayByteSize     = byteSize,
                            ElementSize          = typeInfo.ElementSize
                        }.Schedule(query, inputDeps);
                    }
                    else
                    {
                        jobHandle = new CopyComponentDataToByteArray()
                        {
                            ComponentTypeHandle  = GetDynamicComponentTypeHandle(runtimeType),
                            TypeSize             = typeInfo.ElementSize,
                            PersistenceStateType = persistenceStateTypeHandle,
                            RawContainerData     = dataContainer.GetRawData(),
                            SubArrayOffset       = dataLayout.Offset + typeInfo.Offset,
                            SubArrayByteSize     = byteSize
                        }.Schedule(query, inputDeps);
                    }

                    query.ResetFilter();
                    returnJobHandle = JobHandle.CombineDependencies(returnJobHandle, jobHandle);
                }
            }
        protected internal void SlowSynchronousPersist(PersistentDataContainer dataContainer)
        {
            ComponentTypeHandle <PersistenceState> persistenceStateTypeHandle = GetComponentTypeHandle <PersistenceState>();
            ComponentTypeHandle <PersistencyArchetypeIndexInContainer> archetypeIndexInContainerTypeHandle = GetComponentTypeHandle <PersistencyArchetypeIndexInContainer>();

            JobHandle jobHandle = default;

            jobHandle = SchedulePersist(jobHandle, dataContainer, persistenceStateTypeHandle, archetypeIndexInContainerTypeHandle);
            jobHandle.Complete();
        }
 protected JobHandle ScheduleApply(JobHandle inputDeps, PersistentDataContainer dataContainer, EntityCommandBufferSystem ecbSystem,
                                   EntityTypeHandle entityTypeHandle, ComponentTypeHandle <PersistenceState> persistenceStateTypeHandle, ComponentTypeHandle <PersistencyArchetypeIndexInContainer> indexInContainerTypeHandle)
 {
     if (PersistencySettings.UseGroupedJobs())
     {
         return(ScheduleApplyGroupedJob(inputDeps, dataContainer, ecbSystem, entityTypeHandle, persistenceStateTypeHandle, indexInContainerTypeHandle));
     }
     else
     {
         return(ScheduleApplyJobs(inputDeps, dataContainer, ecbSystem, entityTypeHandle, persistenceStateTypeHandle));
     }
 }
 protected JobHandle SchedulePersist(JobHandle inputDeps, PersistentDataContainer dataContainer, ComponentTypeHandle <PersistenceState> persistenceStateTypeHandle,
                                     ComponentTypeHandle <PersistencyArchetypeIndexInContainer> persistencyArchetypeIndexInContainerTypeHandle)
 {
     if (PersistencySettings.UseGroupedJobs())
     {
         return(SchedulePersistGroupedJob(inputDeps, dataContainer, persistenceStateTypeHandle, persistencyArchetypeIndexInContainerTypeHandle));
     }
     else
     {
         return(SchedulePersistJobs(inputDeps, dataContainer, persistenceStateTypeHandle));
     }
 }
Example #6
0
 internal void RequestInitialStatePersist(PersistentDataContainer dataContainer)
 {
     for (int i = 0; i < _initialStatePersistRequests.Count; i++)
     {
         if (_initialStatePersistRequests[i].DataIdentifier.Equals(dataContainer.DataIdentifier))
         {
             _initialStatePersistRequests[i] = dataContainer;
             Debug.LogWarning($"BeginFramePersistentDataSystem:: Double persist request for container with DataIdentifier: {dataContainer.DataIdentifier.ToString()}! (Will only do last request)");
             return;
         }
     }
     _initialStatePersistRequests.Add(dataContainer);
 }
        public void ReadContainerData(PersistentDataContainer container)
        {
            string path = GetResourcePath(container.DataIdentifier);

            Debug.Assert(File.Exists(path));

            using (var fileStream = new FileStream(path, FileMode.Open))
            {
                int length = (int)fileStream.Length;
                // Read Data
                byte[] dataByteArray = new byte[length];
                fileStream.Read(dataByteArray, 0, length);
                container.GetRawData().CopyFrom(dataByteArray);
            }
        }
        public void WriteContainerData(PersistentDataContainer container)
        {
            string path       = GetResourcePath(container.DataIdentifier);
            string folderPath = Path.GetDirectoryName(path);

            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                var dataArray = container.GetRawData().ToArray();
                fileStream.Write(dataArray, 0, dataArray.Length);
            }
        }
 public void RequestPersist(PersistentDataContainer dataContainer)
 {
     for (int i = 0; i < _persistRequests.Count; i++)
     {
         if (_persistRequests[i].DataIdentifier.Equals(dataContainer.DataIdentifier))
         {
             if (_persistRequests[i].Tick < dataContainer.Tick)
             {
                 _persistRequests[i] = dataContainer;
                 Debug.LogWarning($"BeginFramePersistentDataSystem:: Multiple different persist requests for DataIdentifier: {dataContainer.DataIdentifier.ToString()}! (Will apply oldest data)");
             }
             return;
         }
     }
     _persistRequests.Add(dataContainer);
 }
Example #10
0
        private unsafe void InitSceneSection(SceneSection sceneSection)
        {
            Hash128 containerIdentifier          = sceneSection.SceneGUID;
            PersistencyContainerTag containerTag = new PersistencyContainerTag()
            {
                DataIdentifier = containerIdentifier
            };

            if (PersistentDataStorage.IsInitialized(containerIdentifier))
            {
                PersistentDataContainer latestState = PersistentDataStorage.GetReadContainerForLatestWriteIndex(containerIdentifier, out bool isInitial);
                _beginFrameSystem.RequestApply(latestState);
            }
            else
            {
                _scenePersistencyInfoQuery.SetSharedComponentFilter(containerTag);
                if (_scenePersistencyInfoQuery.CalculateEntityCount() != 1)
                {
                    return;
                }
                var scenePersistencyInfoRef        = _scenePersistencyInfoQuery.GetSingleton <ScenePersistencyInfoRef>();
                ref ScenePersistencyInfo sceneInfo = ref scenePersistencyInfoRef.InfoRef.Value;

                int offset      = 0;
                var dataLayouts = new NativeArray <PersistencyArchetypeDataLayout>(sceneInfo.PersistencyArchetypes.Length, Allocator.Persistent);
                for (var i = 0; i < sceneInfo.PersistencyArchetypes.Length; i++)
                {
                    ref PersistencyArchetype persistencyArchetype = ref sceneInfo.PersistencyArchetypes[i];

                    var hashFilter = new PersistableTypeCombinationHash
                    {
                        Value = persistencyArchetype.PersistableTypeHandleCombinationHash
                    };

                    var dataLayout = new PersistencyArchetypeDataLayout()
                    {
                        Amount = persistencyArchetype.AmountEntities,
                        ArchetypeIndexInContainer = (ushort)i,
                        PersistedTypeInfoArrayRef = persistencyArchetype.BuildTypeInfoBlobAsset(PersistencySettings, persistencyArchetype.AmountEntities, out int sizePerEntity),
                        SizePerEntity             = sizePerEntity,
                        Offset = offset,
                        PersistableTypeHandleCombinationHash = hashFilter.Value
                    };
                    offset += persistencyArchetype.AmountEntities * sizePerEntity;

                    dataLayouts[i] = dataLayout;
                }

                // BlobData is disposed if the owning entity is destroyed, we need this data even if the scene is unloaded
                NativeArray <PersistableTypeHandle> allUniqueTypeHandles = new NativeArray <PersistableTypeHandle>(sceneInfo.AllUniqueTypeHandles.Length, Allocator.Persistent);
                UnsafeUtility.MemCpy(allUniqueTypeHandles.GetUnsafePtr(), sceneInfo.AllUniqueTypeHandles.GetUnsafePtr(), allUniqueTypeHandles.Length * sizeof(PersistableTypeHandle));

                // this function takes ownership over the dataLayouts array & allUniqueTypeHandles array
                PersistentDataStorage.InitializeSceneContainer(containerIdentifier, dataLayouts, allUniqueTypeHandles,
                                                               out PersistentDataContainer initialStateContainer, out PersistentDataContainer containerToApply);
                _beginFrameSystem.RequestInitialStatePersist(initialStateContainer);
                if (containerToApply.IsCreated)
                {
                    // containerToApply only exists if it was already added as uninitialized raw data beforehand (e.g. read from disk)
                    _beginFrameSystem.RequestApply(containerToApply);
                }
            }