add Serializer support for long, float, and ushort[]

This commit is contained in:
beirich 2014-04-07 04:50:19 +00:00
parent 6ca5768a31
commit d02eab8c2d
2 changed files with 169 additions and 0 deletions

View File

@ -248,6 +248,26 @@ namespace BizHawk.Common
}
}
public void Sync(string name, ref ushort[] val, bool useNull)
{
if (IsText)
{
SyncText(name, ref val, useNull);
}
else if (IsReader)
{
val = Util.ByteBufferToUshortBuffer(Util.ReadByteBuffer(_br, false));
if (val == null && !useNull)
{
val = new ushort[0];
}
}
else
{
Util.WriteByteBuffer(_bw, Util.UshortBufferToByteBuffer(val));
}
}
public void SyncText(string name, ref short[] val, bool useNull)
{
if (IsReader)
@ -270,6 +290,28 @@ namespace BizHawk.Common
}
}
public void SyncText(string name, ref ushort[] val, bool useNull)
{
if (IsReader)
{
if (Present(name))
{
var bytes = Util.HexStringToBytes(Item(name));
val = Util.ByteBufferToUshortBuffer(bytes);
}
if (val != null && val.Length == 0 && useNull)
{
val = null;
}
}
else
{
var temp = val ?? new ushort[0];
_tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.UshortBufferToByteBuffer(temp)));
}
}
public void Sync(string name, ref int[] val, bool useNull)
{
if (IsText)
@ -478,6 +520,38 @@ namespace BizHawk.Common
}
}
public void Sync(string name, ref long val)
{
if (IsText)
{
SyncText(name, ref val);
}
else if (IsReader)
{
Read(ref val);
}
else
{
Write(ref val);
}
}
public void Sync(string name, ref float 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)
@ -737,6 +811,30 @@ namespace BizHawk.Common
}
}
private void SyncText(string name, ref long val)
{
if (IsReader)
{
ReadText(name, ref val);
}
else
{
WriteText(name, ref val);
}
}
private void SyncText(string name, ref float val)
{
if (IsReader)
{
ReadText(name, ref val);
}
else
{
WriteText(name, ref val);
}
}
private void SyncText(string name, ref bool val)
{
if (IsReader)
@ -909,6 +1007,52 @@ namespace BizHawk.Common
_tw.WriteLine("{0} 0x{1:X8}", name, val);
}
private void Read(ref long val)
{
val = _br.ReadInt64();
}
private void Write(ref long val)
{
_bw.Write(val);
}
private void ReadText(string name, ref long val)
{
if (Present(name))
{
val = int.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber);
}
}
private void WriteText(string name, ref long val)
{
_tw.WriteLine("{0} 0x{1:X16}", name, val);
}
private void Read(ref float val)
{
val = _br.ReadSingle();
}
private void Write(ref float val)
{
_bw.Write(val);
}
private void ReadText(string name, ref float val)
{
if (Present(name))
{
val = float.Parse(Item(name));
}
}
private void WriteText(string name, ref float val)
{
_tw.WriteLine("{0} {1}", name, val);
}
private void Read(ref bool val)
{
val = _br.ReadBoolean();

View File

@ -205,6 +205,31 @@ namespace BizHawk.Common
return ret;
}
public static ushort[] ByteBufferToUshortBuffer(byte[] buf)
{
int num = buf.Length / 2;
var ret = new ushort[num];
for (int i = 0; i < num; i++)
{
ret[i] = (ushort)(buf[i * 2] | (buf[i * 2 + 1] << 8));
}
return ret;
}
public static byte[] UshortBufferToByteBuffer(ushort[] buf)
{
int num = buf.Length;
var ret = new byte[num * 2];
for (int i = 0; i < num; i++)
{
ret[i * 2 + 0] = (byte)(buf[i] & 0xFF);
ret[i * 2 + 1] = (byte)((buf[i] >> 8) & 0xFF);
}
return ret;
}
public static uint[] ByteBufferToUintBuffer(byte[] buf)
{
int num = buf.Length / 4;