More code refactoring

This commit is contained in:
adelikat 2013-11-25 02:08:45 +00:00
parent 345b628dad
commit 720cf763cd
7 changed files with 463 additions and 638 deletions

View File

@ -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<int> SelectedIndices
{
get { return CheatListView.SelectedIndices.Cast<int>().ToList(); }
get { return CheatListView.SelectedIndices.Cast<int>(); }
}
private IEnumerable<Cheat> SelectedItems
@ -342,7 +335,7 @@ namespace BizHawk.Client.EmuHawk
private IEnumerable<Cheat> 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<int>();
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<int>();
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);

View File

@ -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<LuaFile>();
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<int> 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"))
{

View File

@ -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<Watch> watches)
public static void FreezeAddress(IEnumerable<Watch> 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<Watch> watches)
public static void UnfreezeAddress(IEnumerable<Watch> 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", ""),

View File

@ -21,9 +21,9 @@ namespace BizHawk.Client.EmuHawk
InitializeComponent();
}
public void SetWatch(List<Watch> watches)
public void SetWatch(IEnumerable<Watch> 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.";
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -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<KeyValuePair<string, int>> 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<int>();
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<int>();
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<Watch> SelectedWatches
private IEnumerable<int> SelectedIndices
{
get
{
var selected = new List<Watch>();
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<int>(); }
}
private IEnumerable<Watch> SelectedItems
{
get { return SelectedIndices.Select(index => _watches[index]); }
}
private IEnumerable<Watch> 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<HexEditor>();
@ -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);
}

View File

@ -88,7 +88,7 @@ namespace BizHawk.Client.EmuHawk
}
}
public void SetWatch(MemoryDomain domain, List<Watch> watches = null, Mode mode = Mode.New)
public void SetWatch(MemoryDomain domain, IEnumerable<Watch> watches = null, Mode mode = Mode.New)
{
if (watches != null)
{