private static PackedSharedComponentDataChange[] GetChangedSharedComponents(
            PackedCollection <EntityGuid> packedEntityCollection,
            PackedCollection <ComponentTypeHash> packedStableTypeHashCollection,
            NativeList <DeferredPackedSharedComponentDataChange> changes,
            ManagedComponentStore beforeManagedComponentStore,
            ManagedComponentStore afterManagedComponentStore)
        {
            if (changes.Length == 0)
            {
                return(s_EmptySetSharedComponentDiff);
            }

            var result = new List <PackedSharedComponentDataChange>();

            for (var i = 0; i < changes.Length; i++)
            {
                var change = changes[i];

                object afterValue = null;

                if (change.AfterSharedComponentIndex != 0)
                {
                    afterValue = afterManagedComponentStore.GetSharedComponentDataBoxed(change.AfterSharedComponentIndex, change.TypeIndex);
                }

                if (change.BeforeSharedComponentIndex > -1 && change.AfterSharedComponentIndex != 0)
                {
                    var beforeValue = beforeManagedComponentStore.GetSharedComponentDataBoxed(change.BeforeSharedComponentIndex, change.TypeIndex);

                    if (TypeManager.Equals(beforeValue, afterValue, change.TypeIndex))
                    {
                        continue;
                    }
                }

                var packedEntityIndex = packedEntityCollection.GetOrAdd(change.EntityGuid);
                var packedTypeIndex   = packedStableTypeHashCollection.GetOrAdd(new ComponentTypeHash
                {
                    StableTypeHash = TypeManager.GetTypeInfo(change.TypeIndex).StableTypeHash
                });

                var packedComponent = new PackedComponent
                {
                    PackedEntityIndex = packedEntityIndex,
                    PackedTypeIndex   = packedTypeIndex
                };

                result.Add(new PackedSharedComponentDataChange
                {
                    Component        = packedComponent,
                    BoxedSharedValue = afterValue
                });
            }

            return(result.ToArray());
        }
            public unsafe void SetComponentData(PackedComponent component, void *ptr, int sizeOf)
            {
                m_ComponentDataChanges.Add(new PackedComponentDataChange
                {
                    Component = component,
                    Offset    = 0,
                    Size      = sizeOf
                });

                m_Payload.AddRange(ptr, sizeOf);
            }
            private void ExtractEntityReferencePatches(
                TypeInfo typeInfo,
                PackedComponent component,
                byte *afterAddress,
                int elementCount)
            {
                if (typeInfo.EntityOffsetCount == 0)
                {
                    return;
                }

                var elementOffset = 0;

                for (var elementIndex = 0; elementIndex < elementCount; ++elementIndex)
                {
                    for (var offsetIndex = 0; offsetIndex < typeInfo.EntityOffsetCount; ++offsetIndex)
                    {
                        var offset = elementOffset + typeInfo.EntityOffsets[offsetIndex];
                        var entity = *(Entity *)(afterAddress + offset);

                        // If the entity has no guid, then guid will be null (desired)
                        if (!AfterEntityToEntityGuid.TryGetValue(entity, out var entityGuid))
                        {
                            entityGuid = default;
                        }

                        EntityPatches.Add(new EntityReferenceChange
                        {
                            Component = component,
                            Offset    = offset,
                            Value     = entityGuid
                        });
                    }

                    elementOffset += typeInfo.ElementSize;
                }
            }