From 030f30628d12028a6c606cdea1f0e36814bb30d0 Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 15 Nov 2013 00:22:08 +0000 Subject: [PATCH] some random code cleanup on BizHawk.Common --- BizHawk.Common/Buffer.cs | 25 +- BizHawk.Common/Extensions.cs | 166 ++++++------- BizHawk.Common/Types.cs | 15 +- BizHawk.Common/Util.cs | 223 ++++++++++-------- BizHawk.Emulation.DiscSystem/ECM.cs | 2 +- .../Consoles/Nintendo/NES/PPU.run.cs | 4 +- 6 files changed, 229 insertions(+), 206 deletions(-) diff --git a/BizHawk.Common/Buffer.cs b/BizHawk.Common/Buffer.cs index 2387f36c49..6103add9eb 100644 --- a/BizHawk.Common/Buffer.cs +++ b/BizHawk.Common/Buffer.cs @@ -59,15 +59,26 @@ namespace BizHawk.Common public void Dispose() { - if (arr != null) - hnd.Free(); - arr = null; + Dispose(true); + GC.SuppressFinalize(this); } - ~CBuffer() { Dispose(); } + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + if (arr != null) + { + hnd.Free(); + } + arr = null; + } + } + + ~CBuffer() { Dispose(true); } } - public class ByteBuffer : CBuffer + public sealed class ByteBuffer : CBuffer { public ByteBuffer(int amt) : base(amt,1) { } public ByteBuffer(byte[] arr) : base(arr,1) { } @@ -83,7 +94,7 @@ namespace BizHawk.Common } } - public class IntBuffer : CBuffer + public sealed class IntBuffer : CBuffer { public IntBuffer(int amt) : base(amt, 4) { } public IntBuffer(int[] arr) : base(arr,4) { } @@ -99,7 +110,7 @@ namespace BizHawk.Common } } - public class ShortBuffer : CBuffer + public sealed class ShortBuffer : CBuffer { public ShortBuffer(int amt) : base(amt, 2) { } public ShortBuffer(short[] arr) : base(arr, 2) { } diff --git a/BizHawk.Common/Extensions.cs b/BizHawk.Common/Extensions.cs index 1380e563db..efa2a68982 100644 --- a/BizHawk.Common/Extensions.cs +++ b/BizHawk.Common/Extensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; using System.Collections.Generic; using System.Globalization; @@ -21,7 +22,7 @@ namespace BizHawk.Common { int min = 0; int max = list.Count; - int mid = 0; + int mid; TKey midKey; while (min < max) { @@ -65,22 +66,22 @@ namespace BizHawk.Common 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) { - return string.Format("{0:X" + numdigits + "}", n); + return String.Format("{0:X" + numdigits + "}", n); } 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) { - 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 @@ -151,14 +152,7 @@ namespace BizHawk.Common public static bool IsBinary(this string str) { - for (int i = 0; i < str.Length; i++) - { - char c = str[i]; - if (c == '0' || c == '1') - continue; - return false; - } - return true; + return str.All(c => c == '0' || c == '1'); } 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) { int index = str.IndexOf(value); + if (index < 0) + { return null; - if (index == 0) - return ""; - return str.Substring(0, index); + } + else if (index == 0) + { + return String.Empty; + } + else + { + return str.Substring(0, index); + } } public static bool In(this string str, params string[] options) { - foreach (string opt in options) - { - if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true; - } - return false; + return options.Any(opt => opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)); } public static bool In(this string str, IEnumerable options) { - foreach (string opt in options) - { - if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true; - } - return false; + return options.Any(opt => opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)); } public static bool In(this string str, IEnumerable options, Func eval) { - foreach (T opt in options) - { - if (eval(opt, str)) - return true; - } - return false; + return options.Any(opt => eval(opt, str)); } public static bool NotIn(this string str, params string[] options) { - foreach (string opt in options) - { - if (opt.ToLower() == str.ToLower()) return false; - } - return true; + return options.All(opt => opt.ToLower() != str.ToLower()); } public static bool NotIn(this string str, IEnumerable options) { - foreach (string opt in options) - { - if (opt.ToLower() == str.ToLower()) return false; - } - return true; + return options.All(opt => opt.ToLower() != str.ToLower()); } public static bool In(this int i, params int[] options) { - foreach (int j in options) - { - if (i == j) return true; - } - return false; + return options.Any(j => i == j); } public static bool In(this int i, IEnumerable options) { - foreach (int j in options) - { - if (i == j) return true; - } - return false; + return options.Any(j => i == j); } public static bool ContainsStartsWith(this IEnumerable options, string str) { - foreach (string opt in options) - { - if (opt.StartsWith(str)) return true; - } - return false; + return options.Any(opt => opt.StartsWith(str)); } public static string GetOptionValue(this IEnumerable options, string str) @@ -278,11 +247,7 @@ namespace BizHawk.Common public static bool IsValidRomExtentsion(this string str, params string[] romExtensions) { string strUpper = str.ToUpper(); - foreach (string ext in romExtensions) - { - if (strUpper.EndsWith(ext.ToUpper())) return true; - } - return false; + return romExtensions.Any(ext => strUpper.EndsWith(ext.ToUpper())); } public static string ToCommaSeparated(this List list) @@ -298,9 +263,9 @@ namespace BizHawk.Common 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(); } @@ -332,69 +297,79 @@ namespace BizHawk.Common 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(); } 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(); } 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(); } 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(); } public static void Write(this BinaryWriter bw, int[] buffer) { - for (int i = 0; i < buffer.Length; i++) - bw.Write(buffer[i]); + foreach (int b in buffer) + { + bw.Write(b); + } } public static void Write(this BinaryWriter bw, uint[] buffer) { - for (int i = 0; i < buffer.Length; i++) - bw.Write(buffer[i]); + foreach (uint b in buffer) + { + bw.Write(b); + } } public static void Write(this BinaryWriter bw, short[] buffer) { - for (int i = 0; i < buffer.Length; i++) - bw.Write(buffer[i]); + foreach (short b in buffer) + { + bw.Write(b); + } } public static void Write(this BinaryWriter bw, ushort[] buffer) { - for (int i = 0; i < buffer.Length; i++) - bw.Write(buffer[i]); + 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; } @@ -402,7 +377,9 @@ namespace BizHawk.Common { short[] ret = new short[num]; for (int i = 0; i < num; i++) + { ret[i] = br.ReadInt16(); + } return ret; } @@ -410,14 +387,19 @@ namespace BizHawk.Common { 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) + 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++) { string bytehex = "" + hex[i * 2] + hex[i * 2 + 1]; @@ -428,29 +410,25 @@ namespace BizHawk.Common private static int Hex2Int(char c) { if (c <= '9') + { return c - '0'; + } else if (c <= 'F') + { return c - '7'; + } else + { return c - 'W'; + } } 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++) { 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) @@ -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 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/Types.cs b/BizHawk.Common/Types.cs index 805b5f3131..a1a0c7a48d 100644 --- a/BizHawk.Common/Types.cs +++ b/BizHawk.Common/Types.cs @@ -1,4 +1,3 @@ -using System; using System.Diagnostics; namespace BizHawk.Common @@ -6,8 +5,13 @@ namespace BizHawk.Common //I think this is a little faster with uint than with byte public struct Bit { - Bit(uint val) { this.val = val; } - uint val; + readonly 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(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)); } @@ -17,10 +21,7 @@ namespace BizHawk.Common 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 bool(Bit rhs) { return rhs.val != 0; } - public override string ToString() - { - return val.ToString(); - } + public override string 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 override int GetHashCode() { return val.GetHashCode(); } diff --git a/BizHawk.Common/Util.cs b/BizHawk.Common/Util.cs index 225962eb00..b5b6109cd9 100644 --- a/BizHawk.Common/Util.cs +++ b/BizHawk.Common/Util.cs @@ -1,10 +1,11 @@ using System; -using System.Linq; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Text; +using System.Runtime.Serialization; namespace BizHawk.Common { @@ -42,7 +43,7 @@ namespace BizHawk.Common using (var md5 = System.Security.Cryptography.MD5.Create()) { 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()) { 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]; for (int b = 0; b < bytes; b++) read[b] = r.ReadByte(); - return System.Text.Encoding.UTF8.GetString(read); + return Encoding.UTF8.GetString(read); } public static string ReadStringAsciiZ(this BinaryReader r) { StringBuilder sb = new StringBuilder(); - for(;;) + for (; ; ) { int b = r.ReadByte(); - if(b <= 0) break; + if (b <= 0) break; sb.Append((char)b); } return sb.ToString(); @@ -285,19 +286,19 @@ namespace BizHawk.Common return outStream.ToArray(); } - public static byte BinToBCD(this byte v) - { - return (byte) (((v / 10) * 16) + (v % 10)); - } + public static byte BinToBCD(this byte v) + { + return (byte)(((v / 10) * 16) + (v % 10)); + } - public static byte BCDtoBin(this byte v) - { - return (byte) (((v / 16) * 10) + (v % 16)); - } + public static byte BCDtoBin(this byte v) + { + return (byte)(((v / 16) * 10) + (v % 16)); + } public static string FormatFileSize(long filesize) { - Decimal size = (Decimal)filesize; + Decimal size = filesize; Decimal OneKiloByte = 1024M; Decimal OneMegaByte = OneKiloByte * 1024M; @@ -351,9 +352,10 @@ namespace BizHawk.Common public void StartWrite(BinaryWriter _bw) { bw = _bw; isReader = false; } public void StartRead(BinaryReader _br) { br = _br; isReader = true; } public void StartWrite(TextWriter _tw) { tw = _tw; isReader = false; isText = true; } - public void StartRead(TextReader _tr) { + public void StartRead(TextReader _tr) + { tr = _tr; - isReader = true; + isReader = true; isText = true; BeginTextBlock(); } @@ -368,12 +370,12 @@ namespace BizHawk.Common class Section : Dictionary { - public string Name; + public string Name = String.Empty; public readonly Dictionary Items = new Dictionary(); } - Section ReaderSection, CurrSection; - Stack
SectionStack = new Stack
(); + private Section ReaderSection, CurrSection; + private readonly Stack
SectionStack = new Stack
(); void BeginTextBlock() { @@ -381,13 +383,12 @@ namespace BizHawk.Common if (IsWriter) return; ReaderSection = new Section(); - ReaderSection.Name = ""; Stack
ss = new Stack
(); ss.Push(ReaderSection); Section curs = ReaderSection; - 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 rxEnd = 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 string str; @@ -411,12 +412,15 @@ namespace BizHawk.Common { string name = begin.Groups[1].Value; ss.Push(curs); - var news = new Section(); - news.Name = name; + var news = new Section {Name = name}; if (!curs.ContainsKey(name)) + { curs[name] = news; + } else + { throw new Exception(string.Format("Duplicate key \"{0}\" in serializer savestate!", name)); + } curs = news; } else @@ -439,9 +443,9 @@ namespace BizHawk.Common public void BeginSection(string name) { sections.Push(name); - if (IsText) + if (IsText) if (IsWriter) { tw.WriteLine("[{0}]", name); } - else + else { SectionStack.Push(CurrSection); CurrSection = CurrSection[name]; @@ -452,11 +456,16 @@ namespace BizHawk.Common { string name = sections.Pop(); if (IsText) - if (IsWriter) tw.WriteLine("[/{0}]", name); + { + if (IsWriter) + { + tw.WriteLine("[/{0}]", name); + } else { CurrSection = SectionStack.Pop(); } + } } string Item(string key) @@ -471,11 +480,22 @@ namespace BizHawk.Common public void SyncEnum(string name, ref T val) where T : struct { - if (typeof(T).BaseType != typeof(System.Enum)) + if (typeof (T).BaseType != typeof (Enum)) + { throw new InvalidOperationException(); - if (isText) SyncEnumText(name, ref val); - else if (IsReader) val = (T)Enum.ToObject(typeof(T), br.ReadInt32()); - else bw.Write(Convert.ToInt32(val)); + } + else if (isText) + { + SyncEnumText(name, ref val); + } + else if (IsReader) + { + val = (T) Enum.ToObject(typeof (T), br.ReadInt32()); + } + else + { + bw.Write(Convert.ToInt32(val)); + } } public void SyncEnumText(string name, ref T val) where T : struct @@ -522,13 +542,12 @@ namespace BizHawk.Common { 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; } else { - byte[] temp = val; - if (temp == null) temp = new byte[0]; + byte[] temp = val ?? new byte[0]; tw.WriteLine("{0} {1}", name, Util.BytesToHexString(temp)); } } @@ -556,8 +575,7 @@ namespace BizHawk.Common } else { - short[] temp = val; - if (temp == null) temp = new short[0]; + short[] temp = val ?? new short[0]; tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.ShortBufferToByteBuffer(temp))); } } @@ -585,8 +603,7 @@ namespace BizHawk.Common } else { - int[] temp = val; - if (temp == null) temp = new int[0]; + int[] temp = val ?? new int[0]; tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.IntBufferToByteBuffer(temp))); } } @@ -605,7 +622,7 @@ namespace BizHawk.Common { if (IsReader) { - if(Present(name)) + if (Present(name)) { byte[] bytes = Util.HexStringToBytes(Item(name)); val = Util.ByteBufferToUintBuffer(bytes); @@ -614,8 +631,7 @@ namespace BizHawk.Common } else { - uint[] temp = val; - if (temp == null) temp = new uint[0]; + uint[] temp = val ?? new uint[0]; tw.WriteLine("{0} {1}", name, Util.BytesToHexString(Util.UintBufferToByteBuffer(temp))); } } @@ -626,88 +642,104 @@ namespace BizHawk.Common else if (IsReader) Read(ref val); else Write(ref val); } + public void SyncText(string name, ref Bit val) { if (IsReader) ReadText(name, ref val); else WriteText(name, ref val); } + public void Sync(string name, ref byte val) { if (IsText) SyncText(name, ref val); else if (IsReader) Read(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); else WriteText(name, ref val); } + public void Sync(string name, ref ushort val) { if (IsText) SyncText(name, ref val); else if (IsReader) Read(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); else WriteText(name, ref val); } + public void Sync(string name, ref uint val) { if (IsText) SyncText(name, ref val); else if (IsReader) Read(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); else WriteText(name, ref val); } + public void Sync(string name, ref sbyte val) { if (IsText) SyncText(name, ref val); else if (IsReader) Read(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); else WriteText(name, ref val); } + public void Sync(string name, ref short val) { if (IsText) SyncText(name, ref val); else if (IsReader) Read(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); else WriteText(name, ref val); } + public void Sync(string name, ref int val) { if (IsText) SyncText(name, ref val); else if (IsReader) Read(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); else WriteText(name, ref val); } + public void Sync(string name, ref bool val) { if (IsText) SyncText(name, ref val); else if (IsReader) Read(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); else WriteText(name, ref val); } + 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.. @@ -748,59 +780,59 @@ namespace BizHawk.Common } } - void Read(ref Bit val) { val = br.ReadBit(); } - void Write(ref Bit val) { bw.WriteBit(val); } - 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 Read(ref Bit val) { val = br.ReadBit(); } + private void Write(ref Bit val) { bw.WriteBit(val); } + private void ReadText(string name, ref Bit val) { if (Present(name)) val = (Bit)int.Parse(Item(name)); } + private void WriteText(string name, ref Bit val) { tw.WriteLine("{0} {1}", name, (int)val); } - void Read(ref byte val) { val = br.ReadByte(); } - 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); } - void WriteText(string name, ref byte val) { tw.WriteLine("{0} 0x{1:X2}", name, val); } + private void Read(ref byte val) { val = br.ReadByte(); } + private void Write(ref byte val) { bw.Write(val); } + private void ReadText(string name, ref byte val) { if (Present(name)) val = byte.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } + private void WriteText(string name, ref byte val) { tw.WriteLine("{0} 0x{1:X2}", name, val); } - void Read(ref ushort val) { val = br.ReadUInt16(); } - 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); } - void WriteText(string name, ref ushort val) { tw.WriteLine("{0} 0x{1:X4}", name, val); } + private void Read(ref ushort val) { val = br.ReadUInt16(); } + private void Write(ref ushort val) { bw.Write(val); } + private void ReadText(string name, ref ushort val) { if (Present(name)) val = ushort.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } + private void WriteText(string name, ref ushort val) { tw.WriteLine("{0} 0x{1:X4}", name, val); } - void Read(ref uint val) { val = br.ReadUInt32(); } - 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); } - void WriteText(string name, ref uint val) { tw.WriteLine("{0} 0x{1:X8}", name, val); } + private void Read(ref uint val) { val = br.ReadUInt32(); } + private void Write(ref uint val) { bw.Write(val); } + private void ReadText(string name, ref uint val) { if (Present(name)) val = uint.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } + private void WriteText(string name, ref uint val) { tw.WriteLine("{0} 0x{1:X8}", name, val); } - void Read(ref sbyte val) { val = br.ReadSByte(); } - 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); } - void WriteText(string name, ref sbyte val) { tw.WriteLine("{0} 0x{1:X2}", name, val); } + private void Read(ref sbyte val) { val = br.ReadSByte(); } + private void Write(ref sbyte val) { bw.Write(val); } + private void ReadText(string name, ref sbyte val) { if (Present(name)) val = sbyte.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } + private void WriteText(string name, ref sbyte val) { tw.WriteLine("{0} 0x{1:X2}", name, val); } - void Read(ref short val) { val = br.ReadInt16(); } - 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); } - void WriteText(string name, ref short val) { tw.WriteLine("{0} 0x{1:X4}", name, val); } + private void Read(ref short val) { val = br.ReadInt16(); } + private void Write(ref short val) { bw.Write(val); } + private void ReadText(string name, ref short val) { if (Present(name)) val = short.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } + private void WriteText(string name, ref short val) { tw.WriteLine("{0} 0x{1:X4}", name, val); } - void Read(ref int val) { val = br.ReadInt32(); } - 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); } - void WriteText(string name, ref int val) { tw.WriteLine("{0} 0x{1:X8}", name, val); } + private void Read(ref int val) { val = br.ReadInt32(); } + private void Write(ref int val) { bw.Write(val); } + private void ReadText(string name, ref int val) { if (Present(name)) val = int.Parse(Item(name).Replace("0x", ""), NumberStyles.HexNumber); } + private void WriteText(string name, ref int val) { tw.WriteLine("{0} 0x{1:X8}", name, val); } - void Read(ref bool val) { val = br.ReadBoolean(); } - void Write(ref bool val) { bw.Write(val); } - 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 Read(ref bool val) { val = br.ReadBoolean(); } + private void Write(ref bool val) { bw.Write(val); } + private void ReadText(string name, ref bool val) { if (Present(name)) val = bool.Parse(Item(name)); } + 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; - static BITREV() + static Bitrev() { make_byte_8(); } static void make_byte_8() { int bits = 8; - int n = 1 << 8; + const int n = 1 << 8; byte_8 = new byte[n]; int m = 1; @@ -845,6 +877,7 @@ namespace BizHawk.Common /// /// dictionary keys /// dictionary values + [Serializable] public class WorkingDictionary : Dictionary where V : new() { public new V this[K key] @@ -856,8 +889,15 @@ namespace BizHawk.Common temp = this[key] = new V(); return temp; } - set { base[key] = value; } + set + { + base[key] = value; + } } + + public WorkingDictionary() { } + + protected WorkingDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { } } /// @@ -872,7 +912,7 @@ namespace BizHawk.Common where D : IDictionary, new() where L : IList, IEnumerable, new() { - D dictionary = new D(); + readonly D dictionary = new D(); public void Add(K key, V val) { this[key].Add(val); @@ -883,9 +923,7 @@ namespace BizHawk.Common IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public IEnumerator GetEnumerator() { - foreach (L lv in dictionary.Values) - foreach (V v in lv) - yield return v; + return dictionary.Values.SelectMany(lv => lv).GetEnumerator(); } public IEnumerable KeyValuePairEnumerator { get { return dictionary; } } @@ -911,7 +949,8 @@ namespace BizHawk.Common } } - public class NotTestedException : Exception - { - } + [Serializable] + public class NotTestedException : Exception + { + } } diff --git a/BizHawk.Emulation.DiscSystem/ECM.cs b/BizHawk.Emulation.DiscSystem/ECM.cs index b0753a5499..04534d6205 100644 --- a/BizHawk.Emulation.DiscSystem/ECM.cs +++ b/BizHawk.Emulation.DiscSystem/ECM.cs @@ -69,7 +69,7 @@ namespace BizHawk.Emulation.DiscSystem uint edc_poly = (uint)0x8001801B; //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) { uint crc = i; diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/PPU.run.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/PPU.run.cs index a9b5e87906..473f38efb9 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/PPU.run.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/PPU.run.cs @@ -411,8 +411,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES //hflip if ((oam->oam[2] & 0x40) == 0) { - oam->patterns[0] = BITREV.byte_8[oam->patterns[0]]; - oam->patterns[1] = BITREV.byte_8[oam->patterns[1]]; + oam->patterns[0] = Bitrev.byte_8[oam->patterns[0]]; + oam->patterns[1] = Bitrev.byte_8[oam->patterns[1]]; } } } //sprite pattern fetch loop