Пример #1
0
        public void Serialize(ISerializableData serializableDataManager, uint version)
        {
            Serializer serializer;

            if (!Serializers.TryGetValue(version, out serializer))
            {
                throw new InvalidOperationException($"No Serializer with version {version} found!");
            }

            DebugLog.Message("Serialize version {0}", version);

            if (serializer.ShouldDeleteData())
            {
                DebugLog.Info($"Deleting saved data because serializer version {version} requested deletion.");
                serializableDataManager.EraseData(Id);
            }
            else
            {
                var data = serializer.SerializeData();
                if (data == null)
                {
                    DebugLog.Info($"No data to serialize (-> delete data if present)");
                    serializableDataManager.EraseData(Id);
                }
                else
                {
                    var bytesVersion    = BitConverter.GetBytes(version);
                    var dataWithVersion = bytesVersion.Concat(data).ToArray();

                    serializableDataManager.SaveData(Id, dataWithVersion);

                    DebugLog.Message("Serialized {0} bytes", dataWithVersion.Length - 4);
                }
            }
        }
Пример #2
0
 public static void DeleteAllData(ISerializableData serializableDataManager)
 {
     // test for contains is unnecessary
     if (Contains(Id, serializableDataManager))
     {
         Log.Info($"Removing data with ID {Id}");
         serializableDataManager.EraseData(Id);
     }
 }
Пример #3
0
        public void Deserialize(ISerializableData serializableDataManager)
        {
            if (!Contains(Id, serializableDataManager))
            {
                DebugLog.Message("Save does not contain data with id {0}", Id);
                return;
            }

            var data = serializableDataManager.LoadData(Id);

            DebugLog.Message("Read {0} bytes for Id {1}", data.Length, Id);

            const int versionLength = 4;

            Debug.Assert(data.Length > versionLength);
            if (data.Length < versionLength)
            {
                throw new InvalidOperationException($"Data with id {Id} should be at least {versionLength} bytes long, but is only {data.Length}B");
            }

            var version = BitConverter.ToUInt32(data.Take(versionLength).ToArray(), 0);

            DebugLog.Message("Deserializer version {0}", version);

            Serializer serializer;

            if (!Serializers.TryGetValue(version, out serializer))
            {
                throw new InvalidOperationException($"No Serializer with version {version} found!");
            }

            if (serializer.ShouldDeleteData())
            {
                Log.Info($"Deleting saved data because serializer version {version} requested deletion.");
                serializableDataManager.EraseData(Id);
            }
            else
            {
                var rData = data.Skip(versionLength).ToArray();
                serializer.DeserializeData(rData);
                DebugLog.Message("Deserialized {0} bytes", rData.Length);
            }
        }