new cheat dialog - implement file saving

This commit is contained in:
adelikat 2013-10-05 01:30:59 +00:00
parent 758b2b6675
commit 03302508fc
4 changed files with 194 additions and 52 deletions

View File

@ -146,13 +146,24 @@ namespace BizHawk.MultiClient
public bool Save() public bool Save()
{ {
if (!String.IsNullOrWhiteSpace(_currentFileName)) if (String.IsNullOrWhiteSpace(_currentFileName))
{ {
return SaveFile(); _currentFileName = GenerateDefaultFilename();
}
return SaveFile(_currentFileName);
}
public bool SaveAs()
{
var file = GetSaveFileFromUser();
if (file != null)
{
return SaveFile(file.FullName);
} }
else else
{ {
return SaveAs(); return false;
} }
} }
@ -186,41 +197,48 @@ namespace BizHawk.MultiClient
{ {
try try
{ {
int ADDR, VALUE; if (s == "----")
int? COMPARE;
MemoryDomain DOMAIN;
bool ENABLED;
string NAME;
if (s.Length < 6) continue;
//NewCheat c = new NewCheat(
string[] vals = s.Split('\t');
ADDR = Int32.Parse(vals[0], NumberStyles.HexNumber);
VALUE = Int32.Parse(vals[1], NumberStyles.HexNumber);
if (vals[2] == "N")
{ {
COMPARE = null; _cheatList.Add(NewCheat.Separator);
} }
else else
{ {
COMPARE = Int32.Parse(vals[2], NumberStyles.HexNumber); int ADDR, VALUE;
int? COMPARE;
MemoryDomain DOMAIN;
bool ENABLED;
string NAME;
if (s.Length < 6) continue;
//NewCheat c = new NewCheat(
string[] vals = s.Split('\t');
ADDR = Int32.Parse(vals[0], NumberStyles.HexNumber);
VALUE = Int32.Parse(vals[1], NumberStyles.HexNumber);
if (vals[2] == "N")
{
COMPARE = null;
}
else
{
COMPARE = Int32.Parse(vals[2], NumberStyles.HexNumber);
}
DOMAIN = ToolHelpers.DomainByName(vals[3]);
ENABLED = vals[4] == "1";
NAME = vals[5];
Watch w = Watch.GenerateWatch(
DOMAIN,
ADDR,
Watch.WatchSize.Byte,
Watch.DisplayType.Hex,
NAME,
false
);
NewCheat c = new NewCheat(w, COMPARE, ENABLED);
_cheatList.Add(c);
} }
DOMAIN = ToolHelpers.DomainByName(vals[3]);
ENABLED = vals[4] == "1";
NAME = vals[5];
Watch w = Watch.GenerateWatch(
DOMAIN,
ADDR,
Watch.WatchSize.Byte,
Watch.DisplayType.Hex,
NAME,
false
);
NewCheat c = new NewCheat(w, COMPARE, ENABLED);
_cheatList.Add(c);
} }
catch catch
{ {
@ -239,14 +257,74 @@ namespace BizHawk.MultiClient
#region privates #region privates
private bool SaveFile() private string GenerateDefaultFilename()
{ {
throw new NotImplementedException(); PathEntry pathEntry = Global.Config.PathEntries[Global.Emulator.SystemId, "Cheats"];
if (pathEntry == null)
{
pathEntry = Global.Config.PathEntries[Global.Emulator.SystemId, "Base"];
}
string path = PathManager.MakeAbsolutePath(pathEntry.Path, Global.Emulator.SystemId);
var f = new FileInfo(path);
if (f.Directory != null && f.Directory.Exists == false)
{
f.Directory.Create();
}
return Path.Combine(path, PathManager.FilesystemSafeName(Global.Game) + ".cht");
} }
private bool SaveAs() private bool SaveFile(string path)
{ {
throw new NotImplementedException(); try
{
FileInfo file = new FileInfo(path);
if (file.Directory != null && !file.Directory.Exists)
{
file.Directory.Create();
}
using (StreamWriter sw = new StreamWriter(path))
{
StringBuilder sb = new StringBuilder();
foreach (var cheat in _cheatList)
{
if (cheat.IsSeparator)
{
sb.AppendLine("----");
}
else
{
//Set to hex for saving
Watch.DisplayType type = cheat.Type;
cheat.SetType(Watch.DisplayType.Hex);
sb
.Append(cheat.AddressStr).Append('\t')
.Append(cheat.ValueStr).Append('\t')
.Append(cheat.Compare.HasValue ? cheat.Compare.Value : 'N').Append('\t')
.Append(cheat.Domain != null ? cheat.Domain.Name : String.Empty).Append('\t')
.Append(cheat.Enabled ? '1' : '0').Append('\t')
.Append(cheat.Name)
.AppendLine();
//TODO: save big endian, size, and display type
}
}
sw.WriteLine(sb.ToString());
}
_changes = false;
_currentFileName = path;
return true;
}
catch
{
return false;
}
} }
#endregion #endregion
@ -273,6 +351,33 @@ namespace BizHawk.MultiClient
return file; return file;
} }
private FileInfo GetSaveFileFromUser()
{
var sfd = new SaveFileDialog();
if (!String.IsNullOrWhiteSpace(_currentFileName))
{
sfd.FileName = Path.GetFileNameWithoutExtension(Global.CheatList.CurrentCheatFile);
}
else if (!(Global.Emulator is NullEmulator))
{
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
}
sfd.InitialDirectory = PathManager.GetCheatsPath(Global.Game);
sfd.Filter = "Cheat Files (*.cht)|*.cht|All Files|*.*";
sfd.RestoreDirectory = true;
Global.Sound.StopSound();
var result = sfd.ShowDialog();
Global.Sound.StartSound();
if (result != DialogResult.OK)
{
return null;
}
var file = new FileInfo(sfd.FileName);
Global.Config.LastRomPath = file.DirectoryName;
return file;
}
#endregion #endregion
} }
} }

View File

@ -81,7 +81,13 @@ namespace BizHawk.MultiClient
get { if (_compare.HasValue && !IsSeparator) return _compare; else return null; } get { if (_compare.HasValue && !IsSeparator) return _compare; else return null; }
} }
public MemoryDomain Domain { get { return _watch.Domain; } } public MemoryDomain Domain
{
get
{
return _watch.Domain;
}
}
public Watch.WatchSize Size public Watch.WatchSize Size
{ {
@ -213,6 +219,14 @@ namespace BizHawk.MultiClient
} }
} }
public void SetType(Watch.DisplayType type)
{
if (Watch.AvailableTypes(_watch.Size).Contains(type))
{
_watch.Type = type;
}
}
#endregion #endregion
#region private parts #region private parts

View File

@ -204,21 +204,21 @@
// //
// SaveMenuItem // SaveMenuItem
// //
this.SaveMenuItem.Enabled = false;
this.SaveMenuItem.Image = global::BizHawk.MultiClient.Properties.Resources.SaveAs; this.SaveMenuItem.Image = global::BizHawk.MultiClient.Properties.Resources.SaveAs;
this.SaveMenuItem.Name = "SaveMenuItem"; this.SaveMenuItem.Name = "SaveMenuItem";
this.SaveMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S))); this.SaveMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
this.SaveMenuItem.Size = new System.Drawing.Size(195, 22); this.SaveMenuItem.Size = new System.Drawing.Size(195, 22);
this.SaveMenuItem.Text = "&Save"; this.SaveMenuItem.Text = "&Save";
this.SaveMenuItem.Click += new System.EventHandler(this.SaveMenuItem_Click);
// //
// SaveAsMenuItem // SaveAsMenuItem
// //
this.SaveAsMenuItem.Enabled = false;
this.SaveAsMenuItem.Name = "SaveAsMenuItem"; this.SaveAsMenuItem.Name = "SaveAsMenuItem";
this.SaveAsMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift) this.SaveAsMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift)
| System.Windows.Forms.Keys.S))); | System.Windows.Forms.Keys.S)));
this.SaveAsMenuItem.Size = new System.Drawing.Size(195, 22); this.SaveAsMenuItem.Size = new System.Drawing.Size(195, 22);
this.SaveAsMenuItem.Text = "Save &As..."; this.SaveAsMenuItem.Text = "Save &As...";
this.SaveAsMenuItem.Click += new System.EventHandler(this.SaveAsMenuItem_Click);
// //
// AppendMenuItem // AppendMenuItem
// //
@ -503,12 +503,12 @@
// SaveToolBarItem // SaveToolBarItem
// //
this.SaveToolBarItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.SaveToolBarItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.SaveToolBarItem.Enabled = false;
this.SaveToolBarItem.Image = ((System.Drawing.Image)(resources.GetObject("SaveToolBarItem.Image"))); this.SaveToolBarItem.Image = ((System.Drawing.Image)(resources.GetObject("SaveToolBarItem.Image")));
this.SaveToolBarItem.ImageTransparentColor = System.Drawing.Color.Magenta; this.SaveToolBarItem.ImageTransparentColor = System.Drawing.Color.Magenta;
this.SaveToolBarItem.Name = "SaveToolBarItem"; this.SaveToolBarItem.Name = "SaveToolBarItem";
this.SaveToolBarItem.Size = new System.Drawing.Size(23, 22); this.SaveToolBarItem.Size = new System.Drawing.Size(23, 22);
this.SaveToolBarItem.Text = "&Save"; this.SaveToolBarItem.Text = "&Save";
this.SaveToolBarItem.Click += new System.EventHandler(this.SaveMenuItem_Click);
// //
// toolStripSeparator // toolStripSeparator
// //

View File

@ -56,7 +56,7 @@ namespace BizHawk.MultiClient
{ {
CheatListView.ItemCount = Global.CheatList2.Count; CheatListView.ItemCount = Global.CheatList2.Count;
TotalLabel.Text = Global.CheatList2.CheatCount.ToString() TotalLabel.Text = Global.CheatList2.CheatCount.ToString()
+ (Global.CheatList2.CheatCount == 1 ? " cheat" : " cheats") + (Global.CheatList2.CheatCount == 1 ? " cheat " : " cheats ")
+ Global.CheatList2.ActiveCheatCount.ToString() + " active"; + Global.CheatList2.ActiveCheatCount.ToString() + " active";
} }
@ -87,16 +87,14 @@ namespace BizHawk.MultiClient
private void UpdateMessageLabel(bool saved = false) private void UpdateMessageLabel(bool saved = false)
{ {
string message = String.Empty; string message = String.Empty;
if (!String.IsNullOrWhiteSpace(Global.CheatList2.CurrentFileName))
if (saved)
{ {
if (saved) message = Path.GetFileName(Global.CheatList2.CurrentFileName) + " saved.";
{ }
message = Path.GetFileName(Global.CheatList2.CurrentFileName) + " saved."; else
} {
else message = Path.GetFileName(Global.CheatList2.CurrentFileName) + (Global.CheatList2.Changes ? " *" : String.Empty);
{
message = Path.GetFileName(Global.CheatList2.CurrentFileName) + (Global.CheatList2.Changes ? " *" : String.Empty);
}
} }
MessageLabel.Text = message; MessageLabel.Text = message;
@ -145,6 +143,7 @@ namespace BizHawk.MultiClient
{ {
Global.CheatList2.Load(file.FullName, append); Global.CheatList2.Load(file.FullName, append);
UpdateListView(); UpdateListView();
UpdateMessageLabel();
Global.Config.RecentCheats.Add(Global.CheatList2.CurrentFileName); Global.Config.RecentCheats.Add(Global.CheatList2.CurrentFileName);
} }
} }
@ -397,6 +396,29 @@ namespace BizHawk.MultiClient
SaveMenuItem.Enabled = Global.CheatList2.Changes; SaveMenuItem.Enabled = Global.CheatList2.Changes;
} }
private void SaveMenuItem_Click(object sender, EventArgs e)
{
if (Global.CheatList2.Changes)
{
if (Global.CheatList2.Save())
{
UpdateMessageLabel(saved: true);
}
}
else
{
SaveAsMenuItem_Click(sender, e);
}
}
private void SaveAsMenuItem_Click(object sender, EventArgs e)
{
if (Global.CheatList2.SaveAs())
{
UpdateMessageLabel(saved: true);
}
}
private void RecentSubMenu_DropDownOpened(object sender, EventArgs e) private void RecentSubMenu_DropDownOpened(object sender, EventArgs e)
{ {
RecentSubMenu.DropDownItems.Clear(); RecentSubMenu.DropDownItems.Clear();
@ -467,8 +489,9 @@ namespace BizHawk.MultiClient
{ {
Global.CheatList2.Add(NewCheat.Separator); Global.CheatList2.Add(NewCheat.Separator);
} }
UpdateListView(); UpdateListView();
UpdateMessageLabel();
} }
private void MoveUpMenuItem_Click(object sender, EventArgs e) private void MoveUpMenuItem_Click(object sender, EventArgs e)