Pull the cheat list and some related functions out of the Cheats winform and into a CheatList object. Still some bugs to work out, but at least the cheat window doesn't crash when opened a 2nd time

This commit is contained in:
andres.delikat 2011-08-06 02:34:24 +00:00
parent fb7018b94c
commit c3d08b3b7b
6 changed files with 133 additions and 99 deletions

View File

@ -312,6 +312,7 @@
<DependentUpon>TI83KeyPad.cs</DependentUpon>
</Compile>
<Compile Include="tools\Cheat.cs" />
<Compile Include="tools\CheatList.cs" />
<Compile Include="tools\Cheats.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -24,7 +24,7 @@ namespace BizHawk.MultiClient
private RetainedViewportPanel retainedPanel;
public string CurrentlyOpenRom;
SavestateManager StateSlots = new SavestateManager();
public CheatList CheatList = new CheatList();
public bool PressFrameAdvance = false;
public bool PressRewind = false;
@ -1805,6 +1805,7 @@ namespace BizHawk.MultiClient
{
if (!Cheats1.IsHandleCreated || Cheats1.IsDisposed)
{
Cheats1 = new Cheats();
Cheats1.Show();
}
else

View File

@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Windows.Forms;
namespace BizHawk.MultiClient
{
public class CheatList
{
public List<Cheat> cheatList = new List<Cheat>();
public string currentCheatFile = "";
public bool changes = false;
public bool LoadCheatFile(string path, bool append)
{
int y;
var file = new FileInfo(path);
if (file.Exists == false) return false;
using (StreamReader sr = file.OpenText())
{
if (!append) currentCheatFile = path;
string s = "";
string temp = "";
if (append == false)
cheatList.Clear(); //Wipe existing list and read from file
while ((s = sr.ReadLine()) != null)
{
if (s.Length < 6) continue;
Cheat c = new Cheat();
temp = s.Substring(0, s.IndexOf('\t')); //Address
c.address = int.Parse(temp, NumberStyles.HexNumber);
y = s.IndexOf('\t') + 1;
s = s.Substring(y, s.Length - y); //Value
temp = s.Substring(0, 2);
c.value = byte.Parse(temp, NumberStyles.HexNumber);
y = s.IndexOf('\t') + 1;
s = s.Substring(y, s.Length - y); //Memory Domain
temp = s.Substring(0, s.IndexOf('\t'));
c.domain = SetDomain(temp);
y = s.IndexOf('\t') + 1;
s = s.Substring(y, s.Length - y); //Enabled
y = int.Parse(s[0].ToString());
try
{
if (y == 0)
c.Disable();
else
c.Enable();
}
catch
{
NotSupportedError();
}
y = s.IndexOf('\t') + 1;
s = s.Substring(y, s.Length - y); //Name
c.name = s;
cheatList.Add(c);
}
Global.Config.RecentCheats.Add(file.FullName);
changes = false;
}
if (Global.Config.DisableCheatsOnLoad)
{
for (int x = 0; x < cheatList.Count; x++)
cheatList[x].Disable();
}
return true; //TODO
}
public void NotSupportedError()
{
MessageBox.Show("Unable to enable cheat for this platform, cheats are not supported for " + Global.Emulator.SystemId, "Cheat error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private MemoryDomain SetDomain(string name)
{
//Attempts to find the memory domain by name, if it fails, it defaults to index 0
for (int x = 0; x < Global.Emulator.MemoryDomains.Count; x++)
{
if (Global.Emulator.MemoryDomains[x].Name == name)
return Global.Emulator.MemoryDomains[x];
}
return Global.Emulator.MemoryDomains[0];
}
public bool IsActiveCheat(MemoryDomain d, int address)
{
for (int x = 0; x < cheatList.Count; x++)
{
if (cheatList[x].address == address && cheatList[x].domain.Name == d.Name)
{
return true;
}
}
return false;
}
}
}

View File

@ -29,6 +29,7 @@ namespace BizHawk.MultiClient
string currentCheatFile = "";
bool changes = false;
//TODO: Delete me
public List<Cheat> GetCheatList()
{
List<Cheat> c = new List<Cheat>();
@ -51,7 +52,7 @@ namespace BizHawk.MultiClient
return false;
else
{
LoadCheatFile(CheatFile, false);
Global.MainForm.CheatList.LoadCheatFile(CheatFile, false);
return true;
}
}
@ -585,92 +586,18 @@ namespace BizHawk.MultiClient
return file;
}
private MemoryDomain SetDomain(string name)
{
//Attempts to find the memory domain by name, if it fails, it defaults to index 0
for (int x = 0; x < Global.Emulator.MemoryDomains.Count; x++)
{
if (Global.Emulator.MemoryDomains[x].Name == name)
return Global.Emulator.MemoryDomains[x];
}
return Global.Emulator.MemoryDomains[0];
}
public bool LoadCheatFile(string path, bool append)
{
int y;
var file = new FileInfo(path);
if (file.Exists == false) return false;
using (StreamReader sr = file.OpenText())
{
if (!append) currentCheatFile = path;
string s = "";
string temp = "";
if (append == false)
cheatList.Clear(); //Wipe existing list and read from file
while ((s = sr.ReadLine()) != null)
{
if (s.Length < 6) continue;
Cheat c = new Cheat();
temp = s.Substring(0, s.IndexOf('\t')); //Address
c.address = int.Parse(temp, NumberStyles.HexNumber);
y = s.IndexOf('\t') + 1;
s = s.Substring(y, s.Length - y); //Value
temp = s.Substring(0, 2);
c.value = byte.Parse(temp, NumberStyles.HexNumber);
y = s.IndexOf('\t') + 1;
s = s.Substring(y, s.Length - y); //Memory Domain
temp = s.Substring(0, s.IndexOf('\t'));
c.domain = SetDomain(temp);
y = s.IndexOf('\t') + 1;
s = s.Substring(y, s.Length - y); //Enabled
y = int.Parse(s[0].ToString());
try
{
if (y == 0)
c.Disable();
else
c.Enable();
}
catch
{
NotSupportedError();
}
y = s.IndexOf('\t') + 1;
s = s.Substring(y, s.Length - y); //Name
c.name = s;
cheatList.Add(c);
}
Global.Config.RecentCheats.Add(file.FullName);
changes = false;
MessageLabel.Text = Path.GetFileName(file.FullName);
Global.MainForm.CheatList.LoadCheatFile(path, append);
UpdateNumberOfCheats();
}
if (Global.Config.DisableCheatsOnLoad)
{
for (int x = 0; x < cheatList.Count; x++)
cheatList[x].Disable();
}
MessageLabel.Text = Path.GetFileName(Global.MainForm.CheatList.currentCheatFile);
DisplayCheatsList();
return true; //TODO
}
private void NotSupportedError()
{
MessageBox.Show("Unable to enable cheat for this platform, cheats are not supported for " + Global.Emulator.SystemId, "Cheat error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void OpenCheatFile()
{
@ -682,7 +609,6 @@ namespace BizHawk.MultiClient
if (r)
{
LoadCheatFile(file.FullName, false);
DisplayCheatsList();
}
}
}
@ -737,7 +663,7 @@ namespace BizHawk.MultiClient
c.Enable();
}
catch {
NotSupportedError();
Global.MainForm.CheatList.NotSupportedError();
}
return c;
}
@ -855,7 +781,7 @@ namespace BizHawk.MultiClient
}
catch
{
NotSupportedError();
Global.MainForm.CheatList.NotSupportedError();
}
}
}
@ -969,17 +895,7 @@ namespace BizHawk.MultiClient
CheatListView.Columns[4].Width = defaultOnWidth;
}
public bool IsActiveCheat(MemoryDomain d, int address)
{
for (int x = 0; x < cheatList.Count; x++)
{
if (cheatList[x].address == address && cheatList[x].domain.Name == d.Name)
{
return true;
}
}
return false;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{

View File

@ -529,10 +529,10 @@ namespace BizHawk.MultiClient
if (!weededList.Contains(searchList[index]))
{
color = Color.Pink;
if (Global.MainForm.Cheats1.IsActiveCheat(Domain, searchList[index].address))
if (Global.MainForm.CheatList.IsActiveCheat(Domain, searchList[index].address))
color = Color.Purple;
}
else if (Global.MainForm.Cheats1.IsActiveCheat(Domain, searchList[index].address))
else if (Global.MainForm.CheatList.IsActiveCheat(Domain, searchList[index].address))
color = Color.LightCyan;
else
color = Color.White;

View File

@ -152,7 +152,7 @@ namespace BizHawk.MultiClient
{
if (watchList[index].type == atype.SEPARATOR)
color = this.BackColor;
if (Global.MainForm.Cheats1.IsActiveCheat(Domain, watchList[index].address))
if (Global.MainForm.CheatList.IsActiveCheat(Domain, watchList[index].address))
color = Color.LightCyan;
}