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> <DependentUpon>TI83KeyPad.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="tools\Cheat.cs" /> <Compile Include="tools\Cheat.cs" />
<Compile Include="tools\CheatList.cs" />
<Compile Include="tools\Cheats.cs"> <Compile Include="tools\Cheats.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>

View File

@ -24,7 +24,7 @@ namespace BizHawk.MultiClient
private RetainedViewportPanel retainedPanel; private RetainedViewportPanel retainedPanel;
public string CurrentlyOpenRom; public string CurrentlyOpenRom;
SavestateManager StateSlots = new SavestateManager(); SavestateManager StateSlots = new SavestateManager();
public CheatList CheatList = new CheatList();
public bool PressFrameAdvance = false; public bool PressFrameAdvance = false;
public bool PressRewind = false; public bool PressRewind = false;
@ -1805,6 +1805,7 @@ namespace BizHawk.MultiClient
{ {
if (!Cheats1.IsHandleCreated || Cheats1.IsDisposed) if (!Cheats1.IsHandleCreated || Cheats1.IsDisposed)
{ {
Cheats1 = new Cheats();
Cheats1.Show(); Cheats1.Show();
} }
else 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 = ""; string currentCheatFile = "";
bool changes = false; bool changes = false;
//TODO: Delete me
public List<Cheat> GetCheatList() public List<Cheat> GetCheatList()
{ {
List<Cheat> c = new List<Cheat>(); List<Cheat> c = new List<Cheat>();
@ -51,7 +52,7 @@ namespace BizHawk.MultiClient
return false; return false;
else else
{ {
LoadCheatFile(CheatFile, false); Global.MainForm.CheatList.LoadCheatFile(CheatFile, false);
return true; return true;
} }
} }
@ -585,92 +586,18 @@ namespace BizHawk.MultiClient
return file; 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) public bool LoadCheatFile(string path, bool append)
{ {
int y; Global.MainForm.CheatList.LoadCheatFile(path, append);
var file = new FileInfo(path); UpdateNumberOfCheats();
if (file.Exists == false) return false; MessageLabel.Text = Path.GetFileName(Global.MainForm.CheatList.currentCheatFile);
DisplayCheatsList();
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);
UpdateNumberOfCheats();
}
if (Global.Config.DisableCheatsOnLoad)
{
for (int x = 0; x < cheatList.Count; x++)
cheatList[x].Disable();
}
return true; //TODO 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() private void OpenCheatFile()
{ {
@ -682,7 +609,6 @@ namespace BizHawk.MultiClient
if (r) if (r)
{ {
LoadCheatFile(file.FullName, false); LoadCheatFile(file.FullName, false);
DisplayCheatsList();
} }
} }
} }
@ -737,7 +663,7 @@ namespace BizHawk.MultiClient
c.Enable(); c.Enable();
} }
catch { catch {
NotSupportedError(); Global.MainForm.CheatList.NotSupportedError();
} }
return c; return c;
} }
@ -855,7 +781,7 @@ namespace BizHawk.MultiClient
} }
catch catch
{ {
NotSupportedError(); Global.MainForm.CheatList.NotSupportedError();
} }
} }
} }
@ -969,17 +895,7 @@ namespace BizHawk.MultiClient
CheatListView.Columns[4].Width = defaultOnWidth; 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) private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{ {

View File

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

View File

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