public unsafe NativeArray <int> MoveAllSharedComponents(ManagedComponentStore srcManagedComponents,
                                                                Allocator allocator)
        {
            var remap = new NativeArray <int>(srcManagedComponents.GetSharedComponentCount(), allocator);

            remap[0] = 0;

            for (int srcIndex = 1; srcIndex < remap.Length; ++srcIndex)
            {
                var srcData = srcManagedComponents.m_SharedComponentData[srcIndex];
                if (srcData == null)
                {
                    continue;
                }

                var typeIndex = srcManagedComponents.m_SharedComponentType[srcIndex];

                var hashCode = TypeManager.GetHashCode(srcData, typeIndex);
                var dstIndex = InsertSharedComponentAssumeNonDefaultMove(typeIndex, hashCode, srcData);

                m_SharedComponentRefCount[dstIndex] += srcManagedComponents.m_SharedComponentRefCount[srcIndex] - 1;

                remap[srcIndex] = dstIndex;
            }

            srcManagedComponents.m_HashLookup.Clear();
            srcManagedComponents.m_SharedComponentVersion.ResizeUninitialized(1);
            srcManagedComponents.m_SharedComponentRefCount.ResizeUninitialized(1);
            srcManagedComponents.m_SharedComponentType.ResizeUninitialized(1);
            srcManagedComponents.m_SharedComponentData.Clear();
            srcManagedComponents.m_SharedComponentData.Add(null);
            srcManagedComponents.m_FreeListIndex = -1;

            return(remap);
        }
Exemple #2
0
        private unsafe int FindSharedComponentIndex<T>(int typeIndex, T newData) where T : struct
        {
            var defaultVal = default(T);
            if (TypeManager.Equals(ref defaultVal, ref newData))
                return 0;

            return FindNonDefaultSharedComponentIndex(typeIndex, TypeManager.GetHashCode(ref newData),
                UnsafeUtility.AddressOf(ref newData));
        }
        public static unsafe int GetHashCodeFast(object target, int typeIndex)
        {
            ulong handle;
            var   ptr      = PinGCObjectAndGetAddress(target, out handle);
            var   hashCode = TypeManager.GetHashCode(ptr, typeIndex);

            UnsafeUtility.ReleaseGCObject(handle);

            return(hashCode);
        }
Exemple #4
0
        // ----------------------------------------------------------------------------------------------------------
        // INTERNAL
        // ----------------------------------------------------------------------------------------------------------

        internal void SetSharedComponentDataBoxedDefaultMustBeNull(Entity entity, int typeIndex, object componentData)
        {
            var hashCode = 0;

            if (componentData != null)
            {
                hashCode = TypeManager.GetHashCode(componentData, typeIndex);
            }

            SetSharedComponentDataBoxedDefaultMustBeNull(entity, typeIndex, hashCode, componentData);
        }
Exemple #5
0
        public unsafe void CopySharedComponents(SharedComponentDataManager srcSharedComponents, int* sharedComponentIndices, int sharedComponentIndicesCount)
        {
            for (var i = 0; i != sharedComponentIndicesCount; i++)
            {
                var srcIndex = sharedComponentIndices[i];
                if (srcIndex == 0)
                    continue;

                var srcData = srcSharedComponents.m_SharedComponentData[srcIndex];
                var typeIndex = srcSharedComponents.m_SharedComponentType[srcIndex];
                var hashCode = TypeManager.GetHashCode(srcData, typeIndex);
                var dstIndex = InsertSharedComponentAssumeNonDefault(typeIndex, hashCode, srcData);

                sharedComponentIndices[i] = dstIndex;
            }
        }
        public void RemoveReference(int index, int numRefs = 1)
        {
            if (index == 0)
            {
                return;
            }

            var newCount = m_SharedComponentRefCount[index] -= numRefs;

            Assert.IsTrue(newCount >= 0);

            if (newCount != 0)
            {
                return;
            }

            var typeIndex = m_SharedComponentType[index];
            var hashCode  = TypeManager.GetHashCode(m_SharedComponentData[index], typeIndex);

            object sharedComponent = m_SharedComponentData[index];

            (sharedComponent as IRefCounted)?.Release();

            m_SharedComponentData[index]    = null;
            m_SharedComponentType[index]    = -1;
            m_SharedComponentVersion[index] = m_FreeListIndex;
            m_FreeListIndex = index;

            int itemIndex;
            NativeMultiHashMapIterator <int> iter;

            if (m_HashLookup.TryGetFirstValue(hashCode, out itemIndex, out iter))
            {
                do
                {
                    if (itemIndex == index)
                    {
                        m_HashLookup.Remove(iter);
                        return;
                    }
                } while (m_HashLookup.TryGetNextValue(out itemIndex, ref iter))
                ;
            }

            ThrowIndeterministicHash(sharedComponent);
        }
Exemple #7
0
        public int InsertSharedComponent<T>(T newData) where T : struct
        {
            var typeIndex = TypeManager.GetTypeIndex<T>();
            var index = FindSharedComponentIndex(TypeManager.GetTypeIndex<T>(), newData);

            if (index == 0) return 0;

            if (index != -1)
            {
                m_SharedComponentRefCount[index]++;
                return index;
            }

            var hashcode = TypeManager.GetHashCode<T>(ref newData);

            return Add(typeIndex, hashcode, newData);
        }
        public unsafe NativeArray <int> MoveSharedComponents(ManagedComponentStore srcManagedComponents,
                                                             NativeArray <ArchetypeChunk> chunks, NativeArray <EntityRemapUtility.EntityRemapInfo> remapInfos,
                                                             Allocator allocator)
        {
            var remap = new NativeArray <int>(srcManagedComponents.GetSharedComponentCount(), allocator);

            for (int i = 0; i < chunks.Length; ++i)
            {
                var chunk                 = chunks[i].m_Chunk;
                var archetype             = chunk->Archetype;
                var sharedComponentValues = chunk->SharedComponentValues;
                for (int sharedComponentIndex = 0;
                     sharedComponentIndex < archetype->NumSharedComponents;
                     ++sharedComponentIndex)
                {
                    remap[sharedComponentValues[sharedComponentIndex]]++;
                }
            }

            remap[0] = 0;

            for (int srcIndex = 1; srcIndex < remap.Length; ++srcIndex)
            {
                if (remap[srcIndex] == 0)
                {
                    continue;
                }

                var srcData   = srcManagedComponents.m_SharedComponentData[srcIndex];
                var typeIndex = srcManagedComponents.m_SharedComponentType[srcIndex];

                var hashCode = TypeManager.GetHashCode(srcData, typeIndex);

                var dstIndex = InsertSharedComponentAssumeNonDefault(typeIndex, hashCode, srcData);

                m_SharedComponentRefCount[dstIndex] += remap[srcIndex] - 1;
                srcManagedComponents.RemoveReference(srcIndex, remap[srcIndex]);

                remap[srcIndex] = dstIndex;
            }

            return(remap);
        }
Exemple #9
0
        public void RemoveReference(int index, int numRefs = 1)
        {
            if (index == 0)
                return;

            var newCount = m_SharedComponentRefCount[index] -= numRefs;
            Assert.IsTrue(newCount >= 0);

            if (newCount != 0)
                return;

            var typeIndex = m_SharedComponentType[index];
            var hashCode = TypeManager.GetHashCode(m_SharedComponentData[index], typeIndex);

            object sharedComponent = m_SharedComponentData[index];
            (sharedComponent as IDisposable)?.Dispose();

            m_SharedComponentData[index] = null;
            m_SharedComponentType[index] = -1;
            m_SharedComponentVersion[index] = m_FreeListIndex;
            m_FreeListIndex = index;

            int itemIndex;
            NativeMultiHashMapIterator<int> iter;
            if (!m_HashLookup.TryGetFirstValue(hashCode, out itemIndex, out iter))
            {
                #if ENABLE_UNITY_COLLECTIONS_CHECKS
                throw new System.ArgumentException("RemoveReference didn't find element in in hashtable");
                #endif
            }

            do
            {
                if (itemIndex == index)
                {
                    m_HashLookup.Remove(iter);
                    break;
                }
            }
            while (m_HashLookup.TryGetNextValue(out itemIndex, ref iter))
                ;
        }
        public unsafe void MoveSharedComponents(ManagedComponentStore srcManagedComponents, int *sharedComponentIndices, int sharedComponentIndicesCount)
        {
            for (var i = 0; i != sharedComponentIndicesCount; i++)
            {
                var srcIndex = sharedComponentIndices[i];
                if (srcIndex == 0)
                {
                    continue;
                }

                var srcData   = srcManagedComponents.m_SharedComponentData[srcIndex];
                var typeIndex = srcManagedComponents.m_SharedComponentType[srcIndex];
                var hashCode  = TypeManager.GetHashCode(srcData, typeIndex);
                var dstIndex  = InsertSharedComponentAssumeNonDefault(typeIndex, hashCode, srcData);

                srcManagedComponents.RemoveReference(srcIndex);

                sharedComponentIndices[i] = dstIndex;
            }
        }
        public int InsertSharedComponent <T>(T newData) where T : struct
        {
            var typeIndex = TypeManager.GetTypeIndex <T>();
            var index     = FindSharedComponentIndex(TypeManager.GetTypeIndex <T>(), newData);

            if (index == 0)
            {
                return(0);
            }

            if (index != -1)
            {
                m_SharedComponentRefCount[index]++;
                return(index);
            }

            var hashcode = TypeManager.GetHashCode <T>(ref newData);

            object newDataObj = newData;

            (newDataObj as IRefCounted)?.Retain();
            return(Add(typeIndex, hashcode, newDataObj));
        }
        public void CheckInternalConsistency()
        {
            int refcount = 0;

            for (int i = 0; i < m_SharedComponentData.Count; ++i)
            {
                if (m_SharedComponentData[i] != null)
                {
                    refcount++;

                    var typeIndex = m_SharedComponentType[i];
                    var hashCode  = TypeManager.GetHashCode(m_SharedComponentData[i], typeIndex);

                    bool found = false;
                    int  itemIndex;
                    NativeMultiHashMapIterator <int> iter;
                    if (m_HashLookup.TryGetFirstValue(hashCode, out itemIndex, out iter))
                    {
                        do
                        {
                            if (itemIndex == i)
                            {
                                found = true;
                            }
                        } while (m_HashLookup.TryGetNextValue(out itemIndex, ref iter))
                        ;
                    }

                    if (!found)
                    {
                        ThrowIndeterministicHash(m_SharedComponentData[i]);
                    }
                }
            }

            Assert.AreEqual(refcount, m_HashLookup.Length);
        }
Exemple #13
0
        // ----------------------------------------------------------------------------------------------------------
        // INTERNAL
        // ----------------------------------------------------------------------------------------------------------

        internal void SetSharedComponentDataBoxed(Entity entity, int typeIndex, object componentData)
        {
            var hashCode = TypeManager.GetHashCode(componentData, typeIndex);

            SetSharedComponentDataBoxed(entity, typeIndex, hashCode, componentData);
        }