BizHawk/BizHawk.Client.EmuHawk/tools/ToolHelpers.cs

299 lines
8.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
{
2013-11-28 22:06:38 +00:00
public static class ToolHelpers
{
public static FileInfo GetTasProjFileFromUser(string currentFile)
{
var ofd = new OpenFileDialog();
if (!String.IsNullOrWhiteSpace(currentFile))
{
ofd.FileName = Path.GetFileNameWithoutExtension(currentFile);
}
ofd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null);
ofd.Filter = "Tas Project Files (*.tasproj)|*.tasproj|All Files|*.*";
ofd.RestoreDirectory = true;
var result = ofd.ShowHawkDialog();
if (result != DialogResult.OK)
{
return null;
}
return new FileInfo(ofd.FileName);
}
public static FileInfo GetTasProjSaveFileFromUser(string currentFile)
{
var sfd = new SaveFileDialog();
if (!String.IsNullOrWhiteSpace(currentFile))
{
sfd.FileName = Path.GetFileNameWithoutExtension(currentFile);
sfd.InitialDirectory = Path.GetDirectoryName(currentFile);
}
else if (!(Global.Emulator is NullEmulator))
{
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null);
}
else
{
sfd.FileName = "NULL";
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null);
}
sfd.Filter = "Tas Project Files (*.tasproj)|*.tasproj|All Files|*.*";
sfd.RestoreDirectory = true;
var result = sfd.ShowHawkDialog();
if (result != DialogResult.OK)
{
return null;
}
return new FileInfo(sfd.FileName);
}
2013-10-27 07:54:00 +00:00
public static FileInfo GetWatchFileFromUser(string currentFile)
{
var ofd = new OpenFileDialog();
if (!String.IsNullOrWhiteSpace(currentFile))
{
ofd.FileName = Path.GetFileNameWithoutExtension(currentFile);
}
2013-11-28 22:06:38 +00:00
ofd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.WatchPathFragment, null);
2013-10-27 07:54:00 +00:00
ofd.Filter = "Watch Files (*.wch)|*.wch|All Files|*.*";
ofd.RestoreDirectory = true;
var result = ofd.ShowHawkDialog();
2013-10-27 07:54:00 +00:00
if (result != DialogResult.OK)
2013-11-28 22:06:38 +00:00
{
2013-10-27 07:54:00 +00:00
return null;
2013-11-28 22:06:38 +00:00
}
return new FileInfo(ofd.FileName);
2013-10-27 07:54:00 +00:00
}
public static FileInfo GetWatchSaveFileFromUser(string currentFile)
{
var sfd = new SaveFileDialog();
if (!String.IsNullOrWhiteSpace(currentFile))
{
sfd.FileName = Path.GetFileNameWithoutExtension(currentFile);
sfd.InitialDirectory = Path.GetDirectoryName(currentFile);
}
else if (!(Global.Emulator is NullEmulator))
{
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.WatchPathFragment, null);
2013-10-27 07:54:00 +00:00
}
else
{
sfd.FileName = "NULL";
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.WatchPathFragment, null);
2013-10-27 07:54:00 +00:00
}
2013-11-28 22:06:38 +00:00
2013-10-27 07:54:00 +00:00
sfd.Filter = "Watch Files (*.wch)|*.wch|All Files|*.*";
sfd.RestoreDirectory = true;
var result = sfd.ShowHawkDialog();
2013-10-27 07:54:00 +00:00
if (result != DialogResult.OK)
2013-11-28 22:06:38 +00:00
{
2013-10-27 07:54:00 +00:00
return null;
2013-11-28 22:06:38 +00:00
}
return new FileInfo(sfd.FileName);
2013-10-27 07:54:00 +00:00
}
public static FileInfo GetCheatFileFromUser(string currentFile)
{
var ofd = new OpenFileDialog();
if (!String.IsNullOrWhiteSpace(currentFile))
{
ofd.FileName = Path.GetFileNameWithoutExtension(currentFile);
}
2013-11-28 22:06:38 +00:00
ofd.InitialDirectory = PathManager.GetCheatsPath(Global.Game);
ofd.Filter = "Cheat Files (*.cht)|*.cht|All Files|*.*";
ofd.RestoreDirectory = true;
var result = ofd.ShowHawkDialog();
if (result != DialogResult.OK)
2013-11-28 22:06:38 +00:00
{
return null;
2013-11-28 22:06:38 +00:00
}
return new FileInfo(ofd.FileName);
}
public static FileInfo GetCheatSaveFileFromUser(string currentFile)
{
var sfd = new SaveFileDialog();
if (!String.IsNullOrWhiteSpace(currentFile))
{
sfd.FileName = Path.GetFileNameWithoutExtension(currentFile);
}
else if (!(Global.Emulator is NullEmulator))
{
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
}
2013-11-28 22:06:38 +00:00
sfd.InitialDirectory = PathManager.GetCheatsPath(Global.Game);
sfd.Filter = "Cheat Files (*.cht)|*.cht|All Files|*.*";
sfd.RestoreDirectory = true;
var result = sfd.ShowHawkDialog();
if (result != DialogResult.OK)
{
return null;
}
var file = new FileInfo(sfd.FileName);
Global.Config.LastRomPath = file.DirectoryName;
return file;
}
public static ToolStripMenuItem GenerateAutoLoadItem(RecentFiles recent)
{
var auto = new ToolStripMenuItem { Text = "&Autoload", Checked = recent.AutoLoad };
auto.Click += (o, ev) => recent.ToggleAutoLoad();
return auto;
}
public static ToolStripItem[] GenerateRecentMenu(RecentFiles recent, Action<string> loadFileCallback)
{
var items = new List<ToolStripItem>();
if (recent.Empty)
{
var none = new ToolStripMenuItem { Enabled = false, Text = "None" };
items.Add(none);
}
else
{
2013-11-25 02:08:45 +00:00
foreach (var filename in recent)
{
2013-11-25 02:08:45 +00:00
var temp = filename;
var item = new ToolStripMenuItem { Text = temp };
item.Click += (o, ev) => loadFileCallback(temp);
items.Add(item);
}
}
items.Add(new ToolStripSeparator());
var clearitem = new ToolStripMenuItem { Text = "&Clear" };
clearitem.Click += (o, ev) => recent.Clear();
items.Add(clearitem);
return items.ToArray();
}
public static void HandleLoadError(RecentFiles recent, string path)
{
GlobalWin.Sound.StopSound();
2013-11-25 02:08:45 +00:00
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);
}
GlobalWin.Sound.StartSound();
}
2013-11-28 22:06:38 +00:00
public static IEnumerable<ToolStripItem> GenerateMemoryDomainMenuItems(Action<string> setCallback, string selectedDomain = "", int? maxSize = null)
{
var items = new List<ToolStripMenuItem>();
2013-11-20 01:17:01 +00:00
foreach (var domain in Global.Emulator.MemoryDomains)
{
2013-11-25 02:08:45 +00:00
var name = domain.Name;
2013-11-20 01:17:01 +00:00
var item = new ToolStripMenuItem { Text = name };
item.Click += (o, ev) => setCallback(name);
item.Checked = name == selectedDomain;
item.Enabled = !(maxSize.HasValue && domain.Size > maxSize.Value);
items.Add(item);
}
2013-11-20 01:17:01 +00:00
return items;
}
2013-10-05 15:45:39 +00:00
public static void PopulateMemoryDomainDropdown(ref ComboBox dropdown, MemoryDomain startDomain)
{
dropdown.Items.Clear();
if (Global.Emulator.MemoryDomains.Count > 0)
2013-10-05 15:45:39 +00:00
{
foreach (var domain in Global.Emulator.MemoryDomains)
2013-10-05 15:45:39 +00:00
{
var result = dropdown.Items.Add(domain.ToString());
if (domain.Name == startDomain.Name)
{
dropdown.SelectedIndex = result;
}
}
}
}
public static void UpdateCheatRelatedTools(object sender, CheatCollection.CheatListEventArgs e)
{
GlobalWin.Tools.UpdateValues<RamWatch>();
GlobalWin.Tools.UpdateValues<RamSearch>();
GlobalWin.Tools.UpdateValues<HexEditor>();
if (GlobalWin.Tools.Has<Cheats>())
{
GlobalWin.Tools.Cheats.UpdateDialog();
}
GlobalWin.MainForm.UpdateCheatStatus();
}
2013-11-25 02:08:45 +00:00
public static void FreezeAddress(IEnumerable<Watch> watches)
{
2013-11-25 02:08:45 +00:00
foreach (var watch in watches.Where(watch => !watch.IsSeparator))
{
2013-11-28 22:06:38 +00:00
Global.CheatList.Add(new Cheat(watch, watch.Value ?? 0));
}
}
2013-11-25 02:08:45 +00:00
public static void UnfreezeAddress(IEnumerable<Watch> watches)
{
2013-11-25 02:08:45 +00:00
foreach (var watch in watches.Where(watch => !watch.IsSeparator))
{
2013-11-25 02:08:45 +00:00
Global.CheatList.Remove(watch);
}
}
public static void ViewInHexEditor(MemoryDomain domain, IEnumerable<int> addresses)
{
GlobalWin.Tools.Load<HexEditor>();
GlobalWin.Tools.HexEditor.SetToAddresses(addresses, domain);
}
public static void AddColumn(ListView listView, string columnName, bool enabled, int columnWidth)
{
if (enabled)
{
if (listView.Columns[columnName] == null)
{
2013-11-25 02:08:45 +00:00
var column = new ColumnHeader
{
Name = columnName,
2013-11-28 22:06:38 +00:00
Text = columnName.Replace("Column", String.Empty),
Width = columnWidth,
};
listView.Columns.Add(column);
}
}
}
}
}