From 76b93673787721056b660f15b474618be1360d42 Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 24 Apr 2017 07:41:55 -0500 Subject: [PATCH] more misc cleanups --- .../BasicServiceProvider.cs | 2 +- .../CallbackBasedTraceBuffer.cs | 1 + .../ControllerDefinition.cs | 26 +-- .../MemoryCallbackSystem.cs | 10 +- .../Base Implementations/MemoryDomain.cs | 16 +- .../Base Implementations/NullController.cs | 2 +- .../Base Implementations/NullEmulator.cs | 18 +- .../Base Implementations/NullVideo.cs | 6 +- .../BinaryQuickSerializer.cs | 27 ++- .../BizInvoke/BizInvoker.cs | 54 +++++- .../ControllerDefinitionMerger.cs | 34 ++-- BizHawk.Emulation.Common/CoreComms.cs | 6 +- BizHawk.Emulation.Common/Database/CRC32.cs | 17 +- BizHawk.Emulation.Common/Database/Database.cs | 2 +- .../Database/FirmwareDatabase.cs | 98 ++++++----- BizHawk.Emulation.Common/Database/GameInfo.cs | 94 ++++++----- .../Interfaces/Services/ISoundProvider.cs | 2 +- .../Interfaces/Services/ITraceable.cs | 6 +- .../Sound/Utilities/Equalizer.cs | 43 ++--- .../Sound/Utilities/Metaspu.cs | 158 +++++++++++------- .../Sound/Utilities/SpeexResampler.cs | 33 ++-- .../Sound/Utilities/Waves.cs | 8 +- BizHawk.Emulation.Common/TextState.cs | 16 +- BizHawk.sln.DotSettings | 1 + Version/VersionInfo.cs | 7 +- 25 files changed, 413 insertions(+), 274 deletions(-) diff --git a/BizHawk.Emulation.Common/Base Implementations/BasicServiceProvider.cs b/BizHawk.Emulation.Common/Base Implementations/BasicServiceProvider.cs index d1d7753c70..56a90e1004 100644 --- a/BizHawk.Emulation.Common/Base Implementations/BasicServiceProvider.cs +++ b/BizHawk.Emulation.Common/Base Implementations/BasicServiceProvider.cs @@ -14,7 +14,7 @@ namespace BizHawk.Emulation.Common /// public class BasicServiceProvider : IEmulatorServiceProvider { - private Dictionary Services = new Dictionary(); + private readonly Dictionary Services = new Dictionary(); public BasicServiceProvider(IEmulator core) { diff --git a/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs b/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs index b31010f11b..580c1cce46 100644 --- a/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs +++ b/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs @@ -63,6 +63,7 @@ namespace BizHawk.Emulation.Common { return _sink; } + set { _sink = value; diff --git a/BizHawk.Emulation.Common/Base Implementations/ControllerDefinition.cs b/BizHawk.Emulation.Common/Base Implementations/ControllerDefinition.cs index 935fd4520b..57a3bb9cd1 100644 --- a/BizHawk.Emulation.Common/Base Implementations/ControllerDefinition.cs +++ b/BizHawk.Emulation.Common/Base Implementations/ControllerDefinition.cs @@ -31,37 +31,37 @@ namespace BizHawk.Emulation.Common } /// - /// The name of the controller definition + /// Gets or sets the name of the controller definition /// public string Name { get; set; } /// - /// A list of all button types that have a boolean (on/off) value + /// Gets or sets a list of all button types that have a boolean (on/off) value /// public List BoolButtons { get; set; } /// - /// A list of all non-boolean types, that can be represented by a numerical value (such as analog controls, stylus coordinates, etc + /// Gets a list of all non-boolean types, that can be represented by a numerical value (such as analog controls, stylus coordinates, etc /// - public List FloatControls { get; private set; } + public List FloatControls { get; } /// - /// A list of all float ranges for each float control (must be one to one with FloatControls) + /// Gets a list of all float ranges for each float control (must be one to one with FloatControls) /// FloatRanges include the min/max/default values /// - public List FloatRanges { get; private set; } + public List FloatRanges { get; } /// - /// Axis contraints apply artificial contraints to float values - /// For instance, a N64 controller's analog range is actually larger than the amount allowed by the plastic that artificially contrains it to lower values - /// Axis contraints provide a way to technically allow the full range but have a user option to contrain down to typical values that a real control would have + /// Gets the axis constraints that apply artificial constraints to float values + /// For instance, a N64 controller's analog range is actually larger than the amount allowed by the plastic that artificially constrains it to lower values + /// Axis constraints provide a way to technically allow the full range but have a user option to constrain down to typical values that a real control would have /// - public List AxisConstraints { get; private set; } + public List AxisConstraints { get; } /// - /// A means of categorizing controls in various controller display and config screens + /// Gets the category labels. These labels provide a means of categorizing controls in various controller display and config screens /// - public Dictionary CategoryLabels { get; private set; } + public Dictionary CategoryLabels { get; } public void ApplyAxisConstraints(string constraintClass, IDictionary floatButtons) { @@ -93,6 +93,7 @@ namespace BizHawk.Emulation.Common xval *= ratio; yval *= ratio; } + floatButtons[xaxis] = (float)xval; floatButtons[yaxis] = (float)yval; break; @@ -214,5 +215,4 @@ namespace BizHawk.Emulation.Common return BoolButtons.Any() || FloatControls.Any(); } } - } diff --git a/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs b/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs index 25a459e97c..7a6771c695 100644 --- a/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs +++ b/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs @@ -33,7 +33,7 @@ namespace BizHawk.Emulation.Common private bool _hasWrites; private bool _hasExecutes; - public bool ExecuteCallbacksAvailable { get; set; } + public bool ExecuteCallbacksAvailable { get; } public void Add(IMemoryCallback callback) { @@ -215,19 +215,21 @@ namespace BizHawk.Emulation.Common public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { - switch(args.Action) + switch (args.Action) { case NotifyCollectionChangedAction.Add: - foreach(IMemoryCallback callback in args.NewItems) + foreach (IMemoryCallback callback in args.NewItems) { CallbackAdded?.Invoke(callback); } + break; case NotifyCollectionChangedAction.Remove: - foreach(IMemoryCallback callback in args.OldItems) + foreach (IMemoryCallback callback in args.OldItems) { CallbackRemoved?.Invoke(callback); } + break; } } diff --git a/BizHawk.Emulation.Common/Base Implementations/MemoryDomain.cs b/BizHawk.Emulation.Common/Base Implementations/MemoryDomain.cs index ca279e389b..5205269646 100644 --- a/BizHawk.Emulation.Common/Base Implementations/MemoryDomain.cs +++ b/BizHawk.Emulation.Common/Base Implementations/MemoryDomain.cs @@ -41,7 +41,7 @@ namespace BizHawk.Emulation.Common /// must remain valid as long as the MemoryDomain exists! /// if false, writes will be ignored [Obsolete] - public unsafe static MemoryDomain FromIntPtr(string name, long size, Endian endian, IntPtr data, bool writable = true, int wordSize = 1) + public static unsafe MemoryDomain FromIntPtr(string name, long size, Endian endian, IntPtr data, bool writable = true, int wordSize = 1) { return new MemoryDomainIntPtr(name, endian, data, size, writable, wordSize); } @@ -52,7 +52,7 @@ namespace BizHawk.Emulation.Common /// must remain valid as long as the MemoryDomain exists! /// if false, writes will be ignored [Obsolete] - public unsafe static MemoryDomain FromIntPtrSwap16(string name, long size, Endian endian, IntPtr data, bool writable = true) + public static unsafe MemoryDomain FromIntPtrSwap16(string name, long size, Endian endian, IntPtr data, bool writable = true) { return new MemoryDomainIntPtrSwap16(name, endian, data, size, writable); } @@ -69,9 +69,9 @@ namespace BizHawk.Emulation.Common { default: case Endian.Big: - return (ushort)((PeekByte(addr) << 8) | (PeekByte(addr + 1))); + return (ushort)((PeekByte(addr) << 8) | PeekByte(addr + 1)); case Endian.Little: - return (ushort)((PeekByte(addr)) | (PeekByte(addr + 1) << 8)); + return (ushort)(PeekByte(addr) | (PeekByte(addr + 1) << 8)); } } @@ -102,10 +102,10 @@ namespace BizHawk.Emulation.Common default: case Endian.Big: PokeByte(addr + 0, (byte)(val >> 8)); - PokeByte(addr + 1, (byte)(val)); + PokeByte(addr + 1, (byte)val); break; case Endian.Little: - PokeByte(addr + 0, (byte)(val)); + PokeByte(addr + 0, (byte)val); PokeByte(addr + 1, (byte)(val >> 8)); break; } @@ -121,10 +121,10 @@ namespace BizHawk.Emulation.Common PokeByte(addr + 0, (byte)(val >> 24)); PokeByte(addr + 1, (byte)(val >> 16)); PokeByte(addr + 2, (byte)(val >> 8)); - PokeByte(addr + 3, (byte)(val)); + PokeByte(addr + 3, (byte)val); break; case Endian.Little: - PokeByte(addr + 0, (byte)(val)); + PokeByte(addr + 0, (byte)val); PokeByte(addr + 1, (byte)(val >> 8)); PokeByte(addr + 2, (byte)(val >> 16)); PokeByte(addr + 3, (byte)(val >> 24)); diff --git a/BizHawk.Emulation.Common/Base Implementations/NullController.cs b/BizHawk.Emulation.Common/Base Implementations/NullController.cs index d3fb18f099..aa38d19179 100644 --- a/BizHawk.Emulation.Common/Base Implementations/NullController.cs +++ b/BizHawk.Emulation.Common/Base Implementations/NullController.cs @@ -24,6 +24,6 @@ return 0f; } - public static NullController Instance = new NullController(); + public static readonly NullController Instance = new NullController(); } } \ No newline at end of file diff --git a/BizHawk.Emulation.Common/Base Implementations/NullEmulator.cs b/BizHawk.Emulation.Common/Base Implementations/NullEmulator.cs index 104706c22e..5e3d869375 100644 --- a/BizHawk.Emulation.Common/Base Implementations/NullEmulator.cs +++ b/BizHawk.Emulation.Common/Base Implementations/NullEmulator.cs @@ -201,7 +201,7 @@ namespace BizHawk.Emulation.Common private bool _frameBufferClear = true; private bool _xmas; - private Pleg _pleg; + private readonly Pleg _pleg; private NullEmulatorSettings _settings; @@ -356,9 +356,7 @@ namespace BizHawk.Emulation.Common return; } - var s = new SinMan(1500, n); - s.c = c; - s.n = n; + var s = new SinMan(1500, n) { c = c, n = n }; SinMen.Add(s); } @@ -378,6 +376,7 @@ namespace BizHawk.Emulation.Common ret += s.Next(); } } + if (ret > 32767) ret = 32767; if (ret < -32767) ret = -32767; return (short)ret; @@ -432,10 +431,7 @@ namespace BizHawk.Emulation.Common public bool fading = false; - public bool Done - { - get { return amp < 2.0; } - } + public bool Done => amp < 2.0; static double GetFreq(int note) { @@ -447,9 +443,15 @@ namespace BizHawk.Emulation.Common short result = (short)(Math.Sin(theta) * amp); theta += freq * Math.PI / 22050.0; if (theta >= Math.PI * 2.0) + { theta -= Math.PI * 2.0; + } + if (fading) + { amp *= 0.87; + } + return result; } diff --git a/BizHawk.Emulation.Common/Base Implementations/NullVideo.cs b/BizHawk.Emulation.Common/Base Implementations/NullVideo.cs index 414b268da9..9db6af1ccd 100644 --- a/BizHawk.Emulation.Common/Base Implementations/NullVideo.cs +++ b/BizHawk.Emulation.Common/Base Implementations/NullVideo.cs @@ -12,6 +12,8 @@ return new int[BufferWidth * BufferHeight]; } + public static NullVideo Instance { get; } = new NullVideo(); + public int VirtualWidth => 256; public int VirtualHeight => 192; @@ -21,9 +23,5 @@ public int BufferHeight => 192; public int BackgroundColor => 0; - - private static readonly NullVideo _nullVideo = new NullVideo(); - - public static NullVideo Instance => _nullVideo; } } diff --git a/BizHawk.Emulation.Common/BinaryQuickSerializer.cs b/BizHawk.Emulation.Common/BinaryQuickSerializer.cs index fb61d8e2d2..82136e6a63 100644 --- a/BizHawk.Emulation.Common/BinaryQuickSerializer.cs +++ b/BizHawk.Emulation.Common/BinaryQuickSerializer.cs @@ -23,13 +23,18 @@ namespace BizHawk.Emulation.Common { var caller = e as MethodCallExpression; if (caller == null) + { throw new ArgumentException("Expression must be a method call"); + } + return caller.Method; } + private static MethodInfo Method(Expression f) { return FromExpression(f.Body); } + private static MethodInfo Method(Expression> f) { return FromExpression(f.Body); @@ -39,14 +44,17 @@ namespace BizHawk.Emulation.Common #region read and write handlers for individual fields - private static Dictionary readhandlers = new Dictionary(); - private static Dictionary writehandlers = new Dictionary(); + private static readonly Dictionary readhandlers = new Dictionary(); + private static readonly Dictionary writehandlers = new Dictionary(); private static void AddR(Expression> f) { var method = Method(f); if (!typeof(T).IsAssignableFrom(method.ReturnType)) + { throw new InvalidOperationException("Type mismatch"); + } + readhandlers.Add(typeof(T), method); } @@ -54,7 +62,10 @@ namespace BizHawk.Emulation.Common { var method = Method(f); if (!method.GetParameters()[0].ParameterType.IsAssignableFrom(typeof(T))) + { throw new InvalidOperationException("Type mismatch"); + } + writehandlers.Add(typeof(T), Method(f)); } @@ -125,12 +136,17 @@ namespace BizHawk.Emulation.Common il.Emit(OpCodes.Ldarg_1); MethodInfo m; if (!readhandlers.TryGetValue(field.FieldType, out m)) + { throw new InvalidOperationException("(R) Can't handle nested type " + field.FieldType); + } + il.Emit(OpCodes.Callvirt, m); il.Emit(OpCodes.Stfld, field); } + il.Emit(OpCodes.Ret); } + { var il = wmeth.GetILGenerator(); var target = il.DeclareLocal(t); @@ -145,9 +161,13 @@ namespace BizHawk.Emulation.Common il.Emit(OpCodes.Ldfld, field); MethodInfo m; if (!writehandlers.TryGetValue(field.FieldType, out m)) + { throw new InvalidOperationException("(W) Can't handle nested type " + field.FieldType); + } + il.Emit(OpCodes.Callvirt, m); } + il.Emit(OpCodes.Ret); } @@ -161,7 +181,7 @@ namespace BizHawk.Emulation.Common #endregion - private static IDictionary serializers = + private static readonly IDictionary serializers = new ConcurrentDictionary(); private static SerializationFactory GetFactory(Type t) @@ -172,6 +192,7 @@ namespace BizHawk.Emulation.Common f = CreateFactory(t); serializers[t] = f; } + return f; } diff --git a/BizHawk.Emulation.Common/BizInvoke/BizInvoker.cs b/BizHawk.Emulation.Common/BizInvoke/BizInvoker.cs index 4068b1e8cf..d2bf032b6c 100644 --- a/BizHawk.Emulation.Common/BizInvoke/BizInvoker.cs +++ b/BizHawk.Emulation.Common/BizInvoke/BizInvoker.cs @@ -36,7 +36,7 @@ namespace BizHawk.Emulation.Common.BizInvoke /// /// dictionary of all generated proxy implementations and their basetypes /// - private static IDictionary Impls = new Dictionary(); + private static readonly IDictionary Impls = new Dictionary(); /// /// the assembly that all proxies are placed in @@ -138,13 +138,17 @@ namespace BizHawk.Emulation.Common.BizInvoke { var uo = baseMethods.FirstOrDefault(a => !a.Info.IsVirtual || a.Info.IsFinal); if (uo != null) + { throw new InvalidOperationException("Method " + uo.Info.Name + " cannot be overriden!"); + } // there's no technical reason to disallow this, but we wouldn't be doing anything // with the base implementation, so it's probably a user error var na = baseMethods.FirstOrDefault(a => !a.Info.IsAbstract); if (na != null) + { throw new InvalidOperationException("Method " + na.Info.Name + " is not abstract!"); + } } // hooks that will be run on the created proxy object @@ -171,7 +175,9 @@ namespace BizHawk.Emulation.Common.BizInvoke ImplType = type.CreateType() }; if (monitor) + { ret.ConnectMonitor = (o, m) => o.GetType().GetField(monitorField.Name).SetValue(o, m); + } return ret; } @@ -234,7 +240,9 @@ namespace BizHawk.Emulation.Common.BizInvoke il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, field); for (int i = 0; i < paramTypes.Length; i++) + { il.Emit(OpCodes.Ldarg, (short)(i + 1)); + } il.Emit(OpCodes.Callvirt, delegateInvoke); @@ -354,11 +362,18 @@ namespace BizHawk.Emulation.Common.BizInvoke private static void LoadConstant(ILGenerator il, IntPtr p) { if (p == IntPtr.Zero) + { il.Emit(OpCodes.Ldc_I4_0); + } else if (IntPtr.Size == 4) + { il.Emit(OpCodes.Ldc_I4, (int)p); + } else + { il.Emit(OpCodes.Ldc_I8, (long)p); + } + il.Emit(OpCodes.Conv_I); } @@ -368,11 +383,18 @@ namespace BizHawk.Emulation.Common.BizInvoke private static void LoadConstant(ILGenerator il, UIntPtr p) { if (p == UIntPtr.Zero) + { il.Emit(OpCodes.Ldc_I4_0); + } else if (UIntPtr.Size == 4) + { il.Emit(OpCodes.Ldc_I4, (int)p); + } else + { il.Emit(OpCodes.Ldc_I8, (long)p); + } + il.Emit(OpCodes.Conv_U); } @@ -382,12 +404,18 @@ namespace BizHawk.Emulation.Common.BizInvoke private static Type EmitParamterLoad(ILGenerator il, int idx, Type type) { if (type.IsGenericType) + { throw new InvalidOperationException("Generic types not supported"); + } + if (type.IsByRef) { var et = type.GetElementType(); if (!et.IsPrimitive && !et.IsEnum) + { throw new InvalidOperationException("Only refs of primitive or enum types are supported!"); + } + var loc = il.DeclareLocal(type, true); il.Emit(OpCodes.Ldarg, (short)idx); il.Emit(OpCodes.Dup); @@ -395,17 +423,24 @@ namespace BizHawk.Emulation.Common.BizInvoke il.Emit(OpCodes.Conv_I); return typeof(IntPtr); } - else if (type.IsArray) + + if (type.IsArray) { var et = type.GetElementType(); if (!et.IsPrimitive && !et.IsEnum) + { throw new InvalidOperationException("Only arrays of primitive or enum types are supported!"); + } // these two cases aren't too hard to add if (type.GetArrayRank() > 1) + { throw new InvalidOperationException("Multidimensional arrays are not supported!"); + } if (type.Name.Contains('*')) + { throw new InvalidOperationException("Only 0-based 1-dimensional arrays are supported!"); + } var loc = il.DeclareLocal(type, true); var end = il.DefineLabel(); @@ -428,7 +463,8 @@ namespace BizHawk.Emulation.Common.BizInvoke return typeof(IntPtr); } - else if (typeof(Delegate).IsAssignableFrom(type)) + + if (typeof(Delegate).IsAssignableFrom(type)) { var mi = typeof(Marshal).GetMethod("GetFunctionPointerForDelegate", new[] { typeof(Delegate) }); var end = il.DefineLabel(); @@ -446,15 +482,14 @@ namespace BizHawk.Emulation.Common.BizInvoke il.MarkLabel(end); return typeof(IntPtr); } - else if (type.IsPrimitive || type.IsEnum) + + if (type.IsPrimitive || type.IsEnum) { il.Emit(OpCodes.Ldarg, (short)idx); return type; } - else - { - throw new InvalidOperationException("Unrecognized parameter type!"); - } + + throw new InvalidOperationException("Unrecognized parameter type!"); } private static CustomAttributeBuilder GetAttributeBuilder(object o) @@ -462,7 +497,10 @@ namespace BizHawk.Emulation.Common.BizInvoke // anything more clever we can do here? var t = o.GetType(); if (t == typeof(OutAttribute) || t == typeof(InAttribute)) + { return new CustomAttributeBuilder(t.GetConstructor(Type.EmptyTypes), new object[0]); + } + throw new InvalidOperationException("Unknown parameter attribute " + t.Name); } } diff --git a/BizHawk.Emulation.Common/ControllerDefinitionMerger.cs b/BizHawk.Emulation.Common/ControllerDefinitionMerger.cs index cde008163d..cd8801ff89 100644 --- a/BizHawk.Emulation.Common/ControllerDefinitionMerger.cs +++ b/BizHawk.Emulation.Common/ControllerDefinitionMerger.cs @@ -1,14 +1,11 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; namespace BizHawk.Emulation.Common { // the idea here is that various connected peripherals have their controls all merged // into one definition, including logic to unmerge the data back so each one can work // with it without knowing what else is connected - public static class ControllerDefinitionMerger { private static string Allocate(string input, ref int plr, ref int plrnext) @@ -16,22 +13,24 @@ namespace BizHawk.Emulation.Common int offset = int.Parse(input.Substring(0, 1)); int currplr = plr + offset; if (currplr >= plrnext) + { plrnext = currplr + 1; - return string.Format("P{0} {1}", currplr, input.Substring(1)); + } + + return $"P{currplr} {input.Substring(1)}"; } /// /// merge some controller definitions for different ports, and such. i promise to fully document this tomorrow /// - /// /// - public static ControllerDefinition GetMerged(IEnumerable Controllers, out List Unmergers) + public static ControllerDefinition GetMerged(IEnumerable controllers, out List unmergers) { ControllerDefinition ret = new ControllerDefinition(); - Unmergers = new List(); + unmergers = new List(); int plr = 1; int plrnext = 1; - foreach (var def in Controllers) + foreach (var def in controllers) { Dictionary remaps = new Dictionary(); @@ -41,33 +40,37 @@ namespace BizHawk.Emulation.Common ret.BoolButtons.Add(r); remaps[s] = r; } + foreach (string s in def.FloatControls) { string r = Allocate(s, ref plr, ref plrnext); ret.FloatControls.Add(r); remaps[s] = r; } + ret.FloatRanges.AddRange(def.FloatRanges); plr = plrnext; - Unmergers.Add(new ControlDefUnMerger(remaps)); + unmergers.Add(new ControlDefUnMerger(remaps)); } + return ret; } } public class ControlDefUnMerger { - Dictionary Remaps; + private readonly Dictionary Remaps; - public ControlDefUnMerger(Dictionary Remaps) + public ControlDefUnMerger(Dictionary remaps) { - this.Remaps = Remaps; + Remaps = remaps; } private class DummyController : IController { - IController src; - Dictionary remaps; + private readonly IController src; + private readonly Dictionary remaps; + public DummyController(IController src, Dictionary remaps) { this.src = src; @@ -76,7 +79,7 @@ namespace BizHawk.Emulation.Common public ControllerDefinition Definition { get { throw new NotImplementedException(); } } - public bool this[string button] { get { return IsPressed(button); } } + public bool this[string button] => IsPressed(button); public bool IsPressed(string button) { @@ -93,6 +96,5 @@ namespace BizHawk.Emulation.Common { return new DummyController(c, Remaps); } - } } diff --git a/BizHawk.Emulation.Common/CoreComms.cs b/BizHawk.Emulation.Common/CoreComms.cs index 90572f7f7b..7781c47adc 100644 --- a/BizHawk.Emulation.Common/CoreComms.cs +++ b/BizHawk.Emulation.Common/CoreComms.cs @@ -11,10 +11,10 @@ namespace BizHawk.Emulation.Common /// public class CoreComm { - public CoreComm(Action showMessage, Action NotifyMessage) + public CoreComm(Action showMessage, Action notifyMessage) { ShowMessage = showMessage; - Notify = NotifyMessage; + Notify = notifyMessage; } public ICoreFileProvider CoreFileProvider; @@ -47,7 +47,7 @@ namespace BizHawk.Emulation.Common /// public Action Notify { get; private set; } - public Func RequestGLContext; + public Func RequestGLContext; public Action ReleaseGLContext; public Action ActivateGLContext; public Action DeactivateGLContext; // this shouldnt be necessary.. frontend should be changing context before it does anything.. but for now.. diff --git a/BizHawk.Emulation.Common/Database/CRC32.cs b/BizHawk.Emulation.Common/Database/CRC32.cs index 5772ab3861..58389eea44 100644 --- a/BizHawk.Emulation.Common/Database/CRC32.cs +++ b/BizHawk.Emulation.Common/Database/CRC32.cs @@ -1,9 +1,8 @@ // we could get a little list of crcs from here and make it clear which crc this class was for, and expose others // http://www.ross.net/crc/download/crc_v3.txt - namespace BizHawk.Emulation.Common { - //TODO - why am I here? put me alongside hash_md5 and such in a non-emulation-related class + // TODO - why am I here? put me alongside hash_md5 and such in a non-emulation-related class public static class CRC32 { // Lookup table for speed. @@ -18,9 +17,13 @@ namespace BizHawk.Emulation.Common for (int j = 8; j > 0; --j) { if ((crc & 1) == 1) - crc = ((crc >> 1) ^ 0xEDB88320); + { + crc = (crc >> 1) ^ 0xEDB88320; + } else + { crc >>= 1; + } } CRC32Table[i] = crc; @@ -29,11 +32,13 @@ namespace BizHawk.Emulation.Common public static int Calculate(byte[] data) { - uint Result = 0xFFFFFFFF; + uint result = 0xFFFFFFFF; foreach (var b in data) - Result = (((Result) >> 8) ^ CRC32Table[b ^ ((Result) & 0xFF)]); + { + result = (result >> 8) ^ CRC32Table[b ^ (result & 0xFF)]; + } - return (int) ~Result; + return (int)~result; } } } diff --git a/BizHawk.Emulation.Common/Database/Database.cs b/BizHawk.Emulation.Common/Database/Database.cs index 147f662380..970bd57fd9 100644 --- a/BizHawk.Emulation.Common/Database/Database.cs +++ b/BizHawk.Emulation.Common/Database/Database.cs @@ -212,7 +212,7 @@ namespace BizHawk.Emulation.Common public static GameInfo GetGameInfo(byte[] romData, string fileName) { CompactGameInfo cgi; - var hash = string.Format("{0:X8}", CRC32.Calculate(romData)); + var hash = $"{CRC32.Calculate(romData):X8}"; if (db.TryGetValue(hash, out cgi)) { return new GameInfo(cgi); diff --git a/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs b/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs index 658ed51adb..ac1f60e3a2 100644 --- a/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs +++ b/BizHawk.Emulation.Common/Database/FirmwareDatabase.cs @@ -15,7 +15,7 @@ namespace BizHawk.Emulation.Common Option("NES", "Bios_FDS", fds_nintendo); Option("NES", "Bios_FDS", fds_twinfc); - FirmwareAndOption("973E10840DB683CF3FAF61BD443090786B3A9F04", 262144, "SNES", "Rom_SGB", "sgb.sfc", "Super GameBoy Rom"); //World (Rev B) ? + FirmwareAndOption("973E10840DB683CF3FAF61BD443090786B3A9F04", 262144, "SNES", "Rom_SGB", "sgb.sfc", "Super GameBoy Rom"); // World (Rev B) ? FirmwareAndOption("A002F4EFBA42775A31185D443F3ED1790B0E949A", 3072, "SNES", "CX4", "cx4.rom", "CX4 Rom"); FirmwareAndOption("188D471FEFEA71EB53F0EE7064697FF0971B1014", 8192, "SNES", "DSP1", "dsp1.rom", "DSP1 Rom"); FirmwareAndOption("78B724811F5F18D8C67669D9390397EB1A47A5E2", 8192, "SNES", "DSP1b", "dsp1b.rom", "DSP1b Rom"); @@ -53,8 +53,8 @@ namespace BizHawk.Emulation.Common FirmwareAndOption("AB16F56989B27D89BABE5F89C5A8CB3DA71A82F0", 16384, "C64", "Drive1541", "drive-1541.bin", "1541 Disk Drive Rom"); FirmwareAndOption("D3B78C3DBAC55F5199F33F3FE0036439811F7FB3", 16384, "C64", "Drive1541II", "drive-1541ii.bin", "1541-II Disk Drive Rom"); - //for saturn, we think any bios region can pretty much run any iso - //so, we're going to lay this out carefully so that we choose things in a sensible order, but prefer the correct region + // for saturn, we think any bios region can pretty much run any iso + // so, we're going to lay this out carefully so that we choose things in a sensible order, but prefer the correct region var ss_100_j = File("2B8CB4F87580683EB4D760E4ED210813D667F0A2", 524288, "saturn-1.00-(J).bin", "Bios v1.00 (J)"); var ss_100_ue = File("FAA8EA183A6D7BBE5D4E03BB1332519800D3FBC3", 524288, "saturn-1.00-(U+E).bin", "Bios v1.00 (U+E)"); var ss_100a_ue = File("3BB41FEB82838AB9A35601AC666DE5AACFD17A58", 524288, "saturn-1.00a-(U+E).bin", "Bios v1.00a (U+E)"); //?? is this size correct? @@ -75,14 +75,14 @@ namespace BizHawk.Emulation.Common Option("SAT", "E", ss_100_j); Option("SAT", "E", ss_101_j); - var ti83_102 = File("CE08F6A808701FC6672230A790167EE485157561", 262144, "ti83_102.rom", "TI-83 Rom v1.02"); //?? is this size correct? - var ti83_103 = File("8399E384804D8D29866CAA4C8763D7A61946A467", 262144, "ti83_103.rom", "TI-83 Rom v1.03"); //?? is this size correct? - var ti83_104 = File("33877FF637DC5F4C5388799FD7E2159B48E72893", 262144, "ti83_104.rom", "TI-83 Rom v1.04"); //?? is this size correct? - var ti83_106 = File("3D65C2A1B771CE8E5E5A0476EC1AA9C9CDC0E833", 262144, "ti83_106.rom", "TI-83 Rom v1.06"); //?? is this size correct? - var ti83_107 = File("EF66DAD3E7B2B6A86F326765E7DFD7D1A308AD8F", 262144, "ti83_107.rom", "TI-83 Rom v1.07"); //formerly the 1.?? recommended one - var ti83_108 = File("9C74F0B61655E9E160E92164DB472AD7EE02B0F8", 262144, "ti83_108.rom", "TI-83 Rom v1.08"); //?? is this size correct? - var ti83p_103 = File("37EAEEB9FB5C18FB494E322B75070E80CC4D858E", 262144, "ti83p_103b.rom", "TI-83 Plus Rom v1.03"); //?? is this size correct? - var ti83p_112 = File("6615DF5554076B6B81BD128BF847D2FF046E556B", 262144, "ti83p_112.rom", "TI-83 Plus Rom v1.12"); //?? is this size correct? + var ti83_102 = File("CE08F6A808701FC6672230A790167EE485157561", 262144, "ti83_102.rom", "TI-83 Rom v1.02"); // ?? is this size correct? + var ti83_103 = File("8399E384804D8D29866CAA4C8763D7A61946A467", 262144, "ti83_103.rom", "TI-83 Rom v1.03"); // ?? is this size correct? + var ti83_104 = File("33877FF637DC5F4C5388799FD7E2159B48E72893", 262144, "ti83_104.rom", "TI-83 Rom v1.04"); // ?? is this size correct? + var ti83_106 = File("3D65C2A1B771CE8E5E5A0476EC1AA9C9CDC0E833", 262144, "ti83_106.rom", "TI-83 Rom v1.06"); // ?? is this size correct? + var ti83_107 = File("EF66DAD3E7B2B6A86F326765E7DFD7D1A308AD8F", 262144, "ti83_107.rom", "TI-83 Rom v1.07"); // formerly the 1.?? recommended one + var ti83_108 = File("9C74F0B61655E9E160E92164DB472AD7EE02B0F8", 262144, "ti83_108.rom", "TI-83 Rom v1.08"); // ?? is this size correct? + var ti83p_103 = File("37EAEEB9FB5C18FB494E322B75070E80CC4D858E", 262144, "ti83p_103b.rom", "TI-83 Plus Rom v1.03"); // ?? is this size correct? + var ti83p_112 = File("6615DF5554076B6B81BD128BF847D2FF046E556B", 262144, "ti83p_112.rom", "TI-83 Plus Rom v1.12"); // ?? is this size correct? Firmware("TI83", "Rom", "TI-83 Rom"); Option("TI83", "Rom", ti83_102); @@ -97,7 +97,7 @@ namespace BizHawk.Emulation.Common // mega cd var eu_mcd1_9210 = File("f891e0ea651e2232af0c5c4cb46a0cae2ee8f356", 131072, "eu_mcd1_9210.bin", "Mega CD EU (9210)"); var eu_mcd2_9303 = File("7063192ae9f6b696c5b81bc8f0a9fe6f0c400e58", 131072, "eu_mcd2_9303.bin", "Mega CD EU (9303)"); - var eu_mcd2_9306 = File("523b3125fb0ac094e16aa072bc6ccdca22e520e5", 131072, "eu_mcd2_9306.bin", "Mega CD EU (9310)"); //?? is this size correct? + var eu_mcd2_9306 = File("523b3125fb0ac094e16aa072bc6ccdca22e520e5", 131072, "eu_mcd2_9306.bin", "Mega CD EU (9310)"); // ?? is this size correct? var jp_mcd1_9111 = File("4846f448160059a7da0215a5df12ca160f26dd69", 131072, "jp_mcd1_9111.bin", "Mega CD JP (9111)"); var jp_mcd1_9112 = File("e4193c6ae44c3cea002707d2a88f1fbcced664de", 131072, "jp_mcd1_9112.bin", "Mega CD JP (9112)"); var us_scd1_9210 = File("f4f315adcef9b8feb0364c21ab7f0eaf5457f3ed", 131072, "us_scd1_9210.bin", "Sega CD US (9210)"); @@ -127,12 +127,12 @@ namespace BizHawk.Emulation.Common Option("SMS", "Export", sms_m404); Option("SMS", "Japan", sms_jp_21); - //PSX - //http://forum.fobby.net/index.php?t=msg&goto=2763 [f] - //http://www.psxdev.net/forum/viewtopic.php?f=69&t=56 [p] - //https://en.wikipedia.org/wiki/PlayStation_models#Comparison_of_models [w] - //https://github.com/petrockblog/RetroPie-Setup/wiki/PCSX-Core-Playstation-1 [g] - //http://redump.org/datfile/psx-bios/ also + // PSX + // http://forum.fobby.net/index.php?t=msg&goto=2763 [f] + // http://www.psxdev.net/forum/viewtopic.php?f=69&t=56 [p] + // https://en.wikipedia.org/wiki/PlayStation_models#Comparison_of_models [w] + // https://github.com/petrockblog/RetroPie-Setup/wiki/PCSX-Core-Playstation-1 [g] + // http://redump.org/datfile/psx-bios/ also var ps_10j = File("343883A7B555646DA8CEE54AADD2795B6E7DD070", 524288, "ps-10j.bin", "PSX BIOS (Version 1.0 J)", "Used on SCPH-1000, DTL-H1000 [g]. This is Rev for A hardware [w]."); var ps_11j = File("B06F4A861F74270BE819AA2A07DB8D0563A7CC4E", 524288, "ps-11j.bin", "PSX BIOS (Version 1.1 01/22/95)", "Used on SCPH-3000, DTL-H1000H [g]. This is for Rev B hardware [w]."); var ps_20a = File("649895EFD79D14790EABB362E94EB0622093DFB9", 524288, "ps-20a.bin", "PSX BIOS (Version 2.0 05/07/95 A)", "Used on DTL-H1001 [g]. This is for Rev B hardware [w]."); @@ -141,15 +141,15 @@ namespace BizHawk.Emulation.Common var ps_21a = File("CA7AF30B50D9756CBD764640126C454CFF658479", 524288, "ps-21a.bin", "PSX BIOS (Version 2.1 07/17/95 A)", "Used on DTL-H1101 [g]. This is for Rev B hardware, presumably."); var ps_21e = File("76CF6B1B2A7C571A6AD07F2BAC0DB6CD8F71E2CC", 524288, "ps-21e.bin", "PSX BIOS (Version 2.1 07/17/95 E)", "Used on SCPH-1002, DTL-H1102 [g]. This is for Rev B hardware [w]."); var ps_22j = File("FFA7F9A7FB19D773A0C3985A541C8E5623D2C30D", 524288, "ps-22j.bin", "PSX BIOS (Version 2.2 12/04/95 J)", "Used on SCPH-5000, DTL-H1200, DTL-H3000 [g]. This is for Rev C hardware [w]."); - var ps_22j_bad = File("E340DB2696274DDA5FDC25E434A914DB71E8B02B", 524288, "ps-22j-bad.bin", "BAD DUMP OF SCPH-5000. Found on [p]."); //BAD!! - var ps_22j_bad2 = File("81622ACE63E25696A5D884692E554D350DDF57A6", 526083, "ps-22j-bad2.bin", "PSX BIOS (Version 2.2 12/04/95 J", "BAD DUMP OF SCPH-5000."); //BAD! + var ps_22j_bad = File("E340DB2696274DDA5FDC25E434A914DB71E8B02B", 524288, "ps-22j-bad.bin", "BAD DUMP OF SCPH-5000. Found on [p]."); // BAD!! + var ps_22j_bad2 = File("81622ACE63E25696A5D884692E554D350DDF57A6", 526083, "ps-22j-bad2.bin", "PSX BIOS (Version 2.2 12/04/95 J", "BAD DUMP OF SCPH-5000."); // BAD! var ps_22a = File("10155D8D6E6E832D6EA66DB9BC098321FB5E8EBF", 524288, "ps-22a.bin", "PSX BIOS (Version 2.2 12/04/95 A)", "Used on SCPH-1001, DTL-H1201, DTL-H3001 [g]. This is for Rev C hardware [w]."); var ps_22e = File("B6A11579CAEF3875504FCF3831B8E3922746DF2C", 524288, "ps-22e.bin", "PSX BIOS (Version 2.2 12/04/95 E)", "Used on SCPH-1002, DTL-H1202, DTL-H3002 [g]. This is for Rev C hardware [w]."); var ps_22d = File("73107D468FC7CB1D2C5B18B269715DD889ECEF06", 524288, "ps-22d.bin", "PSX BIOS (Version 2.2 03/06/96 D)", "Used on DTL-H1100 [g]. This is for Rev C hardware, presumably."); var ps_30j = File("B05DEF971D8EC59F346F2D9AC21FB742E3EB6917", 524288, "ps-30j.bin", "PSX BIOS (Version 3.0 09/09/96 J)", "Used on SCPH-5500 [g]. This is for Rev C hardware [w]. Recommended for (J) [f]."); var ps_30a = File("0555C6FAE8906F3F09BAF5988F00E55F88E9F30B", 524288, "ps-30a.bin", "PSX BIOS (Version 3.0 11/18/96 A)", "Used on SCPH-5501, SCPH-5503, SCPH-7003 [g]. This is for Rev C hardware [w]. Recommended for (U) [f]."); var ps_30e = File("F6BC2D1F5EB6593DE7D089C425AC681D6FFFD3F0", 524288, "ps-30e.bin", "PSX BIOS (Version 3.0 01/06/97 E)", "Used on SCPH-5502, SCPH-5552 [g]. This is for Rev C hardware [w]. Recommended for (E) [f]."); - var ps_30e_bad = File("F8DE9325FC36FCFA4B29124D291C9251094F2E54", 524288, "ps-30e-bad.bin", "BAD DUMP OF SCPH-5502. Found on [p]."); //BAD! + var ps_30e_bad = File("F8DE9325FC36FCFA4B29124D291C9251094F2E54", 524288, "ps-30e-bad.bin", "BAD DUMP OF SCPH-5502. Found on [p]."); // BAD! var ps_40j = File("77B10118D21AC7FFA9B35F9C4FD814DA240EB3E9", 524288, "ps-40j.bin", "PSX BIOS (Version 4.0 08/18/97 J)", "Used on SCPH-7000, SCPH-7500, SCPH-9000 [g]. This is for Rev C hardware [w]."); var ps_41a = File("14DF4F6C1E367CE097C11DEAE21566B4FE5647A9", 524288, "ps-41a.bin", "PSX BIOS (Version 4.1 12/16/97 A)", "Used on SCPH-7001, SCPH-7501, SCPH-7503, SCPH-9001, SCPH-9003, SCPH-9903 [g]. This is for Rev C hardware [w]."); var ps_41e = File("8D5DE56A79954F29E9006929BA3FED9B6A418C1D", 524288, "ps-41e.bin", "PSX BIOS (Version 4.1 12/16/97 E)", "Used on SCPH-7002, SCPH-7502, SCPH-9002 [g]. This is for Rev C hardware [w]."); @@ -168,12 +168,12 @@ namespace BizHawk.Emulation.Common Option("PSX", "U", ps_30a); Option("PSX", "J", ps_30j); Option("PSX", "E", ps_30e); - //in general, alternates arent allowed.. their quality isnt known. - //we have this comment from fobby.net: "SCPH7502 works fine for European games" (TBD) - //however, we're sticking with the 3.0 series. - //please note: 2.1 or 2.2 would be a better choice, as the dates are the same and the bioses are more likely to matching in terms of entrypoints and such. - //but 3.0 is what mednafen used + // in general, alternates arent allowed.. their quality isnt known. + // we have this comment from fobby.net: "SCPH7502 works fine for European games" (TBD) + // however, we're sticking with the 3.0 series. + // please note: 2.1 or 2.2 would be a better choice, as the dates are the same and the bioses are more likely to matching in terms of entrypoints and such. + // but 3.0 is what mednafen used Option("PSX", "J", ps_10j, FirmwareOptionStatus.Unacceptable); Option("PSX", "J", ps_11j, FirmwareOptionStatus.Unacceptable); Option("PSX", "U", ps_20a, FirmwareOptionStatus.Unacceptable); @@ -204,8 +204,8 @@ namespace BizHawk.Emulation.Common Option("AppleII", "DiskII", appleII_DiskII, FirmwareOptionStatus.Acceptable); } - //adds a defined firmware ID to the database - static void Firmware(string systemId, string id, string descr) + // adds a defined firmware ID to the database + private static void Firmware(string systemId, string id, string descr) { var fr = new FirmwareRecord { @@ -217,8 +217,8 @@ namespace BizHawk.Emulation.Common FirmwareRecords.Add(fr); } - //adds an acceptable option for a firmware ID to the database - static FirmwareOption Option(string hash, long size, string systemId, string id, FirmwareOptionStatus status = FirmwareOptionStatus.Acceptable) + // adds an acceptable option for a firmware ID to the database + private static FirmwareOption Option(string hash, long size, string systemId, string id, FirmwareOptionStatus status = FirmwareOptionStatus.Acceptable) { var fo = new FirmwareOption { @@ -231,24 +231,31 @@ namespace BizHawk.Emulation.Common FirmwareOptions.Add(fo); - //first option is automatically ideal + // first option is automatically ideal if (FirmwareOptions.Count == 1 && fo.status == FirmwareOptionStatus.Acceptable) + { fo.status = FirmwareOptionStatus.Ideal; + } return fo; } - //adds an acceptable option for a firmware ID to the database - static FirmwareOption Option(string systemId, string id, FirmwareFile ff, FirmwareOptionStatus status = FirmwareOptionStatus.Acceptable) + // adds an acceptable option for a firmware ID to the database + private static FirmwareOption Option(string systemId, string id, FirmwareFile ff, FirmwareOptionStatus status = FirmwareOptionStatus.Acceptable) { var fo = Option(ff.hash, ff.size, systemId, id, status); - //make sure this goes in as bad - if(ff.bad) fo.status = FirmwareOptionStatus.Bad; + + // make sure this goes in as bad + if (ff.bad) + { + fo.status = FirmwareOptionStatus.Bad; + } + return fo; } - //defines a firmware file - static FirmwareFile File(string hash, long size, string recommendedName, string descr, string additionalInfo = "") + // defines a firmware file + private static FirmwareFile File(string hash, long size, string recommendedName, string descr, string additionalInfo = "") { string hashfix = hash.ToUpperInvariant(); @@ -265,8 +272,8 @@ namespace BizHawk.Emulation.Common return ff; } - //adds a defined firmware ID and one file and option - static void FirmwareAndOption(string hash, long size, string systemId, string id, string name, string descr) + // adds a defined firmware ID and one file and option + private static void FirmwareAndOption(string hash, long size, string systemId, string id, string name, string descr) { Firmware(systemId, id, descr); File(hash, size, name, descr, ""); @@ -274,9 +281,9 @@ namespace BizHawk.Emulation.Common } - public static List FirmwareRecords = new List(); - public static List FirmwareOptions = new List(); - public static List FirmwareFiles = new List(); + public static readonly List FirmwareRecords = new List(); + public static readonly List FirmwareOptions = new List(); + public static readonly List FirmwareFiles = new List(); public static Dictionary FirmwareFilesByHash = new Dictionary(); @@ -295,7 +302,7 @@ namespace BizHawk.Emulation.Common public string systemId; public string firmwareId; public string descr; - public string ConfigKey { get { return string.Format("{0}+{1}", systemId, firmwareId); } } + public string ConfigKey => $"{systemId}+{firmwareId}"; } public enum FirmwareOptionStatus @@ -311,7 +318,7 @@ namespace BizHawk.Emulation.Common public long size; public FirmwareOptionStatus status; public bool IsAcceptableOrIdeal { get { return status == FirmwareOptionStatus.Ideal || status == FirmwareOptionStatus.Acceptable; } } - public string ConfigKey { get { return string.Format("{0}+{1}", systemId, firmwareId); } } + public string ConfigKey => $"{systemId}+{firmwareId}"; } @@ -325,6 +332,5 @@ namespace BizHawk.Emulation.Common return found.FirstOrDefault(); } - - } //static class FirmwareDatabase + } // static class FirmwareDatabase } \ No newline at end of file diff --git a/BizHawk.Emulation.Common/Database/GameInfo.cs b/BizHawk.Emulation.Common/Database/GameInfo.cs index deec01cc9f..6e033df873 100644 --- a/BizHawk.Emulation.Common/Database/GameInfo.cs +++ b/BizHawk.Emulation.Common/Database/GameInfo.cs @@ -13,18 +13,20 @@ namespace BizHawk.Emulation.Common return Status == RomStatus.BadDump || Status == RomStatus.Overdump; } - public string Name; - public string System; - public string Hash; - public string Region; - public RomStatus Status = RomStatus.NotInDatabase; - public bool NotInDatabase = true; - public string FirmwareHash; - public string ForcedCore; + public string Name { get; set; } + public string System { get; set; } + public string Hash { get; set; } + public string Region { get; set; } + public RomStatus Status { get; set; } = RomStatus.NotInDatabase; + public bool NotInDatabase { get; set; } = true; + public string FirmwareHash { get; set; } + public string ForcedCore { get; private set; } - Dictionary Options = new Dictionary(); + private Dictionary Options { get; set; } = new Dictionary(); - public GameInfo() { } + public GameInfo() + { + } public GameInfo Clone() { @@ -33,27 +35,18 @@ namespace BizHawk.Emulation.Common return ret; } - public static GameInfo NullInstance + public static GameInfo NullInstance => new GameInfo { - get - { - return new GameInfo - { - Name = "Null", - System = "NULL", - Hash = "", - Region = "", - Status = RomStatus.GoodDump, - ForcedCore = "", - NotInDatabase = false - }; - } - } + Name = "Null", + System = "NULL", + Hash = string.Empty, + Region = string.Empty, + Status = RomStatus.GoodDump, + ForcedCore = string.Empty, + NotInDatabase = false + }; - public bool IsNullInstance - { - get { return System == "NULL"; } - } + public bool IsNullInstance => System == "NULL"; internal GameInfo(CompactGameInfo cgi) { @@ -82,10 +75,7 @@ namespace BizHawk.Emulation.Common Options.Remove(option); } - public bool this[string option] - { - get { return Options.ContainsKey(option); } - } + public bool this[string option] => Options.ContainsKey(option); public bool OptionPresent(string option) { @@ -95,7 +85,10 @@ namespace BizHawk.Emulation.Common public string OptionValue(string option) { if (Options.ContainsKey(option)) + { return Options[option]; + } + return null; } @@ -114,25 +107,37 @@ namespace BizHawk.Emulation.Common return int.Parse(Options[option], NumberStyles.HexNumber); } + /// + /// /// Gets a boolean value from the database + /// /// The option to look up /// The value to return if the option is invalid or doesn't exist - /// The bool value from the database if present, otherwise the given default value + /// The boolean value from the database if present, otherwise the given default value public bool GetBool(string parameter, bool defaultVal) { if (OptionPresent(parameter) && OptionValue(parameter) == "true") + { return true; - else if (OptionPresent(parameter) && OptionValue(parameter) == "false") + } + + if (OptionPresent(parameter) && OptionValue(parameter) == "false") + { return false; - else - return defaultVal; + } + + return defaultVal; } + /// + /// /// Gets an integer value from the database + /// /// The option to look up /// The value to return if the option is invalid or doesn't exist - /// The int value from the database if present, otherwise the given default value + /// The integer value from the database if present, otherwise the given default value public int GetInt(string parameter, int defaultVal) { if (OptionPresent(parameter)) + { try { return int.Parse(OptionValue(parameter)); @@ -141,24 +146,27 @@ namespace BizHawk.Emulation.Common { return defaultVal; } - else - return defaultVal; + } + + return defaultVal; } public ICollection GetOptions() { return Options.Keys; - } + public IDictionary GetOptionsDict() { return new ReadOnlyDictionary(Options); } - void ParseOptionsDictionary(string metaData) + private void ParseOptionsDictionary(string metaData) { if (string.IsNullOrEmpty(metaData)) + { return; + } var options = metaData.Split(';').Where(opt => string.IsNullOrEmpty(opt) == false).ToArray(); @@ -166,7 +174,7 @@ namespace BizHawk.Emulation.Common { var parts = opt.Split('='); var key = parts[0]; - var value = parts.Length > 1 ? parts[1] : ""; + var value = parts.Length > 1 ? parts[1] : string.Empty; Options[key] = value; } } diff --git a/BizHawk.Emulation.Common/Interfaces/Services/ISoundProvider.cs b/BizHawk.Emulation.Common/Interfaces/Services/ISoundProvider.cs index baff2144c9..0cae9fb3ee 100644 --- a/BizHawk.Emulation.Common/Interfaces/Services/ISoundProvider.cs +++ b/BizHawk.Emulation.Common/Interfaces/Services/ISoundProvider.cs @@ -1,6 +1,6 @@ namespace BizHawk.Emulation.Common { - public enum SyncSoundMode { Sync, Async }; + public enum SyncSoundMode { Sync, Async } /// /// This service provides the ability to output sound from the client, diff --git a/BizHawk.Emulation.Common/Interfaces/Services/ITraceable.cs b/BizHawk.Emulation.Common/Interfaces/Services/ITraceable.cs index 371bc08668..0cac8797f7 100644 --- a/BizHawk.Emulation.Common/Interfaces/Services/ITraceable.cs +++ b/BizHawk.Emulation.Common/Interfaces/Services/ITraceable.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace BizHawk.Emulation.Common +namespace BizHawk.Emulation.Common { public interface ITraceSink { @@ -32,7 +30,7 @@ namespace BizHawk.Emulation.Common //void Put(TraceInfo content); - //that's right, we can only have one sink. + // that's right, we can only have one sink. //a sink can route to two other sinks if it has to, though ITraceSink Sink { get; set; } diff --git a/BizHawk.Emulation.Common/Sound/Utilities/Equalizer.cs b/BizHawk.Emulation.Common/Sound/Utilities/Equalizer.cs index 8683c65bab..651101c24a 100644 --- a/BizHawk.Emulation.Common/Sound/Utilities/Equalizer.cs +++ b/BizHawk.Emulation.Common/Sound/Utilities/Equalizer.cs @@ -6,30 +6,31 @@ namespace BizHawk.Emulation.Common { public sealed class Equalizer { - double lowFilter; - double lowFilterPole0; - double lowFilterPole1; - double lowFilterPole2; - double lowFilterPole3; + private double lowFilter; + private double lowFilterPole0; + private double lowFilterPole1; + private double lowFilterPole2; + private double lowFilterPole3; - double highFilter; - double highFilterPole0; - double highFilterPole1; - double highFilterPole2; - double highFilterPole3; + private double highFilter; + private double highFilterPole0; + private double highFilterPole1; + private double highFilterPole2; + private double highFilterPole3; - double sampleDataMinus1; - double sampleDataMinus2; - double sampleDataMinus3; + private double sampleDataMinus1; + private double sampleDataMinus2; + private double sampleDataMinus3; - double lowGain; - double midGain; - double highGain; + private double lowGain; + private double midGain; + private double highGain; - const double sampleRate = 44100.0; - const double verySmallAmount = (1.0 / 4294967295.0); + private const double sampleRate = 44100.0; + private const double verySmallAmount = 1.0 / 4294967295.0; + + private double lowfreq; - double lowfreq; public double LowFreqCutoff { get { return lowfreq; } @@ -40,7 +41,7 @@ namespace BizHawk.Emulation.Common } } - double highfreq; + private double highfreq; public double HighFreqCutoff { get { return highfreq; } @@ -89,7 +90,9 @@ namespace BizHawk.Emulation.Common public void Equalize(short[] samples) { for (int i = 0; i < samples.Length; i++) + { samples[i] = EqualizeSample(samples[i]); + } } } } \ No newline at end of file diff --git a/BizHawk.Emulation.Common/Sound/Utilities/Metaspu.cs b/BizHawk.Emulation.Common/Sound/Utilities/Metaspu.cs index ab631dbece..a9965f0b4f 100644 --- a/BizHawk.Emulation.Common/Sound/Utilities/Metaspu.cs +++ b/BizHawk.Emulation.Common/Sound/Utilities/Metaspu.cs @@ -64,15 +64,15 @@ namespace BizHawk.Emulation.Common // is to provide exact amounts of output samples, // even when the input provided varies.... int output_samples(short[] buf, int samples_requested); - }; + } public enum ESynchMethod { - ESynchMethod_N, //nitsuja's - ESynchMethod_Z, //zero's + ESynchMethod_N, // nitsuja's + ESynchMethod_Z, // zero's //ESynchMethod_P, //PCSX2 spu2-x //ohno! not available yet in c# ESynchMethod_V // vecna - }; + } public static class Metaspu { @@ -136,7 +136,9 @@ namespace BizHawk.Emulation.Common if (!mixqueue_go) { if (adjustobuf.size > 200) + { mixqueue_go = true; + } } else { @@ -147,6 +149,7 @@ namespace BizHawk.Emulation.Common mixqueue_go = false; break; } + done++; short left, right; adjustobuf.dequeue(out left, out right); @@ -159,7 +162,8 @@ namespace BizHawk.Emulation.Common } private readonly Adjustobuf adjustobuf; - class Adjustobuf + + private class Adjustobuf { public Adjustobuf(int _minLatency, int _maxLatency) { @@ -168,12 +172,12 @@ namespace BizHawk.Emulation.Common clear(); } - float rate, cursor; - int minLatency, targetLatency, maxLatency; - Queue buffer = new Queue(); - Queue statsHistory = new Queue(); + private float rate, cursor; + private int minLatency, targetLatency, maxLatency; + private readonly Queue buffer = new Queue(); + private readonly Queue statsHistory = new Queue(); public int size = 0; - short[] curr = new short[2]; + private readonly short[] curr = new short[2]; public void clear() { @@ -195,11 +199,11 @@ namespace BizHawk.Emulation.Common size++; } - long rollingTotalSize; + private long rollingTotalSize; - uint kAverageSize; + private uint kAverageSize; - void addStatistic() + private void addStatistic() { statsHistory.Enqueue(size); rollingTotalSize += size; @@ -227,15 +231,17 @@ namespace BizHawk.Emulation.Common } } - - } public void dequeue(out short left, out short right) { left = right = 0; addStatistic(); - if (size == 0) { return; } + if (size == 0) + { + return; + } + cursor += rate; while (cursor > 1.0f) { @@ -254,22 +260,29 @@ namespace BizHawk.Emulation.Common } } - class NitsujaSynchronizer : ISynchronizingAudioBuffer + internal class NitsujaSynchronizer : ISynchronizingAudioBuffer { - struct ssamp + private struct ssamp { public short l, r; - public ssamp(short ll, short rr) { l = ll; r = rr; } - }; - readonly List sampleQueue = new List(); + public ssamp(short ll, short rr) + { + l = ll; r = rr; + } + } + + private readonly List sampleQueue = new List(); // returns values going between 0 and y-1 in a saw wave pattern, based on x - static int pingpong(int x, int y) + private static int pingpong(int x, int y) { x %= 2 * y; if (x >= y) - x = 2 * y - x - 1; + { + x = (2 * y) - x - 1; + } + return x; // in case we want to switch to odd buffer sizes for more sharpness @@ -279,12 +292,17 @@ namespace BizHawk.Emulation.Common //return x; } - static ssamp crossfade(ssamp lhs, ssamp rhs, int cur, int start, int end) + private static ssamp crossfade(ssamp lhs, ssamp rhs, int cur, int start, int end) { if (cur <= start) + { return lhs; + } + if (cur >= end) + { return rhs; + } // in case we want sine wave interpolation instead of linear here //float ang = 3.14159f * (float)(cur - start) / (float)(end - start); @@ -305,28 +323,38 @@ namespace BizHawk.Emulation.Common sampleQueue.Clear(); } - static void emit_sample(short[] outbuf, ref int cursor, ssamp sample) + private static void emit_sample(short[] outbuf, ref int cursor, ssamp sample) { outbuf[cursor++] = sample.l; outbuf[cursor++] = sample.r; } - static void emit_samples(short[] outbuf, ref int outcursor, ssamp[] samplebuf, int incursor, int samples) + private static void emit_samples(short[] outbuf, ref int outcursor, ssamp[] samplebuf, int incursor, int samples) { for (int i = 0; i < samples; i++) + { emit_sample(outbuf, ref outcursor, samplebuf[i + incursor]); + } } - static short abs(short value) + private static short abs(short value) { - if (value < 0) return (short)-value; - else return value; + if (value < 0) + { + return (short)-value; + } + + return value; } - static int abs(int value) + private static int abs(int value) { - if (value < 0) return -value; - else return value; + if (value < 0) + { + return -value; + } + + return value; } public void enqueue_samples(short[] buf, int samples_provided) @@ -420,6 +448,7 @@ namespace BizHawk.Emulation.Common beststart = i; } } + for (int i = queued - 3; i > queued - 3 - 128; i -= 2) { int diff = abs(sampleQueue[i].l - sampleQueue[i + 1].l) + abs(sampleQueue[i].r - sampleQueue[i + 1].r); @@ -435,16 +464,20 @@ namespace BizHawk.Emulation.Common int oksize = queued; while (oksize + queued * 2 + beststart + extraAtEnd <= samples_requested) + { oksize += queued * 2; + } + audiosize = oksize; for (int x = 0; x < beststart; x++) { emit_sample(buf, ref bufcursor, sampleQueue[x]); } - //sampleQueue.erase(sampleQueue.begin(), sampleQueue.begin() + beststart); + + // sampleQueue.erase(sampleQueue.begin(), sampleQueue.begin() + beststart); sampleQueue.RemoveRange(0, beststart); - //zero 08-nov-2010: did i do this right? + // zero 08-nov-2010: did i do this right? } @@ -456,7 +489,6 @@ namespace BizHawk.Emulation.Common // midpointXOffset = min(something,somethingElse); // but it's a little difficult to work it out exactly // so here's a stupid search for the value for now: - int prevA = 999999; int midpointXOffset = queued / 2; while (true) @@ -494,9 +526,14 @@ namespace BizHawk.Emulation.Common int dyMidLeft = (leftMidpointY < midpointY) ? 1 : -1; int dyMidRight = (rightMidpointY > midpointY) ? 1 : -1; for (int x = leftMidpointX; x < midpointX; x++, y += dyMidLeft) + { emit_sample(buf, ref bufcursor, sampleQueue[y]); + } + for (int x = midpointX; x < rightMidpointX; x++, y += dyMidRight) + { emit_sample(buf, ref bufcursor, sampleQueue[y]); + } // output the end of the queued sound (section "C") for (int x = rightMidpointX; x < audiosize; x++) @@ -510,13 +547,15 @@ namespace BizHawk.Emulation.Common int i = queued + x; emit_sample(buf, ref bufcursor, sampleQueue[i]); } + queued += extraAtEnd; audiosize += beststart + extraAtEnd; - } //end else + } // end else - //sampleQueue.erase(sampleQueue.begin(), sampleQueue.begin() + queued); + // sampleQueue.erase(sampleQueue.begin(), sampleQueue.begin() + queued); sampleQueue.RemoveRange(0, queued); - //zero 08-nov-2010: did i do this right? + + // zero 08-nov-2010: did i do this right? return audiosize; } else @@ -529,38 +568,38 @@ namespace BizHawk.Emulation.Common // and entering the "slow motion speed" branch above. // but that's ok! because all of these branches sound similar enough that we can get away with it. // so the two cases actually complement each other. - if (audiosize >= queued) { emit_samples(buf, ref bufcursor, sampleQueue.ToArray(), 0, queued); - //sampleQueue.erase(sampleQueue.begin(), sampleQueue.begin() + queued); + + // sampleQueue.erase(sampleQueue.begin(), sampleQueue.begin() + queued); sampleQueue.RemoveRange(0, queued); - //zero 08-nov-2010: did i do this right? + + // zero 08-nov-2010: did i do this right? return queued; } else { emit_samples(buf, ref bufcursor, sampleQueue.ToArray(), 0, audiosize); - //sampleQueue.erase(sampleQueue.begin(), sampleQueue.begin()+audiosize); + + // sampleQueue.erase(sampleQueue.begin(), sampleQueue.begin()+audiosize); sampleQueue.RemoveRange(0, audiosize); - //zero 08-nov-2010: did i do this right? + + // zero 08-nov-2010: did i do this right? return audiosize; } - } //end normal speed + } // end normal speed - } //end if there is any work to do + } // end if there is any work to do else { return 0; } + } // output_samples + } // NitsujaSynchronizer - } //output_samples - - - }; //NitsujaSynchronizer - - class VecnaSynchronizer : ISynchronizingAudioBuffer + internal class VecnaSynchronizer : ISynchronizingAudioBuffer { // vecna's attempt at a fully synchronous sound provider. // It's similar in philosophy to my "BufferedAsync" provider, but BufferedAsync is not @@ -580,7 +619,7 @@ namespace BizHawk.Emulation.Common // Since it has done this, it will go ahead and generate some excess silence in order // to restock its excess buffer. - struct Sample + private struct Sample { public short left, right; public Sample(short l, short r) @@ -590,11 +629,11 @@ namespace BizHawk.Emulation.Common } } - Queue buffer; - Sample[] resampleBuffer; - - const int SamplesInOneFrame = 735; - const int MaxExcessSamples = 2048; + private Queue buffer; + private Sample[] resampleBuffer; + + private const int SamplesInOneFrame = 735; + private const int MaxExcessSamples = 2048; public VecnaSynchronizer() { @@ -603,7 +642,9 @@ namespace BizHawk.Emulation.Common // Give us a little buffer wiggle-room for (int i = 0; i < 367; i++) + { buffer.Enqueue(new Sample(0, 0)); + } } public void enqueue_samples(short[] buf, int samples_provided) @@ -624,6 +665,7 @@ namespace BizHawk.Emulation.Common // if buffer is overfull, dequeue old samples to make room for new samples. buffer.Dequeue(); } + buffer.Enqueue(new Sample(left, right)); } @@ -641,7 +683,6 @@ namespace BizHawk.Emulation.Common { // if we're within 75% of target, then I guess we suck it up and resample. // we sample in a goofy way, we could probably do it a bit smarter, if we cared more. - int samples_available = buffer.Count; for (int i = 0; buffer.Count > 0; i++) resampleBuffer[i] = buffer.Dequeue(); @@ -672,6 +713,7 @@ namespace BizHawk.Emulation.Common buf[index++] += sample.right; } } + return samples_requested; } } diff --git a/BizHawk.Emulation.Common/Sound/Utilities/SpeexResampler.cs b/BizHawk.Emulation.Common/Sound/Utilities/SpeexResampler.cs index c1a5974721..9f59a2352b 100644 --- a/BizHawk.Emulation.Common/Sound/Utilities/SpeexResampler.cs +++ b/BizHawk.Emulation.Common/Sound/Utilities/SpeexResampler.cs @@ -8,7 +8,7 @@ namespace BizHawk.Emulation.Common /// public class SpeexResampler : IDisposable, ISoundProvider { - static class LibSpeexDSP + private static class LibSpeexDSP { public const int QUALITY_MAX = 10; public const int QUALITY_MIN = 0; @@ -24,7 +24,7 @@ namespace BizHawk.Emulation.Common INVALID_ARG = 3, PTR_OVERLAP = 4, MAX_ERROR - }; + } /// /// Create a new resampler with integer input and output rates. @@ -281,7 +281,7 @@ namespace BizHawk.Emulation.Common /// throw an exception based on error state /// /// - static void CheckError(LibSpeexDSP.RESAMPLER_ERR e) + private static void CheckError(LibSpeexDSP.RESAMPLER_ERR e) { switch (e) { @@ -299,7 +299,6 @@ namespace BizHawk.Emulation.Common } /// - /// /// /// 0 to 10 /// numerator of srate change ratio (inrate / outrate) @@ -311,13 +310,17 @@ namespace BizHawk.Emulation.Common public SpeexResampler(int quality, uint rationum, uint ratioden, uint sratein, uint srateout, Action drainer = null, ISoundProvider input = null) { if (drainer != null && input != null) + { throw new ArgumentException("Can't autofetch without being an ISyncSoundProvider?"); + } LibSpeexDSP.RESAMPLER_ERR err = LibSpeexDSP.RESAMPLER_ERR.SUCCESS; st = LibSpeexDSP.speex_resampler_init_frac(2, rationum, ratioden, sratein, srateout, quality, ref err); if (st == IntPtr.Zero) + { throw new Exception("LibSpeexDSP returned null!"); + } CheckError(err); @@ -349,7 +352,9 @@ namespace BizHawk.Emulation.Common inbuf[inbufpos++] = right; if (inbufpos == inbuf.Length) + { Flush(); + } } /// @@ -369,7 +374,9 @@ namespace BizHawk.Emulation.Common numused += shortstocopy / 2; if (inbufpos == inbuf.Length) + { Flush(); + } } } @@ -386,9 +393,11 @@ namespace BizHawk.Emulation.Common LibSpeexDSP.speex_resampler_process_interleaved_int(st, inbuf, ref inal, outbuf, ref outal); // reset inbuf - if (inal != inbufpos / 2) + { throw new Exception("Speexresampler didn't eat the whole array?"); + } + inbufpos = 0; //Buffer.BlockCopy(inbuf, (int)inal * 2 * sizeof(short), inbuf, 0, inbufpos - (int)inal * 2); @@ -413,7 +422,7 @@ namespace BizHawk.Emulation.Common Dispose(); } - void InternalDrain(short[] buf, int nsamp) + private void InternalDrain(short[] buf, int nsamp) { if (outbuf2pos + nsamp * 2 > outbuf2.Length) { @@ -421,6 +430,7 @@ namespace BizHawk.Emulation.Common Buffer.BlockCopy(outbuf2, 0, newbuf, 0, outbuf2pos * sizeof(short)); outbuf2 = newbuf; } + Buffer.BlockCopy(buf, 0, outbuf2, outbuf2pos * sizeof(short), nsamp * 2 * sizeof(short)); outbuf2pos += nsamp * 2; } @@ -434,6 +444,7 @@ namespace BizHawk.Emulation.Common input.GetSamplesSync(out sampin, out nsampin); EnqueueSamples(sampin, nsampin); } + Flush(); nsamp = outbuf2pos / 2; samples = outbuf2; @@ -445,15 +456,9 @@ namespace BizHawk.Emulation.Common outbuf2pos = 0; } - public bool CanProvideAsync - { - get { return false; } - } + public bool CanProvideAsync => false; - public SyncSoundMode SyncMode - { - get { return SyncSoundMode.Sync; } - } + public SyncSoundMode SyncMode => SyncSoundMode.Sync; public void GetSamplesAsync(short[] samples) { diff --git a/BizHawk.Emulation.Common/Sound/Utilities/Waves.cs b/BizHawk.Emulation.Common/Sound/Utilities/Waves.cs index 89991ebf1c..78ff9166dd 100644 --- a/BizHawk.Emulation.Common/Sound/Utilities/Waves.cs +++ b/BizHawk.Emulation.Common/Sound/Utilities/Waves.cs @@ -9,15 +9,15 @@ public static void InitWaves() { - SquareWave = new short[] + SquareWave = new short[] { - -32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768, + -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -32768, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767 }; ImperfectSquareWave = new short[] { - -32768,-30145,-27852,-26213,-24902,-23592,-22282,-20971,-19988,-19005,-18350,-17694,-17366,-17039,-16711,-16711, + -32768, -30145, -27852, -26213, -24902, -23592, -22282, -20971, -19988, -19005, -18350, -17694, -17366, -17039, -16711, -16711, 32767, 30145, 27852, 26213, 24902, 23592, 22282, 20971, 19988, 19005, 18350, 17694, 17366, 17039, 16711, 16711 }; @@ -29,7 +29,9 @@ { int r = rnd.Next(); if ((r & 1) > 0) + { NoiseWave[i] = short.MaxValue; + } } /*TriangleWave = new short[512]; diff --git a/BizHawk.Emulation.Common/TextState.cs b/BizHawk.Emulation.Common/TextState.cs index 26ed2a89e9..b661794309 100644 --- a/BizHawk.Emulation.Common/TextState.cs +++ b/BizHawk.Emulation.Common/TextState.cs @@ -18,8 +18,8 @@ namespace BizHawk.Emulation.Common public class Node { - public Dictionary Data = new Dictionary(); - public Dictionary Objects = new Dictionary(); + public readonly Dictionary Data = new Dictionary(); + public readonly Dictionary Objects = new Dictionary(); // methods named "ShouldSerialize*" are detected and dynamically invoked by JSON.NET // if they return false during serialization, the field/prop is omitted from the created json @@ -34,15 +34,15 @@ namespace BizHawk.Emulation.Common } } - public Node Root = new Node(); + public readonly Node Root = new Node(); [JsonIgnore] Stack Nodes; [JsonIgnore] - Node Current { get { return Nodes.Peek(); } } + private Node Current => Nodes.Peek(); - public void Prepare() + public void Prepare() { Nodes = new Stack(); Nodes.Push(Root); @@ -59,7 +59,10 @@ namespace BizHawk.Emulation.Common { byte[] d = Current.Data[name]; if (length != d.Length) + { throw new InvalidOperationException(); + } + Marshal.Copy(d, 0, data, length); } @@ -87,6 +90,7 @@ namespace BizHawk.Emulation.Common next = new Node(); Current.Objects.Add(name, next); } + Nodes.Push(next); } @@ -94,7 +98,9 @@ namespace BizHawk.Emulation.Common { Node last = Nodes.Pop(); if (Current.Objects[name] != last) + { throw new InvalidOperationException(); + } } // other data besides the core diff --git a/BizHawk.sln.DotSettings b/BizHawk.sln.DotSettings index 33870e01ad..356266de69 100644 --- a/BizHawk.sln.DotSettings +++ b/BizHawk.sln.DotSettings @@ -1,4 +1,5 @@  + DO_NOT_SHOW DO_NOT_SHOW DO_NOT_SHOW DO_NOT_SHOW diff --git a/Version/VersionInfo.cs b/Version/VersionInfo.cs index ba7515f321..30e8f2d39e 100644 --- a/Version/VersionInfo.cs +++ b/Version/VersionInfo.cs @@ -1,4 +1,3 @@ -using System; using System.IO; static class VersionInfo @@ -17,9 +16,9 @@ static class VersionInfo static VersionInfo() { - string path = Path.Combine(GetExeDirectoryAbsolute(),"dll"); - path = Path.Combine(path,"custombuild.txt"); - if(File.Exists(path)) + string path = Path.Combine(GetExeDirectoryAbsolute(), "dll"); + path = Path.Combine(path, "custombuild.txt"); + if (File.Exists(path)) { var lines = File.ReadAllLines(path); if (lines.Length > 0)