public void Playback(ref ManagedDeferredCommands managedDeferredCommands)
        {
            fixed(UnsafeAppendBuffer *buffer = &managedDeferredCommands.CommandBuffer)
            {
                var reader = buffer->AsReader();

                while (!reader.EndOfBuffer)
                {
                    var cmd = reader.ReadNext <int>();
                    switch ((ManagedDeferredCommands.Command)cmd)
                    {
                    case (ManagedDeferredCommands.Command.IncrementSharedComponentVersion):
                    {
                        var sharedIndex = reader.ReadNext <int>();
                        IncrementSharedComponentVersion(sharedIndex);
                    }
                    break;

                    case (ManagedDeferredCommands.Command.CopyManagedObjects):
                    {
                        var srcArchetype         = (Archetype *)reader.ReadNext <IntPtr>();
                        var srcManagedArrayIndex = reader.ReadNext <int>();
                        var srcChunkCapacity     = reader.ReadNext <int>();
                        var srcStartIndex        = reader.ReadNext <int>();
                        var dstArchetype         = (Archetype *)reader.ReadNext <IntPtr>();
                        var dstManagedArrayIndex = reader.ReadNext <int>();
                        var dstChunkCapacity     = reader.ReadNext <int>();
                        var dstStartIndex        = reader.ReadNext <int>();
                        var count = reader.ReadNext <int>();

                        CopyManagedObjects(this, srcArchetype, srcManagedArrayIndex, srcChunkCapacity, srcStartIndex,
                                           this, dstArchetype, dstManagedArrayIndex, dstChunkCapacity, dstStartIndex, count);
                    }
                    break;

                    case (ManagedDeferredCommands.Command.ClearManagedObjects):
                    {
                        var archetype         = (Archetype *)reader.ReadNext <IntPtr>();
                        var managedArrayIndex = reader.ReadNext <int>();
                        var chunkCapacity     = reader.ReadNext <int>();
                        var index             = reader.ReadNext <int>();
                        var count             = reader.ReadNext <int>();

                        ClearManagedObjects(archetype, managedArrayIndex, chunkCapacity, index, count);
                    }
                    break;

                    case (ManagedDeferredCommands.Command.AddReference):
                    {
                        var index   = reader.ReadNext <int>();
                        var numRefs = reader.ReadNext <int>();
                        AddReference(index, numRefs);
                    }
                    break;

                    case (ManagedDeferredCommands.Command.RemoveReference):
                    {
                        var index   = reader.ReadNext <int>();
                        var numRefs = reader.ReadNext <int>();
                        RemoveReference(index, numRefs);
                    }
                    break;

                    case (ManagedDeferredCommands.Command.DeallocateManagedArrayStorage):
                    {
                        var index = reader.ReadNext <int>();
                        DeallocateManagedArrayStorage(index);
                    }
                    break;

                    case (ManagedDeferredCommands.Command.AllocateManagedArrayStorage):
                    {
                        var index  = reader.ReadNext <int>();
                        var length = reader.ReadNext <int>();
                        AllocateManagedArrayStorage(index, length);
                    }
                    break;

                    case (ManagedDeferredCommands.Command.ReserveManagedArrayStorage):
                    {
                        var count = reader.ReadNext <int>();
                        ReserveManagedArrayStorage(count);
                    }
                    break;
                    }
                }
            }

            managedDeferredCommands.Reset();
        }
        public void Playback(ref ManagedDeferredCommands managedDeferredCommands)
        {
            var reader = new UnsafeAppendBuffer.Reader(ref managedDeferredCommands.CommandBuffer);

            while (!reader.EndOfBuffer)
            {
                var cmd = reader.ReadNext <int>();
                switch ((ManagedDeferredCommands.Command)cmd)
                {
                case (ManagedDeferredCommands.Command.IncrementSharedComponentVersion):
                {
                    var sharedIndex = reader.ReadNext <int>();
                    IncrementSharedComponentVersion(sharedIndex);
                }
                break;

                case (ManagedDeferredCommands.Command.CopyManagedObjects):
                {
                    var srcArchetype         = (Archetype *)reader.ReadNext <IntPtr>();
                    var srcManagedArrayIndex = reader.ReadNext <int>();
                    var srcChunkCapacity     = reader.ReadNext <int>();
                    var srcStartIndex        = reader.ReadNext <int>();
                    var dstArchetype         = (Archetype *)reader.ReadNext <IntPtr>();
                    var dstManagedArrayIndex = reader.ReadNext <int>();
                    var dstChunkCapacity     = reader.ReadNext <int>();
                    var dstStartIndex        = reader.ReadNext <int>();
                    var count = reader.ReadNext <int>();

                    CopyManagedObjects(this, srcArchetype, srcManagedArrayIndex, srcChunkCapacity, srcStartIndex,
                                       this, dstArchetype, dstManagedArrayIndex, dstChunkCapacity, dstStartIndex, count);
                }
                break;

                case (ManagedDeferredCommands.Command.ReplicateManagedObjects):
                {
                    var srcArchetype         = (Archetype *)reader.ReadNext <IntPtr>();
                    var srcManagedArrayIndex = reader.ReadNext <int>();
                    var srcChunkCapacity     = reader.ReadNext <int>();
                    var srcIndex             = reader.ReadNext <int>();
                    var dstArchetype         = (Archetype *)reader.ReadNext <IntPtr>();
                    var dstManagedArrayIndex = reader.ReadNext <int>();
                    var dstChunkCapacity     = reader.ReadNext <int>();
                    var dstBaseStartIndex    = reader.ReadNext <int>();
                    var count = reader.ReadNext <int>();

                    var srcEntity = reader.ReadNext <Entity>();
                    reader.ReadNext <Entity>(out var dstEntities, Allocator.Temp);

                    ReplicateManagedObjects(this, srcArchetype, srcManagedArrayIndex, srcChunkCapacity, srcIndex,
                                            this, dstArchetype, dstManagedArrayIndex, dstChunkCapacity, dstBaseStartIndex, count, srcEntity, dstEntities);
                }
                break;

                case (ManagedDeferredCommands.Command.ClearManagedObjects):
                {
                    var archetype         = (Archetype *)reader.ReadNext <IntPtr>();
                    var managedArrayIndex = reader.ReadNext <int>();
                    var chunkCapacity     = reader.ReadNext <int>();
                    var index             = reader.ReadNext <int>();
                    var count             = reader.ReadNext <int>();

                    ClearManagedObjects(archetype, managedArrayIndex, chunkCapacity, index, count);
                }
                break;

                case (ManagedDeferredCommands.Command.AddReference):
                {
                    var index   = reader.ReadNext <int>();
                    var numRefs = reader.ReadNext <int>();
                    AddReference(index, numRefs);
                }
                break;

                case (ManagedDeferredCommands.Command.RemoveReference):
                {
                    var index   = reader.ReadNext <int>();
                    var numRefs = reader.ReadNext <int>();
                    RemoveReference(index, numRefs);
                }
                break;

                case (ManagedDeferredCommands.Command.DeallocateManagedArrayStorage):
                {
                    var index = reader.ReadNext <int>();
                    DeallocateManagedArrayStorage(index);
                }
                break;

                case (ManagedDeferredCommands.Command.AllocateManagedArrayStorage):
                {
                    var index  = reader.ReadNext <int>();
                    var length = reader.ReadNext <int>();
                    AllocateManagedArrayStorage(index, length);
                }
                break;

                case (ManagedDeferredCommands.Command.ReserveManagedArrayStorage):
                {
                    var count = reader.ReadNext <int>();
                    ReserveManagedArrayStorage(count);
                }
                break;

                case (ManagedDeferredCommands.Command.MoveChunksManagedObjects):
                {
                    var oldArchetype         = (Archetype *)reader.ReadNext <IntPtr>();
                    var oldManagedArrayIndex = reader.ReadNext <int>();
                    var newArchetype         = (Archetype *)reader.ReadNext <IntPtr>();
                    var newManagedArrayIndex = reader.ReadNext <int>();
                    var chunkCapacity        = reader.ReadNext <int>();
                    var count = reader.ReadNext <int>();

                    CopyManagedObjects(this, oldArchetype, oldManagedArrayIndex, chunkCapacity, 0,
                                       this, newArchetype, newManagedArrayIndex, chunkCapacity, 0, count);
                }
                break;

                case (ManagedDeferredCommands.Command.PatchManagedEntities):
                {
                    var archetype   = (Archetype *)reader.ReadNext <IntPtr>();
                    var chunk       = (Chunk *)reader.ReadNext <IntPtr>();
                    var entityCount = reader.ReadNext <int>();
                    var remapping   = (EntityRemapUtility.EntityRemapInfo *)reader.ReadNext <IntPtr>();
                    var allocator   = (Allocator)reader.ReadNext <int>();

                    PatchEntities(this, archetype, chunk, entityCount, remapping, allocator);
                }
                break;

                case (ManagedDeferredCommands.Command.PatchManagedEntitiesForPrefabs):
                {
                    var archetype      = (Archetype *)reader.ReadNext <IntPtr>();
                    var chunk          = (Chunk *)reader.ReadNext <IntPtr>();
                    var indexInChunk   = reader.ReadNext <int>();
                    var entityCount    = reader.ReadNext <int>();
                    var remapping      = (EntityRemapUtility.SparseEntityRemapInfo *)reader.ReadNext <IntPtr>();
                    var remappingCount = reader.ReadNext <int>();
                    var allocator      = (Allocator)reader.ReadNext <int>();

                    PatchEntitiesForPrefab(this, archetype, chunk, indexInChunk, entityCount, remapping, remappingCount, allocator);
                }
                break;
                }
            }

            managedDeferredCommands.Reset();
        }
Esempio n. 3
0
        public void Playback(ref ManagedDeferredCommands managedDeferredCommands)
        {
            var reader = new UnsafeAppendBuffer.Reader(ref managedDeferredCommands.CommandBuffer);

            while (!reader.EndOfBuffer)
            {
                var cmd = reader.ReadNext <int>();
                switch ((ManagedDeferredCommands.Command)cmd)
                {
                case (ManagedDeferredCommands.Command.IncrementSharedComponentVersion):
                {
                    var sharedIndex = reader.ReadNext <int>();
                    IncrementSharedComponentVersion(sharedIndex);
                }
                break;

                case (ManagedDeferredCommands.Command.AddReference):
                {
                    var index   = reader.ReadNext <int>();
                    var numRefs = reader.ReadNext <int>();
                    AddReference(index, numRefs);
                }
                break;

                case (ManagedDeferredCommands.Command.RemoveReference):
                {
                    var index   = reader.ReadNext <int>();
                    var numRefs = reader.ReadNext <int>();
                    RemoveReference(index, numRefs);
                }
                break;

                case (ManagedDeferredCommands.Command.PatchManagedEntities):
                {
                    var archetype   = (Archetype *)reader.ReadNext <IntPtr>();
                    var chunk       = (Chunk *)reader.ReadNext <IntPtr>();
                    var entityCount = reader.ReadNext <int>();
                    var remapping   = (EntityRemapUtility.EntityRemapInfo *)reader.ReadNext <IntPtr>();

                    PatchEntities(archetype, chunk, entityCount, remapping);
                }
                break;

                case (ManagedDeferredCommands.Command.PatchManagedEntitiesForPrefabs):
                {
                    var remapSrc             = (byte *)reader.ReadNext <IntPtr>();
                    var allocatedCount       = reader.ReadNext <int>();
                    var remappingCount       = reader.ReadNext <int>();
                    var numManagedComponents = reader.ReadNext <int>();
                    var allocator            = (Allocator)reader.ReadNext <int>();


                    var remapSrcSize = UnsafeUtility.SizeOf <Entity>() * remappingCount;
                    var remapDstSize = UnsafeUtility.SizeOf <Entity>() * remappingCount * allocatedCount;

                    var remapDst          = remapSrc + remapSrcSize;
                    var managedComponents = remapDst + remapDstSize;

                    PatchEntitiesForPrefab((int *)managedComponents, numManagedComponents, allocatedCount, remappingCount, (Entity *)remapSrc, (Entity *)remapDst);
                    UnsafeUtility.Free(remapSrc, allocator);
                }
                break;

                case (ManagedDeferredCommands.Command.CloneManagedComponents):
                {
                    var srcArray      = (int *)reader.ReadNextArray <int>(out var componentCount);
                    var instanceCount = reader.ReadNext <int>();
                    var dstArray      = (int *)reader.ReadNextArray <int>(out _);
                    CloneManagedComponents(srcArray, componentCount, dstArray, instanceCount);
                }
                break;

                case (ManagedDeferredCommands.Command.CloneHybridComponents):
                {
                    var srcArray = (int *)reader.ReadNextArray <int>(out var componentCount);
                    var entities = (Entity *)reader.ReadNextArray <Entity>(out var instanceCount);
                    var dstArray = (int *)reader.ReadNextArray <int>(out _);

                    if (InstantiateHybridComponent != null)
                    {
                        InstantiateHybridComponent(srcArray, componentCount, entities, dstArray, instanceCount, this);
                    }
                    else
                    {
                        // InstantiateHybridComponent was not injected just copy the reference to the object and dont clone it
                        for (int src = 0; src < componentCount; ++src)
                        {
                            object sourceComponent = m_ManagedComponentData[srcArray[src]];
                            for (int i = 0; i < instanceCount; ++i)
                            {
                                m_ManagedComponentData[dstArray[i]] = sourceComponent;
                            }
                            dstArray += instanceCount;
                        }
                    }
                }
                break;

                case (ManagedDeferredCommands.Command.FreeManagedComponents):
                {
                    var count = reader.ReadNext <int>();
                    for (int i = 0; i < count; ++i)
                    {
                        var managedComponentIndex = reader.ReadNext <int>();
                        m_ManagedComponentData[managedComponentIndex] = null;
                    }
                }
                break;

                case (ManagedDeferredCommands.Command.SetManagedComponentCapacity):
                {
                    var capacity = reader.ReadNext <int>();
                    SetManagedComponentCapacity(capacity);
                }
                break;
                }
            }

            managedDeferredCommands.Reset();
        }