From 4681fef0c22d7bed4ac2dbce21e0419564bb3b97 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 30 Nov 2014 14:18:44 +0000 Subject: [PATCH] Add an IsNull() extension method to IEmulator (checks for null and if NullEmulator) and replace EmuHawk NullEmulator checks with this method instead --- BizHawk.Client.Common/SaveSlotManager.cs | 4 +-- BizHawk.Client.EmuHawk/MainForm.Events.cs | 33 +++++++++---------- BizHawk.Client.EmuHawk/MainForm.cs | 26 +++++---------- .../tools/HexEditor/HexEditor.cs | 4 +-- .../tools/Lua/LuaConsole.cs | 3 +- BizHawk.Client.EmuHawk/tools/ToolHelpers.cs | 6 ++-- BizHawk.Emulation.Common/Extensions.cs | 5 +++ 7 files changed, 39 insertions(+), 42 deletions(-) diff --git a/BizHawk.Client.Common/SaveSlotManager.cs b/BizHawk.Client.Common/SaveSlotManager.cs index 617c5355dc..21a04c3e84 100644 --- a/BizHawk.Client.Common/SaveSlotManager.cs +++ b/BizHawk.Client.Common/SaveSlotManager.cs @@ -1,7 +1,7 @@ using System.IO; using System.Linq; -using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; namespace BizHawk.Client.Common { @@ -52,7 +52,7 @@ namespace BizHawk.Client.Common public bool HasSlot(int slot) { - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { return false; } diff --git a/BizHawk.Client.EmuHawk/MainForm.Events.cs b/BizHawk.Client.EmuHawk/MainForm.Events.cs index 86c7b4256d..746d8b46e2 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -36,7 +36,7 @@ namespace BizHawk.Client.EmuHawk AVSubMenu.Enabled = ScreenshotSubMenu.Enabled = CloseRomMenuItem.Enabled = - !(Global.Emulator is NullEmulator); + !Global.Emulator.IsNull(); } private void RecentRomMenuItem_DropDownOpened(object sender, EventArgs e) @@ -131,7 +131,7 @@ namespace BizHawk.Client.EmuHawk SaveState8MenuItem.Enabled = SaveState9MenuItem.Enabled = SaveState0MenuItem.Enabled = - !(Global.Emulator is NullEmulator); + !Global.Emulator.IsNull(); } private void LoadStateSubMenu_DropDownOpened(object sender, EventArgs e) @@ -150,7 +150,7 @@ namespace BizHawk.Client.EmuHawk AutoloadLastSlotMenuItem.Checked = Global.Config.AutoLoadLastSaveSlot; - LoadNamedStateMenuItem.Enabled = !(Global.Emulator is NullEmulator); + LoadNamedStateMenuItem.Enabled = !Global.Emulator.IsNull(); LoadState1MenuItem.Enabled = _stateSlots.HasSlot(1); LoadState2MenuItem.Enabled = _stateSlots.HasSlot(2); LoadState3MenuItem.Enabled = _stateSlots.HasSlot(3); @@ -180,7 +180,7 @@ namespace BizHawk.Client.EmuHawk SaveToCurrentSlotMenuItem.ShortcutKeyDisplayString = Global.Config.HotkeyBindings["Quick Save"].Bindings; LoadCurrentSlotMenuItem.ShortcutKeyDisplayString = Global.Config.HotkeyBindings["Quick Load"].Bindings; - SaveToCurrentSlotMenuItem.Enabled = LoadCurrentSlotMenuItem.Enabled = !(Global.Emulator is NullEmulator); + SaveToCurrentSlotMenuItem.Enabled = LoadCurrentSlotMenuItem.Enabled = !Global.Emulator.IsNull(); SelectSlot0MenuItem.Checked = false; SelectSlot1MenuItem.Checked = false; @@ -617,7 +617,7 @@ namespace BizHawk.Client.EmuHawk private void emulationToolStripMenuItem_DropDownOpened(object sender, EventArgs e) { - RebootCoreMenuItem.Enabled = !(Global.Emulator is NullEmulator); + RebootCoreMenuItem.Enabled = !Global.Emulator.IsNull(); PauseMenuItem.Checked = _didMenuPause ? _wasPaused : EmulatorPaused; @@ -791,7 +791,7 @@ namespace BizHawk.Client.EmuHawk private void ConfigSubMenu_DropDownOpened(object sender, EventArgs e) { - ControllersMenuItem.Enabled = !(Global.Emulator is NullEmulator); + ControllersMenuItem.Enabled = !Global.Emulator.IsNull(); } private void FrameSkipMenuItem_DropDownOpened(object sender, EventArgs e) @@ -1053,7 +1053,7 @@ namespace BizHawk.Client.EmuHawk { Global.Config.GB_AsSGB ^= true; - if (!(Global.Emulator is NullEmulator)) + if (!Global.Emulator.IsNull()) { FlagNeedsReboot(); } @@ -1063,7 +1063,7 @@ namespace BizHawk.Client.EmuHawk { Global.Config.NES_InQuickNES ^= true; - if (!(Global.Emulator is NullEmulator)) + if (!Global.Emulator.IsNull()) { FlagNeedsReboot(); } @@ -1073,7 +1073,7 @@ namespace BizHawk.Client.EmuHawk { Global.Config.SNES_InSnes9x ^= true; - if (!(Global.Emulator is NullEmulator)) + if (!Global.Emulator.IsNull()) { FlagNeedsReboot(); } @@ -1116,7 +1116,7 @@ namespace BizHawk.Client.EmuHawk TAStudioMenuItem.Enabled = VirtualPadMenuItem.Enabled = - !(Global.Emulator is NullEmulator); + !Global.Emulator.IsNull(); CheatsMenuItem.Enabled = HexEditorMenuItem.Enabled = @@ -1823,7 +1823,7 @@ namespace BizHawk.Client.EmuHawk { if (new N64VideoPluginconfig().ShowDialog() == DialogResult.OK) { - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { GlobalWin.OSD.AddMessage("Plugin settings saved"); } @@ -1956,14 +1956,13 @@ namespace BizHawk.Client.EmuHawk _didMenuPause = true; PauseEmulator(); - OpenRomContextMenuItem.Visible = (Global.Emulator is NullEmulator) || _inFullscreen; + OpenRomContextMenuItem.Visible = Global.Emulator.IsNull() || _inFullscreen; ShowMenuContextMenuItem.Visible = ShowMenuContextMenuSeparator.Visible = _inFullscreen; - LoadLastRomContextMenuItem.Visible = - (Global.Emulator is NullEmulator); + LoadLastRomContextMenuItem.Visible = Global.Emulator.IsNull(); StopAVContextMenuItem.Visible = _currAviWriter != null; @@ -1972,12 +1971,12 @@ namespace BizHawk.Client.EmuHawk ScreenshotContextMenuItem.Visible = CloseRomContextMenuItem.Visible = UndoSavestateContextMenuItem.Visible = - !(Global.Emulator is NullEmulator); + !Global.Emulator.IsNull(); RecordMovieContextMenuItem.Visible = PlayMovieContextMenuItem.Visible = LoadLastMovieContextMenuItem.Visible = - !(Global.Emulator is NullEmulator) && !Global.MovieSession.Movie.IsActive; + !Global.Emulator.IsNull() && !Global.MovieSession.Movie.IsActive; RestartMovieContextMenuItem.Visible = StopMovieContextMenuItem.Visible = @@ -1990,7 +1989,7 @@ namespace BizHawk.Client.EmuHawk StopNoSaveContextMenuItem.Visible = Global.MovieSession.Movie.IsActive && Global.MovieSession.Movie.Changes; - AddSubtitleContextMenuItem.Visible = !(Global.Emulator is NullEmulator) && Global.MovieSession.Movie.IsActive && !Global.MovieSession.ReadOnly; + AddSubtitleContextMenuItem.Visible = !Global.Emulator.IsNull() && Global.MovieSession.Movie.IsActive && !Global.MovieSession.ReadOnly; ConfigContextMenuItem.Visible = _inFullscreen; diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index 676c28c4df..58eabc0419 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -1290,15 +1290,7 @@ namespace BizHawk.Client.EmuHawk private void SetWindowText() { - string str = ""; - - if (Global.Emulator == null) - { - // in some weird cirumstances, this can get called too early before any emulator exists - // just ignore it - Text = "BizHawk"; - return; - } + string str = string.Empty; if (_inResizeLoop) { @@ -1306,7 +1298,7 @@ namespace BizHawk.Client.EmuHawk str = str + string.Format("({0}x{1}) - ", size.Width, size.Height); } - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { str = str + "BizHawk" + (VersionInfo.DeveloperBuild ? " (interim) " : string.Empty); } @@ -1707,7 +1699,7 @@ namespace BizHawk.Client.EmuHawk // realtime throttle is never going to be so exact that using a double here is wrong _throttle.SetCoreFps(Global.Emulator.CoreComm.VsyncRate); - _throttle.signal_paused = EmulatorPaused || Global.Emulator is NullEmulator; + _throttle.signal_paused = EmulatorPaused || Global.Emulator.IsNull(); _throttle.signal_unthrottle = _unthrottled || superfastforward; _throttle.SetSpeedPercent(fastforward ? Global.Config.SpeedPercentAlternate : Global.Config.SpeedPercent); } @@ -1751,7 +1743,7 @@ namespace BizHawk.Client.EmuHawk private void SaveStateAs() { - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { return; } @@ -1778,7 +1770,7 @@ namespace BizHawk.Client.EmuHawk private void LoadStateAs() { - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { return; } @@ -2523,7 +2515,7 @@ namespace BizHawk.Client.EmuHawk public void LoadQuickSave(string quickSlotName, bool fromLua = false) { - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { return; } @@ -2579,7 +2571,7 @@ namespace BizHawk.Client.EmuHawk private void UpdateCoreStatusBarButton() { - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { CoreNameStatusBarButton.Visible = false; return; @@ -2940,7 +2932,7 @@ namespace BizHawk.Client.EmuHawk else { var sfd = new SaveFileDialog(); - if (!(Global.Emulator is NullEmulator)) + if (!Global.Emulator.IsNull()) { sfd.FileName = PathManager.FilesystemSafeName(Global.Game) + "." + ext; //dont use Path.ChangeExtension, it might wreck game names with dots in them sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.AvPathFragment, null); @@ -3360,7 +3352,7 @@ namespace BizHawk.Client.EmuHawk // TODO: should backup logic be stuffed in into Client.Common.SaveStateManager? public void SaveQuickSave(string quickSlotName) { - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { return; } diff --git a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index 025d887474..348e63b6b7 100644 --- a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -425,7 +425,7 @@ namespace BizHawk.Client.EmuHawk RestoreDirectory = true }; - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { sfd.FileName = "MemoryDump"; sfd.InitialDirectory = PathManager.GetBasePathAbsolute(); @@ -926,7 +926,7 @@ namespace BizHawk.Client.EmuHawk RestoreDirectory = true }; - if (Global.Emulator is NullEmulator) + if (Global.Emulator.IsNull()) { sfd.FileName = "MemoryDump"; sfd.InitialDirectory = PathManager.GetBasePathAbsolute(); diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index a76dda02f7..0067d6ecf9 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -11,6 +11,7 @@ using LuaInterface; using BizHawk.Client.Common; using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Client.EmuHawk.WinFormExtensions; using BizHawk.Client.EmuHawk.ToolExtensions; @@ -464,7 +465,7 @@ namespace BizHawk.Client.EmuHawk sfd.FileName = Path.GetFileNameWithoutExtension(_luaList.Filename); sfd.InitialDirectory = Path.GetDirectoryName(_luaList.Filename); } - else if (!(Global.Emulator is NullEmulator)) + else if (!Global.Emulator.IsNull()) { sfd.FileName = PathManager.FilesystemSafeName(Global.Game); sfd.InitialDirectory = PathManager.GetLuaPath(); diff --git a/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs b/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs index 4ccfcf384d..b5d4ff4bf9 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs @@ -43,7 +43,7 @@ namespace BizHawk.Client.EmuHawk sfd.FileName = Path.GetFileNameWithoutExtension(currentFile); sfd.InitialDirectory = Path.GetDirectoryName(currentFile); } - else if (!(Global.Emulator is NullEmulator)) + else if (!Global.Emulator.IsNull()) { sfd.FileName = PathManager.FilesystemSafeName(Global.Game); sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null); @@ -94,7 +94,7 @@ namespace BizHawk.Client.EmuHawk sfd.FileName = Path.GetFileNameWithoutExtension(currentFile); sfd.InitialDirectory = Path.GetDirectoryName(currentFile); } - else if (!(Global.Emulator is NullEmulator)) + else if (!Global.Emulator.IsNull()) { sfd.FileName = PathManager.FilesystemSafeName(Global.Game); sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.WatchPathFragment, null); @@ -144,7 +144,7 @@ namespace BizHawk.Client.EmuHawk { sfd.FileName = Path.GetFileNameWithoutExtension(currentFile); } - else if (!(Global.Emulator is NullEmulator)) + else if (!Global.Emulator.IsNull()) { sfd.FileName = PathManager.FilesystemSafeName(Global.Game); } diff --git a/BizHawk.Emulation.Common/Extensions.cs b/BizHawk.Emulation.Common/Extensions.cs index b84b23e31f..92e8958424 100644 --- a/BizHawk.Emulation.Common/Extensions.cs +++ b/BizHawk.Emulation.Common/Extensions.cs @@ -16,6 +16,11 @@ namespace BizHawk.Emulation.Common.IEmulatorExtensions return core is IMemoryDomains; } + public static bool IsNull(this IEmulator core) + { + return core == null || core is NullEmulator; + } + // TODO: a better place for these public static bool IsImplemented(this MethodInfo info) {