From f356d5b3549736b17e5f3e13618aac32d30014fb Mon Sep 17 00:00:00 2001 From: zeromus Date: Sun, 31 Jul 2011 19:46:42 +0000 Subject: [PATCH] add core scanning system --- .../Consoles/Nintendo/NES/Core.cs | 1 + BizHawk.Emulation/Util.cs | 536 ++++++++++-------- BizHawk.MultiClient/MainForm.cs | 6 + 3 files changed, 295 insertions(+), 248 deletions(-) diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs index 75c709ca63..df85cb3d6a 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/Core.cs @@ -8,6 +8,7 @@ using BizHawk.Emulation.CPUs.M6502; namespace BizHawk.Emulation.Consoles.Nintendo { + [CoreVersion("0.0.0.1",FriendlyName="NESHawk")] public partial class NES : IEmulator { //hardware/state diff --git a/BizHawk.Emulation/Util.cs b/BizHawk.Emulation/Util.cs index 53d4017b16..e9153ce155 100644 --- a/BizHawk.Emulation/Util.cs +++ b/BizHawk.Emulation/Util.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; using System.Diagnostics; using System.Collections; using System.Collections.Generic; @@ -8,41 +9,80 @@ using System.Text; namespace BizHawk { - public struct Tuple : IEquatable> - { - private readonly T1 first; - private readonly T2 second; - public T1 First { get { return first; } } - public T2 Second { get { return second; } } + public struct Tuple : IEquatable> + { + private readonly T1 first; + private readonly T2 second; + public T1 First { get { return first; } } + public T2 Second { get { return second; } } - public Tuple(T1 o1, T2 o2) - { - first = o1; - second = o2; - } + public Tuple(T1 o1, T2 o2) + { + first = o1; + second = o2; + } - public bool Equals(Tuple other) - { - return first.Equals(other.first) && - second.Equals(other.second); - } + public bool Equals(Tuple other) + { + return first.Equals(other.first) && + second.Equals(other.second); + } - public override bool Equals(object obj) - { - if (obj is Tuple) - return this.Equals((Tuple)obj); - else - return false; - } + public override bool Equals(object obj) + { + if (obj is Tuple) + return this.Equals((Tuple)obj); + else + return false; + } - public override int GetHashCode() - { - return first.GetHashCode() ^ second.GetHashCode(); - } - } + public override int GetHashCode() + { + return first.GetHashCode() ^ second.GetHashCode(); + } + } - public static class Extensions - { + [AttributeUsage(AttributeTargets.Class)] + public class CoreVersion : Attribute + { + public CoreVersion(string version) + { + this.Version = version; + } + + public string Version { get; set; } + public string FriendlyName { get; set; } + } + + public static class Introspection + { + public class CoreInfo + { + public string ClassName, Version, FriendlyName; + } + + public static List GetCoreInfo() + { + var ret = new List(); + //scan types in this assembly to find ones that implement boards to add them to the list + foreach (Type type in Assembly.GetExecutingAssembly().GetTypes()) + { + var attrs = type.GetCustomAttributes(typeof(CoreVersion), true); + if (attrs.Length == 0) continue; + var cv = (CoreVersion)attrs[0]; + var ci = new CoreInfo(); + ci.ClassName = type.Name; + ci.FriendlyName = cv.FriendlyName; + if (string.IsNullOrEmpty(ci.FriendlyName)) ci.FriendlyName = ci.ClassName; + ci.Version = cv.Version; + ret.Add(ci); + } + return ret; + } + } + + public static class Extensions + { public static void CopyTo(this Stream src, Stream dest) { int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000; @@ -76,17 +116,17 @@ namespace BizHawk } - 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; - } + 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; + } public static bool Bit(this byte b, int index) { @@ -99,161 +139,161 @@ namespace BizHawk } - 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); - } + 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); + } - 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; - } + 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; + } - public static bool In(this string str, IEnumerable options) - { - foreach (string opt in options) - { - if (opt.Equals(str,StringComparison.CurrentCultureIgnoreCase)) return true; - } - return false; - } + public static bool In(this string str, IEnumerable options) + { + foreach (string opt in options) + { + if (opt.Equals(str, StringComparison.CurrentCultureIgnoreCase)) return true; + } + return false; + } - public static bool In(this string str, IEnumerable options, Func eval) - { - foreach (T opt in options) - { - if (eval(opt, str) == true) - return true; - } - return false; - } + public static bool In(this string str, IEnumerable options, Func eval) + { + foreach (T opt in options) + { + if (eval(opt, str) == true) + return true; + } + return false; + } - public static bool NotIn(this string str, params string[] options) - { - foreach (string opt in options) - { - if (opt.ToLower() == str.ToLower()) return false; - } - return true; - } + public static bool NotIn(this string str, params string[] options) + { + foreach (string opt in options) + { + if (opt.ToLower() == str.ToLower()) return false; + } + return true; + } - public static bool NotIn(this string str, IEnumerable options) - { - foreach (string opt in options) - { - if (opt.ToLower() == str.ToLower()) return false; - } - return true; - } - - public static bool In(this int i, params int[] options) - { - foreach (int j in options) - { - if (i == j) return true; - } - return false; - } + public static bool NotIn(this string str, IEnumerable options) + { + foreach (string opt in options) + { + if (opt.ToLower() == str.ToLower()) return false; + } + return true; + } - public static bool In(this int i, IEnumerable options) - { - foreach (int j in options) - { - if (i == j) return true; - } - return false; - } + public static bool In(this int i, params int[] options) + { + foreach (int j in options) + { + if (i == j) return true; + } + return false; + } - public static bool ContainsStartsWith(this IEnumerable options, string str) - { - foreach (string opt in options) - { - if (opt.StartsWith(str)) return true; - } - return false; - } + public static bool In(this int i, IEnumerable options) + { + foreach (int j in options) + { + if (i == j) return true; + } + return false; + } - public static string GetOptionValue(this IEnumerable options, string str) - { - try - { - foreach (string opt in options) - { - if (opt.StartsWith(str)) - { - return opt.Split('=')[1]; - } - } - } - catch (Exception) { } - return null; - } + public static bool ContainsStartsWith(this IEnumerable options, string str) + { + foreach (string opt in options) + { + if (opt.StartsWith(str)) return true; + } + return false; + } - 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; - } + public static string GetOptionValue(this IEnumerable options, string str) + { + try + { + foreach (string opt in options) + { + if (opt.StartsWith(str)) + { + return opt.Split('=')[1]; + } + } + } + catch (Exception) { } + return null; + } - public static string ToCommaSeparated(this List list) - { - var sb = new StringBuilder(); - for (int i = 0; i < list.Count; i++) - { - if (i > 0) sb.Append(","); - sb.Append(list[i]); - } - return sb.ToString(); - } + 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; + } - public static void SaveAsHex(this byte[] buffer, TextWriter writer) - { - for (int i=0; i list) + { + var sb = new StringBuilder(); + for (int i = 0; i < list.Count; i++) + { + if (i > 0) sb.Append(","); + sb.Append(list[i]); + } + return sb.ToString(); + } - public static void SaveAsHex(this byte[] buffer, TextWriter writer, int length) - { - for (int i = 0; i < length; i++) - { - writer.Write("{0:X2}", buffer[i]); - } - writer.WriteLine(); - } + public static void SaveAsHex(this byte[] buffer, TextWriter writer) + { + for (int i = 0; i < buffer.Length; i++) + { + writer.Write("{0:X2}", buffer[i]); + } + writer.WriteLine(); + } - public static void SaveAsHex(this short[] buffer, TextWriter writer) - { - for (int i = 0; i < buffer.Length; i++) - { - writer.Write("{0:X4}", buffer[i]); - } - writer.WriteLine(); - } + public static void SaveAsHex(this byte[] buffer, TextWriter writer, int length) + { + for (int i = 0; i < length; i++) + { + writer.Write("{0:X2}", buffer[i]); + } + writer.WriteLine(); + } - public static void SaveAsHex(this ushort[] buffer, TextWriter writer) - { - for (int i = 0; i < buffer.Length; i++) - { - writer.Write("{0:X4}", buffer[i]); - } - writer.WriteLine(); - } + public static void SaveAsHex(this short[] buffer, TextWriter writer) + { + for (int i = 0; i < buffer.Length; i++) + { + writer.Write("{0:X4}", buffer[i]); + } + writer.WriteLine(); + } + + public static void SaveAsHex(this ushort[] buffer, TextWriter writer) + { + for (int i = 0; i < buffer.Length; i++) + { + writer.Write("{0:X4}", buffer[i]); + } + writer.WriteLine(); + } public static void SaveAsHex(this int[] buffer, TextWriter writer) { @@ -314,38 +354,38 @@ namespace BizHawk } - public static void ReadFromHex(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= 0; j--) - if (SaveRAM[j] != 0) - return j + 1; - return 0; - } + public static int SaveRamBytesUsed(byte[] SaveRAM) + { + for (int j = SaveRAM.Length - 1; j >= 0; j--) + if (SaveRAM[j] != 0) + return j + 1; + return 0; + } /// /// conerts bytes to an uppercase string of hex numbers in upper case without any spacing or anything @@ -462,7 +502,7 @@ namespace BizHawk { MemoryStream ms = new MemoryStream(); if (str.Length % 2 != 0) throw new ArgumentException(); - int len = str.Length/2; + int len = str.Length / 2; for (int i = 0; i < len; i++) { int d = 0; @@ -494,11 +534,11 @@ namespace BizHawk public static short[] ByteBufferToShortBuffer(byte[] buf) { - int num = buf.Length/2; + int num = buf.Length / 2; short[] ret = new short[num]; for (int i = 0; i < num; i++) { - ret[i] =(short)(buf[i * 2] | (buf[i * 2 + 1] << 8)); + ret[i] = (short)(buf[i * 2] | (buf[i * 2 + 1] << 8)); } return ret; } @@ -510,7 +550,7 @@ namespace BizHawk for (int i = 0; i < num; i++) { ret[i * 2 + 0] = (byte)(buf[i] & 0xFF); - ret[i * 2 + 1] = (byte)((buf[i]>>8) & 0xFF); + ret[i * 2 + 1] = (byte)((buf[i] >> 8) & 0xFF); } return ret; } @@ -583,14 +623,14 @@ namespace BizHawk Decimal OneGigaByte = OneMegaByte * 1024M; string suffix; - if (size > 1024*1024*1024) + if (size > 1024 * 1024 * 1024) { - size /= 1024*1024*1024; + size /= 1024 * 1024 * 1024; suffix = "GB"; } - else if (size > 1024*1024) + else if (size > 1024 * 1024) { - size /= 1024*1024; + size /= 1024 * 1024; suffix = "MB"; } else if (size > 1024) @@ -643,7 +683,7 @@ namespace BizHawk { sections.Push(name); if (IsText) - if (IsWriter) { tw.WriteLine("[{0}]", name);} + if (IsWriter) { tw.WriteLine("[{0}]", name); } else { tr.ReadLine(); } } @@ -659,7 +699,7 @@ namespace BizHawk { if (typeof(T).BaseType != typeof(System.Enum)) throw new InvalidOperationException(); - if(isText) SyncEnumText(name, ref val); + if (isText) SyncEnumText(name, ref val); else if (IsReader) val = (T)Enum.ToObject(typeof(T), br.ReadInt32()); else bw.Write(Convert.ToInt32(val)); } @@ -907,7 +947,7 @@ namespace BizHawk void Read(ref int val) { val = br.ReadInt32(); } void Write(ref int val) { bw.Write(val); } - void ReadText(string name, ref int val) { val = int.Parse(tr.ReadLine().Split(' ')[1].Replace("0x",""), NumberStyles.HexNumber); } + void ReadText(string name, ref int val) { val = int.Parse(tr.ReadLine().Split(' ')[1].Replace("0x", ""), NumberStyles.HexNumber); } void WriteText(string name, ref int val) { tw.WriteLine("{0} 0x{1:X8}", name, val); } void Read(ref bool val) { val = br.ReadBoolean(); } diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index 971a721f35..0dfc4df698 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -87,6 +87,12 @@ namespace BizHawk.MultiClient Global.CoreInputComm = new CoreInputComm(); SyncCoreInputComm(); + Console.WriteLine("Scanning cores:"); + foreach (var ci in Introspection.GetCoreInfo()) + { + Console.WriteLine("{0} - {1} ({2})", ci.FriendlyName, ci.Version, ci.ClassName); + } + Database.LoadDatabase(PathManager.GetExeDirectoryAbsolute() + "\\gamedb.txt"); SyncPresentationMode();