diff --git a/BizHawk.Client.Common/SavestateManager.cs b/BizHawk.Client.Common/SavestateManager.cs index 2608484e80..0aa59e931d 100644 --- a/BizHawk.Client.Common/SavestateManager.cs +++ b/BizHawk.Client.Common/SavestateManager.cs @@ -2,6 +2,7 @@ using System.IO; using BizHawk.Common; +using BizHawk.Common.IOExtensions; namespace BizHawk.Client.Common { diff --git a/BizHawk.Client.EmuHawk/AVOut/WavWriter.cs b/BizHawk.Client.EmuHawk/AVOut/WavWriter.cs index d78bc078e7..86273587f3 100644 --- a/BizHawk.Client.EmuHawk/AVOut/WavWriter.cs +++ b/BizHawk.Client.EmuHawk/AVOut/WavWriter.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.IO; -using BizHawk.Common; +using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk diff --git a/BizHawk.Common/BizHawk.Common.csproj b/BizHawk.Common/BizHawk.Common.csproj index 656bb4e2b6..6684c432a2 100644 --- a/BizHawk.Common/BizHawk.Common.csproj +++ b/BizHawk.Common/BizHawk.Common.csproj @@ -54,6 +54,7 @@ + diff --git a/BizHawk.Common/Extensions/Extensions.cs b/BizHawk.Common/Extensions/Extensions.cs index c7b29f914e..7f02725c24 100644 --- a/BizHawk.Common/Extensions/Extensions.cs +++ b/BizHawk.Common/Extensions/Extensions.cs @@ -107,38 +107,6 @@ namespace BizHawk.Common throw new InvalidOperationException("Item not found"); } - public static void CopyTo(this Stream src, Stream dest) - { - int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000; - byte[] buffer = new byte[size]; - int n; - do - { - n = src.Read(buffer, 0, buffer.Length); - dest.Write(buffer, 0, n); - } while (n != 0); - } - - public static void CopyTo(this MemoryStream src, Stream dest) - { - dest.Write(src.GetBuffer(), (int)src.Position, (int)(src.Length - src.Position)); - } - - public static void CopyTo(this Stream src, MemoryStream dest) - { - if (src.CanSeek) - { - int pos = (int)dest.Position; - int length = (int)(src.Length - src.Position) + pos; - dest.SetLength(length); - - while (pos < length) - pos += src.Read(dest.GetBuffer(), pos, length - pos); - } - else - src.CopyTo(dest); - } - public static bool Bit(this byte b, int index) { return (b & (1 << index)) != 0; @@ -235,68 +203,6 @@ namespace BizHawk.Common writer.WriteLine(); } - public static void Write(this BinaryWriter bw, int[] buffer) - { - foreach (int b in buffer) - { - bw.Write(b); - } - } - - public static void Write(this BinaryWriter bw, uint[] buffer) - { - foreach (uint b in buffer) - { - bw.Write(b); - } - } - - public static void Write(this BinaryWriter bw, short[] buffer) - { - foreach (short b in buffer) - { - bw.Write(b); - } - } - - public static void Write(this BinaryWriter bw, ushort[] buffer) - { - foreach (ushort t in buffer) - { - bw.Write(t); - } - } - - public static int[] ReadInt32s(this BinaryReader br, int num) - { - int[] ret = new int[num]; - for (int i = 0; i < num; i++) - { - ret[i] = br.ReadInt32(); - } - return ret; - } - - public static short[] ReadInt16s(this BinaryReader br, int num) - { - short[] ret = new short[num]; - for (int i = 0; i < num; i++) - { - ret[i] = br.ReadInt16(); - } - return ret; - } - - public static ushort[] ReadUInt16s(this BinaryReader br, int num) - { - ushort[] ret = new ushort[num]; - for (int i = 0; i < num; i++) - { - ret[i] = br.ReadUInt16(); - } - return ret; - } - public static void ReadFromHex(this byte[] buffer, string hex) { if (hex.Length%2 != 0) @@ -385,9 +291,5 @@ namespace BizHawk.Common } #endregion - - //these don't work??? they dont get chosen by compiler - public static void WriteBit(this BinaryWriter bw, Bit bit) { bw.Write((bool)bit); } - public static Bit ReadBit(this BinaryReader br) { return br.ReadBoolean(); } } } \ No newline at end of file diff --git a/BizHawk.Common/Extensions/IOExtensions.cs b/BizHawk.Common/Extensions/IOExtensions.cs new file mode 100644 index 0000000000..ee628f0853 --- /dev/null +++ b/BizHawk.Common/Extensions/IOExtensions.cs @@ -0,0 +1,119 @@ +using System; +using System.IO; + +namespace BizHawk.Common.IOExtensions +{ + public static class IOExtensions + { + public static void CopyTo(this Stream src, Stream dest) + { + int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000; + byte[] buffer = new byte[size]; + int n; + do + { + n = src.Read(buffer, 0, buffer.Length); + dest.Write(buffer, 0, n); + } while (n != 0); + } + + public static void CopyTo(this MemoryStream src, Stream dest) + { + dest.Write(src.GetBuffer(), (int)src.Position, (int)(src.Length - src.Position)); + } + + public static void CopyTo(this Stream src, MemoryStream dest) + { + if (src.CanSeek) + { + int pos = (int)dest.Position; + int length = (int)(src.Length - src.Position) + pos; + dest.SetLength(length); + + while (pos < length) + { + pos += src.Read(dest.GetBuffer(), pos, length - pos); + } + } + else + { + src.CopyTo(dest); + } + } + + public static void Write(this BinaryWriter bw, int[] buffer) + { + foreach (int b in buffer) + { + bw.Write(b); + } + } + + public static void Write(this BinaryWriter bw, uint[] buffer) + { + foreach (uint b in buffer) + { + bw.Write(b); + } + } + + public static void Write(this BinaryWriter bw, short[] buffer) + { + foreach (short b in buffer) + { + bw.Write(b); + } + } + + public static void Write(this BinaryWriter bw, ushort[] buffer) + { + foreach (ushort t in buffer) + { + bw.Write(t); + } + } + + public static int[] ReadInt32s(this BinaryReader br, int num) + { + int[] ret = new int[num]; + for (int i = 0; i < num; i++) + { + ret[i] = br.ReadInt32(); + } + + return ret; + } + + public static short[] ReadInt16s(this BinaryReader br, int num) + { + short[] ret = new short[num]; + for (int i = 0; i < num; i++) + { + ret[i] = br.ReadInt16(); + } + + return ret; + } + + public static ushort[] ReadUInt16s(this BinaryReader br, int num) + { + ushort[] ret = new ushort[num]; + for (int i = 0; i < num; i++) + { + ret[i] = br.ReadUInt16(); + } + + return ret; + } + + public static void WriteBit(this BinaryWriter bw, Bit bit) + { + bw.Write((bool)bit); + } + + public static Bit ReadBit(this BinaryReader br) + { + return br.ReadBoolean(); + } + } +} diff --git a/BizHawk.Common/Serializer.cs b/BizHawk.Common/Serializer.cs index 12c851e604..9acd9e2f8a 100644 --- a/BizHawk.Common/Serializer.cs +++ b/BizHawk.Common/Serializer.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using BizHawk.Common.IOExtensions; + namespace BizHawk.Common { public unsafe class Serializer diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/Genesis/GenVDP.cs b/BizHawk.Emulation.Cores/Consoles/Sega/Genesis/GenVDP.cs index 21a782188d..ced718ebc0 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/Genesis/GenVDP.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/Genesis/GenVDP.cs @@ -3,6 +3,7 @@ using System.IO; using System.Globalization; using BizHawk.Common; +using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Sega.Genesis