1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 12:33:03 +08:00
osu-lazer/osu.Game/IO/Legacy/SerializationReader.cs

306 lines
9.7 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace osu.Game.IO.Legacy
{
/// <summary> SerializationReader. Extends BinaryReader to add additional data types,
/// handle null strings and simplify use with ISerializable. </summary>
public class SerializationReader : BinaryReader
{
private readonly Stream stream;
public SerializationReader(Stream s)
: base(s, Encoding.UTF8)
{
stream = s;
}
public int RemainingBytes => (int)(stream.Length - stream.Position);
/// <summary> Static method to take a SerializationInfo object (an input to an ISerializable constructor)
/// and produce a SerializationReader from which serialized objects can be read </summary>.
public static SerializationReader GetReader(SerializationInfo info)
{
byte[] byteArray = (byte[])info.GetValue("X", typeof(byte[]));
MemoryStream ms = new MemoryStream(byteArray);
return new SerializationReader(ms);
}
/// <summary> Reads a string from the buffer. Overrides the base implementation so it can cope with nulls. </summary>
public override string ReadString()
{
2019-01-24 16:46:09 +08:00
if (ReadByte() == 0) return null;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
return base.ReadString();
}
/// <summary> Reads a byte array from the buffer, handling nulls and the array length. </summary>
public byte[] ReadByteArray()
{
int len = ReadInt32();
if (len > 0) return ReadBytes(len);
if (len < 0) return null;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
return Array.Empty<byte>();
}
/// <summary> Reads a char array from the buffer, handling nulls and the array length. </summary>
public char[] ReadCharArray()
{
int len = ReadInt32();
if (len > 0) return ReadChars(len);
if (len < 0) return null;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
return Array.Empty<char>();
}
/// <summary> Reads a DateTime from the buffer. </summary>
public DateTime ReadDateTime()
{
long ticks = ReadInt64();
if (ticks < 0) throw new IOException("Bad ticks count read!");
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
return new DateTime(ticks, DateTimeKind.Utc);
}
/// <summary> Reads a generic list from the buffer. </summary>
public IList<T> ReadBList<T>(bool skipErrors = false) where T : ILegacySerializable, new()
{
int count = ReadInt32();
if (count < 0) return null;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
IList<T> d = new List<T>(count);
SerializationReader sr = new SerializationReader(BaseStream);
for (int i = 0; i < count; i++)
{
T obj = new T();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
try
{
obj.ReadFromStream(sr);
}
catch (Exception)
{
if (skipErrors)
continue;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
throw;
}
d.Add(obj);
}
return d;
}
/// <summary> Reads a generic list from the buffer. </summary>
public IList<T> ReadList<T>()
{
int count = ReadInt32();
if (count < 0) return null;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
IList<T> d = new List<T>(count);
for (int i = 0; i < count; i++) d.Add((T)ReadObject());
return d;
}
/// <summary> Reads a generic Dictionary from the buffer. </summary>
public IDictionary<TKey, TValue> ReadDictionary<TKey, TValue>()
2018-04-13 17:19:50 +08:00
{
int count = ReadInt32();
if (count < 0) return null;
2019-02-28 12:31:40 +08:00
IDictionary<TKey, TValue> d = new Dictionary<TKey, TValue>();
for (int i = 0; i < count; i++) d[(TKey)ReadObject()] = (TValue)ReadObject();
2018-04-13 17:19:50 +08:00
return d;
}
/// <summary> Reads an object which was added to the buffer by WriteObject. </summary>
public object ReadObject()
{
ObjType t = (ObjType)ReadByte();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
switch (t)
{
case ObjType.boolType:
return ReadBoolean();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.byteType:
return ReadByte();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.uint16Type:
return ReadUInt16();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.uint32Type:
return ReadUInt32();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.uint64Type:
return ReadUInt64();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.sbyteType:
return ReadSByte();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.int16Type:
return ReadInt16();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.int32Type:
return ReadInt32();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.int64Type:
return ReadInt64();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.charType:
return ReadChar();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.stringType:
return base.ReadString();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.singleType:
return ReadSingle();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.doubleType:
return ReadDouble();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.decimalType:
return ReadDecimal();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.dateTimeType:
return ReadDateTime();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.byteArrayType:
return ReadByteArray();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.charArrayType:
return ReadCharArray();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ObjType.otherType:
return DynamicDeserializer.Deserialize(BaseStream);
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
default:
return null;
}
}
2019-11-28 22:26:10 +08:00
public static class DynamicDeserializer
2018-04-13 17:19:50 +08:00
{
private static VersionConfigToNamespaceAssemblyObjectBinder versionBinder;
private static BinaryFormatter formatter;
private static void initialize()
{
versionBinder = new VersionConfigToNamespaceAssemblyObjectBinder();
formatter = new BinaryFormatter
{
2019-02-28 12:31:40 +08:00
// AssemblyFormat = FormatterAssemblyStyle.Simple,
2018-04-13 17:19:50 +08:00
Binder = versionBinder
};
}
public static object Deserialize(Stream stream)
{
if (formatter == null)
initialize();
Debug.Assert(formatter != null, "formatter != null");
// ReSharper disable once PossibleNullReferenceException
return formatter.Deserialize(stream);
}
#region Nested type: VersionConfigToNamespaceAssemblyObjectBinder
public sealed class VersionConfigToNamespaceAssemblyObjectBinder : SerializationBinder
{
private readonly Dictionary<string, Type> cache = new Dictionary<string, Type>();
public override Type BindToType(string assemblyName, string typeName)
{
2019-11-12 18:22:35 +08:00
if (cache.TryGetValue(assemblyName + typeName, out var typeToDeserialize))
2018-04-13 17:19:50 +08:00
return typeToDeserialize;
List<Type> tmpTypes = new List<Type>();
Type genType = null;
if (typeName.Contains("System.Collections.Generic") && typeName.Contains("[["))
{
string[] splitTyps = typeName.Split('[');
foreach (string typ in splitTyps)
{
if (typ.Contains("Version"))
{
string asmTmp = typ.Substring(typ.IndexOf(',') + 1);
string asmName = asmTmp.Remove(asmTmp.IndexOf(']')).Trim();
string typName = typ.Remove(typ.IndexOf(','));
tmpTypes.Add(BindToType(asmName, typName));
}
else if (typ.Contains("Generic"))
{
genType = BindToType(assemblyName, typ);
}
}
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
if (genType != null && tmpTypes.Count > 0)
{
return genType.MakeGenericType(tmpTypes.ToArray());
}
}
string toAssemblyName = assemblyName.Split(',')[0];
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
foreach (Assembly a in assemblies)
{
if (a.FullName.Split(',')[0] == toAssemblyName)
{
typeToDeserialize = a.GetType(typeName);
break;
}
}
cache.Add(assemblyName + typeName, typeToDeserialize);
return typeToDeserialize;
}
}
#endregion
}
}
public enum ObjType : byte
{
nullType,
boolType,
byteType,
uint16Type,
uint32Type,
uint64Type,
sbyteType,
int16Type,
int32Type,
int64Type,
charType,
stringType,
singleType,
doubleType,
decimalType,
dateTimeType,
byteArrayType,
charArrayType,
otherType,
ILegacySerializableType
}
}