protected override IEnumerator ExecuteRoutine() { Log.Info("Start Sim Deserialization Process..."); SerializedWorld serializedWorld = NetSerializer.Deserialize <SerializedWorld>(_serializedData); SerializeUtilityX.DeserializeWorld(serializedWorld.WorldData, _simulationWorld.EntityManager); Dictionary <uint, byte[]> blobAssetsMap = new Dictionary <uint, byte[]>(serializedWorld.BlobAssets.Length); for (int i = 0; i < serializedWorld.BlobAssets.Length; i++) { blobAssetsMap.Add(serializedWorld.BlobAssets[i].Id, serializedWorld.BlobAssets[i].Data); } Dictionary <ComponentType, Type> compToManaged = new Dictionary <ComponentType, Type>(); Dictionary <ComponentType, DynamicComponentTypeHandle> compToHandle = new Dictionary <ComponentType, DynamicComponentTypeHandle>(); NativeArray <ArchetypeChunk> chunks = _simulationWorld.EntityManager.GetAllChunks(Allocator.TempJob); foreach (var item in BlobAssetDataDistributors.Values) { item.BeginDistribute(_simulationWorld); } // iterate over all chunks for (int i = 0; i < chunks.Length; i++) { ArchetypeChunk chunk = chunks[i]; // iterate over all components in chunk foreach (ComponentType componentType in chunk.Archetype.GetComponentTypes()) { // get managed type if (!compToManaged.TryGetValue(componentType, out Type managedType)) { managedType = componentType.GetManagedType(); compToManaged[componentType] = managedType; } // if collector exists for given component, invoke it if (BlobAssetDataDistributors.TryGetValue(managedType, out IPtrObjectDistributor collector)) { // get componentTypeHandle (necessary for chunk data access) if (!compToHandle.TryGetValue(componentType, out DynamicComponentTypeHandle typeHandle)) { typeHandle = _simulationWorld.EntityManager.GetDynamicComponentTypeHandle(componentType); compToHandle[componentType] = typeHandle; } // invoke! collector.Distribute(typeHandle, chunk, blobAssetsMap); } } } chunks.Dispose(); foreach (var item in BlobAssetDataDistributors.Values) { item.EndDistribute(); } Log.Info("Sim Deserialization Complete!"); TerminateWithSuccess(); yield break; }
protected override IEnumerator ExecuteRoutine() { Log.Info("Start Sim Serialization Process..."); Dictionary <uint, byte[]> blobAssetsMap = new Dictionary <uint, byte[]>(); Dictionary <ComponentType, Type> compToManaged = new Dictionary <ComponentType, Type>(); Dictionary <ComponentType, DynamicComponentTypeHandle> compToHandle = new Dictionary <ComponentType, DynamicComponentTypeHandle>(); NativeArray <ArchetypeChunk> chunks = _simulationWorld.EntityManager.GetAllChunks(Allocator.TempJob); foreach (var item in BlobAssetDataCollectors.Values) { item.BeginCollect(_simulationWorld); } // iterate over all chunks for (int i = 0; i < chunks.Length; i++) { ArchetypeChunk chunk = chunks[i]; // iterate over all components in chunk foreach (ComponentType c in chunk.Archetype.GetComponentTypes()) { ComponentType componentType = c; componentType.AccessModeType = ComponentType.AccessMode.ReadOnly; // get managed type if (!compToManaged.TryGetValue(componentType, out Type managedType)) { managedType = componentType.GetManagedType(); compToManaged[componentType] = managedType; } // if collector exists for given component, invoke it if (BlobAssetDataCollectors.TryGetValue(managedType, out IPtrObjectCollector collector)) { // get componentTypeHandle (necessary for chunk data access) if (!compToHandle.TryGetValue(componentType, out DynamicComponentTypeHandle typeHandle)) { typeHandle = _simulationWorld.EntityManager.GetDynamicComponentTypeHandle(componentType); compToHandle[componentType] = typeHandle; } // invoke! collector.Collect(typeHandle, chunk, blobAssetsMap); } } } chunks.Dispose(); foreach (var item in BlobAssetDataCollectors.Values) { item.EndCollect(); } SerializedWorld serializedWorld = new SerializedWorld(); { serializedWorld.BlobAssets = new SerializedWorld.BlobAsset[blobAssetsMap.Count]; int i = 0; foreach (var item in blobAssetsMap) { serializedWorld.BlobAssets[i] = new SerializedWorld.BlobAsset() { Id = item.Key, Data = item.Value, }; i++; } } serializedWorld.WorldData = SerializeUtilityX.SerializeWorld(_simulationWorld.EntityManager, out object[] referencedObjects); SerializationData = NetSerializer.Serialize(serializedWorld); foreach (var obj in referencedObjects) { Log.Warning($"The ECS simulation references {obj}, which is a managed object. " + $"This is not allowed for now due to serialization"); } Log.Info("Sim Serialization Complete!"); TerminateWithSuccess(); yield break; }