diff --git a/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs b/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs index 7e1bb645c7..ef212ca544 100644 --- a/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs +++ b/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs @@ -74,14 +74,7 @@ namespace BizHawk.Client.EmuHawk public void UpdateValues() { - if (!IsHandleCreated || IsDisposed) - { - return; - } - else - { - UpdateListView(); - } + //Do nothing; } public void Restart() @@ -89,7 +82,10 @@ namespace BizHawk.Client.EmuHawk StartNewList(); } - private void UpdateListView() + /// + /// Tools that want to refresh the cheats list should call this, not UpdateValues + /// + public void UpdateDialog() { CheatListView.ItemCount = Global.CheatList.Count; TotalLabel.Text = Global.CheatList.CheatCount.ToString() @@ -115,7 +111,7 @@ namespace BizHawk.Client.EmuHawk else { Global.Config.RecentWatches.Add(path); - UpdateListView(); + UpdateDialog(); UpdateMessageLabel(); } } @@ -180,7 +176,7 @@ namespace BizHawk.Client.EmuHawk if (result) { Global.CheatList.Load(file.FullName, append); - UpdateListView(); + UpdateDialog(); UpdateMessageLabel(); Global.Config.RecentCheats.Add(Global.CheatList.CurrentFileName); } @@ -223,7 +219,7 @@ namespace BizHawk.Client.EmuHawk private void AddCheat() { Global.CheatList.Add(CheatEditor.Cheat); - UpdateListView(); + UpdateDialog(); UpdateMessageLabel(); } @@ -407,7 +403,7 @@ namespace BizHawk.Client.EmuHawk CheatListView.SelectItem(newi, true); } - UpdateListView(); + UpdateDialog(); } private void MoveDown() @@ -443,7 +439,7 @@ namespace BizHawk.Client.EmuHawk CheatListView.SelectItem(newi, true); } - UpdateListView(); + UpdateDialog(); } private void Remove() @@ -457,7 +453,7 @@ namespace BizHawk.Client.EmuHawk CheatListView.SelectedIndices.Clear(); } - UpdateListView(); + UpdateDialog(); } private void Toggle() @@ -547,7 +543,7 @@ namespace BizHawk.Client.EmuHawk private void StartNewList() { Global.CheatList.NewList(GlobalWin.MainForm.GenerateDefaultCheatFilename()); - UpdateListView(); + UpdateDialog(); UpdateMessageLabel(); ToggleGameGenieButton(); } @@ -679,7 +675,7 @@ namespace BizHawk.Client.EmuHawk } } - UpdateListView(); + UpdateDialog(); UpdateMessageLabel(); } @@ -694,7 +690,7 @@ namespace BizHawk.Client.EmuHawk Global.CheatList.Add(Cheat.Separator); } - UpdateListView(); + UpdateDialog(); UpdateMessageLabel(); } @@ -944,7 +940,7 @@ namespace BizHawk.Client.EmuHawk _sortedColumn = column.Name; _sortReverse ^= true; - UpdateListView(); + UpdateDialog(); } private void NewCheatForm_DragDrop(object sender, DragEventArgs e) @@ -953,7 +949,7 @@ namespace BizHawk.Client.EmuHawk if (Path.GetExtension(filePaths[0]) == (".cht")) { LoadFile(new FileInfo(filePaths[0]), append: false); - UpdateListView(); + UpdateDialog(); UpdateMessageLabel(); } } diff --git a/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs b/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs index 2b1a6367c4..45a4542490 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs @@ -210,7 +210,12 @@ namespace BizHawk.Client.EmuHawk GlobalWin.Tools.UpdateValues(); GlobalWin.Tools.UpdateValues(); GlobalWin.Tools.UpdateValues(); - GlobalWin.Tools.UpdateValues(); + + if (GlobalWin.Tools.Has()) + { + GlobalWin.Tools.Cheats.UpdateDialog(); + } + GlobalWin.MainForm.UpdateCheatStatus(); } diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index dd8b7dcf13..1936b6406a 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -60,17 +60,20 @@ namespace BizHawk.Client.EmuHawk var existingTool = _tools.FirstOrDefault(x => x is T); if (existingTool != null) { - return existingTool; + if (existingTool.IsDisposed) + { + Close(); + return CreateInstance(); + } + else + { + return existingTool; + } } else { - var tool = Activator.CreateInstance(typeof(T)); - - //Add to the list and extract it, so it will be strongly typed as T - _tools.Add(tool as IToolForm); - return _tools.FirstOrDefault(x => x is T); + return CreateInstance(); } - } public void UpdateBefore() @@ -78,7 +81,10 @@ namespace BizHawk.Client.EmuHawk var beforeList = _tools.Where(x => x.UpdateBefore); foreach (var tool in beforeList) { - tool.UpdateValues(); + if (!tool.IsDisposed) + { + tool.UpdateValues(); + } } } @@ -87,7 +93,10 @@ namespace BizHawk.Client.EmuHawk var afterList = _tools.Where(x => !x.UpdateBefore); foreach (var tool in afterList) { - tool.UpdateValues(); + if (!tool.IsDisposed) + { + tool.UpdateValues(); + } } } @@ -96,9 +105,11 @@ namespace BizHawk.Client.EmuHawk /// public void UpdateValues() where T : IToolForm { + CloseIfDisposed(); var tool = _tools.FirstOrDefault(x => x is T); if (tool != null) { + tool.UpdateValues(); } } @@ -114,6 +125,7 @@ namespace BizHawk.Client.EmuHawk /// public void Restart() where T : IToolForm { + CloseIfDisposed(); var tool = _tools.FirstOrDefault(x => x is T); if (tool != null) { @@ -178,6 +190,24 @@ namespace BizHawk.Client.EmuHawk _tools.Clear(); } + private IToolForm CreateInstance() + { + var tool = Activator.CreateInstance(typeof(T)); + + //Add to the list and extract it, so it will be strongly typed as T + _tools.Add(tool as IToolForm); + return _tools.FirstOrDefault(x => x is T); + } + + private void CloseIfDisposed() where T : IToolForm + { + var existingTool = _tools.FirstOrDefault(x => x is T); + if (existingTool != null && existingTool.IsDisposed) + { + Close(); + } + } + //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 @@ -227,6 +257,29 @@ namespace BizHawk.Client.EmuHawk } } + public Cheats Cheats + { + get + { + var tool = _tools.FirstOrDefault(x => x is Cheats); + if (tool != null) + { + if (tool.IsDisposed) + { + _tools.Remove(tool); + } + else + { + return tool as Cheats; + } + } + + var newTool = new Cheats(); + _tools.Add(newTool); + return newTool; + } + } + public HexEditor HexEditor { get