From cdb0802230c2c7d0eb22d5bb4c994c9c5d6b3115 Mon Sep 17 00:00:00 2001 From: adelikat Date: Tue, 9 May 2017 07:21:38 -0500 Subject: [PATCH] Misc Client.Common cleanups --- BizHawk.Client.Common/BinarySaveStates.cs | 19 ++-- .../BitmapBufferVideoProvider.cs | 98 ++++++++----------- BizHawk.Client.Common/ControllerBinding.cs | 25 +---- BizHawk.Client.Common/CoreFileProvider.cs | 39 ++++---- BizHawk.Client.Common/Extensions.cs | 8 ++ BizHawk.Client.Common/FirmwareManager.cs | 2 +- BizHawk.Client.Common/IonicZipWriter.cs | 36 +++---- BizHawk.Client.Common/PathManager.cs | 14 +-- BizHawk.Client.Common/RomLoader.cs | 5 +- 9 files changed, 114 insertions(+), 132 deletions(-) diff --git a/BizHawk.Client.Common/BinarySaveStates.cs b/BizHawk.Client.Common/BinarySaveStates.cs index fa55374fc4..7b8a3a7ab4 100644 --- a/BizHawk.Client.Common/BinarySaveStates.cs +++ b/BizHawk.Client.Common/BinarySaveStates.cs @@ -72,12 +72,13 @@ namespace BizHawk.Client.Common [AttributeUsage(AttributeTargets.Property)] private class NameAttribute : Attribute { - public string Name { get; private set; } - public string Ext { get; private set; } + public string Name { get; } + public string Ext { get; } public NameAttribute(string name) { Name = name; } + public NameAttribute(string name, string ext) { Name = name; @@ -85,8 +86,8 @@ namespace BizHawk.Client.Common } } - public virtual string ReadName { get { return Name; } } - public virtual string WriteName { get { return Ext != null ? Name + '.' + Ext : Name; } } + public virtual string ReadName => Name; + public virtual string WriteName => Ext != null ? Name + '.' + Ext : Name; public string Name { get; protected set; } public string Ext { get; protected set; } @@ -97,7 +98,9 @@ namespace BizHawk.Client.Common Ext = ext; } - protected BinaryStateLump() { } + protected BinaryStateLump() + { + } static BinaryStateLump() { @@ -115,7 +118,7 @@ namespace BizHawk.Client.Common /// public class IndexedStateLump : BinaryStateLump { - private BinaryStateLump _root; + private readonly BinaryStateLump _root; private int _idx; public IndexedStateLump(BinaryStateLump root) { @@ -200,7 +203,7 @@ namespace BizHawk.Client.Common } } - private static byte[] zipheader = new byte[] { 0x50, 0x4b, 0x03, 0x04 }; + private static byte[] zipheader = { 0x50, 0x4b, 0x03, 0x04 }; public static BinaryStateLoader LoadAndDetect(string filename, bool isMovieLoad = false) { var ret = new BinaryStateLoader(); @@ -210,7 +213,9 @@ namespace BizHawk.Client.Common byte[] data = new byte[4]; fs.Read(data, 0, 4); if (!data.SequenceEqual(zipheader)) + { return null; + } } try diff --git a/BizHawk.Client.Common/BitmapBufferVideoProvider.cs b/BizHawk.Client.Common/BitmapBufferVideoProvider.cs index 2f8bc1ae79..686444bce4 100644 --- a/BizHawk.Client.Common/BitmapBufferVideoProvider.cs +++ b/BizHawk.Client.Common/BitmapBufferVideoProvider.cs @@ -1,58 +1,40 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Drawing; - -using BizHawk.Emulation.Common; -using BizHawk.Bizware.BizwareGL; - -namespace BizHawk.Client.Common -{ - public class BitmapBufferVideoProvider : IVideoProvider, IDisposable - { - BitmapBuffer bb; - public BitmapBufferVideoProvider(BitmapBuffer bb) - { - this.bb = bb; - } - - public void Dispose() - { - if (bb != null) bb.Dispose(); - bb = null; - } - - public int[] GetVideoBuffer() - { - return bb.Pixels; - } - - public int VirtualWidth - { - get { return bb.Width; } - } - - public int VirtualHeight - { - get { return bb.Height; } - } - - public int BufferWidth - { - get { return bb.Width; } - } - - public int BufferHeight - { - get { return bb.Height; } - } - - public int BackgroundColor - { - get { return 0; } - } - +using System; + +using BizHawk.Emulation.Common; +using BizHawk.Bizware.BizwareGL; + +namespace BizHawk.Client.Common +{ + public class BitmapBufferVideoProvider : IVideoProvider, IDisposable + { + private BitmapBuffer _bb; + + public BitmapBufferVideoProvider(BitmapBuffer bb) + { + _bb = bb; + } + + public void Dispose() + { + _bb?.Dispose(); + _bb = null; + } + + public int[] GetVideoBuffer() + { + return _bb.Pixels; + } + + public int VirtualWidth => _bb.Width; + + public int VirtualHeight => _bb.Height; + + public int BufferWidth => _bb.Width; + + public int BufferHeight => _bb.Height; + + public int BackgroundColor => 0; + public int VsyncNumerator { get { throw new InvalidOperationException(); } @@ -61,6 +43,6 @@ namespace BizHawk.Client.Common public int VsyncDenominator { get { throw new InvalidOperationException(); } - } - } -} + } + } +} diff --git a/BizHawk.Client.Common/ControllerBinding.cs b/BizHawk.Client.Common/ControllerBinding.cs index 442b826399..5aa86244b9 100644 --- a/BizHawk.Client.Common/ControllerBinding.cs +++ b/BizHawk.Client.Common/ControllerBinding.cs @@ -42,10 +42,7 @@ namespace BizHawk.Client.Common /// don't do this public void ForceType(ControllerDefinition newtype) { _type = newtype; } - public bool this[string button] - { - get { return IsPressed(button); } - } + public bool this[string button] => IsPressed(button); public bool AnyPressed { @@ -63,7 +60,7 @@ namespace BizHawk.Client.Common // Looks for bindings which are activated by the supplied physical button. public List SearchBindings(string button) { - return (from kvp in _bindings from bound_button in kvp.Value where bound_button == button select kvp.Key).ToList(); + return (from kvp in _bindings from boundButton in kvp.Value where boundButton == button select kvp.Key).ToList(); } // Searches bindings for the controller and returns true if this binding is mapped somewhere in this controller @@ -235,13 +232,7 @@ namespace BizHawk.Client.Common return (from key in _bindings from binding in key.Value select new KeyValuePair(binding, key.Key)).ToList(); } - public List PressedButtons - { - get - { - return (from button in _buttons where button.Value select button.Key).ToList(); - } - } + public List PressedButtons => (from button in _buttons where button.Value select button.Key).ToList(); } public class AutofireController : IController @@ -267,7 +258,7 @@ namespace BizHawk.Client.Common public int On { get; set; } public int Off { get; set; } - public ControllerDefinition Definition { get { return _type; } } + public ControllerDefinition Definition => _type; public bool IsPressed(string button) { @@ -364,12 +355,6 @@ namespace BizHawk.Client.Common } } - public List PressedButtons - { - get - { - return (from button in _buttons where button.Value select button.Key).ToList(); - } - } + public List PressedButtons => (from button in _buttons where button.Value select button.Key).ToList(); } } \ No newline at end of file diff --git a/BizHawk.Client.Common/CoreFileProvider.cs b/BizHawk.Client.Common/CoreFileProvider.cs index 7e46e32003..dce89b5e2c 100644 --- a/BizHawk.Client.Common/CoreFileProvider.cs +++ b/BizHawk.Client.Common/CoreFileProvider.cs @@ -19,7 +19,7 @@ namespace BizHawk.Client.Common public string PathSubfile(string fname) { - return Path.Combine(SubfileDirectory ?? String.Empty, fname); + return Path.Combine(SubfileDirectory ?? string.Empty, fname); } public string DllPath() @@ -54,22 +54,20 @@ namespace BizHawk.Client.Common { if (required) { - var fullmsg = String.Format( - "Couldn't find required firmware \"{0}:{1}\". This is fatal{2}", sysID, firmwareID, msg != null ? ": " + msg : "."); + var fullmsg = $"Couldn't find required firmware \"{sysID}:{firmwareID}\". This is fatal{(msg != null ? ": " + msg : ".")}"; throw new MissingFirmwareException(fullmsg); } if (msg != null) { - var fullmsg = String.Format( - "Couldn't find firmware \"{0}:{1}\". Will attempt to continue: {2}", sysID, firmwareID, msg); + var fullmsg = $"Couldn't find firmware \"{sysID}:{firmwareID}\". Will attempt to continue: {msg}"; _showWarning(fullmsg); } } - public string GetFirmwarePath(string sysID, string firmwareID, bool required, string msg = null) + public string GetFirmwarePath(string sysId, string firmwareId, bool required, string msg = null) { - var path = FirmwareManager.Request(sysID, firmwareID); + var path = FirmwareManager.Request(sysId, firmwareId); if (path != null && !File.Exists(path)) { path = null; @@ -77,44 +75,46 @@ namespace BizHawk.Client.Common if (path == null) { - FirmwareWarn(sysID, firmwareID, required, msg); + FirmwareWarn(sysId, firmwareId, required, msg); } return path; } - private byte[] GetFirmwareWithPath(string sysID, string firmwareID, bool required, string msg, out string path) + private byte[] GetFirmwareWithPath(string sysId, string firmwareId, bool required, string msg, out string path) { byte[] ret = null; - var path_ = GetFirmwarePath(sysID, firmwareID, required, msg); + var path_ = GetFirmwarePath(sysId, firmwareId, required, msg); if (path_ != null && File.Exists(path_)) { try { ret = File.ReadAllBytes(path_); } - catch (IOException) { } + catch (IOException) + { + } } if (ret == null && path_ != null) { - FirmwareWarn(sysID, firmwareID, required, msg); + FirmwareWarn(sysId, firmwareId, required, msg); } path = path_; return ret; } - public byte[] GetFirmware(string sysID, string firmwareID, bool required, string msg = null) + public byte[] GetFirmware(string sysId, string firmwareId, bool required, string msg = null) { string unused; - return GetFirmwareWithPath(sysID, firmwareID, required, msg, out unused); + return GetFirmwareWithPath(sysId, firmwareId, required, msg, out unused); } - public byte[] GetFirmwareWithGameInfo(string sysID, string firmwareID, bool required, out GameInfo gi, string msg = null) + public byte[] GetFirmwareWithGameInfo(string sysId, string firmwareId, bool required, out GameInfo gi, string msg = null) { string path; - byte[] ret = GetFirmwareWithPath(sysID, firmwareID, required, msg, out path); + byte[] ret = GetFirmwareWithPath(sysId, firmwareId, required, msg, out path); if (ret != null && path != null) { gi = Database.GetGameInfo(ret, path); @@ -123,6 +123,7 @@ namespace BizHawk.Client.Common { gi = null; } + return ret; } @@ -133,9 +134,11 @@ namespace BizHawk.Client.Common { string superhack = null; if (target.CoreFileProvider != null && target.CoreFileProvider is CoreFileProvider) + { superhack = ((CoreFileProvider)target.CoreFileProvider ).SubfileDirectory; - var cfp = new CoreFileProvider(target.ShowMessage); - cfp.SubfileDirectory = superhack; + } + + var cfp = new CoreFileProvider(target.ShowMessage) { SubfileDirectory = superhack }; target.CoreFileProvider = cfp; cfp.FirmwareManager = Global.FirmwareManager; } diff --git a/BizHawk.Client.Common/Extensions.cs b/BizHawk.Client.Common/Extensions.cs index 02fae5f4af..8385f19d7b 100644 --- a/BizHawk.Client.Common/Extensions.cs +++ b/BizHawk.Client.Common/Extensions.cs @@ -11,11 +11,19 @@ namespace BizHawk.Client.Common public static IGuiRenderer CreateRenderer(this IGL gl) { if (gl is IGL_TK) + { return new GuiRenderer(gl); + } + if (gl is IGL_SlimDX9) + { return new GuiRenderer(gl); + } + if (gl is IGL_GdiPlus) + { return new GDIPlusGuiRenderer((IGL_GdiPlus)gl); + } throw new NotSupportedException(); } diff --git a/BizHawk.Client.Common/FirmwareManager.cs b/BizHawk.Client.Common/FirmwareManager.cs index 87dee28277..1306b35e04 100644 --- a/BizHawk.Client.Common/FirmwareManager.cs +++ b/BizHawk.Client.Common/FirmwareManager.cs @@ -45,7 +45,6 @@ namespace BizHawk.Client.Common RecentlyServed = new List(); } - public ResolutionInfo Resolve(string sysId, string firmwareId) { return Resolve(FirmwareDatabase.LookupFirmwareRecord(sysId, firmwareId)); @@ -68,6 +67,7 @@ namespace BizHawk.Client.Common { DoScanAndResolve(); } + first = false; goto RETRY; } diff --git a/BizHawk.Client.Common/IonicZipWriter.cs b/BizHawk.Client.Common/IonicZipWriter.cs index 3e10be3a00..a858be48f1 100644 --- a/BizHawk.Client.Common/IonicZipWriter.cs +++ b/BizHawk.Client.Common/IonicZipWriter.cs @@ -7,42 +7,36 @@ namespace BizHawk.Client.Common { public class IonicZipWriter : IZipWriter { - private ZipOutputStream z; - private int level; + private readonly int _level; + private ZipOutputStream _zipOutputStream; public IonicZipWriter(string path, int compressionlevel) { - level = compressionlevel; - z = new ZipOutputStream(path) + _level = compressionlevel; + _zipOutputStream = new ZipOutputStream(path) { EnableZip64 = Zip64Option.Never, - CompressionLevel = (Ionic.Zlib.CompressionLevel)level + CompressionLevel = (Ionic.Zlib.CompressionLevel)_level, + CompressionMethod = CompressionMethod.Deflate }; - z.CompressionMethod = CompressionMethod.Deflate; - } + } public void WriteItem(string name, Action callback) { - var e = z.PutNextEntry(name); - if (level == 0) - { - e.CompressionMethod = CompressionMethod.None; - } - else - { - e.CompressionMethod = CompressionMethod.Deflate; - } + var e = _zipOutputStream.PutNextEntry(name); + e.CompressionMethod = _level == 0 + ? CompressionMethod.None + : CompressionMethod.Deflate; - callback(z); - // there is no CloseEntry() call + callback(_zipOutputStream); // there is no CloseEntry() call } public void Dispose() { - if (z != null) + if (_zipOutputStream != null) { - z.Dispose(); - z = null; + _zipOutputStream.Dispose(); + _zipOutputStream = null; } } } diff --git a/BizHawk.Client.Common/PathManager.cs b/BizHawk.Client.Common/PathManager.cs index 1f142ca82a..1dcef23ed4 100644 --- a/BizHawk.Client.Common/PathManager.cs +++ b/BizHawk.Client.Common/PathManager.cs @@ -222,14 +222,14 @@ namespace BizHawk.Client.Common return false; } - public static string GetRomsPath(string sysID) + public static string GetRomsPath(string sysId) { if (Global.Config.UseRecentForROMs) { return Environment.SpecialFolder.Recent.ToString(); } - var path = Global.Config.PathEntries[sysID, "ROM"]; + var path = Global.Config.PathEntries[sysId, "ROM"]; if (path == null || !PathIsSet(path.Path)) { @@ -241,7 +241,7 @@ namespace BizHawk.Client.Common } } - return MakeAbsolutePath(path.Path, sysID); + return MakeAbsolutePath(path.Path, sysId); } public static string RemoveInvalidFileSystemChars(string name) @@ -297,7 +297,10 @@ namespace BizHawk.Client.Common // hijinx here to get the core name out of the game name var name = FilesystemSafeName(game); name = Path.GetDirectoryName(name); - if (name == string.Empty) name = FilesystemSafeName(game); + if (name == string.Empty) + { + name = FilesystemSafeName(game); + } if (Global.MovieSession.Movie.IsActive) { @@ -310,7 +313,6 @@ namespace BizHawk.Client.Common return Path.Combine(MakeAbsolutePath(pathEntry.Path, game.System), name); } - public static string RetroSystemPath(GameInfo game) { // hijinx here to get the core name out of the game name @@ -385,7 +387,7 @@ namespace BizHawk.Client.Common public static string GetPathType(string system, string type) { - var path = PathManager.GetPathEntryWithFallback(type, system).Path; + var path = GetPathEntryWithFallback(type, system).Path; return MakeAbsolutePath(path, system); } diff --git a/BizHawk.Client.Common/RomLoader.cs b/BizHawk.Client.Common/RomLoader.cs index 073134a9c4..0592b4b883 100644 --- a/BizHawk.Client.Common/RomLoader.cs +++ b/BizHawk.Client.Common/RomLoader.cs @@ -26,7 +26,10 @@ namespace BizHawk.Client.Common { public class RomLoader { - public enum LoadErrorType { Unknown, MissingFirmware, XML, DiscError } + public enum LoadErrorType + { + Unknown, MissingFirmware, XML, DiscError + } // helper methods for the settings events private object GetCoreSettings()