From ff4acab261cbabeddc309d15d14d07aee27cc500 Mon Sep 17 00:00:00 2001 From: Asnivor Date: Mon, 8 Apr 2019 08:59:01 +0100 Subject: [PATCH] Serializer: Add support for float[], double and double[] --- BizHawk.Common/Serializer.cs | 135 +++++++++++++++++++++++++++++++++++ BizHawk.Common/Util.cs | 32 +++++++++ 2 files changed, 167 insertions(+) diff --git a/BizHawk.Common/Serializer.cs b/BizHawk.Common/Serializer.cs index baca009720..ae0a3df4c0 100644 --- a/BizHawk.Common/Serializer.cs +++ b/BizHawk.Common/Serializer.cs @@ -440,6 +440,90 @@ namespace BizHawk.Common } } + public void Sync(string name, ref float[] val, bool useNull) + { + if (IsText) + { + SyncText(name, ref val, useNull); + } + else if (IsReader) + { + val = Util.ByteBufferToFloatBuffer(Util.ReadByteBuffer(_br, false)); + if (val == null && !useNull) + { + val = new float[0]; + } + } + else + { + Util.WriteByteBuffer(_bw, Util.FloatBufferToByteBuffer(val)); + } + } + + public void SyncText(string name, ref float[] val, bool useNull) + { + if (IsReader) + { + if (Present(name)) + { + var bytes = Util.HexStringToBytes(Item(name)); + val = Util.ByteBufferToFloatBuffer(bytes); + } + + if (val != null && val.Length == 0 && useNull) + { + val = null; + } + } + else + { + var temp = val ?? new float[0]; + _tw.WriteLine("{0} {1}", name, Util.FloatBufferToByteBuffer(temp).BytesToHexString()); + } + } + + public void Sync(string name, ref double[] val, bool useNull) + { + if (IsText) + { + SyncText(name, ref val, useNull); + } + else if (IsReader) + { + val = Util.ByteBufferToDoubleBuffer(Util.ReadByteBuffer(_br, false)); + if (val == null && !useNull) + { + val = new double[0]; + } + } + else + { + Util.WriteByteBuffer(_bw, Util.DoubleBufferToByteBuffer(val)); + } + } + + public void SyncText(string name, ref double[] val, bool useNull) + { + if (IsReader) + { + if (Present(name)) + { + var bytes = Util.HexStringToBytes(Item(name)); + val = Util.ByteBufferToDoubleBuffer(bytes); + } + + if (val != null && val.Length == 0 && useNull) + { + val = null; + } + } + else + { + var temp = val ?? new double[0]; + _tw.WriteLine("{0} {1}", name, Util.DoubleBufferToByteBuffer(temp).BytesToHexString()); + } + } + public void Sync(string name, ref Bit val) { if (IsText) @@ -612,6 +696,22 @@ namespace BizHawk.Common } } + public void Sync(string name, ref double val) + { + if (IsText) + { + SyncText(name, ref val); + } + else if (IsReader) + { + Read(ref val); + } + else + { + Write(ref val); + } + } + public void Sync(string name, ref bool val) { if (IsText) @@ -907,6 +1007,18 @@ namespace BizHawk.Common } } + private void SyncText(string name, ref double val) + { + if (IsReader) + { + ReadText(name, ref val); + } + else + { + WriteText(name, ref val); + } + } + private void SyncText(string name, ref bool val) { if (IsReader) @@ -1135,6 +1247,16 @@ namespace BizHawk.Common _bw.Write(val); } + private void Read(ref double val) + { + val = _br.ReadDouble(); + } + + private void Write(ref double val) + { + _bw.Write(val); + } + private void ReadText(string name, ref float val) { if (Present(name)) @@ -1148,6 +1270,19 @@ namespace BizHawk.Common _tw.WriteLine("{0} {1}", name, val); } + private void ReadText(string name, ref double val) + { + if (Present(name)) + { + val = double.Parse(Item(name)); + } + } + + private void WriteText(string name, ref double val) + { + _tw.WriteLine("{0} {1}", name, val); + } + private void Read(ref bool val) { val = _br.ReadBoolean(); diff --git a/BizHawk.Common/Util.cs b/BizHawk.Common/Util.cs index c0d6f94185..17c8a90722 100644 --- a/BizHawk.Common/Util.cs +++ b/BizHawk.Common/Util.cs @@ -299,6 +299,38 @@ namespace BizHawk.Common return ret; } + public static float[] ByteBufferToFloatBuffer(byte[] buf) + { + int num = buf.Length / sizeof(float); + var ret = new float[num]; + Buffer.BlockCopy(buf, 0, ret, 0, num); + return ret; + } + + public static byte[] FloatBufferToByteBuffer(float[] buf) + { + int num = buf.Length; + var ret = new byte[num * sizeof(float)]; + Buffer.BlockCopy(buf, 0, ret, 0, ret.Length); + return ret; + } + + public static double[] ByteBufferToDoubleBuffer(byte[] buf) + { + int num = buf.Length; + var ret = new double[num / sizeof(double)]; + Buffer.BlockCopy(buf, 0, ret, 0, num); + return ret; + } + + public static byte[] DoubleBufferToByteBuffer(double[] buf) + { + int num = buf.Length; + var ret = new byte[num * sizeof(double)]; + Buffer.BlockCopy(buf, 0, ret, 0, ret.Length); + return ret; + } + public static byte[] ReadByteBuffer(BinaryReader br, bool returnNull) { int len = br.ReadInt32();