From 95c8e709b6ccab59ffb72156e3951882cf77dc7e Mon Sep 17 00:00:00 2001 From: "andres.delikat" Date: Sat, 6 Aug 2011 22:03:10 +0000 Subject: [PATCH] Cheats - more fixes, in particular saving & loading the .cht file from the same path! --- BizHawk.MultiClient/MainForm.cs | 3 +- BizHawk.MultiClient/tools/CheatList.cs | 82 +++++++++++++++++++++++++- BizHawk.MultiClient/tools/Cheats.cs | 81 +++---------------------- 3 files changed, 89 insertions(+), 77 deletions(-) diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index 2d4fa9cb0b..d714436890 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -107,6 +107,7 @@ namespace BizHawk.MultiClient Closing += (o, e) => { + Global.CheatList.SaveSettings(); CloseGame(); UserMovie.StopMovie(); SaveConfig(); @@ -982,7 +983,7 @@ namespace BizHawk.MultiClient TAStudio1.Restart(); if (Global.Config.LoadCheatFileByGame) { - if (Cheats1.AttemptLoadCheatFile()) + if (Global.CheatList.AttemptLoadCheatFile()) Global.RenderPanel.AddMessage("Cheats file loaded"); } Cheats1.Restart(); diff --git a/BizHawk.MultiClient/tools/CheatList.cs b/BizHawk.MultiClient/tools/CheatList.cs index cc81e4f54f..ab4f99a71a 100644 --- a/BizHawk.MultiClient/tools/CheatList.cs +++ b/BizHawk.MultiClient/tools/CheatList.cs @@ -72,8 +72,8 @@ namespace BizHawk.MultiClient Global.Config.RecentCheats.Add(file.FullName); changes = false; - - + + } if (Global.Config.DisableCheatsOnLoad) @@ -152,5 +152,83 @@ namespace BizHawk.MultiClient f.Directory.Create(); return path; } + + public bool SaveCheatFile(string path) + { + var file = new FileInfo(path); + if (!file.Directory.Exists) + file.Directory.Create(); + + using (StreamWriter sw = new StreamWriter(path)) + { + string str = ""; + + for (int x = 0; x < cheatList.Count; x++) + { + str += FormatAddress(cheatList[x].address) + "\t"; + str += String.Format("{0:X2}", cheatList[x].value) + "\t"; + str += cheatList[x].domain.Name + "\t"; + if (cheatList[x].IsEnabled()) + str += "1\t"; + else + str += "0\t"; + str += cheatList[x].name + "\n"; + } + + sw.WriteLine(str); + } + changes = false; + return true; + } + + public string FormatAddress(int address) + { + return String.Format("{0:X" + GetNumDigits((Global.Emulator.MainMemory.Size - 1)).ToString() + "}", address); + } + + + public void SaveSettings() + { + if (Global.Config.CheatsAutoSaveOnClose) + { + if (changes && cheatList.Count > 0) + { + if (currentCheatFile.Length == 0) + currentCheatFile = MakeDefaultFilename(); + + SaveCheatFile(Global.CheatList.currentCheatFile); + } + } + } + + public string MakeDefaultFilename() + { + return Path.Combine(Global.CheatList.GetCheatsPath(), PathManager.FilesystemSafeName(Global.Game) + ".cht"); + } + + private int GetNumDigits(Int32 i) + { + if (i < 0x10000) return 4; + if (i < 0x1000000) return 6; + else return 8; + } + + /// + /// Looks for a .cht file that matches the name of the ROM loaded + /// It is up to the caller to determine which directory it looks + /// + public bool AttemptLoadCheatFile() + { + string CheatFile = MakeDefaultFilename(); + + var file = new FileInfo(CheatFile); + if (file.Exists == false) + return false; + else + { + LoadCheatFile(CheatFile, false); + return true; + } + } } } diff --git a/BizHawk.MultiClient/tools/Cheats.cs b/BizHawk.MultiClient/tools/Cheats.cs index eeeba179db..42e5658586 100644 --- a/BizHawk.MultiClient/tools/Cheats.cs +++ b/BizHawk.MultiClient/tools/Cheats.cs @@ -24,28 +24,7 @@ namespace BizHawk.MultiClient int defaultDomainWidth; int defaultOnWidth; - /// - /// Looks for a .cht file that matches the name of the ROM loaded - /// It is up to the caller to determine which directory it looks - /// - public bool AttemptLoadCheatFile() - { - string CheatFile = MakeDefaultFilename(); - - var file = new FileInfo(CheatFile); - if (file.Exists == false) - return false; - else - { - Global.CheatList.LoadCheatFile(CheatFile, false); - return true; - } - } - - private string MakeDefaultFilename() - { - return Path.Combine(Global.CheatList.GetCheatsPath(), PathManager.FilesystemSafeName(Global.Game) + ".cht"); - } + private void ClearFields() { @@ -102,7 +81,7 @@ namespace BizHawk.MultiClient } if (column == 1) //Address { - text = FormatAddress(Global.CheatList.cheatList[index].address); + text = Global.CheatList.FormatAddress(Global.CheatList.cheatList[index].address); } if (column == 2) //Value { @@ -135,7 +114,6 @@ namespace BizHawk.MultiClient AddressBox.MaxLength = GetNumDigits(Global.Emulator.MainMemory.Size - 1); DisplayCheatsList(); CheatListView.Refresh(); - UpdateNumberOfCheats(); //Hacky Disabling if not a supported core switch (Global.Emulator.SystemId) @@ -272,17 +250,6 @@ namespace BizHawk.MultiClient Global.Config.CheatsValueWidth = CheatListView.Columns[Global.Config.CheatsValueIndex].Width; Global.Config.CheatsDomainWidth = CheatListView.Columns[Global.Config.CheatsDomainIndex].Width; Global.Config.CheatsOnWidth = CheatListView.Columns[Global.Config.CheatsOnIndex].Width; - - if (Global.Config.CheatsAutoSaveOnClose) - { - if (Global.CheatList.changes) - { - if (Global.CheatList.currentCheatFile.Length == 0) - Global.CheatList.currentCheatFile = MakeDefaultFilename(); - - SaveCheatFile(Global.CheatList.currentCheatFile); - } - } } public void DisplayCheatsList() @@ -402,41 +369,13 @@ namespace BizHawk.MultiClient var file = GetSaveFileFromUser(); if (file != null) { - SaveCheatFile(file.FullName); + Global.CheatList.SaveCheatFile(file.FullName); Global.CheatList.currentCheatFile = file.FullName; MessageLabel.Text = Path.GetFileName(Global.CheatList.currentCheatFile) + " saved."; Global.Config.RecentCheats.Add(Global.CheatList.currentCheatFile); } } - private bool SaveCheatFile(string path) - { - var file = new FileInfo(path); - if (!file.Directory.Exists) - file.Directory.Create(); - - using (StreamWriter sw = new StreamWriter(path)) - { - string str = ""; - - for (int x = 0; x < Global.CheatList.cheatList.Count; x++) - { - str += FormatAddress(Global.CheatList.cheatList[x].address) + "\t"; - str += String.Format("{0:X2}", Global.CheatList.cheatList[x].value) + "\t"; - str += Global.CheatList.cheatList[x].domain.Name + "\t"; - if (Global.CheatList.cheatList[x].IsEnabled()) - str += "1\t"; - else - str += "0\t"; - str += Global.CheatList.cheatList[x].name + "\n"; - } - - sw.WriteLine(str); - } - Global.CheatList.changes = false; - return true; - } - public bool AskSave() { if (Global.CheatList.changes) @@ -451,7 +390,7 @@ namespace BizHawk.MultiClient SaveAs(); } else - SaveCheatFile(Global.CheatList.currentCheatFile); + Global.CheatList.SaveCheatFile(Global.CheatList.currentCheatFile); return true; } else if (result == DialogResult.No) @@ -491,7 +430,7 @@ namespace BizHawk.MultiClient { if (Global.CheatList.changes) { - SaveCheatFile(Global.CheatList.currentCheatFile); + Global.CheatList.SaveCheatFile(Global.CheatList.currentCheatFile); } else { @@ -504,7 +443,7 @@ namespace BizHawk.MultiClient if (string.Compare(Global.CheatList.currentCheatFile, "") == 0) return; if (Global.CheatList.changes) - SaveCheatFile(Global.CheatList.currentCheatFile); + Global.CheatList.SaveCheatFile(Global.CheatList.currentCheatFile); } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) @@ -512,7 +451,6 @@ namespace BizHawk.MultiClient SaveAs(); } - private FileInfo GetFileFromUser() { var ofd = new OpenFileDialog(); @@ -747,17 +685,12 @@ namespace BizHawk.MultiClient if (indexes.Count > 0) { NameBox.Text = Global.CheatList.cheatList[indexes[0]].name; - AddressBox.Text = FormatAddress(Global.CheatList.cheatList[indexes[0]].address); + AddressBox.Text = Global.CheatList.FormatAddress(Global.CheatList.cheatList[indexes[0]].address); ValueBox.Text = String.Format("{0:X2}", Global.CheatList.cheatList[indexes[0]].value); CheatListView.Refresh(); } } - private string FormatAddress(int address) - { - return String.Format("{0:X" + GetNumDigits((Global.Emulator.MainMemory.Size - 1)).ToString() + "}", address); - } - private void EditButton_Click(object sender, EventArgs e) { //TODO: this fails because selected index must go away to edit values, either prevent that or keep track of the last selected index