Пример #1
0
        public ContentEntitySerializationFormat GetSerializedFormat()
        {
            List <ContentEntity.DataInstance> data = new List <ContentEntity.DataInstance>();

            // the data instances that have been modified in the current update
            HashSet <int> modifiedThisFrame = new HashSet <int>();
            {
                List <DataAccessor> modifications = _concurrentModifications.ToList();
                foreach (DataAccessor modification in modifications)
                {
                    modifiedThisFrame.Add(modification.Id);
                }
            }

            // the data instances that have been removed in the current update
            HashSet <int> removedThisFrame = new HashSet <int>();

            {
                foreach (DataAccessor item in _toRemoveStage1)
                {
                    removedThisFrame.Add(item.Id);
                }
            }

            foreach (KeyValuePair <int, DataContainer> tuple in _data)
            {
                DataAccessor accessor = new DataAccessor(tuple.Key);

                // If the data was removed this frame, then next frame it won't exist anymore, so we
                // don't serialize it
                if (WasRemoved(accessor))
                {
                    continue;
                }

                var dataInstance = new ContentEntity.DataInstance()
                {
                    // these items are never added this frame; if WasAdded is true now, it will be
                    // false next frame
                    WasAdded = false,

                    // the data *may* have been removed this frame, though
                    WasRemoved = removedThisFrame.Contains(accessor.Id)
                };

                // if we were modified this frame, then we have to do a data swap (and set
                // WasModified to true)
                if (modifiedThisFrame.Contains(accessor.Id))
                {
                    // do a data swap so our modified data is correct
                    if (_data[accessor.Id] is VersionedDataContainer)
                    {
                        VersionedDataContainer container = (VersionedDataContainer)_data[accessor.Id];
                        dataInstance.CurrentData  = container.Modifying;
                        dataInstance.PreviousData = container.Current;
                    }
                    else
                    {
                        NonVersionedDataContainer container = (NonVersionedDataContainer)_data[accessor.Id];
                        dataInstance.CurrentData = container.Data;
                    }

                    dataInstance.WasModified = true;
                }

                // we were not modified this frame, so don't perform a data swap
                else
                {
                    // do a data swap so our modified data is correct
                    if (_data[accessor.Id] is VersionedDataContainer)
                    {
                        VersionedDataContainer container = (VersionedDataContainer)_data[accessor.Id];
                        dataInstance.CurrentData  = container.Current;
                        dataInstance.PreviousData = container.Previous;
                    }
                    else
                    {
                        NonVersionedDataContainer container = (NonVersionedDataContainer)_data[accessor.Id];
                        dataInstance.CurrentData = container.Data;
                    }

                    dataInstance.WasModified = false;
                }

                data.Add(dataInstance);
            }

            foreach (var toAdd in _toAddStage1)
            {
                data.Add(new ContentEntity.DataInstance()
                {
                    CurrentData  = toAdd,
                    PreviousData = toAdd,
                    WasAdded     = true,

                    // added data is never modified
                    WasModified = false,

                    // added data also cannot be removed in the same frame it was added in
                    WasRemoved = false
                });
            }

            return(new ContentEntitySerializationFormat()
            {
                PrettyName = PrettyName,
                UniqueId = UniqueId,
                Data = data
            });
        }
Пример #2
0
        public ContentEntitySerializationFormat GetSerializedFormat() {
            List<ContentEntity.DataInstance> data = new List<ContentEntity.DataInstance>();

            // the data instances that have been modified in the current update
            HashSet<int> modifiedThisFrame = new HashSet<int>();
            {
                List<DataAccessor> modifications = _concurrentModifications.ToList();
                foreach (DataAccessor modification in modifications) {
                    modifiedThisFrame.Add(modification.Id);
                }
            }

            // the data instances that have been removed in the current update
            HashSet<int> removedThisFrame = new HashSet<int>();
            {
                foreach (DataAccessor item in _toRemoveStage1) {
                    removedThisFrame.Add(item.Id);
                }
            }

            foreach (KeyValuePair<int, DataContainer> tuple in _data) {
                DataAccessor accessor = new DataAccessor(tuple.Key);

                // If the data was removed this frame, then next frame it won't exist anymore, so we
                // don't serialize it
                if (WasRemoved(accessor)) {
                    continue;
                }

                var dataInstance = new ContentEntity.DataInstance() {
                    // these items are never added this frame; if WasAdded is true now, it will be
                    // false next frame
                    WasAdded = false,

                    // the data *may* have been removed this frame, though
                    WasRemoved = removedThisFrame.Contains(accessor.Id)
                };

                // if we were modified this frame, then we have to do a data swap (and set
                // WasModified to true)
                if (modifiedThisFrame.Contains(accessor.Id)) {
                    // do a data swap so our modified data is correct
                    if (_data[accessor.Id] is VersionedDataContainer) {
                        VersionedDataContainer container = (VersionedDataContainer)_data[accessor.Id];
                        dataInstance.CurrentData = container.Modifying;
                        dataInstance.PreviousData = container.Current;
                    }
                    else {
                        NonVersionedDataContainer container = (NonVersionedDataContainer)_data[accessor.Id];
                        dataInstance.CurrentData = container.Data;
                    }

                    dataInstance.WasModified = true;
                }

                // we were not modified this frame, so don't perform a data swap
                else {
                    // do a data swap so our modified data is correct
                    if (_data[accessor.Id] is VersionedDataContainer) {
                        VersionedDataContainer container = (VersionedDataContainer)_data[accessor.Id];
                        dataInstance.CurrentData = container.Current;
                        dataInstance.PreviousData = container.Previous;
                    }
                    else {
                        NonVersionedDataContainer container = (NonVersionedDataContainer)_data[accessor.Id];
                        dataInstance.CurrentData = container.Data;
                    }

                    dataInstance.WasModified = false;
                }

                data.Add(dataInstance);
            }

            foreach (var toAdd in _toAddStage1) {
                data.Add(new ContentEntity.DataInstance() {
                    CurrentData = toAdd,
                    PreviousData = toAdd,
                    WasAdded = true,

                    // added data is never modified
                    WasModified = false,

                    // added data also cannot be removed in the same frame it was added in
                    WasRemoved = false
                });
            }

            return new ContentEntitySerializationFormat() {
                PrettyName = PrettyName,
                UniqueId = UniqueId,
                Data = data
            };
        }