Пример #1
0
        public BaseSnapObject MakeCopy()
        {
            var item = SnapObjectsInfo.GetInstanceByType(Type);

            item.Deserialize(Serialize(), 0);
            return(item);
        }
Пример #2
0
        public static Snapshot UnpackDelta(Snapshot from, int[] inputData,
                                           int inputOffset, int inputSize)
        {
            var snapshotBuilder = new SnapshotBuilder();
            var endIndex        = inputOffset + inputSize;

            var numDeletedItems = inputData[inputOffset++];
            var numUpdatedItems = inputData[inputOffset++];
            var numTempItems    = inputData[inputOffset++];
            var deletedOffset   = inputOffset;

            inputOffset += numDeletedItems;

            if (inputOffset > endIndex)
            {
                return(null);
            }

            snapshotBuilder.StartBuild();

            for (var i = 0; i < from.ItemsCount; i++)
            {
                var item = from[i];
                var keep = true;

                for (var d = 0; d < numDeletedItems; d++)
                {
                    if (inputData[deletedOffset + d] == item.Key)
                    {
                        keep = false;
                        break;
                    }
                }

                if (keep)
                {
                    snapshotBuilder.AddItem(item.Object.MakeCopy(), item.Id);
                }
            }

            for (var i = 0; i < numUpdatedItems; i++)
            {
                if (inputOffset + 2 > endIndex)
                {
                    return(null);
                }

                var type = (SnapObject)inputData[inputOffset++];
                var id   = inputData[inputOffset++];
                int itemSize; // in bytes

                if (SnapObjectsInfo.GetSizeByType(type) != 0)
                {
                    itemSize = SnapObjectsInfo.GetSizeByType(type);
                }
                else
                {
                    if (inputOffset + 1 > endIndex)
                    {
                        return(null);
                    }

                    itemSize = inputData[inputOffset++] * sizeof(int);
                }

                if (itemSize < 0 || !RangeCheck(endIndex, inputOffset, itemSize / sizeof(int)))
                {
                    return(null);
                }

                var key     = ((int)type << 16) | id;
                var newItem = snapshotBuilder.FindItem(key)?.Object;

                if (newItem == null)
                {
                    var item = SnapObjectsInfo.GetInstanceByType(type);
                    if (snapshotBuilder.AddItem(item, id))
                    {
                        newItem = item;
                    }
                }

                if (newItem == null)
                {
                    return(null);
                }

                var fromItem = from.FindItem(key);
                if (fromItem != null)
                {
                    UndiffItem(fromItem.Object, inputData, inputOffset, newItem);
                }
                else
                {
                    newItem.Deserialize(inputData, inputOffset);
                }

                inputOffset += itemSize / sizeof(int);
            }

            return(snapshotBuilder.EndBuild());
        }