From 4445a425a2b37005c2896945e8561683851404a0 Mon Sep 17 00:00:00 2001
From: "J.D. Purcell" <jdpurcell@gmail.com>
Date: Sun, 20 Oct 2019 10:47:23 -0400
Subject: [PATCH] ToolManager cleanup.

---
 BizHawk.Client.EmuHawk/tools/ToolManager.cs | 180 ++------------------
 1 file changed, 17 insertions(+), 163 deletions(-)

diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs
index a2e428a405..1c0a1c17e7 100644
--- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs
+++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs
@@ -774,166 +774,35 @@ namespace BizHawk.Client.EmuHawk
 		// Note: Referencing these properties creates an instance of the tool and persists it.  They should be referenced by type if this is not desired
 		#region Tools
 
-		public RamWatch RamWatch
+		private T GetTool<T>() where T : class, IToolForm, new()
 		{
-			get
+			T tool = _tools.OfType<T>().FirstOrDefault();
+			if (tool != null)
 			{
-				var tool = _tools.FirstOrDefault(t => t is RamWatch);
-				if (tool != null)
+				if (!tool.IsDisposed)
 				{
-					if (tool.IsDisposed)
-					{
-						_tools.Remove(tool);
-					}
-					else
-					{
-						return tool as RamWatch;
-					}
+					return tool;
 				}
-
-				var newTool = new RamWatch();
-				_tools.Add(newTool);
-				return newTool;
+				_tools.Remove(tool);
 			}
+			tool = new T();
+			_tools.Add(tool);
+			return tool;
 		}
 
-		public RamSearch RamSearch
-		{
-			get
-			{
-				var tool = _tools.FirstOrDefault(t => t is RamSearch);
-				if (tool != null)
-				{
-					if (tool.IsDisposed)
-					{
-						_tools.Remove(tool);
-					}
-					else
-					{
-						return tool as RamSearch;
-					}
-				}
+		public RamWatch RamWatch => GetTool<RamWatch>();
 
-				var newTool = new RamSearch();
-				_tools.Add(newTool);
-				return newTool;
-			}
-		}
+		public RamSearch RamSearch => GetTool<RamSearch>();
 
-		public Cheats Cheats
-		{
-			get
-			{
-				var tool = _tools.FirstOrDefault(t => t is Cheats);
-				if (tool != null)
-				{
-					if (tool.IsDisposed)
-					{
-						_tools.Remove(tool);
-					}
-					else
-					{
-						return tool as Cheats;
-					}
-				}
+		public Cheats Cheats => GetTool<Cheats>();
 
-				var newTool = new Cheats();
-				_tools.Add(newTool);
-				return newTool;
-			}
-		}
+		public HexEditor HexEditor => GetTool<HexEditor>();
 
-		public HexEditor HexEditor
-		{
-			get
-			{
-				var tool = _tools.FirstOrDefault(t => t is HexEditor);
-				if (tool != null)
-				{
-					if (tool.IsDisposed)
-					{
-						_tools.Remove(tool);
-					}
-					else
-					{
-						return tool as HexEditor;
-					}
-				}
+		public VirtualpadTool VirtualPad => GetTool<VirtualpadTool>();
 
-				var newTool = new HexEditor();
-				_tools.Add(newTool);
-				return newTool;
-			}
-		}
+		public SNESGraphicsDebugger SNESGraphicsDebugger => GetTool<SNESGraphicsDebugger>();
 
-		public VirtualpadTool VirtualPad
-		{
-			get
-			{
-				var tool = _tools.FirstOrDefault(t => t is VirtualpadTool);
-				if (tool != null)
-				{
-					if (tool.IsDisposed)
-					{
-						_tools.Remove(tool);
-					}
-					else
-					{
-						return tool as VirtualpadTool;
-					}
-				}
-
-				var newTool = new VirtualpadTool();
-				_tools.Add(newTool);
-				return newTool;
-			}
-		}
-
-		public SNESGraphicsDebugger SNESGraphicsDebugger
-		{
-			get
-			{
-				var tool = _tools.FirstOrDefault(t => t is SNESGraphicsDebugger);
-				if (tool != null)
-				{
-					if (tool.IsDisposed)
-					{
-						_tools.Remove(tool);
-					}
-					else
-					{
-						return tool as SNESGraphicsDebugger;
-					}
-				}
-
-				var newTool = new SNESGraphicsDebugger();
-				_tools.Add(newTool);
-				return newTool;
-			}
-		}
-
-		public LuaConsole LuaConsole
-		{
-			get
-			{
-				var tool = _tools.FirstOrDefault(t => t is LuaConsole);
-				if (tool != null)
-				{
-					if (tool.IsDisposed)
-					{
-						_tools.Remove(tool);
-					}
-					else
-					{
-						return tool as LuaConsole;
-					}
-				}
-
-				var newTool = new LuaConsole();
-				_tools.Add(newTool);
-				return newTool;
-			}
-		}
+		public LuaConsole LuaConsole => GetTool<LuaConsole>();
 
 		public TAStudio TAStudio
 		{
@@ -945,22 +814,7 @@ namespace BizHawk.Client.EmuHawk
 					System.Diagnostics.Debug.Fail("TAStudio does not exist!");
 				}
 
-				var tool = _tools.FirstOrDefault(t => t is TAStudio);
-				if (tool != null)
-				{
-					if (tool.IsDisposed)
-					{
-						_tools.Remove(tool);
-					}
-					else
-					{
-						return tool as TAStudio;
-					}
-				}
-
-				var newTool = new TAStudio();
-				_tools.Add(newTool);
-				return newTool;
+				return GetTool<TAStudio>();
 			}
 		}