some random code cleanup on BizHawk.Common

This commit is contained in:
adelikat 2013-11-15 00:22:08 +00:00
parent d67ff542ba
commit 030f30628d
6 changed files with 229 additions and 206 deletions

View File

@ -59,15 +59,26 @@ namespace BizHawk.Common
public void Dispose() public void Dispose()
{ {
if (arr != null) Dispose(true);
hnd.Free(); GC.SuppressFinalize(this);
arr = null;
} }
~CBuffer() { Dispose(); } protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (arr != null)
{
hnd.Free();
}
arr = null;
}
}
~CBuffer() { Dispose(true); }
} }
public class ByteBuffer : CBuffer<byte> public sealed class ByteBuffer : CBuffer<byte>
{ {
public ByteBuffer(int amt) : base(amt,1) { } public ByteBuffer(int amt) : base(amt,1) { }
public ByteBuffer(byte[] arr) : base(arr,1) { } public ByteBuffer(byte[] arr) : base(arr,1) { }
@ -83,7 +94,7 @@ namespace BizHawk.Common
} }
} }
public class IntBuffer : CBuffer<int> public sealed class IntBuffer : CBuffer<int>
{ {
public IntBuffer(int amt) : base(amt, 4) { } public IntBuffer(int amt) : base(amt, 4) { }
public IntBuffer(int[] arr) : base(arr,4) { } public IntBuffer(int[] arr) : base(arr,4) { }
@ -99,7 +110,7 @@ namespace BizHawk.Common
} }
} }
public class ShortBuffer : CBuffer<short> public sealed class ShortBuffer : CBuffer<short>
{ {
public ShortBuffer(int amt) : base(amt, 2) { } public ShortBuffer(int amt) : base(amt, 2) { }
public ShortBuffer(short[] arr) : base(arr, 2) { } public ShortBuffer(short[] arr) : base(arr, 2) { }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -21,7 +22,7 @@ namespace BizHawk.Common
{ {
int min = 0; int min = 0;
int max = list.Count; int max = list.Count;
int mid = 0; int mid;
TKey midKey; TKey midKey;
while (min < max) while (min < max)
{ {
@ -65,22 +66,22 @@ namespace BizHawk.Common
public static string ToHexString(this int n, int numdigits) public static string ToHexString(this int n, int numdigits)
{ {
return string.Format("{0:X" + numdigits + "}", n); return String.Format("{0:X" + numdigits + "}", n);
} }
public static string ToHexString(this uint n, int numdigits) public static string ToHexString(this uint n, int numdigits)
{ {
return string.Format("{0:X" + numdigits + "}", n); return String.Format("{0:X" + numdigits + "}", n);
} }
public static string ToHexString(this byte n, int numdigits) public static string ToHexString(this byte n, int numdigits)
{ {
return string.Format("{0:X" + numdigits + "}", n); return String.Format("{0:X" + numdigits + "}", n);
} }
public static string ToHexString(this ushort n, int numdigits) public static string ToHexString(this ushort n, int numdigits)
{ {
return string.Format("{0:X" + numdigits + "}", n); return String.Format("{0:X" + numdigits + "}", n);
} }
//http://stackoverflow.com/questions/1766328/can-linq-use-binary-search-when-the-collection-is-ordered //http://stackoverflow.com/questions/1766328/can-linq-use-binary-search-when-the-collection-is-ordered
@ -151,14 +152,7 @@ namespace BizHawk.Common
public static bool IsBinary(this string str) public static bool IsBinary(this string str)
{ {
for (int i = 0; i < str.Length; i++) return str.All(c => c == '0' || c == '1');
{
char c = str[i];
if (c == '0' || c == '1')
continue;
return false;
}
return true;
} }
public static bool Bit(this byte b, int index) public static bool Bit(this byte b, int index)
@ -179,84 +173,59 @@ namespace BizHawk.Common
public static string GetPrecedingString(this string str, string value) public static string GetPrecedingString(this string str, string value)
{ {
int index = str.IndexOf(value); int index = str.IndexOf(value);
if (index < 0) if (index < 0)
{
return null; return null;
if (index == 0) }
return ""; else if (index == 0)
return str.Substring(0, index); {
return String.Empty;
}
else
{
return str.Substring(0, index);
}
} }
public static bool In(this string str, params string[] options) public static bool In(this string str, params string[] options)
{ {
foreach (string opt in options) return options.Any(opt => opt.Equals(str, StringComparison.CurrentCultureIgnoreCase));
{
if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true;
}
return false;
} }
public static bool In(this string str, IEnumerable<string> options) public static bool In(this string str, IEnumerable<string> options)
{ {
foreach (string opt in options) return options.Any(opt => opt.Equals(str, StringComparison.CurrentCultureIgnoreCase));
{
if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true;
}
return false;
} }
public static bool In<T>(this string str, IEnumerable<T> options, Func<T, string, bool> eval) public static bool In<T>(this string str, IEnumerable<T> options, Func<T, string, bool> eval)
{ {
foreach (T opt in options) return options.Any(opt => eval(opt, str));
{
if (eval(opt, str))
return true;
}
return false;
} }
public static bool NotIn(this string str, params string[] options) public static bool NotIn(this string str, params string[] options)
{ {
foreach (string opt in options) return options.All(opt => opt.ToLower() != str.ToLower());
{
if (opt.ToLower() == str.ToLower()) return false;
}
return true;
} }
public static bool NotIn(this string str, IEnumerable<string> options) public static bool NotIn(this string str, IEnumerable<string> options)
{ {
foreach (string opt in options) return options.All(opt => opt.ToLower() != str.ToLower());
{
if (opt.ToLower() == str.ToLower()) return false;
}
return true;
} }
public static bool In(this int i, params int[] options) public static bool In(this int i, params int[] options)
{ {
foreach (int j in options) return options.Any(j => i == j);
{
if (i == j) return true;
}
return false;
} }
public static bool In(this int i, IEnumerable<int> options) public static bool In(this int i, IEnumerable<int> options)
{ {
foreach (int j in options) return options.Any(j => i == j);
{
if (i == j) return true;
}
return false;
} }
public static bool ContainsStartsWith(this IEnumerable<string> options, string str) public static bool ContainsStartsWith(this IEnumerable<string> options, string str)
{ {
foreach (string opt in options) return options.Any(opt => opt.StartsWith(str));
{
if (opt.StartsWith(str)) return true;
}
return false;
} }
public static string GetOptionValue(this IEnumerable<string> options, string str) public static string GetOptionValue(this IEnumerable<string> options, string str)
@ -278,11 +247,7 @@ namespace BizHawk.Common
public static bool IsValidRomExtentsion(this string str, params string[] romExtensions) public static bool IsValidRomExtentsion(this string str, params string[] romExtensions)
{ {
string strUpper = str.ToUpper(); string strUpper = str.ToUpper();
foreach (string ext in romExtensions) return romExtensions.Any(ext => strUpper.EndsWith(ext.ToUpper()));
{
if (strUpper.EndsWith(ext.ToUpper())) return true;
}
return false;
} }
public static string ToCommaSeparated(this List<string> list) public static string ToCommaSeparated(this List<string> list)
@ -298,9 +263,9 @@ namespace BizHawk.Common
public static void SaveAsHex(this byte[] buffer, TextWriter writer) public static void SaveAsHex(this byte[] buffer, TextWriter writer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (byte b in buffer)
{ {
writer.Write("{0:X2}", buffer[i]); writer.Write("{0:X2}", b);
} }
writer.WriteLine(); writer.WriteLine();
} }
@ -332,69 +297,79 @@ namespace BizHawk.Common
public static void SaveAsHex(this short[] buffer, TextWriter writer) public static void SaveAsHex(this short[] buffer, TextWriter writer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (short b in buffer)
{ {
writer.Write("{0:X4}", buffer[i]); writer.Write("{0:X4}", b);
} }
writer.WriteLine(); writer.WriteLine();
} }
public static void SaveAsHex(this ushort[] buffer, TextWriter writer) public static void SaveAsHex(this ushort[] buffer, TextWriter writer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (ushort b in buffer)
{ {
writer.Write("{0:X4}", buffer[i]); writer.Write("{0:X4}", b);
} }
writer.WriteLine(); writer.WriteLine();
} }
public static void SaveAsHex(this int[] buffer, TextWriter writer) public static void SaveAsHex(this int[] buffer, TextWriter writer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (int b in buffer)
{ {
writer.Write("{0:X8}", buffer[i]); writer.Write("{0:X8}", b);
} }
writer.WriteLine(); writer.WriteLine();
} }
public static void SaveAsHex(this uint[] buffer, TextWriter writer) public static void SaveAsHex(this uint[] buffer, TextWriter writer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (uint b in buffer)
{ {
writer.Write("{0:X8}", buffer[i]); writer.Write("{0:X8}", b);
} }
writer.WriteLine(); writer.WriteLine();
} }
public static void Write(this BinaryWriter bw, int[] buffer) public static void Write(this BinaryWriter bw, int[] buffer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (int b in buffer)
bw.Write(buffer[i]); {
bw.Write(b);
}
} }
public static void Write(this BinaryWriter bw, uint[] buffer) public static void Write(this BinaryWriter bw, uint[] buffer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (uint b in buffer)
bw.Write(buffer[i]); {
bw.Write(b);
}
} }
public static void Write(this BinaryWriter bw, short[] buffer) public static void Write(this BinaryWriter bw, short[] buffer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (short b in buffer)
bw.Write(buffer[i]); {
bw.Write(b);
}
} }
public static void Write(this BinaryWriter bw, ushort[] buffer) public static void Write(this BinaryWriter bw, ushort[] buffer)
{ {
for (int i = 0; i < buffer.Length; i++) foreach (ushort t in buffer)
bw.Write(buffer[i]); {
bw.Write(t);
}
} }
public static int[] ReadInt32s(this BinaryReader br, int num) public static int[] ReadInt32s(this BinaryReader br, int num)
{ {
int[] ret = new int[num]; int[] ret = new int[num];
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++)
{
ret[i] = br.ReadInt32(); ret[i] = br.ReadInt32();
}
return ret; return ret;
} }
@ -402,7 +377,9 @@ namespace BizHawk.Common
{ {
short[] ret = new short[num]; short[] ret = new short[num];
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++)
{
ret[i] = br.ReadInt16(); ret[i] = br.ReadInt16();
}
return ret; return ret;
} }
@ -410,14 +387,19 @@ namespace BizHawk.Common
{ {
ushort[] ret = new ushort[num]; ushort[] ret = new ushort[num];
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++)
{
ret[i] = br.ReadUInt16(); ret[i] = br.ReadUInt16();
}
return ret; return ret;
} }
public static void ReadFromHex(this byte[] buffer, string hex) public static void ReadFromHex(this byte[] buffer, string hex)
{ {
if (hex.Length % 2 != 0) if (hex.Length%2 != 0)
{
throw new Exception("Hex value string does not appear to be properly formatted."); throw new Exception("Hex value string does not appear to be properly formatted.");
}
for (int i = 0; i < buffer.Length && i * 2 < hex.Length; i++) for (int i = 0; i < buffer.Length && i * 2 < hex.Length; i++)
{ {
string bytehex = "" + hex[i * 2] + hex[i * 2 + 1]; string bytehex = "" + hex[i * 2] + hex[i * 2 + 1];
@ -428,29 +410,25 @@ namespace BizHawk.Common
private static int Hex2Int(char c) private static int Hex2Int(char c)
{ {
if (c <= '9') if (c <= '9')
{
return c - '0'; return c - '0';
}
else if (c <= 'F') else if (c <= 'F')
{
return c - '7'; return c - '7';
}
else else
{
return c - 'W'; return c - 'W';
}
} }
public static void ReadFromHexFast(this byte[] buffer, string hex) public static void ReadFromHexFast(this byte[] buffer, string hex)
{ {
//if (hex.Length % 2 != 0)
// throw new Exception("Hex value string does not appear to be properly formatted.");
for (int i = 0; i < buffer.Length && i * 2 < hex.Length; i++) for (int i = 0; i < buffer.Length && i * 2 < hex.Length; i++)
{ {
buffer[i] = (byte)(Hex2Int(hex[i * 2]) * 16 + Hex2Int(hex[i * 2 + 1])); buffer[i] = (byte)(Hex2Int(hex[i * 2]) * 16 + Hex2Int(hex[i * 2 + 1]));
} }
/*
var b = new byte[buffer.Length];
b.ReadFromHex(hex);
for (int i = 0; i < buffer.Length; i++)
{
if (b[i] != buffer[i])
throw new Exception();
}*/
} }
public static void ReadFromHex(this short[] buffer, string hex) public static void ReadFromHex(this short[] buffer, string hex)
@ -487,12 +465,6 @@ namespace BizHawk.Common
} }
} }
//public static void SaveAsHex(this uint[] buffer, BinaryWriter bw)
//{
// for (int i = 0; i < buffer.Length; i++)
// bw.Write(buffer[i]);
//}
//these don't work??? they dont get chosen by compiler //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 void WriteBit(this BinaryWriter bw, Bit bit) { bw.Write((bool)bit); }
public static Bit ReadBit(this BinaryReader br) { return br.ReadBoolean(); } public static Bit ReadBit(this BinaryReader br) { return br.ReadBoolean(); }

View File

@ -1,4 +1,3 @@
using System;
using System.Diagnostics; using System.Diagnostics;
namespace BizHawk.Common namespace BizHawk.Common
@ -6,8 +5,13 @@ namespace BizHawk.Common
//I think this is a little faster with uint than with byte //I think this is a little faster with uint than with byte
public struct Bit public struct Bit
{ {
Bit(uint val) { this.val = val; } readonly uint val;
uint val;
Bit(uint val)
{
this.val = val;
}
public static implicit operator Bit(int rhs) { Debug.Assert((rhs & ~1) == 0); return new Bit((uint)(rhs)); } public static implicit operator Bit(int rhs) { Debug.Assert((rhs & ~1) == 0); return new Bit((uint)(rhs)); }
public static implicit operator Bit(uint rhs) { Debug.Assert((rhs & ~1) == 0); return new Bit((uint)(rhs)); } public static implicit operator Bit(uint rhs) { Debug.Assert((rhs & ~1) == 0); return new Bit((uint)(rhs)); }
public static implicit operator Bit(byte rhs) { Debug.Assert((rhs & ~1) == 0); return new Bit((uint)(rhs)); } public static implicit operator Bit(byte rhs) { Debug.Assert((rhs & ~1) == 0); return new Bit((uint)(rhs)); }
@ -17,10 +21,7 @@ namespace BizHawk.Common
public static implicit operator uint(Bit rhs) { return (uint)rhs.val; } public static implicit operator uint(Bit rhs) { return (uint)rhs.val; }
public static implicit operator byte(Bit rhs) { return (byte)rhs.val; } public static implicit operator byte(Bit rhs) { return (byte)rhs.val; }
public static implicit operator bool(Bit rhs) { return rhs.val != 0; } public static implicit operator bool(Bit rhs) { return rhs.val != 0; }
public override string ToString() public override string ToString() {return val.ToString(); }
{
return val.ToString();
}
public static bool operator ==(Bit lhs, Bit rhs) { return lhs.val == rhs.val; } public static bool operator ==(Bit lhs, Bit rhs) { return lhs.val == rhs.val; }
public static bool operator !=(Bit lhs, Bit rhs) { return lhs.val != rhs.val; } public static bool operator !=(Bit lhs, Bit rhs) { return lhs.val != rhs.val; }
public override int GetHashCode() { return val.GetHashCode(); } public override int GetHashCode() { return val.GetHashCode(); }

View File

@ -1,10 +1,11 @@
using System; using System;
using System.Linq;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Runtime.Serialization;
namespace BizHawk.Common namespace BizHawk.Common
{ {
@ -42,7 +43,7 @@ namespace BizHawk.Common
using (var md5 = System.Security.Cryptography.MD5.Create()) using (var md5 = System.Security.Cryptography.MD5.Create())
{ {
md5.TransformFinalBlock(data, offset, len); md5.TransformFinalBlock(data, offset, len);
return Util.BytesToHexString(md5.Hash); return BytesToHexString(md5.Hash);
} }
} }
@ -51,7 +52,7 @@ namespace BizHawk.Common
using (var sha1 = System.Security.Cryptography.SHA1.Create()) using (var sha1 = System.Security.Cryptography.SHA1.Create())
{ {
sha1.TransformFinalBlock(data, offset, len); sha1.TransformFinalBlock(data, offset, len);
return Util.BytesToHexString(sha1.Hash); return BytesToHexString(sha1.Hash);
} }
} }
@ -76,16 +77,16 @@ namespace BizHawk.Common
byte[] read = new byte[bytes]; byte[] read = new byte[bytes];
for (int b = 0; b < bytes; b++) for (int b = 0; b < bytes; b++)
read[b] = r.ReadByte(); read[b] = r.ReadByte();
return System.Text.Encoding.UTF8.GetString(read); return Encoding.UTF8.GetString(read);
} }
public static string ReadStringAsciiZ(this BinaryReader r) public static string ReadStringAsciiZ(this BinaryReader r)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for(;;) for (; ; )
{ {
int b = r.ReadByte(); int b = r.ReadByte();
if(b <= 0) break; if (b <= 0) break;
sb.Append((char)b); sb.Append((char)b);
} }
return sb.ToString(); return sb.ToString();
@ -285,19 +286,19 @@ namespace BizHawk.Common
return outStream.ToArray(); return outStream.ToArray();
} }
public static byte BinToBCD(this byte v) public static byte BinToBCD(this byte v)
{ {
return (byte) (((v / 10) * 16) + (v % 10)); return (byte)(((v / 10) * 16) + (v % 10));
} }
public static byte BCDtoBin(this byte v) public static byte BCDtoBin(this byte v)
{ {
return (byte) (((v / 16) * 10) + (v % 16)); return (byte)(((v / 16) * 10) + (v % 16));
} }
public static string FormatFileSize(long filesize) public static string FormatFileSize(long filesize)
{ {
Decimal size = (Decimal)filesize; Decimal size = filesize;
Decimal OneKiloByte = 1024M; Decimal OneKiloByte = 1024M;
Decimal OneMegaByte = OneKiloByte * 1024M; Decimal OneMegaByte = OneKiloByte * 1024M;
@ -351,9 +352,10 @@ namespace BizHawk.Common
public void StartWrite(BinaryWriter _bw) { bw = _bw; isReader = false; } public void StartWrite(BinaryWriter _bw) { bw = _bw; isReader = false; }
public void StartRead(BinaryReader _br) { br = _br; isReader = true; } public void StartRead(BinaryReader _br) { br = _br; isReader = true; }
public void StartWrite(TextWriter _tw) { tw = _tw; isReader = false; isText = true; } public void StartWrite(TextWriter _tw) { tw = _tw; isReader = false; isText = true; }
public void StartRead(TextReader _tr) { public void StartRead(TextReader _tr)
{
tr = _tr; tr = _tr;
isReader = true; isReader = true;
isText = true; isText = true;
BeginTextBlock(); BeginTextBlock();
} }
@ -368,12 +370,12 @@ namespace BizHawk.Common
class Section : Dictionary<string, Section> class Section : Dictionary<string, Section>
{ {
public string Name; public string Name = String.Empty;
public readonly Dictionary<string, string> Items = new Dictionary<string, string>(); public readonly Dictionary<string, string> Items = new Dictionary<string, string>();
} }
Section ReaderSection, CurrSection; private Section ReaderSection, CurrSection;
Stack<Section> SectionStack = new Stack<Section>(); private readonly Stack<Section> SectionStack = new Stack<Section>();
void BeginTextBlock() void BeginTextBlock()
{ {
@ -381,13 +383,12 @@ namespace BizHawk.Common
if (IsWriter) return; if (IsWriter) return;
ReaderSection = new Section(); ReaderSection = new Section();
ReaderSection.Name = "";
Stack<Section> ss = new Stack<Section>(); Stack<Section> ss = new Stack<Section>();
ss.Push(ReaderSection); ss.Push(ReaderSection);
Section curs = ReaderSection; Section curs = ReaderSection;
var rxEnd = new System.Text.RegularExpressions.Regex(@"\[/(.*?)\]",System.Text.RegularExpressions.RegexOptions.Compiled); var rxEnd = new System.Text.RegularExpressions.Regex(@"\[/(.*?)\]", System.Text.RegularExpressions.RegexOptions.Compiled);
var rxBegin = new System.Text.RegularExpressions.Regex(@"\[(.*?)\]",System.Text.RegularExpressions.RegexOptions.Compiled); var rxBegin = new System.Text.RegularExpressions.Regex(@"\[(.*?)\]", System.Text.RegularExpressions.RegexOptions.Compiled);
//read the entire file into a data structure for flexi-parsing //read the entire file into a data structure for flexi-parsing
string str; string str;
@ -411,12 +412,15 @@ namespace BizHawk.Common
{ {
string name = begin.Groups[1].Value; string name = begin.Groups[1].Value;
ss.Push(curs); ss.Push(curs);
var news = new Section(); var news = new Section {Name = name};
news.Name = name;
if (!curs.ContainsKey(name)) if (!curs.ContainsKey(name))
{
curs[name] = news; curs[name] = news;
}
else else
{
throw new Exception(string.Format("Duplicate key \"{0}\" in serializer savestate!", name)); throw new Exception(string.Format("Duplicate key \"{0}\" in serializer savestate!", name));
}
curs = news; curs = news;
} }
else else
@ -439,9 +443,9 @@ namespace BizHawk.Common
public void BeginSection(string name) public void BeginSection(string name)
{ {
sections.Push(name); sections.Push(name);
if (IsText) if (IsText)
if (IsWriter) { tw.WriteLine("[{0}]", name); } if (IsWriter) { tw.WriteLine("[{0}]", name); }
else else
{ {
SectionStack.Push(CurrSection); SectionStack.Push(CurrSection);
CurrSection = CurrSection[name]; CurrSection = CurrSection[name];
@ -452,11 +456,16 @@ namespace BizHawk.Common
{ {
string name = sections.Pop(); string name = sections.Pop();
if (IsText) if (IsText)
if (IsWriter) tw.WriteLine("[/{0}]", name); {
if (IsWriter)
{
tw.WriteLine("[/{0}]", name);
}
else else
{ {
CurrSection = SectionStack.Pop(); CurrSection = SectionStack.Pop();
} }
}
} }
string Item(string key) string Item(string key)
@ -471,11 +480,22 @@ namespace BizHawk.Common
public void SyncEnum<T>(string name, ref T val) where T : struct public void SyncEnum<T>(string name, ref T val) where T : struct
{ {
if (typeof(T).BaseType != typeof(System.Enum)) if (typeof (T).BaseType != typeof (Enum))
{
throw new InvalidOperationException(); throw new InvalidOperationException();
if (isText) SyncEnumText<T>(name, ref val); }
else if (IsReader) val = (T)Enum.ToObject(typeof(T), br.ReadInt32()); else if (isText)
else bw.Write(Convert.ToInt32(val)); {
SyncEnumText(name, ref val);
}
else if (IsReader)
{
val = (T) Enum.ToObject(typeof (T), br.ReadInt32());
}
else
{
bw.Write(Convert.ToInt32(val));
}
} }
public void SyncEnumText<T>(string name, ref T val) where T : struct public void SyncEnumText<T>(string name, ref T val) where T : struct
@ -522,13 +542,12 @@ namespace BizHawk.Common
{ {
if (IsReader) if (IsReader)
{ {
if(Present(name)) val = Util.HexStringToBytes(Item(name)); if (Present(name)) val = Util.HexStringToBytes(Item(name));
if (val != null && val.Length == 0 && use_null) val = null; if (val != null && val.Length == 0 && use_null) val = null;
} }
else else
{ {
byte[] temp = val; byte[] temp = val ?? new byte[0];
if (temp == null) temp = new byte[0];
tw.WriteLine("{0} {1}", name, Util.BytesToHexString(temp)); tw.WriteLine("{0} {1}", name, Util.BytesToHexString(temp));
} }
} }
@ -556,8 +575,7 @@ namespace BizHawk.Common
} }
else else
{ {
short[] temp = val; short[] temp = val ?? new short[0];
if (temp == null) temp = new short[0];
tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.ShortBufferToByteBuffer(temp))); tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.ShortBufferToByteBuffer(temp)));
} }
} }
@ -585,8 +603,7 @@ namespace BizHawk.Common
} }
else else
{ {
int[] temp = val; int[] temp = val ?? new int[0];
if (temp == null) temp = new int[0];
tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.IntBufferToByteBuffer(temp))); tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.IntBufferToByteBuffer(temp)));
} }
} }
@ -605,7 +622,7 @@ namespace BizHawk.Common
{ {
if (IsReader) if (IsReader)
{ {
if(Present(name)) if (Present(name))
{ {
byte[] bytes = Util.HexStringToBytes(Item(name)); byte[] bytes = Util.HexStringToBytes(Item(name));
val = Util.ByteBufferToUintBuffer(bytes); val = Util.ByteBufferToUintBuffer(bytes);
@ -614,8 +631,7 @@ namespace BizHawk.Common
} }
else else
{ {
uint[] temp = val; uint[] temp = val ?? new uint[0];
if (temp == null) temp = new uint[0];
tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.UintBufferToByteBuffer(temp))); tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.UintBufferToByteBuffer(temp)));
} }
} }
@ -626,88 +642,104 @@ namespace BizHawk.Common
else if (IsReader) Read(ref val); else if (IsReader) Read(ref val);
else Write(ref val); else Write(ref val);
} }
public void SyncText(string name, ref Bit val) public void SyncText(string name, ref Bit val)
{ {
if (IsReader) ReadText(name, ref val); if (IsReader) ReadText(name, ref val);
else WriteText(name, ref val); else WriteText(name, ref val);
} }
public void Sync(string name, ref byte val) public void Sync(string name, ref byte val)
{ {
if (IsText) SyncText(name, ref val); if (IsText) SyncText(name, ref val);
else if (IsReader) Read(ref val); else if (IsReader) Read(ref val);
else Write(ref val); else Write(ref val);
} }
void SyncText(string name, ref byte val)
private void SyncText(string name, ref byte val)
{ {
if (IsReader) ReadText(name, ref val); if (IsReader) ReadText(name, ref val);
else WriteText(name, ref val); else WriteText(name, ref val);
} }
public void Sync(string name, ref ushort val) public void Sync(string name, ref ushort val)
{ {
if (IsText) SyncText(name, ref val); if (IsText) SyncText(name, ref val);
else if (IsReader) Read(ref val); else if (IsReader) Read(ref val);
else Write(ref val); else Write(ref val);
} }
void SyncText(string name, ref ushort val)
private void SyncText(string name, ref ushort val)
{ {
if (IsReader) ReadText(name, ref val); if (IsReader) ReadText(name, ref val);
else WriteText(name, ref val); else WriteText(name, ref val);
} }
public void Sync(string name, ref uint val) public void Sync(string name, ref uint val)
{ {
if (IsText) SyncText(name, ref val); if (IsText) SyncText(name, ref val);
else if (IsReader) Read(ref val); else if (IsReader) Read(ref val);
else Write(ref val); else Write(ref val);
} }
void SyncText(string name, ref uint val)
private void SyncText(string name, ref uint val)
{ {
if (IsReader) ReadText(name, ref val); if (IsReader) ReadText(name, ref val);
else WriteText(name, ref val); else WriteText(name, ref val);
} }
public void Sync(string name, ref sbyte val) public void Sync(string name, ref sbyte val)
{ {
if (IsText) SyncText(name, ref val); if (IsText) SyncText(name, ref val);
else if (IsReader) Read(ref val); else if (IsReader) Read(ref val);
else Write(ref val); else Write(ref val);
} }
void SyncText(string name, ref sbyte val)
private void SyncText(string name, ref sbyte val)
{ {
if (IsReader) ReadText(name, ref val); if (IsReader) ReadText(name, ref val);
else WriteText(name, ref val); else WriteText(name, ref val);
} }
public void Sync(string name, ref short val) public void Sync(string name, ref short val)
{ {
if (IsText) SyncText(name, ref val); if (IsText) SyncText(name, ref val);
else if (IsReader) Read(ref val); else if (IsReader) Read(ref val);
else Write(ref val); else Write(ref val);
} }
void SyncText(string name, ref short val)
private void SyncText(string name, ref short val)
{ {
if (IsReader) ReadText(name, ref val); if (IsReader) ReadText(name, ref val);
else WriteText(name, ref val); else WriteText(name, ref val);
} }
public void Sync(string name, ref int val) public void Sync(string name, ref int val)
{ {
if (IsText) SyncText(name, ref val); if (IsText) SyncText(name, ref val);
else if (IsReader) Read(ref val); else if (IsReader) Read(ref val);
else Write(ref val); else Write(ref val);
} }
void SyncText(string name, ref int val)
private void SyncText(string name, ref int val)
{ {
if (IsReader) ReadText(name, ref val); if (IsReader) ReadText(name, ref val);
else WriteText(name, ref val); else WriteText(name, ref val);
} }
public void Sync(string name, ref bool val) public void Sync(string name, ref bool val)
{ {
if (IsText) SyncText(name, ref val); if (IsText) SyncText(name, ref val);
else if (IsReader) Read(ref val); else if (IsReader) Read(ref val);
else Write(ref val); else Write(ref val);
} }
void SyncText(string name, ref bool val)
private void SyncText(string name, ref bool val)
{ {
if (IsReader) ReadText(name, ref val); if (IsReader) ReadText(name, ref val);
else WriteText(name, ref val); else WriteText(name, ref val);
} }
public void SyncFixedString(string name, ref string val, int length) public void SyncFixedString(string name, ref string val, int length)
{ {
//TODO - this could be made more efficient perhaps just by writing values right out of the string.. //TODO - this could be made more efficient perhaps just by writing values right out of the string..
@ -748,59 +780,59 @@ namespace BizHawk.Common
} }
} }
void Read(ref Bit val) { val = br.ReadBit(); } private void Read(ref Bit val) { val = br.ReadBit(); }
void Write(ref Bit val) { bw.WriteBit(val); } private void Write(ref Bit val) { bw.WriteBit(val); }
void ReadText(string name, ref Bit val) { if(Present(name)) val = (Bit)int.Parse(Item(name)); } private void ReadText(string name, ref Bit val) { if (Present(name)) val = (Bit)int.Parse(Item(name)); }
void WriteText(string name, ref Bit val) { tw.WriteLine("{0} {1}", name, (int)val); } private void WriteText(string name, ref Bit val) { tw.WriteLine("{0} {1}", name, (int)val); }
void Read(ref byte val) { val = br.ReadByte(); } private void Read(ref byte val) { val = br.ReadByte(); }
void Write(ref byte val) { bw.Write(val); } private void Write(ref byte val) { bw.Write(val); }
void ReadText(string name, ref byte val) { if (Present(name)) val = byte.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } private void ReadText(string name, ref byte val) { if (Present(name)) val = byte.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); }
void WriteText(string name, ref byte val) { tw.WriteLine("{0} 0x{1:X2}", name, val); } private void WriteText(string name, ref byte val) { tw.WriteLine("{0} 0x{1:X2}", name, val); }
void Read(ref ushort val) { val = br.ReadUInt16(); } private void Read(ref ushort val) { val = br.ReadUInt16(); }
void Write(ref ushort val) { bw.Write(val); } private void Write(ref ushort val) { bw.Write(val); }
void ReadText(string name, ref ushort val) { if (Present(name)) val = ushort.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } private void ReadText(string name, ref ushort val) { if (Present(name)) val = ushort.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); }
void WriteText(string name, ref ushort val) { tw.WriteLine("{0} 0x{1:X4}", name, val); } private void WriteText(string name, ref ushort val) { tw.WriteLine("{0} 0x{1:X4}", name, val); }
void Read(ref uint val) { val = br.ReadUInt32(); } private void Read(ref uint val) { val = br.ReadUInt32(); }
void Write(ref uint val) { bw.Write(val); } private void Write(ref uint val) { bw.Write(val); }
void ReadText(string name, ref uint val) { if (Present(name)) val = uint.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } private void ReadText(string name, ref uint val) { if (Present(name)) val = uint.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); }
void WriteText(string name, ref uint val) { tw.WriteLine("{0} 0x{1:X8}", name, val); } private void WriteText(string name, ref uint val) { tw.WriteLine("{0} 0x{1:X8}", name, val); }
void Read(ref sbyte val) { val = br.ReadSByte(); } private void Read(ref sbyte val) { val = br.ReadSByte(); }
void Write(ref sbyte val) { bw.Write(val); } private void Write(ref sbyte val) { bw.Write(val); }
void ReadText(string name, ref sbyte val) { if (Present(name)) val = sbyte.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } private void ReadText(string name, ref sbyte val) { if (Present(name)) val = sbyte.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); }
void WriteText(string name, ref sbyte val) { tw.WriteLine("{0} 0x{1:X2}", name, val); } private void WriteText(string name, ref sbyte val) { tw.WriteLine("{0} 0x{1:X2}", name, val); }
void Read(ref short val) { val = br.ReadInt16(); } private void Read(ref short val) { val = br.ReadInt16(); }
void Write(ref short val) { bw.Write(val); } private void Write(ref short val) { bw.Write(val); }
void ReadText(string name, ref short val) { if (Present(name)) val = short.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } private void ReadText(string name, ref short val) { if (Present(name)) val = short.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); }
void WriteText(string name, ref short val) { tw.WriteLine("{0} 0x{1:X4}", name, val); } private void WriteText(string name, ref short val) { tw.WriteLine("{0} 0x{1:X4}", name, val); }
void Read(ref int val) { val = br.ReadInt32(); } private void Read(ref int val) { val = br.ReadInt32(); }
void Write(ref int val) { bw.Write(val); } private void Write(ref int val) { bw.Write(val); }
void ReadText(string name, ref int val) { if (Present(name)) val = int.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } private void ReadText(string name, ref int val) { if (Present(name)) val = int.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); }
void WriteText(string name, ref int val) { tw.WriteLine("{0} 0x{1:X8}", name, val); } private void WriteText(string name, ref int val) { tw.WriteLine("{0} 0x{1:X8}", name, val); }
void Read(ref bool val) { val = br.ReadBoolean(); } private void Read(ref bool val) { val = br.ReadBoolean(); }
void Write(ref bool val) { bw.Write(val); } private void Write(ref bool val) { bw.Write(val); }
void ReadText(string name, ref bool val) { if (Present(name)) val = bool.Parse(Item(name)); } private void ReadText(string name, ref bool val) { if (Present(name)) val = bool.Parse(Item(name)); }
void WriteText(string name, ref bool val) { tw.WriteLine("{0} {1}", name, val); } private void WriteText(string name, ref bool val) { tw.WriteLine("{0} {1}", name, val); }
} }
public static class BITREV public static class Bitrev
{ {
public static byte[] byte_8; public static byte[] byte_8;
static BITREV() static Bitrev()
{ {
make_byte_8(); make_byte_8();
} }
static void make_byte_8() static void make_byte_8()
{ {
int bits = 8; int bits = 8;
int n = 1 << 8; const int n = 1 << 8;
byte_8 = new byte[n]; byte_8 = new byte[n];
int m = 1; int m = 1;
@ -845,6 +877,7 @@ namespace BizHawk.Common
/// </summary> /// </summary>
/// <typeparam name="K">dictionary keys</typeparam> /// <typeparam name="K">dictionary keys</typeparam>
/// <typeparam name="V">dictionary values</typeparam> /// <typeparam name="V">dictionary values</typeparam>
[Serializable]
public class WorkingDictionary<K, V> : Dictionary<K, V> where V : new() public class WorkingDictionary<K, V> : Dictionary<K, V> where V : new()
{ {
public new V this[K key] public new V this[K key]
@ -856,8 +889,15 @@ namespace BizHawk.Common
temp = this[key] = new V(); temp = this[key] = new V();
return temp; return temp;
} }
set { base[key] = value; } set
{
base[key] = value;
}
} }
public WorkingDictionary() { }
protected WorkingDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { }
} }
/// <summary> /// <summary>
@ -872,7 +912,7 @@ namespace BizHawk.Common
where D : IDictionary<K, L>, new() where D : IDictionary<K, L>, new()
where L : IList<V>, IEnumerable<V>, new() where L : IList<V>, IEnumerable<V>, new()
{ {
D dictionary = new D(); readonly D dictionary = new D();
public void Add(K key, V val) public void Add(K key, V val)
{ {
this[key].Add(val); this[key].Add(val);
@ -883,9 +923,7 @@ namespace BizHawk.Common
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public IEnumerator<V> GetEnumerator() public IEnumerator<V> GetEnumerator()
{ {
foreach (L lv in dictionary.Values) return dictionary.Values.SelectMany(lv => lv).GetEnumerator();
foreach (V v in lv)
yield return v;
} }
public IEnumerable KeyValuePairEnumerator { get { return dictionary; } } public IEnumerable KeyValuePairEnumerator { get { return dictionary; } }
@ -911,7 +949,8 @@ namespace BizHawk.Common
} }
} }
public class NotTestedException : Exception [Serializable]
{ public class NotTestedException : Exception
} {
}
} }

View File

@ -69,7 +69,7 @@ namespace BizHawk.Emulation.DiscSystem
uint edc_poly = (uint)0x8001801B; uint edc_poly = (uint)0x8001801B;
//generate the CRC table //generate the CRC table
uint reverse_edc_poly = BITREV.reverse_32(edc_poly); uint reverse_edc_poly = Bitrev.reverse_32(edc_poly);
for (uint i = 0; i < 256; ++i) for (uint i = 0; i < 256; ++i)
{ {
uint crc = i; uint crc = i;

View File

@ -411,8 +411,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
//hflip //hflip
if ((oam->oam[2] & 0x40) == 0) if ((oam->oam[2] & 0x40) == 0)
{ {
oam->patterns[0] = BITREV.byte_8[oam->patterns[0]]; oam->patterns[0] = Bitrev.byte_8[oam->patterns[0]];
oam->patterns[1] = BITREV.byte_8[oam->patterns[1]]; oam->patterns[1] = Bitrev.byte_8[oam->patterns[1]];
} }
} }
} //sprite pattern fetch loop } //sprite pattern fetch loop