From 198a1c761281fd164fce138927f6d621317fac5e Mon Sep 17 00:00:00 2001 From: goyuken Date: Sat, 20 Dec 2014 04:25:31 +0000 Subject: [PATCH] cleanup --- .../BinaryQuickSerializer.cs | 40 +++++++++++++++---- .../Consoles/WonderSwan/WonderSwan.cs | 3 +- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/BizHawk.Emulation.Common/BinaryQuickSerializer.cs b/BizHawk.Emulation.Common/BinaryQuickSerializer.cs index 21b2c42379..e50133150a 100644 --- a/BizHawk.Emulation.Common/BinaryQuickSerializer.cs +++ b/BizHawk.Emulation.Common/BinaryQuickSerializer.cs @@ -18,7 +18,8 @@ namespace BizHawk.Emulation.Common // fields are serialized/deserialized in their memory order as reported by Marshal.OffsetOf // to do anything useful, passed targets should be [StructLayout.Sequential] or [StructLayout.Explicit] - #region reflection infrastructure + #region methodinfo from a lambda expression. cool. + private static MethodInfo FromExpression(Expression e) { var caller = e as MethodCallExpression; @@ -26,7 +27,6 @@ namespace BizHawk.Emulation.Common throw new ArgumentException("Expression must be a method call"); return caller.Method; } - private static MethodInfo Method(Expression f) { return FromExpression(f.Body); @@ -36,6 +36,10 @@ namespace BizHawk.Emulation.Common return FromExpression(f.Body); } + #endregion + + #region read and write handlers for individual fields + private static Dictionary readhandlers = new Dictionary(); private static Dictionary writehandlers = new Dictionary(); @@ -46,6 +50,7 @@ namespace BizHawk.Emulation.Common throw new InvalidOperationException("Type mismatch"); readhandlers.Add(typeof(T), method); } + private static void AddW(Expression> f) { var method = Method(f); @@ -82,9 +87,12 @@ namespace BizHawk.Emulation.Common AddR(r => r.ReadUInt64()); AddW(r => r.Write((ulong)0)); - } + #endregion + + #region dynamic code generation + private delegate void Reader(object target, BinaryReader r); private delegate void Writer(object target, BinaryWriter w); @@ -95,7 +103,7 @@ namespace BizHawk.Emulation.Common public Writer Write; } - private static SerializationFactory CreateSer(Type t) + private static SerializationFactory CreateFactory(Type t) { var fields = DeepEquality.GetAllFields(t) //.OrderBy(fi => (int)fi.GetManagedOffset()) // [StructLayout.Sequential] doesn't work with this @@ -151,22 +159,38 @@ namespace BizHawk.Emulation.Common Write = (Writer)wmeth.CreateDelegate(typeof(Writer)) }; } + #endregion - private static IDictionary sers = + private static IDictionary serializers = new ConcurrentDictionary(); private static SerializationFactory GetFactory(Type t) { SerializationFactory f; - if (!sers.TryGetValue(t, out f)) + if (!serializers.TryGetValue(t, out f)) { - f = CreateSer(t); - sers[t] = f; + f = CreateFactory(t); + serializers[t] = f; } return f; } + public static T Create(BinaryReader r) + where T : new() + { + T target = new T(); + Read(target, r); + return target; + } + + public static object Create(Type t, BinaryReader r) + { + object target = Activator.CreateInstance(t); + Read(target, r); + return target; + } + public static void Read(object target, BinaryReader r) { GetFactory(target.GetType()).Read(target, r); diff --git a/BizHawk.Emulation.Cores/Consoles/WonderSwan/WonderSwan.cs b/BizHawk.Emulation.Cores/Consoles/WonderSwan/WonderSwan.cs index 3cbe563dd0..5c92fbc4ef 100644 --- a/BizHawk.Emulation.Cores/Consoles/WonderSwan/WonderSwan.cs +++ b/BizHawk.Emulation.Cores/Consoles/WonderSwan/WonderSwan.cs @@ -282,8 +282,7 @@ namespace BizHawk.Emulation.Cores.WonderSwan if (!BizSwan.bizswan_binstateload(Core, savebuff, savebuff.Length)) throw new InvalidOperationException("bizswan_binstateload() returned false!"); - var d = new TextStateData(); - BinaryQuickSerializer.Read(d, reader); + var d = BinaryQuickSerializer.Create(reader); LoadTextStateData(d); }