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

This commit is contained in:
adelikat 2019-11-24 16:54:08 -06:00
parent 1f4a2274d3
commit 5bbdf2a49e
4 changed files with 80 additions and 85 deletions

View File

@ -130,7 +130,7 @@ namespace BizHawk.Client.EmuHawk
if (result == DialogResult.Yes) if (result == DialogResult.Yes)
{ {
_exiting = true; // Asking to save changes should only ever be called when closing something _exiting = true; // Asking to save changes should only ever be called when closing something
SaveTas(null, null); SaveTas();
} }
else if (result == DialogResult.No) else if (result == DialogResult.No)
{ {

View File

@ -95,7 +95,7 @@ namespace BizHawk.Client.EmuHawk
private bool _exiting; private bool _exiting;
private void SaveTas(object sender, EventArgs e) private void SaveTas()
{ {
if (string.IsNullOrEmpty(CurrentTasMovie.Filename) if (string.IsNullOrEmpty(CurrentTasMovie.Filename)
|| CurrentTasMovie.Filename == DefaultTasProjName()) || CurrentTasMovie.Filename == DefaultTasProjName())
@ -125,7 +125,7 @@ namespace BizHawk.Client.EmuHawk
// call this one from the menu only // call this one from the menu only
private void SaveTasMenuItem_Click(object sender, EventArgs e) private void SaveTasMenuItem_Click(object sender, EventArgs e)
{ {
SaveTas(sender, e); SaveTas();
if (Settings.BackupPerFileSave) if (Settings.BackupPerFileSave)
{ {
SaveBackupMenuItem_Click(sender, e); SaveBackupMenuItem_Click(sender, e);
@ -228,18 +228,21 @@ namespace BizHawk.Client.EmuHawk
private void SaveSelectionToMacroMenuItem_Click(object sender, EventArgs e) private void SaveSelectionToMacroMenuItem_Click(object sender, EventArgs e)
{ {
if (TasView.LastSelectedIndex == CurrentTasMovie.InputLogLength)
{
TasView.SelectRow(CurrentTasMovie.InputLogLength, false);
}
if (!TasView.AnyRowsSelected) if (!TasView.AnyRowsSelected)
{ {
return; return;
} }
MovieZone macro = new MovieZone(CurrentTasMovie, TasView.FirstSelectedIndex.Value, if (TasView.LastSelectedIndex == CurrentTasMovie.InputLogLength)
TasView.LastSelectedIndex.Value - TasView.FirstSelectedIndex.Value + 1); {
TasView.SelectRow(CurrentTasMovie.InputLogLength, false);
}
var macro = new MovieZone(
CurrentTasMovie,
TasView.FirstSelectedIndex ?? 0,
TasView.LastSelectedIndex ?? 0 - TasView.FirstSelectedIndex ?? 0 + 1);
MacroInputTool.SaveMacroAs(macro); MacroInputTool.SaveMacroAs(macro);
} }
@ -250,10 +253,10 @@ namespace BizHawk.Client.EmuHawk
return; return;
} }
MovieZone macro = MacroInputTool.LoadMacro(); var macro = MacroInputTool.LoadMacro();
if (macro != null) if (macro != null)
{ {
macro.Start = TasView.FirstSelectedIndex.Value; macro.Start = TasView.FirstSelectedIndex ?? 0;
macro.PlaceZone(CurrentTasMovie); macro.PlaceZone(CurrentTasMovie);
} }
} }
@ -285,7 +288,7 @@ namespace BizHawk.Client.EmuHawk
MessageStatusLabel.Text = "Exporting to .bk2..."; MessageStatusLabel.Text = "Exporting to .bk2...";
Cursor = Cursors.WaitCursor; Cursor = Cursors.WaitCursor;
Update(); Update();
string d_exp = " not exported."; string exportResult = " not exported.";
var file = new FileInfo(bk2.Filename); var file = new FileInfo(bk2.Filename);
if (file.Exists) if (file.Exists)
{ {
@ -301,13 +304,13 @@ namespace BizHawk.Client.EmuHawk
if (result == DialogResult.Yes) if (result == DialogResult.Yes)
{ {
bk2.Save(); bk2.Save();
d_exp = " exported."; exportResult = " exported.";
} }
} }
else else
{ {
bk2.Save(); bk2.Save();
d_exp = " exported."; exportResult = " exported.";
} }
if (Settings.AutosaveInterval > 0) if (Settings.AutosaveInterval > 0)
@ -315,7 +318,7 @@ namespace BizHawk.Client.EmuHawk
_autosaveTimer.Start(); _autosaveTimer.Start();
} }
MessageStatusLabel.Text = bk2.Name + d_exp; MessageStatusLabel.Text = bk2.Name + exportResult;
Cursor = Cursors.Default; Cursor = Cursors.Default;
} }
@ -345,7 +348,7 @@ namespace BizHawk.Client.EmuHawk
ReselectClipboardMenuItem.Enabled = ReselectClipboardMenuItem.Enabled =
PasteMenuItem.Enabled = PasteMenuItem.Enabled =
PasteInsertMenuItem.Enabled = TasView.AnyRowsSelected PasteInsertMenuItem.Enabled = TasView.AnyRowsSelected
&& Clipboard.GetDataObject().GetDataPresent(DataFormats.StringFormat); && (Clipboard.GetDataObject()?.GetDataPresent(DataFormats.StringFormat) ?? false);
ClearGreenzoneMenuItem.Enabled = ClearGreenzoneMenuItem.Enabled =
CurrentTasMovie != null && CurrentTasMovie.TasStateManager.Any(); CurrentTasMovie != null && CurrentTasMovie.TasStateManager.Any();
@ -435,8 +438,8 @@ namespace BizHawk.Client.EmuHawk
{ {
if (TasView.AnyRowsSelected) if (TasView.AnyRowsSelected)
{ {
var prevMarker = CurrentTasMovie.Markers.PreviousOrCurrent(TasView.LastSelectedIndex.Value); var prevMarker = CurrentTasMovie.Markers.PreviousOrCurrent(TasView.LastSelectedIndex ?? 0);
var nextMarker = CurrentTasMovie.Markers.Next(TasView.LastSelectedIndex.Value); var nextMarker = CurrentTasMovie.Markers.Next(TasView.LastSelectedIndex ?? 0);
int prev = prevMarker?.Frame ?? 0; int prev = prevMarker?.Frame ?? 0;
int next = nextMarker?.Frame ?? CurrentTasMovie.InputLogLength; int next = nextMarker?.Frame ?? CurrentTasMovie.InputLogLength;
@ -499,7 +502,7 @@ namespace BizHawk.Client.EmuHawk
// TODO: copy paste from PasteInsertMenuItem_Click! // TODO: copy paste from PasteInsertMenuItem_Click!
IDataObject data = Clipboard.GetDataObject(); IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.StringFormat)) if (data != null && data.GetDataPresent(DataFormats.StringFormat))
{ {
string input = (string)data.GetData(DataFormats.StringFormat); string input = (string)data.GetData(DataFormats.StringFormat);
if (!string.IsNullOrWhiteSpace(input)) if (!string.IsNullOrWhiteSpace(input))
@ -520,7 +523,7 @@ namespace BizHawk.Client.EmuHawk
} }
var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; 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) if (needsToRollback)
{ {
GoToLastEmulatedFrameIfNecessary(TasView.FirstSelectedIndex.Value); GoToLastEmulatedFrameIfNecessary(TasView.FirstSelectedIndex.Value);
@ -542,7 +545,7 @@ namespace BizHawk.Client.EmuHawk
{ {
// copy paste from PasteMenuItem_Click! // copy paste from PasteMenuItem_Click!
IDataObject data = Clipboard.GetDataObject(); IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.StringFormat)) if (data != null && data.GetDataPresent(DataFormats.StringFormat))
{ {
string input = (string)data.GetData(DataFormats.StringFormat); string input = (string)data.GetData(DataFormats.StringFormat);
if (!string.IsNullOrWhiteSpace(input)) if (!string.IsNullOrWhiteSpace(input))
@ -565,7 +568,7 @@ namespace BizHawk.Client.EmuHawk
} }
var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; 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) if (needsToRollback)
{ {
GoToLastEmulatedFrameIfNecessary(TasView.FirstSelectedIndex.Value); GoToLastEmulatedFrameIfNecessary(TasView.FirstSelectedIndex.Value);
@ -586,7 +589,7 @@ namespace BizHawk.Client.EmuHawk
if (TasView.AnyRowsSelected) if (TasView.AnyRowsSelected)
{ {
var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame;
var rollBackFrame = TasView.FirstSelectedIndex.Value; var rollBackFrame = TasView.FirstSelectedIndex ?? 0;
_tasClipboard.Clear(); _tasClipboard.Clear();
var list = TasView.SelectedRows.ToArray(); var list = TasView.SelectedRows.ToArray();
@ -626,9 +629,8 @@ namespace BizHawk.Client.EmuHawk
{ {
if (TasView.AnyRowsSelected) if (TasView.AnyRowsSelected)
{ {
bool wasPaused = Mainform.EmulatorPaused; bool needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame;
bool needsToRollback = !(TasView.FirstSelectedIndex > Emulator.Frame); int rollBackFrame = TasView.FirstSelectedIndex ?? 0;
int rollBackFrame = TasView.FirstSelectedIndex.Value;
CurrentTasMovie.ChangeLog.BeginNewBatch($"Clear frames {TasView.SelectedRows.Min()}-{TasView.SelectedRows.Max()}"); CurrentTasMovie.ChangeLog.BeginNewBatch($"Clear frames {TasView.SelectedRows.Min()}-{TasView.SelectedRows.Max()}");
foreach (int frame in TasView.SelectedRows) foreach (int frame in TasView.SelectedRows)
@ -655,7 +657,7 @@ namespace BizHawk.Client.EmuHawk
if (TasView.AnyRowsSelected) if (TasView.AnyRowsSelected)
{ {
var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame;
var rollBackFrame = TasView.FirstSelectedIndex.Value; var rollBackFrame = TasView.FirstSelectedIndex ?? 0;
if (rollBackFrame >= CurrentTasMovie.InputLogLength) if (rollBackFrame >= CurrentTasMovie.InputLogLength)
{ {
// Cannot delete non-existent frames // Cannot delete non-existent frames
@ -682,9 +684,8 @@ namespace BizHawk.Client.EmuHawk
{ {
if (TasView.AnyRowsSelected) if (TasView.AnyRowsSelected)
{ {
var wasPaused = Mainform.EmulatorPaused;
var framesToInsert = TasView.SelectedRows; 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 needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame;
var inputLog = framesToInsert var inputLog = framesToInsert
@ -708,8 +709,7 @@ namespace BizHawk.Client.EmuHawk
{ {
if (TasView.AnyRowsSelected) if (TasView.AnyRowsSelected)
{ {
var wasPaused = Mainform.EmulatorPaused; var insertionFrame = TasView.FirstSelectedIndex ?? 0;
var insertionFrame = TasView.AnyRowsSelected ? TasView.FirstSelectedIndex.Value : 0;
var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame;
CurrentTasMovie.InsertEmptyFrame(insertionFrame); CurrentTasMovie.InsertEmptyFrame(insertionFrame);
@ -730,8 +730,8 @@ namespace BizHawk.Client.EmuHawk
{ {
if (TasView.AnyRowsSelected) if (TasView.AnyRowsSelected)
{ {
int insertionFrame = TasView.FirstSelectedIndex.Value; int insertionFrame = TasView.FirstSelectedIndex ?? 0;
var framesPrompt = new FramesPrompt(); using var framesPrompt = new FramesPrompt();
DialogResult result = framesPrompt.ShowDialog(); DialogResult result = framesPrompt.ShowDialog();
if (result == DialogResult.OK) if (result == DialogResult.OK)
{ {
@ -744,7 +744,7 @@ namespace BizHawk.Client.EmuHawk
{ {
if (TasView.AnyRowsSelected) if (TasView.AnyRowsSelected)
{ {
var rollbackFrame = TasView.LastSelectedIndex.Value; var rollbackFrame = TasView.LastSelectedIndex ?? 0;
var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame; var needsToRollback = TasView.FirstSelectedIndex < Emulator.Frame;
CurrentTasMovie.Truncate(rollbackFrame); CurrentTasMovie.Truncate(rollbackFrame);
@ -853,97 +853,89 @@ namespace BizHawk.Client.EmuHawk
private void SetMaxUndoLevelsMenuItem_Click(object sender, EventArgs e) private void SetMaxUndoLevelsMenuItem_Click(object sender, EventArgs e)
{ {
using (var prompt = new InputPrompt using var prompt = new InputPrompt
{ {
TextInputType = InputPrompt.InputType.Unsigned, TextInputType = InputPrompt.InputType.Unsigned,
Message = "Number of Undo Levels to keep", Message = "Number of Undo Levels to keep",
InitialValue = CurrentTasMovie.ChangeLog.MaxSteps.ToString() InitialValue = CurrentTasMovie.ChangeLog.MaxSteps.ToString()
}) };
DialogResult result = prompt.ShowDialog();
if (result == DialogResult.OK)
{ {
DialogResult result = prompt.ShowDialog(); int val = 0;
if (result == DialogResult.OK) try
{ {
int val = 0; val = int.Parse(prompt.PromptText);
try }
{ catch
val = int.Parse(prompt.PromptText); {
} MessageBox.Show("Invalid Entry.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
catch }
{
MessageBox.Show("Invalid Entry.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (val > 0) if (val > 0)
{ {
CurrentTasMovie.ChangeLog.MaxSteps = val; CurrentTasMovie.ChangeLog.MaxSteps = val;
}
} }
} }
} }
private void SetBranchCellHoverIntervalMenuItem_Click(object sender, EventArgs e) private void SetBranchCellHoverIntervalMenuItem_Click(object sender, EventArgs e)
{ {
using (var prompt = new InputPrompt using var prompt = new InputPrompt
{ {
TextInputType = InputPrompt.InputType.Unsigned, TextInputType = InputPrompt.InputType.Unsigned,
Message = "ScreenshotPopUp Delay", Message = "ScreenshotPopUp Delay",
InitialValue = Settings.BranchCellHoverInterval.ToString() InitialValue = Settings.BranchCellHoverInterval.ToString()
}) };
DialogResult result = prompt.ShowDialog();
if (result == DialogResult.OK)
{ {
DialogResult result = prompt.ShowDialog(); int val = int.Parse(prompt.PromptText);
if (result == DialogResult.OK) if (val > 0)
{ {
int val = int.Parse(prompt.PromptText); Settings.BranchCellHoverInterval = val;
if (val > 0) BookMarkControl.HoverInterval = val;
{
Settings.BranchCellHoverInterval = val;
BookMarkControl.HoverInterval = val;
}
} }
} }
} }
private void SetSeekingCutoffIntervalMenuItem_Click(object sender, EventArgs e) private void SetSeekingCutoffIntervalMenuItem_Click(object sender, EventArgs e)
{ {
using (var prompt = new InputPrompt using var prompt = new InputPrompt
{ {
TextInputType = InputPrompt.InputType.Unsigned, TextInputType = InputPrompt.InputType.Unsigned,
Message = "Seeking Cutoff Interval", Message = "Seeking Cutoff Interval",
InitialValue = Settings.SeekingCutoffInterval.ToString() InitialValue = Settings.SeekingCutoffInterval.ToString()
}) };
DialogResult result = prompt.ShowDialog();
if (result == DialogResult.OK)
{ {
DialogResult result = prompt.ShowDialog(); int val = int.Parse(prompt.PromptText);
if (result == DialogResult.OK) if (val > 0)
{ {
int val = int.Parse(prompt.PromptText); Settings.SeekingCutoffInterval = val;
if (val > 0) TasView.SeekingCutoffInterval = val;
{
Settings.SeekingCutoffInterval = val;
TasView.SeekingCutoffInterval = val;
}
} }
} }
} }
private void SetAutosaveIntervalMenuItem_Click(object sender, EventArgs e) private void SetAutosaveIntervalMenuItem_Click(object sender, EventArgs e)
{ {
using (var prompt = new InputPrompt using var prompt = new InputPrompt
{ {
TextInputType = InputPrompt.InputType.Unsigned, TextInputType = InputPrompt.InputType.Unsigned,
Message = "Autosave Interval in seconds\nSet to 0 to disable", Message = "Autosave Interval in seconds\nSet to 0 to disable",
InitialValue = (Settings.AutosaveInterval / 1000).ToString() InitialValue = (Settings.AutosaveInterval / 1000).ToString()
}) };
DialogResult result = prompt.ShowDialog();
if (result == DialogResult.OK)
{ {
DialogResult result = prompt.ShowDialog(); uint val = uint.Parse(prompt.PromptText) * 1000;
if (result == DialogResult.OK) Settings.AutosaveInterval = val;
if (val > 0)
{ {
uint val = uint.Parse(prompt.PromptText) * 1000; _autosaveTimer.Interval = (int)val;
Settings.AutosaveInterval = val; _autosaveTimer.Start();
if (val > 0)
{
_autosaveTimer.Interval = (int)val;
_autosaveTimer.Start();
}
} }
} }
} }
@ -1400,6 +1392,7 @@ namespace BizHawk.Client.EmuHawk
TasView.AllColumns.ColumnsChanged(); TasView.AllColumns.ColumnsChanged();
} }
// ReSharper disable once UnusedMember.Local
[RestoreDefaults] [RestoreDefaults]
private void RestoreDefaults() private void RestoreDefaults()
{ {
@ -1432,7 +1425,8 @@ namespace BizHawk.Client.EmuHawk
pasteToolStripMenuItem.Enabled = pasteToolStripMenuItem.Enabled =
pasteInsertToolStripMenuItem.Enabled = pasteInsertToolStripMenuItem.Enabled =
Clipboard.GetDataObject().GetDataPresent(DataFormats.StringFormat) && TasView.AnyRowsSelected; (Clipboard.GetDataObject()?.GetDataPresent(DataFormats.StringFormat) ?? false)
&& TasView.AnyRowsSelected;
StartNewProjectFromNowMenuItem.Visible = StartNewProjectFromNowMenuItem.Visible =
TasView.SelectedRows.Count() == 1 TasView.SelectedRows.Count() == 1

View File

@ -179,7 +179,7 @@ namespace BizHawk.Client.EmuHawk
} }
else else
{ {
SaveTas(sender, e); SaveTas();
} }
} }
} }

View File

@ -202,6 +202,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dega/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Dega/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=delaminated/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=delaminated/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dendy/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Dendy/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=desync/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Dipswitch/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Dipswitch/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Disasm/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Disasm/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Disassemblable/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Disassemblable/@EntryIndexedValue">True</s:Boolean>