From 5bbdf2a49eda3a408fa573281a915750e8151dca Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 24 Nov 2019 16:54:08 -0600 Subject: [PATCH] Tastudio.MenuItems - cleanup, fix potential NRE's surrounding clipboard access, fix off by one on clear menu item invalidation logic, fix not dispointing of a disposable dialog --- .../tools/TAStudio/TAStudio.IToolForm.cs | 2 +- .../tools/TAStudio/TAStudio.MenuItems.cs | 160 +++++++++--------- .../tools/TAStudio/TAStudio.cs | 2 +- BizHawk.sln.DotSettings | 1 + 4 files changed, 80 insertions(+), 85 deletions(-) diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs index c08edc405d..f2b3ddd7a4 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IToolForm.cs @@ -130,7 +130,7 @@ namespace BizHawk.Client.EmuHawk if (result == DialogResult.Yes) { _exiting = true; // Asking to save changes should only ever be called when closing something - SaveTas(null, null); + SaveTas(); } else if (result == DialogResult.No) { diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs index c32dcb52fb..ac747c01e1 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs @@ -95,7 +95,7 @@ namespace BizHawk.Client.EmuHawk private bool _exiting; - private void SaveTas(object sender, EventArgs e) + private void SaveTas() { if (string.IsNullOrEmpty(CurrentTasMovie.Filename) || CurrentTasMovie.Filename == DefaultTasProjName()) @@ -125,7 +125,7 @@ namespace BizHawk.Client.EmuHawk // call this one from the menu only private void SaveTasMenuItem_Click(object sender, EventArgs e) { - SaveTas(sender, e); + SaveTas(); if (Settings.BackupPerFileSave) { SaveBackupMenuItem_Click(sender, e); @@ -228,18 +228,21 @@ namespace BizHawk.Client.EmuHawk private void SaveSelectionToMacroMenuItem_Click(object sender, EventArgs e) { - if (TasView.LastSelectedIndex == CurrentTasMovie.InputLogLength) - { - TasView.SelectRow(CurrentTasMovie.InputLogLength, false); - } - if (!TasView.AnyRowsSelected) { return; } - MovieZone macro = new MovieZone(CurrentTasMovie, TasView.FirstSelectedIndex.Value, - TasView.LastSelectedIndex.Value - TasView.FirstSelectedIndex.Value + 1); + if (TasView.LastSelectedIndex == CurrentTasMovie.InputLogLength) + { + TasView.SelectRow(CurrentTasMovie.InputLogLength, false); + } + + var macro = new MovieZone( + CurrentTasMovie, + TasView.FirstSelectedIndex ?? 0, + TasView.LastSelectedIndex ?? 0 - TasView.FirstSelectedIndex ?? 0 + 1); + MacroInputTool.SaveMacroAs(macro); } @@ -250,10 +253,10 @@ namespace BizHawk.Client.EmuHawk return; } - MovieZone macro = MacroInputTool.LoadMacro(); + var macro = MacroInputTool.LoadMacro(); if (macro != null) { - macro.Start = TasView.FirstSelectedIndex.Value; + macro.Start = TasView.FirstSelectedIndex ?? 0; macro.PlaceZone(CurrentTasMovie); } } @@ -285,7 +288,7 @@ namespace BizHawk.Client.EmuHawk MessageStatusLabel.Text = "Exporting to .bk2..."; Cursor = Cursors.WaitCursor; Update(); - string d_exp = " not exported."; + string exportResult = " not exported."; var file = new FileInfo(bk2.Filename); if (file.Exists) { @@ -301,13 +304,13 @@ namespace BizHawk.Client.EmuHawk if (result == DialogResult.Yes) { bk2.Save(); - d_exp = " exported."; + exportResult = " exported."; } } else { bk2.Save(); - d_exp = " exported."; + exportResult = " exported."; } if (Settings.AutosaveInterval > 0) @@ -315,7 +318,7 @@ namespace BizHawk.Client.EmuHawk _autosaveTimer.Start(); } - MessageStatusLabel.Text = bk2.Name + d_exp; + MessageStatusLabel.Text = bk2.Name + exportResult; Cursor = Cursors.Default; } @@ -345,7 +348,7 @@ namespace BizHawk.Client.EmuHawk ReselectClipboardMenuItem.Enabled = PasteMenuItem.Enabled = PasteInsertMenuItem.Enabled = TasView.AnyRowsSelected - && Clipboard.GetDataObject().GetDataPresent(DataFormats.StringFormat); + && (Clipboard.GetDataObject()?.GetDataPresent(DataFormats.StringFormat) ?? false); ClearGreenzoneMenuItem.Enabled = CurrentTasMovie != null && CurrentTasMovie.TasStateManager.Any(); @@ -435,8 +438,8 @@ namespace BizHawk.Client.EmuHawk { if (TasView.AnyRowsSelected) { - var prevMarker = CurrentTasMovie.Markers.PreviousOrCurrent(TasView.LastSelectedIndex.Value); - var nextMarker = CurrentTasMovie.Markers.Next(TasView.LastSelectedIndex.Value); + var prevMarker = CurrentTasMovie.Markers.PreviousOrCurrent(TasView.LastSelectedIndex ?? 0); + var nextMarker = CurrentTasMovie.Markers.Next(TasView.LastSelectedIndex ?? 0); int prev = prevMarker?.Frame ?? 0; int next = nextMarker?.Frame ?? CurrentTasMovie.InputLogLength; @@ -499,7 +502,7 @@ namespace BizHawk.Client.EmuHawk // TODO: copy paste from PasteInsertMenuItem_Click! IDataObject data = Clipboard.GetDataObject(); - if (data.GetDataPresent(DataFormats.StringFormat)) + if (data != null && data.GetDataPresent(DataFormats.StringFormat)) { string input = (string)data.GetData(DataFormats.StringFormat); if (!string.IsNullOrWhiteSpace(input)) @@ -520,7 +523,7 @@ namespace BizHawk.Client.EmuHawk } var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; - CurrentTasMovie.CopyOverInput(TasView.FirstSelectedIndex.Value, _tasClipboard.Select(x => x.ControllerState)); + CurrentTasMovie.CopyOverInput(TasView.FirstSelectedIndex ?? 0, _tasClipboard.Select(x => x.ControllerState)); if (needsToRollback) { GoToLastEmulatedFrameIfNecessary(TasView.FirstSelectedIndex.Value); @@ -542,7 +545,7 @@ namespace BizHawk.Client.EmuHawk { // copy paste from PasteMenuItem_Click! IDataObject data = Clipboard.GetDataObject(); - if (data.GetDataPresent(DataFormats.StringFormat)) + if (data != null && data.GetDataPresent(DataFormats.StringFormat)) { string input = (string)data.GetData(DataFormats.StringFormat); if (!string.IsNullOrWhiteSpace(input)) @@ -565,7 +568,7 @@ namespace BizHawk.Client.EmuHawk } var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; - CurrentTasMovie.InsertInput(TasView.FirstSelectedIndex.Value, _tasClipboard.Select(x => x.ControllerState)); + CurrentTasMovie.InsertInput(TasView.FirstSelectedIndex ?? 0, _tasClipboard.Select(x => x.ControllerState)); if (needsToRollback) { GoToLastEmulatedFrameIfNecessary(TasView.FirstSelectedIndex.Value); @@ -586,7 +589,7 @@ namespace BizHawk.Client.EmuHawk if (TasView.AnyRowsSelected) { var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; - var rollBackFrame = TasView.FirstSelectedIndex.Value; + var rollBackFrame = TasView.FirstSelectedIndex ?? 0; _tasClipboard.Clear(); var list = TasView.SelectedRows.ToArray(); @@ -626,9 +629,8 @@ namespace BizHawk.Client.EmuHawk { if (TasView.AnyRowsSelected) { - bool wasPaused = Mainform.EmulatorPaused; - bool needsToRollback = !(TasView.FirstSelectedIndex > Emulator.Frame); - int rollBackFrame = TasView.FirstSelectedIndex.Value; + bool needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; + int rollBackFrame = TasView.FirstSelectedIndex ?? 0; CurrentTasMovie.ChangeLog.BeginNewBatch($"Clear frames {TasView.SelectedRows.Min()}-{TasView.SelectedRows.Max()}"); foreach (int frame in TasView.SelectedRows) @@ -655,7 +657,7 @@ namespace BizHawk.Client.EmuHawk if (TasView.AnyRowsSelected) { var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; - var rollBackFrame = TasView.FirstSelectedIndex.Value; + var rollBackFrame = TasView.FirstSelectedIndex ?? 0; if (rollBackFrame >= CurrentTasMovie.InputLogLength) { // Cannot delete non-existent frames @@ -682,9 +684,8 @@ namespace BizHawk.Client.EmuHawk { if (TasView.AnyRowsSelected) { - var wasPaused = Mainform.EmulatorPaused; var framesToInsert = TasView.SelectedRows; - var insertionFrame = Math.Min(TasView.LastSelectedIndex.Value + 1, CurrentTasMovie.InputLogLength); + var insertionFrame = Math.Min(TasView.LastSelectedIndex ?? 0 + 1, CurrentTasMovie.InputLogLength); var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; var inputLog = framesToInsert @@ -708,8 +709,7 @@ namespace BizHawk.Client.EmuHawk { if (TasView.AnyRowsSelected) { - var wasPaused = Mainform.EmulatorPaused; - var insertionFrame = TasView.AnyRowsSelected ? TasView.FirstSelectedIndex.Value : 0; + var insertionFrame = TasView.FirstSelectedIndex ?? 0; var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; CurrentTasMovie.InsertEmptyFrame(insertionFrame); @@ -730,8 +730,8 @@ namespace BizHawk.Client.EmuHawk { if (TasView.AnyRowsSelected) { - int insertionFrame = TasView.FirstSelectedIndex.Value; - var framesPrompt = new FramesPrompt(); + int insertionFrame = TasView.FirstSelectedIndex ?? 0; + using var framesPrompt = new FramesPrompt(); DialogResult result = framesPrompt.ShowDialog(); if (result == DialogResult.OK) { @@ -744,7 +744,7 @@ namespace BizHawk.Client.EmuHawk { if (TasView.AnyRowsSelected) { - var rollbackFrame = TasView.LastSelectedIndex.Value; + var rollbackFrame = TasView.LastSelectedIndex ?? 0; var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; CurrentTasMovie.Truncate(rollbackFrame); @@ -853,97 +853,89 @@ namespace BizHawk.Client.EmuHawk private void SetMaxUndoLevelsMenuItem_Click(object sender, EventArgs e) { - using (var prompt = new InputPrompt + using var prompt = new InputPrompt { TextInputType = InputPrompt.InputType.Unsigned, Message = "Number of Undo Levels to keep", InitialValue = CurrentTasMovie.ChangeLog.MaxSteps.ToString() - }) + }; + DialogResult result = prompt.ShowDialog(); + if (result == DialogResult.OK) { - DialogResult result = prompt.ShowDialog(); - if (result == DialogResult.OK) + int val = 0; + try { - int val = 0; - try - { - val = int.Parse(prompt.PromptText); - } - catch - { - MessageBox.Show("Invalid Entry.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + val = int.Parse(prompt.PromptText); + } + catch + { + MessageBox.Show("Invalid Entry.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } - if (val > 0) - { - CurrentTasMovie.ChangeLog.MaxSteps = val; - } + if (val > 0) + { + CurrentTasMovie.ChangeLog.MaxSteps = val; } } } private void SetBranchCellHoverIntervalMenuItem_Click(object sender, EventArgs e) { - using (var prompt = new InputPrompt + using var prompt = new InputPrompt { TextInputType = InputPrompt.InputType.Unsigned, Message = "ScreenshotPopUp Delay", InitialValue = Settings.BranchCellHoverInterval.ToString() - }) + }; + DialogResult result = prompt.ShowDialog(); + if (result == DialogResult.OK) { - DialogResult result = prompt.ShowDialog(); - if (result == DialogResult.OK) + int val = int.Parse(prompt.PromptText); + if (val > 0) { - int val = int.Parse(prompt.PromptText); - if (val > 0) - { - Settings.BranchCellHoverInterval = val; - BookMarkControl.HoverInterval = val; - } + Settings.BranchCellHoverInterval = val; + BookMarkControl.HoverInterval = val; } } } private void SetSeekingCutoffIntervalMenuItem_Click(object sender, EventArgs e) { - using (var prompt = new InputPrompt + using var prompt = new InputPrompt { TextInputType = InputPrompt.InputType.Unsigned, Message = "Seeking Cutoff Interval", InitialValue = Settings.SeekingCutoffInterval.ToString() - }) + }; + DialogResult result = prompt.ShowDialog(); + if (result == DialogResult.OK) { - DialogResult result = prompt.ShowDialog(); - if (result == DialogResult.OK) + int val = int.Parse(prompt.PromptText); + if (val > 0) { - int val = int.Parse(prompt.PromptText); - if (val > 0) - { - Settings.SeekingCutoffInterval = val; - TasView.SeekingCutoffInterval = val; - } + Settings.SeekingCutoffInterval = val; + TasView.SeekingCutoffInterval = val; } } } private void SetAutosaveIntervalMenuItem_Click(object sender, EventArgs e) { - using (var prompt = new InputPrompt + using var prompt = new InputPrompt { TextInputType = InputPrompt.InputType.Unsigned, Message = "Autosave Interval in seconds\nSet to 0 to disable", InitialValue = (Settings.AutosaveInterval / 1000).ToString() - }) + }; + DialogResult result = prompt.ShowDialog(); + if (result == DialogResult.OK) { - DialogResult result = prompt.ShowDialog(); - if (result == DialogResult.OK) + uint val = uint.Parse(prompt.PromptText) * 1000; + Settings.AutosaveInterval = val; + if (val > 0) { - uint val = uint.Parse(prompt.PromptText) * 1000; - Settings.AutosaveInterval = val; - if (val > 0) - { - _autosaveTimer.Interval = (int)val; - _autosaveTimer.Start(); - } + _autosaveTimer.Interval = (int)val; + _autosaveTimer.Start(); } } } @@ -1400,6 +1392,7 @@ namespace BizHawk.Client.EmuHawk TasView.AllColumns.ColumnsChanged(); } + // ReSharper disable once UnusedMember.Local [RestoreDefaults] private void RestoreDefaults() { @@ -1432,7 +1425,8 @@ namespace BizHawk.Client.EmuHawk pasteToolStripMenuItem.Enabled = pasteInsertToolStripMenuItem.Enabled = - Clipboard.GetDataObject().GetDataPresent(DataFormats.StringFormat) && TasView.AnyRowsSelected; + (Clipboard.GetDataObject()?.GetDataPresent(DataFormats.StringFormat) ?? false) + && TasView.AnyRowsSelected; StartNewProjectFromNowMenuItem.Visible = TasView.SelectedRows.Count() == 1 diff --git a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs index 53dfaf3f1a..59b7320f96 100644 --- a/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs +++ b/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs @@ -179,7 +179,7 @@ namespace BizHawk.Client.EmuHawk } else { - SaveTas(sender, e); + SaveTas(); } } } diff --git a/BizHawk.sln.DotSettings b/BizHawk.sln.DotSettings index 2302a1e233..41e7eca770 100644 --- a/BizHawk.sln.DotSettings +++ b/BizHawk.sln.DotSettings @@ -202,6 +202,7 @@ True True True + True True True True