From 8eda262ebee83fa70a56129c197c7eb94c6b8604 Mon Sep 17 00:00:00 2001
From: adelikat <adelikat@tasvideos.org>
Date: Sat, 13 Dec 2014 22:54:07 +0000
Subject: [PATCH] convert Ram Watch and Hex Editor to use EmulatorServices

---
 .../tools/HexEditor/HexEditor.cs                  | 15 ++-------------
 .../tools/Watch/RamWatch.Designer.cs              |  2 +-
 BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs    | 10 +++-------
 3 files changed, 6 insertions(+), 21 deletions(-)

diff --git a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs
index b7462ddc60..6507fc7b8b 100644
--- a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs
+++ b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs
@@ -20,9 +20,11 @@ using BizHawk.Client.EmuHawk.ToolExtensions;
 
 namespace BizHawk.Client.EmuHawk
 {
+	[RequiredServices(typeof(IMemoryDomains))]
 	public partial class HexEditor : Form, IToolForm
 	{
 		public IDictionary<Type, object> EmulatorServices { private get; set; }
+		private MemoryDomainList MemoryDomains { get { return (EmulatorServices[typeof(IMemoryDomains)] as IMemoryDomains).MemoryDomains; } }
 
 		private bool fontSizeSet = false;
 		private int fontWidth;
@@ -61,8 +63,6 @@ namespace BizHawk.Client.EmuHawk
 		private bool _bigEndian;
 		private int _dataSize;
 
-		private MemoryDomainList MemoryDomains;
-
 		public HexEditor()
 		{
 			var font = new Font("Courier New", 8);
@@ -135,22 +135,11 @@ namespace BizHawk.Client.EmuHawk
 
 		public void Restart()
 		{
-			MemoryDomains = null;
-
-			if (!Global.Emulator.HasMemoryDomains())
-			{
-				Close();
-				return;
-			}
-
-
 			if (!IsHandleCreated || IsDisposed)
 			{
 				return;
 			}
 
-			MemoryDomains = ((IMemoryDomains)Global.Emulator).MemoryDomains; // The cast is intentional, we want a specific cast error, not an eventual null reference error
-
 			var theDomain = _domain.Name.ToLower() == "file on disk" ? 999 : GetDomainInt(_domain.Name);
 
 			SetMemoryDomainMenu(); // Calls update routines, TODO: refactor, that is confusing!
diff --git a/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.Designer.cs b/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.Designer.cs
index dc5285408f..29c442b7da 100644
--- a/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.Designer.cs
+++ b/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.Designer.cs
@@ -1007,7 +1007,7 @@
 			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
 			this.Name = "RamWatch";
 			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
-			this.Text = "Ram Watch";
+			this.Text = " ";
 			this.Activated += new System.EventHandler(this.NewRamWatch_Activated);
 			this.Load += new System.EventHandler(this.NewRamWatch_Load);
 			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.NewRamWatch_DragDrop);
diff --git a/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs b/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs
index 1621023541..59ed01bb7e 100644
--- a/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs
+++ b/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs
@@ -16,6 +16,7 @@ using BizHawk.Client.EmuHawk.ToolExtensions;
 
 namespace BizHawk.Client.EmuHawk
 {
+	[RequiredServices(typeof(IMemoryDomains))]
 	public partial class RamWatch : Form, IToolForm
 	{
 		private readonly Dictionary<string, int> _defaultColumnWidths = new Dictionary<string, int>
@@ -30,8 +31,6 @@ namespace BizHawk.Client.EmuHawk
 		};
 
 		private WatchList _watches;
-		private IMemoryDomains _core;
-
 
 		private int _defaultWidth;
 		private int _defaultHeight;
@@ -40,11 +39,10 @@ namespace BizHawk.Client.EmuHawk
 		private bool _paused = false;
 
 		public IDictionary<Type, object> EmulatorServices { private get; set; }
+		private IMemoryDomains _core { get { return (IMemoryDomains)EmulatorServices[typeof(IMemoryDomains)]; } }
 
 		public RamWatch()
 		{
-			_core = (IMemoryDomains)Global.Emulator; // Cast is intentional, better to get a cast exception than a null reference exception later
-			_watches = new WatchList(_core, _core.MemoryDomains.MainMemory);
 			InitializeComponent();
 			WatchListView.QueryItemText += WatchListView_QueryItemText;
 			WatchListView.QueryItemBkColor += WatchListView_QueryItemBkColor;
@@ -203,8 +201,6 @@ namespace BizHawk.Client.EmuHawk
 				return;
 			}
 
-			_core = (IMemoryDomains)Global.Emulator; // Cast is intentional, better to get a cast exception than a null reference exception later
-
 			if (!string.IsNullOrWhiteSpace(_watches.CurrentFileName))
 			{
 				_watches.RefreshDomans(_core, _core.MemoryDomains.MainMemory);
@@ -1087,7 +1083,7 @@ namespace BizHawk.Client.EmuHawk
 
 		private void NewRamWatch_Load(object sender, EventArgs e)
 		{
-
+			_watches = new WatchList(_core, _core.MemoryDomains.MainMemory);
 			LoadConfigSettings();
 			UpdateStatusBar();
 		}