From 04f9081943f0d202d4e5d7ef5e04d373ea9b0136 Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 5 Dec 2014 00:32:29 +0000 Subject: [PATCH] in IEmulator extensions add methods such as AsMemoryDomains(), AsStatable() that do the service calling magic. In the client code replace appplicable casts of IMemoryDomains with AsMemoryDomains(), more commits of this nature for the other IEmulatorServices soon to follow --- .../lua/EmuLuaLibrary.MainMemory.cs | 2 +- .../lua/EmuLuaLibrary.Memory.cs | 6 +++--- .../lua/EmuLuaLibrary.NES.cs | 3 ++- BizHawk.Client.Common/lua/LuaMemoryBase.cs | 2 +- BizHawk.Client.Common/tools/CheatList.cs | 3 ++- .../tools/RamSearchEngine.cs | 3 ++- .../tools/GB/GBGameGenie.cs | 3 ++- .../tools/Genesis/GenGameGenie.cs | 3 ++- .../tools/NES/NESGameGenie.cs | 3 ++- BizHawk.Client.EmuHawk/tools/PCE/PCECDL.cs | 3 ++- .../tools/SNES/SNESGameGenie.cs | 3 ++- BizHawk.Client.EmuHawk/tools/TraceLogger.cs | 2 +- BizHawk.Emulation.Common/Extensions.cs | 19 +++++++++++++++++-- 13 files changed, 39 insertions(+), 16 deletions(-) diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs index 9817ee615d..c4bef7ce38 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs @@ -24,7 +24,7 @@ namespace BizHawk.Client.Common { if (Global.Emulator.HasMemoryDomains()) { - return (Global.Emulator as IMemoryDomains).MemoryDomains.MainMemory; + return Global.Emulator.AsMemoryDomains().MemoryDomains.MainMemory; } else { diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs index c8021371f4..9520e2f90c 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs @@ -17,7 +17,7 @@ namespace BizHawk.Client.Common { if (Global.Emulator.HasMemoryDomains()) { - var domains = (Global.Emulator as IMemoryDomains).MemoryDomains; + var domains = Global.Emulator.AsMemoryDomains().MemoryDomains; _currentMemoryDomain = domains.IndexOf(domains.MainMemory); } } @@ -27,7 +27,7 @@ namespace BizHawk.Client.Common { if (Global.Emulator.HasMemoryDomains()) { - var domains = (Global.Emulator as IMemoryDomains).MemoryDomains; + var domains = Global.Emulator.AsMemoryDomains().MemoryDomains; _currentMemoryDomain = domains.IndexOf(domains.MainMemory); } } @@ -40,7 +40,7 @@ namespace BizHawk.Client.Common { if (Global.Emulator.HasMemoryDomains()) { - return (Global.Emulator as IMemoryDomains).MemoryDomains[_currentMemoryDomain]; + return Global.Emulator.AsMemoryDomains().MemoryDomains[_currentMemoryDomain]; } else { diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.NES.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.NES.cs index 9af571ed53..bb93501254 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.NES.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.NES.cs @@ -4,6 +4,7 @@ using System.Linq; using LuaInterface; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Emulation.Cores.Nintendo.NES; using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES; @@ -34,7 +35,7 @@ namespace BizHawk.Client.Common { var decoder = new NESGameGenieDecoder(code); var watch = Watch.GenerateWatch( - (Global.Emulator as IMemoryDomains).MemoryDomains["System Bus"], + Global.Emulator.AsMemoryDomains().MemoryDomains["System Bus"], decoder.Address, Watch.WatchSize.Byte, Watch.DisplayType.Hex, diff --git a/BizHawk.Client.Common/lua/LuaMemoryBase.cs b/BizHawk.Client.Common/lua/LuaMemoryBase.cs index 6877008c84..457b57222c 100644 --- a/BizHawk.Client.Common/lua/LuaMemoryBase.cs +++ b/BizHawk.Client.Common/lua/LuaMemoryBase.cs @@ -24,7 +24,7 @@ namespace BizHawk.Client.Common { if (Global.Emulator.HasMemoryDomains()) { - return (Global.Emulator as IMemoryDomains).MemoryDomains; + return Global.Emulator.AsMemoryDomains().MemoryDomains; } else { diff --git a/BizHawk.Client.Common/tools/CheatList.cs b/BizHawk.Client.Common/tools/CheatList.cs index 4a4e61661f..ac05627548 100644 --- a/BizHawk.Client.Common/tools/CheatList.cs +++ b/BizHawk.Client.Common/tools/CheatList.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Text; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; namespace BizHawk.Client.Common { @@ -456,7 +457,7 @@ namespace BizHawk.Client.Common compare = int.Parse(vals[2], NumberStyles.HexNumber); } - var domain = (Global.Emulator as IMemoryDomains).MemoryDomains[vals[3]]; + var domain = Global.Emulator.AsMemoryDomains().MemoryDomains[vals[3]]; var enabled = vals[4] == "1"; var name = vals[5]; diff --git a/BizHawk.Client.Common/tools/RamSearchEngine.cs b/BizHawk.Client.Common/tools/RamSearchEngine.cs index 2e4cdf759c..4e577474c9 100644 --- a/BizHawk.Client.Common/tools/RamSearchEngine.cs +++ b/BizHawk.Client.Common/tools/RamSearchEngine.cs @@ -5,6 +5,7 @@ using System.Linq; using BizHawk.Common; using BizHawk.Common.CollectionExtensions; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; namespace BizHawk.Client.Common { @@ -27,7 +28,7 @@ namespace BizHawk.Client.Common public RamSearchEngine(Settings settings) { - _settings = new Settings((IMemoryDomains)Global.Emulator); + _settings = new Settings(Global.Emulator.AsMemoryDomains()); _settings.Mode = settings.Mode; _settings.Domain = settings.Domain; _settings.Size = settings.Size; diff --git a/BizHawk.Client.EmuHawk/tools/GB/GBGameGenie.cs b/BizHawk.Client.EmuHawk/tools/GB/GBGameGenie.cs index 734b5c9d0b..2f4b27d5c5 100644 --- a/BizHawk.Client.EmuHawk/tools/GB/GBGameGenie.cs +++ b/BizHawk.Client.EmuHawk/tools/GB/GBGameGenie.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using System.Windows.Forms; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Client.Common; namespace BizHawk.Client.EmuHawk @@ -296,7 +297,7 @@ namespace BizHawk.Client.EmuHawk } var watch = Watch.GenerateWatch( - (Global.Emulator as IMemoryDomains).MemoryDomains["System Bus"], + Global.Emulator.AsMemoryDomains().MemoryDomains["System Bus"], address, Watch.WatchSize.Byte, Watch.DisplayType.Hex, diff --git a/BizHawk.Client.EmuHawk/tools/Genesis/GenGameGenie.cs b/BizHawk.Client.EmuHawk/tools/Genesis/GenGameGenie.cs index 5f2894cab7..01ba0533aa 100644 --- a/BizHawk.Client.EmuHawk/tools/Genesis/GenGameGenie.cs +++ b/BizHawk.Client.EmuHawk/tools/Genesis/GenGameGenie.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using System.Windows.Forms; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Client.Common; #pragma warning disable 675 //TOOD: fix the potential problem this is masking @@ -357,7 +358,7 @@ namespace BizHawk.Client.EmuHawk } var watch = Watch.GenerateWatch( - (Global.Emulator as IMemoryDomains).MemoryDomains["MD CART"], + Global.Emulator.AsMemoryDomains().MemoryDomains["MD CART"], address, Watch.WatchSize.Word, Watch.DisplayType.Hex, diff --git a/BizHawk.Client.EmuHawk/tools/NES/NESGameGenie.cs b/BizHawk.Client.EmuHawk/tools/NES/NESGameGenie.cs index b0d7d887c7..e663dd2445 100644 --- a/BizHawk.Client.EmuHawk/tools/NES/NESGameGenie.cs +++ b/BizHawk.Client.EmuHawk/tools/NES/NESGameGenie.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Windows.Forms; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Client.Common; namespace BizHawk.Client.EmuHawk @@ -231,7 +232,7 @@ namespace BizHawk.Client.EmuHawk if (!string.IsNullOrWhiteSpace(AddressBox.Text) && !string.IsNullOrWhiteSpace(ValueBox.Text)) { var watch = Watch.GenerateWatch( - (Global.Emulator as IMemoryDomains).MemoryDomains["System Bus"], + Global.Emulator.AsMemoryDomains().MemoryDomains["System Bus"], AddressBox.ToRawInt().Value, Watch.WatchSize.Byte, Watch.DisplayType.Hex, diff --git a/BizHawk.Client.EmuHawk/tools/PCE/PCECDL.cs b/BizHawk.Client.EmuHawk/tools/PCE/PCECDL.cs index a119b7742f..5a48d25d3e 100644 --- a/BizHawk.Client.EmuHawk/tools/PCE/PCECDL.cs +++ b/BizHawk.Client.EmuHawk/tools/PCE/PCECDL.cs @@ -5,6 +5,7 @@ using System.IO; using System.Windows.Forms; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Emulation.Cores.Components.H6280; using BizHawk.Emulation.Cores.PCEngine; @@ -304,7 +305,7 @@ namespace BizHawk.Client.EmuHawk { using (var fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write)) { - _cdl.Disassemble(fs, (Global.Emulator as IMemoryDomains).MemoryDomains); + _cdl.Disassemble(fs, Global.Emulator.AsMemoryDomains().MemoryDomains); } } } diff --git a/BizHawk.Client.EmuHawk/tools/SNES/SNESGameGenie.cs b/BizHawk.Client.EmuHawk/tools/SNES/SNESGameGenie.cs index 446cdc6a20..c2351d705b 100644 --- a/BizHawk.Client.EmuHawk/tools/SNES/SNESGameGenie.cs +++ b/BizHawk.Client.EmuHawk/tools/SNES/SNESGameGenie.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using System.Windows.Forms; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Emulation.Cores.Nintendo.SNES; using BizHawk.Client.Common; @@ -275,7 +276,7 @@ namespace BizHawk.Client.EmuHawk } var watch = Watch.GenerateWatch( - (Global.Emulator as IMemoryDomains).MemoryDomains["BUS"], + Global.Emulator.AsMemoryDomains().MemoryDomains["BUS"], address, Watch.WatchSize.Byte, Watch.DisplayType.Hex, diff --git a/BizHawk.Client.EmuHawk/tools/TraceLogger.cs b/BizHawk.Client.EmuHawk/tools/TraceLogger.cs index b7d0b384a3..b0b13b2871 100644 --- a/BizHawk.Client.EmuHawk/tools/TraceLogger.cs +++ b/BizHawk.Client.EmuHawk/tools/TraceLogger.cs @@ -38,7 +38,7 @@ namespace BizHawk.Client.EmuHawk if (Global.Emulator.CpuTraceAvailable()) { - Tracer = Global.Emulator.GetDebugger().Tracer; + Tracer = Global.Emulator.AsDebuggable().Tracer; } else { diff --git a/BizHawk.Emulation.Common/Extensions.cs b/BizHawk.Emulation.Common/Extensions.cs index 48563a1d26..5e17487a82 100644 --- a/BizHawk.Emulation.Common/Extensions.cs +++ b/BizHawk.Emulation.Common/Extensions.cs @@ -22,7 +22,7 @@ namespace BizHawk.Emulation.Common.IEmulatorExtensions return core != null && core.ServiceProvider.HasService(); } - public static IMemoryDomains GetMemoryDomains(this IEmulator core) + public static IMemoryDomains AsMemoryDomains(this IEmulator core) { return (IMemoryDomains)core.ServiceProvider.GetService(); } @@ -32,16 +32,31 @@ namespace BizHawk.Emulation.Common.IEmulatorExtensions return core != null && core.ServiceProvider.HasService(); } + public static ISaveRam AsSaveRam(this IEmulator core) + { + return (ISaveRam)core.ServiceProvider.GetService(); + } + public static bool HasSavestates(this IEmulator core) { return core != null && core.ServiceProvider.HasService(); } + public static IStatable AsStatable(this IEmulator core) + { + return (IStatable)core.ServiceProvider.GetService(); + } + public static bool CanPollInput(this IEmulator core) { return core != null && core.ServiceProvider.HasService(); } + public static IInputPollable AsInputPollable(this IEmulator core) + { + return (IInputPollable)core.ServiceProvider.GetService(); + } + public static bool CpuTraceAvailable(this IEmulator core) { // TODO: this is a pretty ugly way to handle this @@ -67,7 +82,7 @@ namespace BizHawk.Emulation.Common.IEmulatorExtensions return core.ServiceProvider.HasService(); } - public static IDebuggable GetDebugger(this IEmulator core) + public static IDebuggable AsDebuggable(this IEmulator core) { return (IDebuggable)core.ServiceProvider.GetService(); }