From 720cf763cd04584735dd3e9ef793bf0a3934c40f Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 25 Nov 2013 02:08:45 +0000 Subject: [PATCH] More code refactoring --- BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs | 51 +- .../tools/Lua/LuaConsole.cs | 125 ++--- BizHawk.Client.EmuHawk/tools/ToolHelpers.cs | 31 +- BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs | 29 +- .../tools/Watch/RamSearch.cs | 501 ++++++++---------- .../tools/Watch/RamWatch.cs | 362 ++++++------- .../tools/Watch/WatchEditor.cs | 2 +- 7 files changed, 463 insertions(+), 638 deletions(-) diff --git a/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs b/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs index ce6da5f129..32a7e49ab2 100644 --- a/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs +++ b/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs @@ -85,14 +85,14 @@ namespace BizHawk.Client.EmuHawk public void UpdateDialog() { CheatListView.ItemCount = Global.CheatList.Count; - TotalLabel.Text = Global.CheatList.CheatCount.ToString() + TotalLabel.Text = Global.CheatList.CheatCount + (Global.CheatList.CheatCount == 1 ? " cheat " : " cheats ") - + Global.CheatList.ActiveCount.ToString() + " active"; + + Global.CheatList.ActiveCount + " active"; } public void LoadFileFromRecent(string path) { - bool ask_result = true; + var ask_result = true; if (Global.CheatList.Changes) { ask_result = AskSave(); @@ -100,7 +100,7 @@ namespace BizHawk.Client.EmuHawk if (ask_result) { - bool load_result = Global.CheatList.Load(path, append: false); + var load_result = Global.CheatList.Load(path, append: false); if (!load_result) { ToolHelpers.HandleLoadError(Global.Config.RecentWatches, path); @@ -140,7 +140,7 @@ namespace BizHawk.Client.EmuHawk if (Global.CheatList.Changes) { GlobalWin.Sound.StopSound(); - DialogResult result = MessageBox.Show("Save Changes?", "Cheats", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); + var result = MessageBox.Show("Save Changes?", "Cheats", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); GlobalWin.Sound.StartSound(); if (result == DialogResult.Yes) { @@ -160,11 +160,11 @@ namespace BizHawk.Client.EmuHawk return true; } - private void LoadFile(FileInfo file, bool append) + private void LoadFile(FileSystemInfo file, bool append) { if (file != null) { - bool result = true; + var result = true; if (Global.CheatList.Changes) { result = AskSave(); @@ -180,17 +180,10 @@ namespace BizHawk.Client.EmuHawk } } - private bool SaveAs() + private static bool SaveAs() { var file = ToolHelpers.GetCheatSaveFileFromUser(Global.CheatList.CurrentFileName); - if (file != null) - { - return Global.CheatList.SaveFile(file.FullName); - } - else - { - return false; - } + return file != null && Global.CheatList.SaveFile(file.FullName); } private void NewCheatForm_Load(object sender, EventArgs e) @@ -281,7 +274,7 @@ namespace BizHawk.Client.EmuHawk return; } - string columnName = CheatListView.Columns[column].Name; + var columnName = CheatListView.Columns[column].Name; switch (columnName) { @@ -332,7 +325,7 @@ namespace BizHawk.Client.EmuHawk private IEnumerable SelectedIndices { - get { return CheatListView.SelectedIndices.Cast().ToList(); } + get { return CheatListView.SelectedIndices.Cast(); } } private IEnumerable SelectedItems @@ -342,7 +335,7 @@ namespace BizHawk.Client.EmuHawk private IEnumerable SelectedCheats { - get { return SelectedItems.Where(x => !x.IsSeparator).ToList(); } + get { return SelectedItems.Where(x => !x.IsSeparator); } } private void MoveUp() @@ -363,13 +356,13 @@ namespace BizHawk.Client.EmuHawk UpdateMessageLabel(); var newindices = new List(); - for (int i = 0; i < indices.Count; i++) + for (var i = 0; i < indices.Count; i++) { newindices.Add(indices[i] - 1); } CheatListView.SelectedIndices.Clear(); - foreach (int newi in newindices) + foreach (var newi in newindices) { CheatListView.SelectItem(newi, true); } @@ -399,13 +392,13 @@ namespace BizHawk.Client.EmuHawk UpdateMessageLabel(); var newindices = new List(); - for (int i = 0; i < indices.Count; i++) + for (var i = 0; i < indices.Count; i++) { newindices.Add(indices[i] + 1); } CheatListView.SelectedIndices.Clear(); - foreach (int newi in newindices) + foreach (var newi in newindices) { CheatListView.SelectItem(newi, true); } @@ -521,7 +514,7 @@ namespace BizHawk.Client.EmuHawk private void NewList() { - bool result = true; + var result = true; if (Global.CheatList.Changes) { result = AskSave(); @@ -535,9 +528,9 @@ namespace BizHawk.Client.EmuHawk public string GenerateDefaultCheatFilename() { - PathEntry pathEntry = Global.Config.PathEntries[Global.Emulator.SystemId, "Cheats"] ?? + var pathEntry = Global.Config.PathEntries[Global.Emulator.SystemId, "Cheats"] ?? Global.Config.PathEntries[Global.Emulator.SystemId, "Base"]; - string path = PathManager.MakeAbsolutePath(pathEntry.Path, Global.Emulator.SystemId); + var path = PathManager.MakeAbsolutePath(pathEntry.Path, Global.Emulator.SystemId); var f = new FileInfo(path); if (f.Directory != null && f.Directory.Exists == false) @@ -572,7 +565,7 @@ namespace BizHawk.Client.EmuHawk private void OpenMenuItem_Click(object sender, EventArgs e) { - bool append = sender == AppendMenuItem; + var append = sender == AppendMenuItem; LoadFile(ToolHelpers.GetCheatFileFromUser(Global.CheatList.CurrentFileName), append); } @@ -674,7 +667,7 @@ namespace BizHawk.Client.EmuHawk private void SelectAllMenuItem_Click(object sender, EventArgs e) { - for (int i = 0; i < Global.CheatList.Count; i++) + for (var i = 0; i < Global.CheatList.Count; i++) { CheatListView.SelectItem(i, true); } @@ -913,7 +906,7 @@ namespace BizHawk.Client.EmuHawk private void NewCheatForm_DragDrop(object sender, DragEventArgs e) { - string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); + var filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); if (Path.GetExtension(filePaths[0]) == (".cht")) { LoadFile(new FileInfo(filePaths[0]), append: false); diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index 7ff22acdc8..e33eea1f79 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -115,9 +115,9 @@ namespace BizHawk.Client.EmuHawk private void StopAllScripts() { - foreach (LuaFile t in _luaList) + foreach (var file in _luaList) { - t.Enabled = false; + file.Enabled = false; } Changes(true); } @@ -176,7 +176,7 @@ namespace BizHawk.Client.EmuHawk { if (LuaAlreadyInSession(path) == false) { - LuaFile luaFile = new LuaFile(String.Empty, path); + var luaFile = new LuaFile(String.Empty, path); _luaList.Add(luaFile); LuaListView.ItemCount = _luaList.Count; LuaListView.Refresh(); @@ -205,11 +205,11 @@ namespace BizHawk.Client.EmuHawk } else { - foreach (LuaFile t in _luaList) + foreach (var file in _luaList) { - if (path == t.Path && t.Enabled == false && !Global.Config.DisableLuaScriptsOnLoad) + if (path == file.Path && file.Enabled == false && !Global.Config.DisableLuaScriptsOnLoad) { - t.Toggle(); + file.Toggle(); RunLuaScripts(); LuaListView.Refresh(); Changes(true); @@ -226,19 +226,19 @@ namespace BizHawk.Client.EmuHawk public void RunLuaScripts() { - for (int x = 0; x < _luaList.Count; x++) + for (var i = 0; i < _luaList.Count; i++) { - if (_luaList[x].Enabled && _luaList[x].Thread == null) + if (_luaList[i].Enabled && _luaList[i].Thread == null) { try { - _luaList[x].Thread = LuaImp.SpawnCoroutine(_luaList[x].Path); + _luaList[i].Thread = LuaImp.SpawnCoroutine(_luaList[i].Path); } catch (Exception e) { if (e.ToString().Substring(0, 32) == "LuaInterface.LuaScriptException:") { - _luaList[x].Enabled = false; + _luaList[i].Enabled = false; AddText(e.Message); } else MessageBox.Show(e.ToString()); @@ -246,24 +246,26 @@ namespace BizHawk.Client.EmuHawk } else { - StopScript(x); + StopScript(i); } } } private void UpdateNumberOfScripts() { - string message = ""; + var message = String.Empty; int active = 0, paused = 0, separators = 0; - foreach (LuaFile t in _luaList) + foreach (var file in _luaList) { - if (!t.IsSeparator) + if (!file.IsSeparator) { - if (t.Enabled) + if (file.Enabled) { active++; - if (t.Paused) + if (file.Paused) + { paused++; + } } } else @@ -272,19 +274,19 @@ namespace BizHawk.Client.EmuHawk } } - int L = _luaList.Count - separators; + var L = _luaList.Count - separators; if (L == 1) { - message += L.ToString() + " script (" + active.ToString() + " active, " + paused.ToString() + " paused)"; + message += L + " script (" + active + " active, " + paused + " paused)"; } else if (L == 0) { - message += L.ToString() + " script"; + message += L + " script"; } else { - message += L.ToString() + " scripts (" + active.ToString() + " active, " + paused.ToString() + " paused)"; + message += L + " scripts (" + active + " active, " + paused + " paused)"; } NumberOfScripts.Text = message; @@ -334,7 +336,7 @@ namespace BizHawk.Client.EmuHawk StopAllScripts(); _luaList = new List(); - using (StreamReader sr = file.OpenText()) + using (var sr = file.OpenText()) { string s; @@ -349,19 +351,12 @@ namespace BizHawk.Client.EmuHawk } else { - string temp = s.Substring(0, 1); + var temp = s.Substring(0, 1); bool enabled; try { - if (int.Parse(temp) == 0) - { - enabled = false; - } - else - { - enabled = true; - } + enabled = int.Parse(temp) != 0; } catch { @@ -370,12 +365,7 @@ namespace BizHawk.Client.EmuHawk s = s.Substring(2, s.Length - 2); //Get path - l = new LuaFile(s); - - if (!Global.Config.DisableLuaScriptsOnLoad) - l.Enabled = enabled; - else - l.Enabled = false; + l = new LuaFile(s) {Enabled = !Global.Config.DisableLuaScriptsOnLoad && enabled}; } _luaList.Add(l); } @@ -401,14 +391,14 @@ namespace BizHawk.Client.EmuHawk foreach (var lf in _luaList) { //save old current directory before this lua thread clobbers it for the .net thread - string oldcd = Environment.CurrentDirectory; + var oldcd = Environment.CurrentDirectory; try { //LuaImp.gui_clearGraphics(); if (lf.Enabled && lf.Thread != null && !(lf.Paused)) { - bool prohibit = lf.FrameWaiting && !includeFrameWaiters; + var prohibit = lf.FrameWaiting && !includeFrameWaiters; if (!prohibit) { //restore this lua thread's preferred current directory @@ -521,10 +511,10 @@ namespace BizHawk.Client.EmuHawk private void SaveSession(string path) { - using (StreamWriter sw = new StreamWriter(path)) + using (var sw = new StreamWriter(path)) { - string str = ""; - foreach (LuaFile t in _luaList) + var str = String.Empty; + foreach (var t in _luaList) { if (!t.IsSeparator) { @@ -552,7 +542,7 @@ namespace BizHawk.Client.EmuHawk public void LoadSessionFromRecent(string path) { - bool doload = true; + var doload = true; if (_changes) doload = AskSave(); if (doload) @@ -582,7 +572,7 @@ namespace BizHawk.Client.EmuHawk if (_changes) { GlobalWin.Sound.StopSound(); - DialogResult result = MessageBox.Show("Save changes to session?", "Lua Console", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); + var result = MessageBox.Show("Save changes to session?", "Lua Console", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); GlobalWin.Sound.StartSound(); if (result == DialogResult.Yes) { @@ -609,32 +599,17 @@ namespace BizHawk.Client.EmuHawk return true; } - private void DoLuaWriter() + private static void OpenLuaWriter(string path) { - ListView.SelectedIndexCollection indexes = LuaListView.SelectedIndices; - if (indexes.Count == 0) - return; - - if (indexes.Count > 0) - { - var item = _luaList[indexes[0]]; - if (!item.IsSeparator) - { - OpenLuaWriter(_luaList[indexes[0]].Path); - } - } - } - - private void OpenLuaWriter(string path) - { - LuaWriter writer = new LuaWriter {CurrentFile = path}; + var writer = new LuaWriter {CurrentFile = path}; writer.Show(); } private Point GetPromptPoint() { - Point p = new Point(LuaListView.Location.X + 30, LuaListView.Location.Y + 30); - return PointToScreen(p); + return PointToScreen( + new Point(LuaListView.Location.X + 30, LuaListView.Location.Y + 30) + ); } private IEnumerable SelectedIndices @@ -679,7 +654,7 @@ namespace BizHawk.Client.EmuHawk private void NewSessionMenuItem_Click(object sender, EventArgs e) { - bool result = !_changes || AskSave(); + var result = !_changes || AskSave(); if (result) { @@ -771,7 +746,7 @@ namespace BizHawk.Client.EmuHawk private void ToggleScriptMenuItem_Click(object sender, EventArgs e) { - foreach(int index in SelectedIndices) + foreach(var index in SelectedIndices) { var item = _luaList[index]; if (!item.IsSeparator) @@ -859,7 +834,7 @@ namespace BizHawk.Client.EmuHawk return; } - foreach (int index in indices) + foreach (var index in indices) { var file = _luaList[index]; _luaList.Remove(file); @@ -870,7 +845,7 @@ namespace BizHawk.Client.EmuHawk var newindices = indices.Select(t => t - 1).ToList(); LuaListView.SelectedIndices.Clear(); - foreach (int newi in newindices) + foreach (var newi in newindices) { LuaListView.SelectItem(newi, true); } @@ -886,7 +861,7 @@ namespace BizHawk.Client.EmuHawk return; } - foreach (int index in indices) + foreach (var index in indices) { var file = _luaList[index]; _luaList.Remove(file); @@ -896,7 +871,7 @@ namespace BizHawk.Client.EmuHawk var newindices = indices.Select(t => t + 1).ToList(); LuaListView.SelectedIndices.Clear(); - foreach (int newi in newindices) + foreach (var newi in newindices) { LuaListView.SelectItem(newi, true); } @@ -906,9 +881,9 @@ namespace BizHawk.Client.EmuHawk private void SelectAllMenuItem_Click(object sender, EventArgs e) { - for (int x = 0; x < _luaList.Count; x++) + for (var i = 0; i < _luaList.Count; i++) { - LuaListView.SelectItem(x, true); + LuaListView.SelectItem(i, true); } } @@ -921,7 +896,7 @@ namespace BizHawk.Client.EmuHawk { if (LuaImp.RegisteredFunctions.Any()) { - bool alreadyOpen = false; + var alreadyOpen = false; foreach (Form form in Application.OpenForms) { if (form is LuaRegisteredFunctionsList) @@ -1000,7 +975,7 @@ namespace BizHawk.Client.EmuHawk { if (VersionInfo.INTERIM) { - DoLuaWriter(); + SelectedFiles.ToList().ForEach(x => OpenLuaWriter(x.Path)); } else { @@ -1036,10 +1011,10 @@ namespace BizHawk.Client.EmuHawk private void LuaConsole_DragDrop(object sender, DragEventArgs e) { - string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); + var filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); try { - foreach (string path in filePaths) + foreach (var path in filePaths) { if (Path.GetExtension(path) == (".lua") || Path.GetExtension(path) == (".txt")) { diff --git a/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs b/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs index 7f57b8cd6e..5597a6d52f 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs @@ -125,9 +125,9 @@ namespace BizHawk.Client.EmuHawk } else { - foreach (string filename in recent) + foreach (var filename in recent) { - string temp = filename; + var temp = filename; var item = new ToolStripMenuItem { Text = temp }; item.Click += (o, ev) => loadFileCallback(temp); items.Add(item); @@ -146,7 +146,7 @@ namespace BizHawk.Client.EmuHawk public static void HandleLoadError(RecentFiles recent, string path) { GlobalWin.Sound.StopSound(); - DialogResult result = MessageBox.Show("Could not open " + path + "\nRemove from list?", "File not found", MessageBoxButtons.YesNo, MessageBoxIcon.Error); + var result = MessageBox.Show("Could not open " + path + "\nRemove from list?", "File not found", MessageBoxButtons.YesNo, MessageBoxIcon.Error); if (result == DialogResult.Yes) { recent.Remove(path); @@ -161,8 +161,7 @@ namespace BizHawk.Client.EmuHawk foreach (var domain in Global.Emulator.MemoryDomains) { - string name = domain.Name; - + var name = domain.Name; var item = new ToolStripMenuItem { Text = name }; item.Click += (o, ev) => setCallback(name); item.Checked = name == selectedDomain; @@ -203,27 +202,21 @@ namespace BizHawk.Client.EmuHawk GlobalWin.MainForm.UpdateCheatStatus(); } - public static void FreezeAddress(List watches) + public static void FreezeAddress(IEnumerable watches) { - foreach(var watch in watches) + foreach (var watch in watches.Where(watch => !watch.IsSeparator)) { - if (!watch.IsSeparator) - { - Global.CheatList.Add( - new Cheat(watch, watch.Value ?? 0) + Global.CheatList.Add( + new Cheat(watch, watch.Value ?? 0) ); - } } } - public static void UnfreezeAddress(List watches) + public static void UnfreezeAddress(IEnumerable watches) { - foreach (var watch in watches) + foreach (var watch in watches.Where(watch => !watch.IsSeparator)) { - if (!watch.IsSeparator) - { - Global.CheatList.Remove(watch); - } + Global.CheatList.Remove(watch); } } @@ -240,7 +233,7 @@ namespace BizHawk.Client.EmuHawk { if (listView.Columns[columnName] == null) { - ColumnHeader column = new ColumnHeader + var column = new ColumnHeader { Name = columnName, Text = columnName.Replace("Column", ""), diff --git a/BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs b/BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs index 28891bda6b..c1a1120539 100644 --- a/BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs +++ b/BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs @@ -21,9 +21,9 @@ namespace BizHawk.Client.EmuHawk InitializeComponent(); } - public void SetWatch(List watches) + public void SetWatch(IEnumerable watches) { - _watchList = watches; + _watchList = watches.ToList(); } private void UnSupportedConfiguration() @@ -49,9 +49,9 @@ namespace BizHawk.Client.EmuHawk if (_watchList.Count > 1) { - bool hasMixedSizes = _watchList.Select(x => x.Size).Distinct().Count() > 1; - bool hasMixedTypes = _watchList.Select(x => x.Type).Distinct().Count() > 1; - bool hasMixedEndian = _watchList.Select(x => x.BigEndian).Distinct().Count() > 1; + var hasMixedSizes = _watchList.Select(x => x.Size).Distinct().Count() > 1; + var hasMixedTypes = _watchList.Select(x => x.Type).Distinct().Count() > 1; + var hasMixedEndian = _watchList.Select(x => x.BigEndian).Distinct().Count() > 1; if (hasMixedSizes || hasMixedTypes || hasMixedEndian) { @@ -85,23 +85,14 @@ namespace BizHawk.Client.EmuHawk private void OK_Click(object sender, EventArgs e) { - bool success = true; - foreach (var watch in _watchList) + var success = true; + foreach (var watch in _watchList.Where(watch => !watch.Poke(ValueBox.Text))) { - if (!watch.Poke(ValueBox.Text)) - { - success = false; - } + success = false; } - if (success) - { - OutputLabel.Text = "Value successfully written."; - } - else - { - OutputLabel.Text = "An error occured when writing Value."; - } + OutputLabel.Text = success ? "Value successfully written." + : "An error occured when writing Value."; } } } diff --git a/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs b/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs index 7197431fdd..59b73eb881 100644 --- a/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs +++ b/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; @@ -21,7 +20,7 @@ namespace BizHawk.Client.EmuHawk { //TODO: DoSearch grabs the state of widgets and passes it to the engine before running, so rip out code that is attempting to keep the state up to date through change events - private readonly Dictionary DefaultColumnWidths = new Dictionary + private readonly Dictionary _defaultColumnWidths = new Dictionary { { WatchList.ADDRESS, 60 }, { WatchList.VALUE, 59 }, @@ -30,19 +29,19 @@ namespace BizHawk.Client.EmuHawk { WatchList.DIFF, 59 }, }; - private string CurrentFileName = String.Empty; + private string _currentFileName = String.Empty; - private RamSearchEngine Searches; - private RamSearchEngine.Settings Settings; + private RamSearchEngine _searches; + private RamSearchEngine.Settings _settings; private int _defaultWidth; //For saving the default size of the dialog, so the user can restore if desired private int _defaultHeight; private string _sortedColumn = String.Empty; - private bool _sortReverse = false; - private bool _forcePreviewClear = false; - private bool _autoSearch = false; + private bool _sortReverse; + private bool _forcePreviewClear; + private bool _autoSearch; - private bool dropdown_dontfire = false; //Used as a hack to get around lame .net dropdowns, there's no way to set their index without firing the selectedindexchanged event! + private bool _dropdownDontfire; //Used as a hack to get around lame .net dropdowns, there's no way to set their index without firing the selectedindexchanged event! public const int MaxDetailedSize = (1024 * 1024); //1mb, semi-arbituary decision, sets the size to check for and automatically switch to fast mode for the user public const int MaxSupportedSize = (1024 * 1024 * 64); //64mb, semi-arbituary decision, sets the maximum size ram search will support (as it will crash beyond this) @@ -66,8 +65,8 @@ namespace BizHawk.Client.EmuHawk _sortedColumn = String.Empty; _sortReverse = false; - Settings = new RamSearchEngine.Settings(); - Searches = new RamSearchEngine(Settings); + _settings = new RamSearchEngine.Settings(); + _searches = new RamSearchEngine(_settings); TopMost = Global.Config.RamSearchAlwaysOnTop; } @@ -101,29 +100,29 @@ namespace BizHawk.Client.EmuHawk private void RamSearch_Load(object sender, EventArgs e) { - dropdown_dontfire = true; + _dropdownDontfire = true; LoadConfigSettings(); - SpecificValueBox.ByteSize = Settings.Size; - SpecificValueBox.Type = Settings.Type; + SpecificValueBox.ByteSize = _settings.Size; + SpecificValueBox.Type = _settings.Type; MessageLabel.Text = String.Empty; SpecificAddressBox.MaxLength = IntHelpers.GetNumDigits(Global.Emulator.MemoryDomains.MainMemory.Size); - HardSetSizeDropDown(Settings.Size); + HardSetSizeDropDown(_settings.Size); PopulateTypeDropDown(); - HardSetDisplayTypeDropDown(Settings.Type); + HardSetDisplayTypeDropDown(_settings.Type); DoDomainSizeCheck(); SetReboot(false); - SpecificAddressBox.SetHexProperties(Settings.Domain.Size); + SpecificAddressBox.SetHexProperties(_settings.Domain.Size); SpecificValueBox.ResetText(); SpecificAddressBox.ResetText(); NumberOfChangesBox.ResetText(); DifferenceBox.ResetText(); DifferentByBox.ResetText(); - dropdown_dontfire = false; + _dropdownDontfire = false; - if (Settings.Mode == RamSearchEngine.Settings.SearchMode.Fast) + if (_settings.Mode == RamSearchEngine.Settings.SearchMode.Fast) { SetToFastMode(); } @@ -136,23 +135,16 @@ namespace BizHawk.Client.EmuHawk private void ListView_QueryItemBkColor(int index, int column, ref Color color) { - if (Searches.Count > 0 && column == 0) + if (_searches.Count > 0 && column == 0) { - Color nextColor = Color.White; + var nextColor = Color.White; - bool isCheat = Global.CheatList.IsActive(Settings.Domain, Searches[index].Address.Value); - bool isWeeded = Global.Config.RamSearchPreviewMode && !_forcePreviewClear && Searches.Preview(Searches[index].Address.Value); + var isCheat = Global.CheatList.IsActive(_settings.Domain, _searches[index].Address ?? 0); + var isWeeded = Global.Config.RamSearchPreviewMode && !_forcePreviewClear && _searches.Preview(_searches[index].Address ?? 0); if (isCheat) { - if (isWeeded) - { - nextColor = Color.Lavender; - } - else - { - nextColor = Color.LightCyan; - } + nextColor = isWeeded ? Color.Lavender : Color.LightCyan; } else { @@ -162,39 +154,36 @@ namespace BizHawk.Client.EmuHawk } } - if (color != nextColor) - { - color = nextColor; - } + color = nextColor; } } private void ListView_QueryItemText(int index, int column, out string text) { - text = ""; + text = String.Empty; - if (index >= Searches.Count) + if (index >= _searches.Count) { return; } - string columnName = WatchListView.Columns[column].Name; + var columnName = WatchListView.Columns[column].Name; switch (columnName) { case WatchList.ADDRESS: - text = Searches[index].AddressString; + text = _searches[index].AddressString; break; case WatchList.VALUE: - text = Searches[index].ValueString; + text = _searches[index].ValueString; break; case WatchList.PREV: - text = Searches[index].PreviousStr; + text = _searches[index].PreviousStr; break; case WatchList.CHANGES: - text = Searches[index].ChangeCount.ToString(); + text = _searches[index].ChangeCount.ToString(); break; case WatchList.DIFF: - text = Searches[index].Diff; + text = _searches[index].Diff; break; } } @@ -226,9 +215,9 @@ namespace BizHawk.Client.EmuHawk public void UpdateValues() { - if (Searches.Count > 0) + if (_searches.Count > 0) { - Searches.Update(); + _searches.Update(); if (_autoSearch) { @@ -244,7 +233,7 @@ namespace BizHawk.Client.EmuHawk { if (!IsHandleCreated || IsDisposed) return; - Settings.Domain = Global.Emulator.MemoryDomains.MainMemory; + _settings.Domain = Global.Emulator.MemoryDomains.MainMemory; MessageLabel.Text = "Search restarted"; DoDomainSizeCheck(); NewSearch(); @@ -262,19 +251,19 @@ namespace BizHawk.Client.EmuHawk public void NewSearch() { - var compareTo = Searches.CompareTo; - var compareVal = Searches.CompareValue; - var differentBy = Searches.DifferentBy; + var compareTo = _searches.CompareTo; + var compareVal = _searches.CompareValue; + var differentBy = _searches.DifferentBy; - Searches = new RamSearchEngine(Settings, compareTo, compareVal, differentBy); - Searches.Start(); + _searches = new RamSearchEngine(_settings, compareTo, compareVal, differentBy); + _searches.Start(); if (Global.Config.RamSearchAlwaysExcludeRamWatch) { RemoveRamWatchesFromList(); } SetTotal(); - WatchListView.ItemCount = Searches.Count; + WatchListView.ItemCount = _searches.Count; ToggleSearchDependentToolBarItems(); SetReboot(false); MessageLabel.Text = String.Empty; @@ -282,19 +271,14 @@ namespace BizHawk.Client.EmuHawk public void NextCompareTo(bool reverse = false) { - List radios = new List(); - foreach (Control control in CompareToBox.Controls) - { - if (control is RadioButton) - { - radios.Add(control as RadioButton); - } - } - - radios = radios.OrderBy(x => x.TabIndex).ToList(); + var radios = CompareToBox.Controls + .OfType() + .Select(control => control) + .OrderBy(x => x.TabIndex) + .ToList(); var selected = radios.FirstOrDefault(x => x.Checked); - int index = radios.IndexOf(selected); + var index = radios.IndexOf(selected); if (reverse) { @@ -317,25 +301,20 @@ namespace BizHawk.Client.EmuHawk } radios[index].Checked = true; - MethodInfo mi = radios[index].GetType().GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic); + var mi = radios[index].GetType().GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic); mi.Invoke(radios[index], new object[] { new EventArgs() }); } public void NextOperator(bool reverse = false) { - List radios = new List(); - foreach (Control control in ComparisonBox.Controls) - { - if (control is RadioButton) - { - radios.Add(control as RadioButton); - } - } - - radios = radios.OrderBy(x => x.TabIndex).ToList(); + var radios = ComparisonBox.Controls + .OfType() + .Select(control => control) + .OrderBy(x => x.TabIndex) + .ToList(); var selected = radios.FirstOrDefault(x => x.Checked); - int index = radios.IndexOf(selected); + var index = radios.IndexOf(selected); if (reverse) { @@ -358,7 +337,7 @@ namespace BizHawk.Client.EmuHawk } radios[index].Checked = true; - MethodInfo mi = radios[index].GetType().GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic); + var mi = radios[index].GetType().GetMethod("OnClick", BindingFlags.Instance | BindingFlags.NonPublic); mi.Invoke(radios[index], new object[] { new EventArgs() }); } @@ -370,7 +349,7 @@ namespace BizHawk.Client.EmuHawk { DoSearchToolButton.Enabled = CopyValueToPrevToolBarItem.Enabled = - Searches.Count > 0; + _searches.Count > 0; UpdateUndoToolBarButtons(); } @@ -448,67 +427,52 @@ namespace BizHawk.Client.EmuHawk public void DoSearch() { - Searches.CompareValue = CompareToValue; - Searches.DifferentBy = DifferentByValue; - Searches.Operator = Operator; - Searches.CompareTo = Compare; + _searches.CompareValue = CompareToValue; + _searches.DifferentBy = DifferentByValue; + _searches.Operator = Operator; + _searches.CompareTo = Compare; - int removed = Searches.DoSearch(); + var removed = _searches.DoSearch(); SetTotal(); - WatchListView.ItemCount = Searches.Count; + WatchListView.ItemCount = _searches.Count; SetRemovedMessage(removed); ToggleSearchDependentToolBarItems(); _forcePreviewClear = true; } - private List SelectedIndices + private IList SelectedIndices { - get - { - var indices = new List(); - foreach (int index in WatchListView.SelectedIndices) - { - indices.Add(index); - } - return indices; - } + get { return WatchListView.SelectedIndices.Cast().ToList(); } + } + + private IEnumerable SelectedItems + { + get { return SelectedIndices.Select(index => _searches[index]); } } private List SelectedWatches { - get - { - var selected = new List(); - ListView.SelectedIndexCollection indices = WatchListView.SelectedIndices; - if (indices.Count > 0) - { - foreach (int index in indices) - { - selected.Add(Searches[index]); - } - } - return selected; - } + get { return SelectedItems.Where(x => !x.IsSeparator).ToList(); } } private void SetRemovedMessage(int val) { - MessageLabel.Text = val.ToString() + " address" + (val != 1 ? "es" : String.Empty) + " removed"; + MessageLabel.Text = val + " address" + (val != 1 ? "es" : String.Empty) + " removed"; } private void SetTotal() { - TotalSearchLabel.Text = String.Format("{0:n0}", Searches.Count) + " addresses"; + TotalSearchLabel.Text = String.Format("{0:n0}", _searches.Count) + " addresses"; } private void SetDomainLabel() { - MemDomainLabel.Text = Searches.Domain.Name; + MemDomainLabel.Text = _searches.Domain.Name; } private void LoadFileFromRecent(string path) { - FileInfo file = new FileInfo(path); + var file = new FileInfo(path); if (!file.Exists) { @@ -520,26 +484,21 @@ namespace BizHawk.Client.EmuHawk } } - private void SetPlatformAndMemoryDomainLabel() - { - MemDomainLabel.Text = Global.Emulator.SystemId + " " + Searches.Domain.Name; - } - private void SetMemoryDomain(string name) { - Settings.Domain = Global.Emulator.MemoryDomains[name]; + _settings.Domain = Global.Emulator.MemoryDomains[name]; SetDomainLabel(); SetReboot(true); - SpecificAddressBox.MaxLength = IntHelpers.GetNumDigits(Settings.Domain.Size); + SpecificAddressBox.MaxLength = IntHelpers.GetNumDigits(_settings.Domain.Size); DoDomainSizeCheck(); } private void DoDomainSizeCheck() { - if (Settings.Domain.Size >= MaxDetailedSize - && Settings.Mode == RamSearchEngine.Settings.SearchMode.Detailed) + if (_settings.Domain.Size >= MaxDetailedSize + && _settings.Mode == RamSearchEngine.Settings.SearchMode.Detailed) { - Settings.Mode = RamSearchEngine.Settings.SearchMode.Fast; + _settings.Mode = RamSearchEngine.Settings.SearchMode.Fast; SetReboot(true); MessageLabel.Text = "Large domain, switching to fast mode"; } @@ -559,12 +518,11 @@ namespace BizHawk.Client.EmuHawk private void ColumnPositions() { - List> Columns = - Global.Config.RamSearchColumnIndexes + var Columns = Global.Config.RamSearchColumnIndexes .Where(x => WatchListView.Columns.ContainsKey(x.Key)) .OrderBy(x => x.Value).ToList(); - for (int i = 0; i < Columns.Count; i++) + for (var i = 0; i < Columns.Count; i++) { WatchListView.Columns[Columns[i].Key].DisplayIndex = i; } @@ -608,7 +566,7 @@ namespace BizHawk.Client.EmuHawk var width = Global.Config.RamSearchColumnWidths[columnName]; if (width == -1) { - width = DefaultColumnWidths[columnName]; + width = _defaultColumnWidths[columnName]; } return width; @@ -616,29 +574,29 @@ namespace BizHawk.Client.EmuHawk private void DoDisplayTypeClick(Watch.DisplayType type) { - if (Settings.Type != type && !String.IsNullOrEmpty(SpecificValueBox.Text)) + if (_settings.Type != type && !String.IsNullOrEmpty(SpecificValueBox.Text)) { SpecificValueBox.Text = "0"; } - SpecificValueBox.Type = Settings.Type = type; - Searches.SetType(type); + SpecificValueBox.Type = _settings.Type = type; + _searches.SetType(type); - dropdown_dontfire = true; + _dropdownDontfire = true; DisplayTypeDropdown.SelectedItem = Watch.DisplayTypeToString(type); - dropdown_dontfire = false; + _dropdownDontfire = false; SpecificValueBox.Type = type; WatchListView.Refresh(); } private void SetPreviousStype(Watch.PreviousType type) { - Settings.PreviousType = type; - Searches.SetPreviousType(type); + _settings.PreviousType = type; + _searches.SetPreviousType(type); } private void SetSize(Watch.WatchSize size) { - SpecificValueBox.ByteSize = Settings.Size = size; + SpecificValueBox.ByteSize = _settings.Size = size; if (!String.IsNullOrEmpty(SpecificAddressBox.Text)) { SpecificAddressBox.Text = "0"; @@ -649,12 +607,12 @@ namespace BizHawk.Client.EmuHawk SpecificValueBox.Text = "0"; } - if (!Watch.AvailableTypes(size).Contains(Settings.Type)) + if (!Watch.AvailableTypes(size).Contains(_settings.Type)) { - Settings.Type = Watch.AvailableTypes(size)[0]; + _settings.Type = Watch.AvailableTypes(size)[0]; } - dropdown_dontfire = true; + _dropdownDontfire = true; switch(size) { case Watch.WatchSize.Byte: @@ -668,21 +626,21 @@ namespace BizHawk.Client.EmuHawk break; } PopulateTypeDropDown(); - dropdown_dontfire = false; - SpecificValueBox.Type = Settings.Type; + _dropdownDontfire = false; + SpecificValueBox.Type = _settings.Type; SetReboot(true); } private void PopulateTypeDropDown() { - string previous = DisplayTypeDropdown.SelectedItem != null ? DisplayTypeDropdown.SelectedItem.ToString() : String.Empty; - string next = String.Empty; + var previous = DisplayTypeDropdown.SelectedItem != null ? DisplayTypeDropdown.SelectedItem.ToString() : String.Empty; + var next = String.Empty; DisplayTypeDropdown.Items.Clear(); - var types = Watch.AvailableTypes(Settings.Size); + var types = Watch.AvailableTypes(_settings.Size); foreach (var type in types) { - string typeStr = Watch.DisplayTypeToString(type); + var typeStr = Watch.DisplayTypeToString(type); DisplayTypeDropdown.Items.Add(typeStr); if (previous == typeStr) { @@ -702,19 +660,19 @@ namespace BizHawk.Client.EmuHawk private void SetComparisonOperator(RamSearchEngine.ComparisonOperator op) { - Searches.Operator = op; + _searches.Operator = op; WatchListView.Refresh(); } private void SetCompareTo(RamSearchEngine.Compare comp) { - Searches.CompareTo = comp; + _searches.CompareTo = comp; WatchListView.Refresh(); } private void SetCompareValue(int? value) { - Searches.CompareValue = value; + _searches.CompareValue = value; WatchListView.Refresh(); } @@ -727,7 +685,7 @@ namespace BizHawk.Client.EmuHawk private void SetToDetailedMode() { - Settings.Mode = RamSearchEngine.Settings.SearchMode.Detailed; + _settings.Mode = RamSearchEngine.Settings.SearchMode.Detailed; NumberOfChangesRadio.Enabled = true; NumberOfChangesBox.Enabled = true; DifferenceRadio.Enabled = true; @@ -739,9 +697,9 @@ namespace BizHawk.Client.EmuHawk private void SetToFastMode() { - Settings.Mode = RamSearchEngine.Settings.SearchMode.Fast; + _settings.Mode = RamSearchEngine.Settings.SearchMode.Fast; - if (Settings.PreviousType == Watch.PreviousType.LastFrame || Settings.PreviousType == Watch.PreviousType.LastChange) + if (_settings.PreviousType == Watch.PreviousType.LastFrame || _settings.PreviousType == Watch.PreviousType.LastChange) { SetPreviousStype(Watch.PreviousType.LastSearch); } @@ -767,14 +725,10 @@ namespace BizHawk.Client.EmuHawk { SetRemovedMessage(SelectedIndices.Count); - var addresses = new List(); - foreach (int index in SelectedIndices) - { - addresses.Add(Searches[index].Address.Value); - } - Searches.RemoveRange(addresses); + var addresses = SelectedIndices.Select(index => _searches[index].Address ?? 0).ToList(); + _searches.RemoveRange(addresses); - WatchListView.ItemCount = Searches.Count; + WatchListView.ItemCount = _searches.Count; SetTotal(); WatchListView.SelectedIndices.Clear(); } @@ -786,31 +740,31 @@ namespace BizHawk.Client.EmuHawk { if (!truncate) { - CurrentFileName = file.FullName; + _currentFileName = file.FullName; } - WatchList watches = new WatchList(Settings.Domain); + var watches = new WatchList(_settings.Domain); watches.Load(file.FullName, append); - List addresses = watches.Where(x => !x.IsSeparator).Select(x => x.Address.Value).ToList(); + var addresses = watches.Where(x => !x.IsSeparator).Select(x => x.Address ?? 0).ToList(); if (truncate) { SetRemovedMessage(addresses.Count); - Searches.RemoveRange(addresses); + _searches.RemoveRange(addresses); } else { - Searches.AddRange(addresses, append); + _searches.AddRange(addresses, append); MessageLabel.Text = file.Name + " loaded"; } - WatchListView.ItemCount = Searches.Count; + WatchListView.ItemCount = _searches.Count; SetTotal(); Global.Config.RecentSearches.Add(file.FullName); if (!append && !truncate) { - Searches.ClearHistory(); + _searches.ClearHistory(); } ToggleSearchDependentToolBarItems(); @@ -822,9 +776,9 @@ namespace BizHawk.Client.EmuHawk if (SelectedIndices.Count > 0) { GlobalWin.MainForm.LoadRamWatch(true); - for (int x = 0; x < SelectedIndices.Count; x++) + foreach(var watch in SelectedWatches) { - GlobalWin.Tools.RamWatch.AddWatch(Searches[SelectedIndices[x]]); + GlobalWin.Tools.RamWatch.AddWatch(watch); } if (Global.Config.RamSearchAlwaysExcludeRamWatch) @@ -845,13 +799,7 @@ namespace BizHawk.Client.EmuHawk { GlobalWin.Sound.StopSound(); var poke = new RamPoke(); - - var watches = new List(); - for (int i = 0; i < SelectedIndices.Count; i++) - { - watches.Add(Searches[SelectedIndices[i]]); - } - + var watches = SelectedIndices.Select(t => _searches[t]).ToList(); poke.SetWatch(watches); poke.InitialLocation = GetPromptPoint(); poke.ShowDialog(); @@ -864,16 +812,16 @@ namespace BizHawk.Client.EmuHawk { if (GlobalWin.Tools.Has()) { - Searches.RemoveRange(GlobalWin.Tools.RamWatch.AddressList); - WatchListView.ItemCount = Searches.Count; + _searches.RemoveRange(GlobalWin.Tools.RamWatch.AddressList); + WatchListView.ItemCount = _searches.Count; SetTotal(); } } private void UpdateUndoToolBarButtons() { - UndoToolBarButton.Enabled = Searches.CanUndo; - RedoToolBarItem.Enabled = Searches.CanRedo; + UndoToolBarButton.Enabled = _searches.CanUndo; + RedoToolBarItem.Enabled = _searches.CanRedo; } private string GetColumnValue(string name, int index) @@ -883,15 +831,15 @@ namespace BizHawk.Client.EmuHawk default: return String.Empty; case WatchList.ADDRESS: - return Searches[index].AddressString; + return _searches[index].AddressString; case WatchList.VALUE: - return Searches[index].ValueString; + return _searches[index].ValueString; case WatchList.PREV: - return Searches[index].PreviousStr; + return _searches[index].PreviousStr; case WatchList.CHANGES: - return Searches[index].ChangeCount.ToString(); + return _searches[index].ChangeCount.ToString(); case WatchList.DIFF: - return Searches[index].Diff; + return _searches[index].Diff; } } @@ -907,18 +855,17 @@ namespace BizHawk.Client.EmuHawk private void GoToSpecifiedAddress() { WatchListView.SelectedIndices.Clear(); - InputPrompt i = new InputPrompt { Text = "Go to Address" }; - i._Location = GetPromptPoint(); - i.SetMessage("Enter a hexadecimal value"); + var prompt = new InputPrompt {Text = "Go to Address", _Location = GetPromptPoint()}; + prompt.SetMessage("Enter a hexadecimal value"); GlobalWin.Sound.StopSound(); - i.ShowDialog(); + prompt.ShowDialog(); GlobalWin.Sound.StartSound(); - if (i.UserOK) + if (prompt.UserOK) { - if (InputValidate.IsValidHexNumber(i.UserText)) + if (InputValidate.IsValidHexNumber(prompt.UserText)) { - int addr = int.Parse(i.UserText, NumberStyles.HexNumber); + var addr = int.Parse(prompt.UserText, NumberStyles.HexNumber); WatchListView.SelectItem(addr, true); WatchListView.ensureVisible(); } @@ -933,7 +880,7 @@ namespace BizHawk.Client.EmuHawk private void FileSubMenu_DropDownOpened(object sender, EventArgs e) { - SaveMenuItem.Enabled = !String.IsNullOrWhiteSpace(CurrentFileName); + SaveMenuItem.Enabled = !String.IsNullOrWhiteSpace(_currentFileName); } private void RecentSubMenu_DropDownOpened(object sender, EventArgs e) @@ -955,29 +902,28 @@ namespace BizHawk.Client.EmuHawk private void SaveMenuItem_Click(object sender, EventArgs e) { - if (!String.IsNullOrWhiteSpace(CurrentFileName)) + if (!String.IsNullOrWhiteSpace(_currentFileName)) { - WatchList watches = new WatchList(Settings.Domain); - watches.CurrentFileName = CurrentFileName; - for (int i = 0; i < Searches.Count; i++) + var watches = new WatchList(_settings.Domain) {CurrentFileName = _currentFileName}; + for (var i = 0; i < _searches.Count; i++) { - watches.Add(Searches[i]); + watches.Add(_searches[i]); } if (!String.IsNullOrWhiteSpace(watches.CurrentFileName)) { if (watches.Save()) { - CurrentFileName = watches.CurrentFileName; - MessageLabel.Text = Path.GetFileName(CurrentFileName) + " saved"; + _currentFileName = watches.CurrentFileName; + MessageLabel.Text = Path.GetFileName(_currentFileName) + " saved"; } } else { - bool result = watches.SaveAs(ToolHelpers.GetWatchSaveFileFromUser(watches.CurrentFileName)); + var result = watches.SaveAs(ToolHelpers.GetWatchSaveFileFromUser(watches.CurrentFileName)); if (result) { - MessageLabel.Text = Path.GetFileName(CurrentFileName) + " saved"; + MessageLabel.Text = Path.GetFileName(_currentFileName) + " saved"; Global.Config.RecentWatches.Add(watches.CurrentFileName); } } @@ -986,17 +932,16 @@ namespace BizHawk.Client.EmuHawk private void SaveAsMenuItem_Click(object sender, EventArgs e) { - WatchList watches = new WatchList(Settings.Domain); - watches.CurrentFileName = CurrentFileName; - for (int i = 0; i < Searches.Count; i++) + var watches = new WatchList(_settings.Domain) {CurrentFileName = _currentFileName}; + for (var i = 0; i < _searches.Count; i++) { - watches.Add(Searches[i]); + watches.Add(_searches[i]); } if (watches.SaveAs(ToolHelpers.GetWatchSaveFileFromUser(watches.CurrentFileName))) { - CurrentFileName = watches.CurrentFileName; - MessageLabel.Text = Path.GetFileName(CurrentFileName) + " saved"; + _currentFileName = watches.CurrentFileName; + MessageLabel.Text = Path.GetFileName(_currentFileName) + " saved"; Global.Config.RecentSearches.Add(watches.CurrentFileName); } } @@ -1012,42 +957,43 @@ namespace BizHawk.Client.EmuHawk private void SettingsSubMenu_DropDownOpened(object sender, EventArgs e) { - CheckMisalignedMenuItem.Checked = Settings.CheckMisAligned; - BigEndianMenuItem.Checked = Settings.BigEndian; + CheckMisalignedMenuItem.Checked = _settings.CheckMisAligned; + BigEndianMenuItem.Checked = _settings.BigEndian; } private void ModeSubMenu_DropDownOpened(object sender, EventArgs e) { - DetailedMenuItem.Checked = Settings.Mode == RamSearchEngine.Settings.SearchMode.Detailed; - FastMenuItem.Checked = Settings.Mode == RamSearchEngine.Settings.SearchMode.Fast; + DetailedMenuItem.Checked = _settings.Mode == RamSearchEngine.Settings.SearchMode.Detailed; + FastMenuItem.Checked = _settings.Mode == RamSearchEngine.Settings.SearchMode.Fast; } private void MemoryDomainsSubMenu_DropDownOpened(object sender, EventArgs e) { MemoryDomainsSubMenu.DropDownItems.Clear(); - MemoryDomainsSubMenu.DropDownItems.AddRange(ToolHelpers.GenerateMemoryDomainMenuItems(SetMemoryDomain, Searches.Domain.Name, MaxSupportedSize).ToArray()); + MemoryDomainsSubMenu.DropDownItems.AddRange(ToolHelpers.GenerateMemoryDomainMenuItems(SetMemoryDomain, _searches.Domain.Name, MaxSupportedSize).ToArray()); } private void SizeSubMenu_DropDownOpened(object sender, EventArgs e) { - _1ByteMenuItem.Checked = Settings.Size == Watch.WatchSize.Byte; - _2ByteMenuItem.Checked = Settings.Size == Watch.WatchSize.Word; - _4ByteMenuItem.Checked = Settings.Size == Watch.WatchSize.DWord; + _1ByteMenuItem.Checked = _settings.Size == Watch.WatchSize.Byte; + _2ByteMenuItem.Checked = _settings.Size == Watch.WatchSize.Word; + _4ByteMenuItem.Checked = _settings.Size == Watch.WatchSize.DWord; } private void DisplayTypeSubMenu_DropDownOpened(object sender, EventArgs e) { DisplayTypeSubMenu.DropDownItems.Clear(); - foreach (var type in Watch.AvailableTypes(Settings.Size)) + foreach (var type in Watch.AvailableTypes(_settings.Size)) { - var item = new ToolStripMenuItem() - { - Name = type.ToString() + "ToolStripMenuItem", + var item = new ToolStripMenuItem + { + Name = type + "ToolStripMenuItem", Text = Watch.DisplayTypeToString(type), - Checked = Settings.Type == type, + Checked = _settings.Type == type, }; - item.Click += (o, ev) => DoDisplayTypeClick(type); + var type1 = type; + item.Click += (o, ev) => DoDisplayTypeClick(type1); DisplayTypeSubMenu.DropDownItems.Add(item); } @@ -1059,7 +1005,7 @@ namespace BizHawk.Client.EmuHawk PreviousFrameMenuItem.Checked = false; Previous_OriginalMenuItem.Checked = false; - switch (Settings.PreviousType) + switch (_settings.PreviousType) { default: case Watch.PreviousType.LastSearch: @@ -1073,14 +1019,7 @@ namespace BizHawk.Client.EmuHawk break; } - if (Settings.Mode == RamSearchEngine.Settings.SearchMode.Fast) - { - PreviousFrameMenuItem.Enabled = false; - } - else - { - PreviousFrameMenuItem.Enabled = true; - } + PreviousFrameMenuItem.Enabled = _settings.Mode != RamSearchEngine.Settings.SearchMode.Fast; } private void DetailedMenuItem_Click(object sender, EventArgs e) @@ -1110,7 +1049,7 @@ namespace BizHawk.Client.EmuHawk private void CheckMisalignedMenuItem_Click(object sender, EventArgs e) { - Settings.CheckMisAligned ^= true; + _settings.CheckMisAligned ^= true; SetReboot(true); } @@ -1131,8 +1070,8 @@ namespace BizHawk.Client.EmuHawk private void BigEndianMenuItem_Click(object sender, EventArgs e) { - Settings.BigEndian ^= true; - Searches.SetEndian(Settings.BigEndian); + _settings.BigEndian ^= true; + _searches.SetEndian(_settings.BigEndian); } #endregion @@ -1141,7 +1080,7 @@ namespace BizHawk.Client.EmuHawk private void SearchSubMenu_DropDownOpened(object sender, EventArgs e) { - ClearChangeCountsMenuItem.Enabled = Settings.Mode == RamSearchEngine.Settings.SearchMode.Detailed; + ClearChangeCountsMenuItem.Enabled = _settings.Mode == RamSearchEngine.Settings.SearchMode.Detailed; RemoveMenuItem.Enabled = AddToRamWatchMenuItem.Enabled = @@ -1151,9 +1090,9 @@ namespace BizHawk.Client.EmuHawk UndoMenuItem.Enabled = ClearUndoMenuItem.Enabled = - Searches.CanUndo; + _searches.CanUndo; - RedoMenuItem.Enabled = Searches.CanRedo; + RedoMenuItem.Enabled = _searches.CanRedo; } private void NewSearchMenuMenuItem_Click(object sender, EventArgs e) @@ -1168,11 +1107,11 @@ namespace BizHawk.Client.EmuHawk private void UndoMenuItem_Click(object sender, EventArgs e) { - if (Searches.CanUndo) + if (_searches.CanUndo) { - Searches.Undo(); + _searches.Undo(); SetTotal(); - WatchListView.ItemCount = Searches.Count; + WatchListView.ItemCount = _searches.Count; ToggleSearchDependentToolBarItems(); _forcePreviewClear = true; UpdateUndoToolBarButtons(); @@ -1181,11 +1120,11 @@ namespace BizHawk.Client.EmuHawk private void RedoMenuItem_Click(object sender, EventArgs e) { - if (Searches.CanRedo) + if (_searches.CanRedo) { - Searches.Redo(); + _searches.Redo(); SetTotal(); - WatchListView.ItemCount = Searches.Count; + WatchListView.ItemCount = _searches.Count; ToggleSearchDependentToolBarItems(); _forcePreviewClear = true; UpdateUndoToolBarButtons(); @@ -1194,13 +1133,13 @@ namespace BizHawk.Client.EmuHawk private void CopyValueToPrevMenuItem_Click(object sender, EventArgs e) { - Searches.SetPrevousToCurrent(); + _searches.SetPrevousToCurrent(); WatchListView.Refresh(); } private void ClearChangeCountsMenuItem_Click(object sender, EventArgs e) { - Searches.ClearChangeCounts(); + _searches.ClearChangeCounts(); WatchListView.Refresh(); } @@ -1226,18 +1165,7 @@ namespace BizHawk.Client.EmuHawk private void FreezeAddressMenuItem_Click(object sender, EventArgs e) { - bool allCheats = true; - foreach (var watch in SelectedWatches) - { - if (!watch.IsSeparator) - { - if (!Global.CheatList.IsActive(watch.Domain, watch.Address.Value)) - { - allCheats = false; - } - } - } - + var allCheats = SelectedWatches.All(x => Global.CheatList.IsActive(x.Domain, x.Address ?? 0)); if (allCheats) { ToolHelpers.UnfreezeAddress(SelectedWatches); @@ -1250,7 +1178,7 @@ namespace BizHawk.Client.EmuHawk private void ClearUndoMenuItem_Click(object sender, EventArgs e) { - Searches.ClearHistory(); + _searches.ClearHistory(); UpdateUndoToolBarButtons(); } @@ -1263,7 +1191,7 @@ namespace BizHawk.Client.EmuHawk AutoloadDialogMenuItem.Checked = Global.Config.RecentSearches.AutoLoad; SaveWinPositionMenuItem.Checked = Global.Config.RamSearchSaveWindowPosition; ExcludeRamWatchMenuItem.Checked = Global.Config.RamSearchAlwaysExcludeRamWatch; - UseUndoHistoryMenuItem.Checked = Searches.UndoEnabled; + UseUndoHistoryMenuItem.Checked = _searches.UndoEnabled; PreviewModeMenuItem.Checked = Global.Config.RamSearchPreviewMode; AlwaysOnTopMenuItem.Checked = Global.Config.RamSearchAlwaysOnTop; AutoSearchMenuItem.Checked = _autoSearch; @@ -1290,7 +1218,7 @@ namespace BizHawk.Client.EmuHawk private void UseUndoHistoryMenuItem_Click(object sender, EventArgs e) { - Searches.UndoEnabled ^= true; + _searches.UndoEnabled ^= true; } private void AutoloadDialogMenuItem_Click(object sender, EventArgs e) @@ -1330,10 +1258,10 @@ namespace BizHawk.Client.EmuHawk Global.Config.RamSearchShowPrevColumn = true; Global.Config.RamSearchShowDiffColumn = false; - WatchListView.Columns[WatchList.ADDRESS].Width = DefaultColumnWidths[WatchList.ADDRESS]; - WatchListView.Columns[WatchList.VALUE].Width = DefaultColumnWidths[WatchList.VALUE]; + WatchListView.Columns[WatchList.ADDRESS].Width = _defaultColumnWidths[WatchList.ADDRESS]; + WatchListView.Columns[WatchList.VALUE].Width = _defaultColumnWidths[WatchList.VALUE]; //WatchListView.Columns[WatchList.PREV].Width = DefaultColumnWidths[WatchList.PREV]; - WatchListView.Columns[WatchList.CHANGES].Width = DefaultColumnWidths[WatchList.CHANGES]; + WatchListView.Columns[WatchList.CHANGES].Width = _defaultColumnWidths[WatchList.CHANGES]; //WatchListView.Columns[WatchList.DIFF].Width = DefaultColumnWidths[WatchList.DIFF]; Global.Config.RamSearchSaveWindowPosition = true; @@ -1350,8 +1278,8 @@ namespace BizHawk.Client.EmuHawk LoadColumnInfo(); - Settings = new RamSearchEngine.Settings(); - if (Settings.Mode == RamSearchEngine.Settings.SearchMode.Fast) + _settings = new RamSearchEngine.Settings(); + if (_settings.Mode == RamSearchEngine.Settings.SearchMode.Fast) { SetToFastMode(); } @@ -1395,7 +1323,7 @@ namespace BizHawk.Client.EmuHawk private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { - DoSearchContextMenuItem.Enabled = Searches.Count > 0; + DoSearchContextMenuItem.Enabled = _searches.Count > 0; RemoveContextMenuItem.Visible = AddToRamWatchContextMenuItem.Visible = @@ -1410,10 +1338,10 @@ namespace BizHawk.Client.EmuHawk ContextMenuSeparator3.Visible = (SelectedIndices.Count > 0) || (Global.CheatList.ActiveCount > 0); - bool allCheats = true; - foreach (int index in SelectedIndices) + var allCheats = true; + foreach (var index in SelectedIndices) { - if (!Global.CheatList.IsActive(Settings.Domain, Searches[index].Address.Value)) + if (!Global.CheatList.IsActive(_settings.Domain, _searches[index].Address ?? 0)) { allCheats = false; } @@ -1440,7 +1368,7 @@ namespace BizHawk.Client.EmuHawk { if (SelectedWatches.Any()) { - ToolHelpers.ViewInHexEditor(Searches.Domain, SelectedWatches.Select(x => x.Address.Value)); + ToolHelpers.ViewInHexEditor(_searches.Domain, SelectedWatches.Select(x => x.Address ?? 0)); } } @@ -1452,7 +1380,7 @@ namespace BizHawk.Client.EmuHawk private void SizeDropdown_SelectedIndexChanged(object sender, EventArgs e) { - if (dropdown_dontfire) + if (_dropdownDontfire) { return; } @@ -1473,7 +1401,7 @@ namespace BizHawk.Client.EmuHawk private void DisplayTypeDropdown_SelectedIndexChanged(object sender, EventArgs e) { - if (!dropdown_dontfire) + if (!_dropdownDontfire) { DoDisplayTypeClick(Watch.StringToDisplayType(DisplayTypeDropdown.SelectedItem.ToString())); } @@ -1499,9 +1427,9 @@ namespace BizHawk.Client.EmuHawk { SpecificAddressBox.ResetText(); } - Searches.CompareValue = SpecificValueBox.ToRawInt(); + _searches.CompareValue = SpecificValueBox.ToRawInt(); - if (this.Focused) + if (Focused) { SpecificValueBox.Focus(); } @@ -1521,9 +1449,9 @@ namespace BizHawk.Client.EmuHawk SpecificAddressBox.ResetText(); } - Searches.CompareValue = SpecificAddressBox.ToRawInt(); + _searches.CompareValue = SpecificAddressBox.ToRawInt(); - if (this.Focused) + if (Focused) { SpecificAddressBox.Focus(); } @@ -1543,9 +1471,9 @@ namespace BizHawk.Client.EmuHawk NumberOfChangesBox.ResetText(); } - Searches.CompareValue = NumberOfChangesBox.ToRawInt(); + _searches.CompareValue = NumberOfChangesBox.ToRawInt(); - if (this.Focused) + if (Focused) { NumberOfChangesBox.Focus(); } @@ -1564,9 +1492,9 @@ namespace BizHawk.Client.EmuHawk { DifferenceBox.ResetText(); } - Searches.CompareValue = DifferenceBox.ToRawInt(); + _searches.CompareValue = DifferenceBox.ToRawInt(); - if (this.Focused) + if (Focused) { DifferenceBox.Focus(); } @@ -1627,9 +1555,9 @@ namespace BizHawk.Client.EmuHawk { DifferentByBox.ResetText(); } - Searches.DifferentBy = DifferenceBox.ToRawInt(); + _searches.DifferentBy = DifferenceBox.ToRawInt(); - if (this.Focused) + if (Focused) { DifferentByBox.Focus(); } @@ -1639,14 +1567,7 @@ namespace BizHawk.Client.EmuHawk private void DifferentByBox_TextChanged(object sender, EventArgs e) { - if (!String.IsNullOrWhiteSpace(DifferentByBox.Text)) - { - Searches.DifferentBy = DifferentByBox.ToRawInt(); - } - else - { - Searches.DifferentBy = null; - } + _searches.DifferentBy = !String.IsNullOrWhiteSpace(DifferentByBox.Text) ? DifferentByBox.ToRawInt() : null; WatchListView.Refresh(); } @@ -1663,17 +1584,17 @@ namespace BizHawk.Client.EmuHawk } else if (e.KeyCode == Keys.A && e.Control && !e.Alt && !e.Shift) //Select All { - for (int x = 0; x < Searches.Count; x++) + for (var i = 0; i < _searches.Count; i++) { - WatchListView.SelectItem(x, true); + WatchListView.SelectItem(i, true); } } else if (e.KeyCode == Keys.C && e.Control && !e.Alt && !e.Shift) //Copy { if (SelectedIndices.Count > 0) { - StringBuilder sb = new StringBuilder(); - foreach (int index in SelectedIndices) + var sb = new StringBuilder(); + foreach (var index in SelectedIndices) { foreach (ColumnHeader column in WatchListView.Columns) { @@ -1726,7 +1647,7 @@ namespace BizHawk.Client.EmuHawk _sortReverse = false; } - Searches.Sort(column.Name, _sortReverse); + _searches.Sort(column.Name, _sortReverse); _sortedColumn = column.Name; _sortReverse ^= true; @@ -1757,7 +1678,7 @@ namespace BizHawk.Client.EmuHawk private void NewRamSearch_DragDrop(object sender, DragEventArgs e) { - string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); + var filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); if (Path.GetExtension(filePaths[0]) == (".wch")) { var file = new FileInfo(filePaths[0]); diff --git a/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs b/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs index 11e038df5c..583d241b8f 100644 --- a/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs +++ b/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs @@ -29,14 +29,14 @@ namespace BizHawk.Client.EmuHawk private string _sortedColumn = String.Empty; private bool _sortReverse; - private readonly WatchList Watches = new WatchList(Global.Emulator.MemoryDomains.MainMemory); + private readonly WatchList _watches = new WatchList(Global.Emulator.MemoryDomains.MainMemory); #region API public void AddWatch(Watch watch) { - Watches.Add(watch); - WatchListView.ItemCount = Watches.ItemCount; + _watches.Add(watch); + WatchListView.ItemCount = _watches.ItemCount; UpdateValues(); UpdateWatchCount(); Changes(); @@ -49,18 +49,18 @@ namespace BizHawk.Client.EmuHawk return true; } - if (Watches.Changes) + if (_watches.Changes) { GlobalWin.Sound.StopSound(); - DialogResult result = MessageBox.Show("Save Changes?", "Ram Watch", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); + var result = MessageBox.Show("Save Changes?", "Ram Watch", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); GlobalWin.Sound.StartSound(); if (result == DialogResult.Yes) { - Watches.Save(); + _watches.Save(); } else if (result == DialogResult.No) { - Watches.Changes = false; + _watches.Changes = false; return true; } else if (result == DialogResult.Cancel) @@ -76,21 +76,21 @@ namespace BizHawk.Client.EmuHawk { get { - return Watches.Where(x => !x.IsSeparator).Select(x => x.Address ?? 0).ToList(); + return _watches.Where(x => !x.IsSeparator).Select(x => x.Address ?? 0).ToList(); } } public void LoadFileFromRecent(string path) { - bool ask_result = true; - if (Watches.Changes) + var ask_result = true; + if (_watches.Changes) { ask_result = AskSave(); } if (ask_result) { - bool load_result = Watches.Load(path, append: false); + var load_result = _watches.Load(path, append: false); if (!load_result) { ToolHelpers.HandleLoadError(Global.Config.RecentWatches, path); @@ -98,10 +98,10 @@ namespace BizHawk.Client.EmuHawk else { Global.Config.RecentWatches.Add(path); - WatchListView.ItemCount = Watches.ItemCount; + WatchListView.ItemCount = _watches.ItemCount; UpdateWatchCount(); UpdateMessageLabel(); - Watches.Changes = false; + _watches.Changes = false; } } } @@ -110,20 +110,20 @@ namespace BizHawk.Client.EmuHawk { if (file != null) { - bool result = true; - if (Watches.Changes) + var result = true; + if (_watches.Changes) { result = AskSave(); } if (result) { - Watches.Load(file.FullName, append); - WatchListView.ItemCount = Watches.ItemCount; + _watches.Load(file.FullName, append); + WatchListView.ItemCount = _watches.ItemCount; UpdateMessageLabel(); UpdateWatchCount(); - Global.Config.RecentWatches.Add(Watches.CurrentFileName); - SetMemoryDomain(Watches.Domain.ToString()); + Global.Config.RecentWatches.Add(_watches.CurrentFileName); + SetMemoryDomain(_watches.Domain.ToString()); } } } @@ -158,9 +158,9 @@ namespace BizHawk.Client.EmuHawk return; } - if (!String.IsNullOrWhiteSpace(Watches.CurrentFileName)) + if (!String.IsNullOrWhiteSpace(_watches.CurrentFileName)) { - Watches.Reload(); + _watches.Reload(); } else { @@ -177,17 +177,17 @@ namespace BizHawk.Client.EmuHawk return; } - if (Watches.Any()) + if (_watches.Any()) { - Watches.UpdateValues(); + _watches.UpdateValues(); if (Global.Config.DisplayRamWatch) { - for (int x = 0; x < Watches.Count; x++) + for (var x = 0; x < _watches.Count; x++) { - bool alert = !Watches[x].IsSeparator && Global.CheatList.IsActive(Watches[x].Domain, (Watches[x].Address ?? 0)); + var alert = !_watches[x].IsSeparator && Global.CheatList.IsActive(_watches[x].Domain, (_watches[x].Address ?? 0)); GlobalWin.OSD.AddGUIText( - Watches[x].ToString(), + _watches[x].ToString(), Global.Config.DispRamWatchx, (Global.Config.DispRamWatchy + (x * 14)), alert, @@ -213,39 +213,38 @@ namespace BizHawk.Client.EmuHawk private void AddNewWatch() { - WatchEditor we = new WatchEditor + var we = new WatchEditor { InitialLocation = GetPromptPoint() }; - we.SetWatch(Watches.Domain); + we.SetWatch(_watches.Domain); GlobalWin.Sound.StopSound(); we.ShowDialog(); GlobalWin.Sound.StartSound(); if (we.DialogResult == DialogResult.OK) { - Watches.Add(we.Watches[0]); + _watches.Add(we.Watches[0]); Changes(); UpdateWatchCount(); - WatchListView.ItemCount = Watches.ItemCount; + WatchListView.ItemCount = _watches.ItemCount; UpdateValues(); } } private void Changes() { - Watches.Changes = true; + _watches.Changes = true; UpdateMessageLabel(); } private void ColumnPositions() { - List> Columns = - Global.Config.RamWatchColumnIndexes + var Columns = Global.Config.RamWatchColumnIndexes .Where(x => WatchListView.Columns.ContainsKey(x.Key)) .OrderBy(x => x.Value).ToList(); - for (int i = 0; i < Columns.Count; i++) + for (var i = 0; i < Columns.Count; i++) { if (WatchListView.Columns.ContainsKey(Columns[i].Key)) { @@ -256,12 +255,12 @@ namespace BizHawk.Client.EmuHawk private void CopyWatchesToClipBoard() { - var indexes = WatchListView.SelectedIndices; + var indexes = SelectedIndices.ToList(); - if (indexes.Count > 0) + if (indexes.Any()) { - StringBuilder sb = new StringBuilder(); - foreach (int index in indexes) + var sb = new StringBuilder(); + foreach (var index in indexes) { foreach (ColumnHeader column in WatchListView.Columns) { @@ -280,21 +279,19 @@ namespace BizHawk.Client.EmuHawk private void EditWatch(bool duplicate = false) { - var indexes = WatchListView.SelectedIndices; + var indexes = SelectedIndices.ToList(); - if (indexes.Count > 0) + if (SelectedWatches.Any()) { - WatchEditor we = new WatchEditor + var we = new WatchEditor { InitialLocation = GetPromptPoint(), }; - if (!SelectedWatches.Any()) - { - return; - } - - we.SetWatch(Watches.Domain, SelectedWatches, duplicate ? WatchEditor.Mode.Duplicate : WatchEditor.Mode.Edit); + we.SetWatch(_watches.Domain, + SelectedWatches, duplicate ? WatchEditor.Mode.Duplicate : WatchEditor.Mode.Edit + ); + GlobalWin.Sound.StopSound(); var result = we.ShowDialog(); if (result == DialogResult.OK) @@ -302,14 +299,14 @@ namespace BizHawk.Client.EmuHawk Changes(); if (duplicate) { - Watches.AddRange(we.Watches); - WatchListView.ItemCount = Watches.ItemCount; + _watches.AddRange(we.Watches); + WatchListView.ItemCount = _watches.ItemCount; } else { - for (int i = 0; i < we.Watches.Count; i++) + for (var i = 0; i < we.Watches.Count; i++) { - Watches[indexes[i]] = we.Watches[i]; + _watches[indexes[i]] = we.Watches[i]; } } } @@ -326,19 +323,19 @@ namespace BizHawk.Client.EmuHawk default: return String.Empty; case WatchList.ADDRESS: - return Watches[index].AddressString; + return _watches[index].AddressString; case WatchList.VALUE: - return Watches[index].ValueString; + return _watches[index].ValueString; case WatchList.PREV: - return Watches[index].PreviousStr; + return _watches[index].PreviousStr; case WatchList.CHANGES: - return Watches[index].ChangeCount.ToString(); + return _watches[index].ChangeCount.ToString(); case WatchList.DIFF: - return Watches[index].Diff; + return _watches[index].Diff; case WatchList.DOMAIN: - return Watches[index].Domain.Name; + return _watches[index].Domain.Name; case WatchList.NOTES: - return Watches[index].Notes; + return _watches[index].Notes; } } @@ -360,16 +357,16 @@ namespace BizHawk.Client.EmuHawk private void InsertSeparator() { - var indexes = WatchListView.SelectedIndices; - if (indexes.Count > 0) + var indexes = SelectedIndices.ToList(); + if (indexes.Any()) { - Watches.Insert(indexes[0], SeparatorWatch.Instance); + _watches.Insert(indexes[0], SeparatorWatch.Instance); } else { - Watches.Add(SeparatorWatch.Instance); + _watches.Add(SeparatorWatch.Instance); } - WatchListView.ItemCount = Watches.ItemCount; + WatchListView.ItemCount = _watches.ItemCount; Changes(); UpdateWatchCount(); } @@ -409,88 +406,76 @@ namespace BizHawk.Client.EmuHawk private void MoveDown() { - var indexes = WatchListView.SelectedIndices; - if (indexes.Count == 0) + var indexes = SelectedIndices.ToList(); + if (!indexes.Any()) { return; } - foreach (int index in indexes) + foreach (var index in indexes) { - var watch = Watches[index]; + var watch = _watches[index]; - if (index < Watches.Count - 1) + if (index < _watches.Count - 1) { - Watches.Remove(Watches[index]); - Watches.Insert(index + 1, watch); + _watches.Remove(_watches[index]); + _watches.Insert(index + 1, watch); } - - //Note: here it will get flagged many times redundantly potentially, - //but this avoids it being flagged falsely when the user did not select an index - Changes(); } - var indices = new List(); - for (int i = 0; i < indexes.Count; i++) - { - indices.Add(indexes[i] + 1); - } + Changes(); + + var indices = indexes.Select(t => t + 1).ToList(); WatchListView.SelectedIndices.Clear(); - foreach (int t in indices) + foreach (var t in indices) { WatchListView.SelectItem(t, true); } - WatchListView.ItemCount = Watches.ItemCount; + WatchListView.ItemCount = _watches.ItemCount; } private void MoveUp() { - ListView.SelectedIndexCollection indexes = WatchListView.SelectedIndices; - if (indexes.Count == 0 || indexes[0] == 0) + var indexes = SelectedIndices.ToList(); + if (!indexes.Any() || indexes[0] == 0) { return; } - foreach (int index in indexes) + foreach (var index in indexes) { - var watch = Watches[index]; - Watches.Remove(Watches[index]); - Watches.Insert(index - 1, watch); - - //Note: here it will get flagged many times redundantly potentially, - //but this avoids it being flagged falsely when the user did not select an index - Changes(); + var watch = _watches[index]; + _watches.Remove(_watches[index]); + _watches.Insert(index - 1, watch); } - var indices = new List(); - for (int i = 0; i < indexes.Count; i++) - { - indices.Add(indexes[i] - 1); - } + Changes(); + + var indices = indexes.Select(t => t - 1).ToList(); WatchListView.SelectedIndices.Clear(); - foreach (int t in indices) + foreach (var t in indices) { WatchListView.SelectItem(t, true); } - WatchListView.ItemCount = Watches.ItemCount; + WatchListView.ItemCount = _watches.ItemCount; } private void NewWatchList(bool suppressAsk) { - bool result = true; - if (Watches.Changes) + var result = true; + if (_watches.Changes) { result = AskSave(); } if (result || suppressAsk) { - Watches.Clear(); - WatchListView.ItemCount = Watches.ItemCount; + _watches.Clear(); + WatchListView.ItemCount = _watches.ItemCount; UpdateWatchCount(); UpdateMessageLabel(); _sortReverse = false; @@ -506,7 +491,7 @@ namespace BizHawk.Client.EmuHawk _sortReverse = false; } - Watches.OrderWatches(column.Name, _sortReverse); + _watches.OrderWatches(column.Name, _sortReverse); _sortedColumn = column.Name; _sortReverse ^= true; @@ -517,7 +502,7 @@ namespace BizHawk.Client.EmuHawk { if (SelectedWatches.Any()) { - RamPoke poke = new RamPoke + var poke = new RamPoke { InitialLocation = GetPromptPoint() }; @@ -539,27 +524,26 @@ namespace BizHawk.Client.EmuHawk private void RemoveWatch() { - var indexes = WatchListView.SelectedIndices; - if (indexes.Count > 0) + if (SelectedItems.Any()) { - foreach (int index in indexes) + foreach (var item in SelectedItems) { - Watches.Remove(Watches[indexes[0]]); //index[0] used since each iteration will make this the correct list index + _watches.Remove(item); } - indexes.Clear(); - WatchListView.ItemCount = Watches.ItemCount; + + WatchListView.ItemCount = _watches.ItemCount; + UpdateValues(); + UpdateWatchCount(); } - UpdateValues(); - UpdateWatchCount(); } private void SaveAs() { - bool result = Watches.SaveAs(ToolHelpers.GetWatchSaveFileFromUser(Watches.CurrentFileName)); + var result = _watches.SaveAs(ToolHelpers.GetWatchSaveFileFromUser(_watches.CurrentFileName)); if (result) { UpdateMessageLabel(saved: true); - Global.Config.RecentWatches.Add(Watches.CurrentFileName); + Global.Config.RecentWatches.Add(_watches.CurrentFileName); } } @@ -619,50 +603,51 @@ namespace BizHawk.Client.EmuHawk private void SelectAll() { - for (int i = 0; i < Watches.Count; i++) + for (var i = 0; i < _watches.Count; i++) { WatchListView.SelectItem(i, true); } } - private List SelectedWatches + private IEnumerable SelectedIndices { - get - { - var selected = new List(); - ListView.SelectedIndexCollection indices = WatchListView.SelectedIndices; - if (indices.Count > 0) - { - selected.AddRange(from int index in indices where !Watches[index].IsSeparator select Watches[index]); - } - return selected; - } + get { return WatchListView.SelectedIndices.Cast(); } + } + + private IEnumerable SelectedItems + { + get { return SelectedIndices.Select(index => _watches[index]); } + } + + private IEnumerable SelectedWatches + { + get { return SelectedItems.Where(x => !x.IsSeparator); } } private void SetMemoryDomain(string name) { - Watches.Domain = Global.Emulator.MemoryDomains[name]; + _watches.Domain = Global.Emulator.MemoryDomains[name]; SetPlatformAndMemoryDomainLabel(); Update(); } private void SetPlatformAndMemoryDomainLabel() { - MemDomainLabel.Text = Global.Emulator.SystemId + " " + Watches.Domain.Name; + MemDomainLabel.Text = Global.Emulator.SystemId + " " + _watches.Domain.Name; } private void UpdateMessageLabel(bool saved = false) { - string message = String.Empty; - if (!String.IsNullOrWhiteSpace(Watches.CurrentFileName)) + var message = String.Empty; + if (!String.IsNullOrWhiteSpace(_watches.CurrentFileName)) { if (saved) { - message = Path.GetFileName(Watches.CurrentFileName) + " saved."; + message = Path.GetFileName(_watches.CurrentFileName) + " saved."; } else { - message = Path.GetFileName(Watches.CurrentFileName) + (Watches.Changes ? " *" : String.Empty); + message = Path.GetFileName(_watches.CurrentFileName) + (_watches.Changes ? " *" : String.Empty); } } @@ -671,23 +656,23 @@ namespace BizHawk.Client.EmuHawk private void UpdateWatchCount() { - WatchCountLabel.Text = Watches.WatchCount.ToString() + (Watches.WatchCount == 1 ? " watch" : " watches"); + WatchCountLabel.Text = _watches.WatchCount + (_watches.WatchCount == 1 ? " watch" : " watches"); } private void WatchListView_QueryItemBkColor(int index, int column, ref Color color) { - if (index >= Watches.ItemCount) + if (index >= _watches.ItemCount) { return; } if (column == 0) { - if (Watches[index].IsSeparator) + if (_watches[index].IsSeparator) { color = BackColor; } - else if (Global.CheatList.IsActive(Watches.Domain, Watches[index].Address ?? 0)) + else if (Global.CheatList.IsActive(_watches.Domain, _watches[index].Address ?? 0)) { color = Color.LightCyan; } @@ -696,39 +681,39 @@ namespace BizHawk.Client.EmuHawk private void WatchListView_QueryItemText(int index, int column, out string text) { - text = ""; + text = String.Empty; - if (index >= Watches.ItemCount || Watches[index].IsSeparator) + if (index >= _watches.ItemCount || _watches[index].IsSeparator) { return; } - string columnName = WatchListView.Columns[column].Name; + var columnName = WatchListView.Columns[column].Name; switch (columnName) { case WatchList.ADDRESS: - text = Watches[index].AddressString; + text = _watches[index].AddressString; break; case WatchList.VALUE: - text = Watches[index].ValueString; + text = _watches[index].ValueString; break; case WatchList.PREV: - text = Watches[index].PreviousStr; + text = _watches[index].PreviousStr; break; case WatchList.CHANGES: - if (!Watches[index].IsSeparator) + if (!_watches[index].IsSeparator) { - text = Watches[index].ChangeCount.ToString(); + text = _watches[index].ChangeCount.ToString(); } break; case WatchList.DIFF: - text = Watches[index].Diff; + text = _watches[index].Diff; break; case WatchList.DOMAIN: - text = Watches[index].Domain.Name; + text = _watches[index].Domain.Name; break; case WatchList.NOTES: - text = Watches[index].Notes; + text = _watches[index].Notes; break; } } @@ -754,11 +739,11 @@ namespace BizHawk.Client.EmuHawk private void NewRamWatch_DragDrop(object sender, DragEventArgs e) { - string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); + var filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); if (Path.GetExtension(filePaths[0]) == (".wch")) { - Watches.Load(filePaths[0], append:false); - WatchListView.ItemCount = Watches.ItemCount; + _watches.Load(filePaths[0], append:false); + WatchListView.ItemCount = _watches.ItemCount; } } @@ -770,7 +755,7 @@ namespace BizHawk.Client.EmuHawk /*************File***********************/ private void filesToolStripMenuItem_DropDownOpened(object sender, EventArgs e) { - saveToolStripMenuItem.Enabled = Watches.Changes; + saveToolStripMenuItem.Enabled = _watches.Changes; } private void newListToolStripMenuItem_Click(object sender, EventArgs e) @@ -780,15 +765,15 @@ namespace BizHawk.Client.EmuHawk private void openToolStripMenuItem_Click(object sender, EventArgs e) { - bool append = sender == appendFileToolStripMenuItem; - LoadWatchFile(ToolHelpers.GetWatchFileFromUser(Watches.CurrentFileName), append); + var append = sender == appendFileToolStripMenuItem; + LoadWatchFile(ToolHelpers.GetWatchFileFromUser(_watches.CurrentFileName), append); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { - if (!String.IsNullOrWhiteSpace(Watches.CurrentFileName)) + if (!String.IsNullOrWhiteSpace(_watches.CurrentFileName)) { - if (Watches.Save()) + if (_watches.Save()) { UpdateMessageLabel(saved: true); } @@ -830,33 +815,20 @@ namespace BizHawk.Client.EmuHawk /*************Watches***********************/ private void watchesToolStripMenuItem_DropDownOpened(object sender, EventArgs e) { - ListView.SelectedIndexCollection indexes = WatchListView.SelectedIndices; - if (indexes.Count > 0) - { - editWatchToolStripMenuItem.Enabled = true; - duplicateWatchToolStripMenuItem.Enabled = true; - removeWatchToolStripMenuItem.Enabled = true; - moveUpToolStripMenuItem.Enabled = true; - moveDownToolStripMenuItem.Enabled = true; - pokeAddressToolStripMenuItem.Enabled = true; - freezeAddressToolStripMenuItem.Enabled = true; - } - else - { - editWatchToolStripMenuItem.Enabled = false; - duplicateWatchToolStripMenuItem.Enabled = false; - removeWatchToolStripMenuItem.Enabled = false; - moveUpToolStripMenuItem.Enabled = false; - moveDownToolStripMenuItem.Enabled = false; - pokeAddressToolStripMenuItem.Enabled = false; - freezeAddressToolStripMenuItem.Enabled = false; - } + editWatchToolStripMenuItem.Enabled = + duplicateWatchToolStripMenuItem.Enabled = + removeWatchToolStripMenuItem.Enabled = + moveUpToolStripMenuItem.Enabled = + moveDownToolStripMenuItem.Enabled = + pokeAddressToolStripMenuItem.Enabled = + freezeAddressToolStripMenuItem.Enabled = + SelectedIndices.Any(); } private void memoryDomainsToolStripMenuItem_DropDownOpened(object sender, EventArgs e) { memoryDomainsToolStripMenuItem.DropDownItems.Clear(); - memoryDomainsToolStripMenuItem.DropDownItems.AddRange(ToolHelpers.GenerateMemoryDomainMenuItems(SetMemoryDomain, Watches.Domain.Name).ToArray()); + memoryDomainsToolStripMenuItem.DropDownItems.AddRange(ToolHelpers.GenerateMemoryDomainMenuItems(SetMemoryDomain, _watches.Domain.Name).ToArray()); } private void newWatchToolStripMenuItem_Click(object sender, EventArgs e) @@ -886,18 +858,7 @@ namespace BizHawk.Client.EmuHawk private void freezeAddressToolStripMenuItem_Click(object sender, EventArgs e) { - bool allCheats = true; - foreach (var watch in SelectedWatches) - { - if (!watch.IsSeparator) - { - if (!Global.CheatList.IsActive(watch.Domain, watch.Address ?? 0)) - { - allCheats = false; - } - } - } - + var allCheats = SelectedWatches.All(watch => !Global.CheatList.IsActive(watch.Domain, watch.Address ?? 0)); if (allCheats) { ToolHelpers.UnfreezeAddress(SelectedWatches); @@ -915,7 +876,7 @@ namespace BizHawk.Client.EmuHawk private void clearChangeCountsToolStripMenuItem_Click(object sender, EventArgs e) { - Watches.ClearChangeCounts(); + _watches.ClearChangeCounts(); } private void moveUpToolStripMenuItem_Click(object sender, EventArgs e) @@ -1082,7 +1043,8 @@ namespace BizHawk.Client.EmuHawk /*************Context Menu***********************/ private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { - ListView.SelectedIndexCollection indexes = WatchListView.SelectedIndices; + var indexes = WatchListView.SelectedIndices; + EditContextMenuItem.Visible = RemoveContextMenuItem.Visible = DuplicateContextMenuItem.Visible = @@ -1097,17 +1059,7 @@ namespace BizHawk.Client.EmuHawk indexes.Count > 0; - bool allCheats = true; - foreach (int i in indexes) - { - if (!Watches[i].IsSeparator) - { - if (!Global.CheatList.IsActive(Watches[i].Domain, Watches[i].Address ?? 0)) - { - allCheats = false; - } - } - } + var allCheats = _watches.All(x => Global.CheatList.IsActive(x.Domain, x.Address ?? 0)); if (allCheats) { @@ -1127,7 +1079,7 @@ namespace BizHawk.Client.EmuHawk UnfreezeAllContextMenuItem.Visible = Global.CheatList.ActiveCount > 0; - ViewInHexEditorContextMenuItem.Visible = SelectedWatches.Count == 1; + ViewInHexEditorContextMenuItem.Visible = SelectedWatches.Count() == 1; } private void UnfreezeAllContextMenuItem_Click(object sender, EventArgs e) @@ -1137,7 +1089,7 @@ namespace BizHawk.Client.EmuHawk private void ViewInHexEditorContextMenuItem_Click(object sender, EventArgs e) { - var selected = SelectedWatches; + var selected = SelectedWatches.ToList(); if (selected.Any()) { GlobalWin.Tools.Load(); @@ -1163,7 +1115,7 @@ namespace BizHawk.Client.EmuHawk } else if (e.KeyCode == Keys.A && e.Control && !e.Alt && !e.Shift) //Select All { - for (int x = 0; x < Watches.Count; x++) + for (var x = 0; x < _watches.Count; x++) { WatchListView.SelectItem(x, true); } diff --git a/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs b/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs index 7bebf799a7..adea491911 100644 --- a/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs +++ b/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs @@ -88,7 +88,7 @@ namespace BizHawk.Client.EmuHawk } } - public void SetWatch(MemoryDomain domain, List watches = null, Mode mode = Mode.New) + public void SetWatch(MemoryDomain domain, IEnumerable watches = null, Mode mode = Mode.New) { if (watches != null) {