Пример #1
0
        public void UpdateManagedComponentValue(int *index, object value, ref EntityComponentStore entityComponentStore)
        {
            entityComponentStore.AssertNoQueuedManagedDeferredCommands();
            var iManagedComponent = *index;

            if (value != null)
            {
                if (iManagedComponent == 0)
                {
                    ReserveManagedComponentIndicesDirect(1, ref entityComponentStore);
                    iManagedComponent = *index = entityComponentStore.AllocateManagedComponentIndex();
                }
            }
            else
            {
                if (iManagedComponent == 0)
                {
                    return;
                }
                *index = 0;
                entityComponentStore.FreeManagedComponentIndex(iManagedComponent);
            }
            m_ManagedComponentData[iManagedComponent] = value;
        }
        public static void Convert(Chunk *srcChunk, int srcIndex, Chunk *dstChunk, int dstIndex, int count, ref EntityComponentStore entityComponentStore)
        {
            Assert.IsFalse(srcChunk == dstChunk);
            var srcArch = srcChunk->Archetype;
            var dstArch = dstChunk->Archetype;

            if (srcArch != dstArch)
            {
                Assert.IsFalse(srcArch == null);
            }

            var srcI = srcArch->NonZeroSizedTypesCount - 1;
            var dstI = dstArch->NonZeroSizedTypesCount - 1;

            var sourceTypesToDealloc      = stackalloc int[srcI + 1];
            int sourceTypesToDeallocCount = 0;

            while (dstI >= 0)
            {
                var srcType = srcArch->Types[srcI];
                var dstType = dstArch->Types[dstI];

                if (srcType > dstType)
                {
                    //Type in source is not moved so deallocate it
                    sourceTypesToDealloc[sourceTypesToDeallocCount++] = srcI;
                    --srcI;
                    continue;
                }

                var srcStride = srcArch->SizeOfs[srcI];
                var dstStride = dstArch->SizeOfs[dstI];
                var src       = srcChunk->Buffer + srcArch->Offsets[srcI] + srcIndex * srcStride;
                var dst       = dstChunk->Buffer + dstArch->Offsets[dstI] + dstIndex * dstStride;

                if (srcType == dstType)
                {
                    UnsafeUtility.MemCpy(dst, src, count * srcStride);
                    --srcI;
                    --dstI;
                }
                else
                {
                    if (dstType.IsBuffer)
                    {
                        InitializeBuffersInChunk(dst, count, dstStride, dstArch->BufferCapacities[dstI]);
                    }
                    else
                    {
                        UnsafeUtility.MemClear(dst, count * dstStride);
                    }
                    --dstI;
                }
            }

            if (sourceTypesToDeallocCount == 0)
            {
                return;
            }

            sourceTypesToDealloc[sourceTypesToDeallocCount] = 0;

            int iDealloc = 0;

            if (sourceTypesToDealloc[iDealloc] >= srcArch->FirstManagedComponent)
            {
                var freeCommandHandle = entityComponentStore.ManagedChangesTracker.BeginFreeManagedComponentCommand();
                do
                {
                    srcI = sourceTypesToDealloc[iDealloc];
                    var srcStride = srcArch->SizeOfs[srcI];
                    var src       = srcChunk->Buffer + srcArch->Offsets[srcI] + srcIndex * srcStride;

                    var a = (int *)src;
                    for (int i = 0; i < count; i++)
                    {
                        var managedComponentIndex = a[i];
                        if (managedComponentIndex == 0)
                        {
                            continue;
                        }
                        entityComponentStore.FreeManagedComponentIndex(managedComponentIndex);
                        entityComponentStore.ManagedChangesTracker.AddToFreeManagedComponentCommand(managedComponentIndex);
                    }
                } while ((sourceTypesToDealloc[++iDealloc] >= srcArch->FirstManagedComponent));
                entityComponentStore.ManagedChangesTracker.EndDeallocateManagedComponentCommand(freeCommandHandle);
            }

            while (sourceTypesToDealloc[iDealloc] >= srcArch->FirstBufferComponent)
            {
                srcI = sourceTypesToDealloc[iDealloc];
                var srcStride = srcArch->SizeOfs[srcI];
                var srcPtr    = srcChunk->Buffer + srcArch->Offsets[srcI] + srcIndex * srcStride;
                for (int i = 0; i < count; i++)
                {
                    BufferHeader.Destroy((BufferHeader *)srcPtr);
                    srcPtr += srcStride;
                }
                ++iDealloc;
            }
        }