convert psx and saturn decoders and simplify

This commit is contained in:
adelikat 2020-02-29 13:56:31 -06:00
parent 4337328709
commit 8f00024361
4 changed files with 52 additions and 145 deletions

View File

@ -25,9 +25,9 @@ namespace BizHawk.Client.Common.cheats
public string Error => "";
}
public class InvalidResult : IDecodeResult
public class InvalidCheatCode : IDecodeResult
{
public InvalidResult(string error)
public InvalidCheatCode(string error)
{
Error = error;
}

View File

@ -4,41 +4,30 @@ using System.Globalization;
namespace BizHawk.Client.Common.cheats
{
// TODO: cheats support comparison type, so we could support a lot more codes, by having Compare and Type properties and parsing
public class PsxGameSharkDecoder
public static class PsxGameSharkDecoder
{
private readonly string _code;
public PsxGameSharkDecoder(string code)
{
_code = code;
Decode();
}
public int Address { get; private set; }
public int Value { get; private set; }
public WatchSize Size { get; private set; } = WatchSize.Word;
// 30XXXXXX 00YY
public void Decode()
public static IDecodeResult Decode(string code)
{
if (_code == null)
if (code == null)
{
throw new ArgumentNullException(nameof(_code));
throw new ArgumentNullException(nameof(code));
}
if (_code.IndexOf(" ") != 8)
if (code.IndexOf(" ") != 8)
{
throw new InvalidOperationException("All PSX GameShark Codes need to contain a space after the eighth character.");
return new InvalidCheatCode("All PSX GameShark Codes need to contain a space after the eighth character.");
}
if (_code.Length != 13)
if (code.Length != 13)
{
throw new InvalidOperationException("All PSX GameShark Cheats need to be 13 characters in length.");
return new InvalidCheatCode("All PSX GameShark Cheats need to be 13 characters in length.");
}
var type = _code.Substring(0, 2);
Size = type switch
var result = new DecodeResult();
var type = code.Substring(0, 2);
result.Size = type switch
{
"10" => WatchSize.Word,
"11" => WatchSize.Word,
@ -60,9 +49,11 @@ namespace BizHawk.Client.Common.cheats
_ => WatchSize.Byte
};
var s = _code.Remove(0, 2);
Address = int.Parse(s.Remove(6, 5), NumberStyles.HexNumber);
Value = int.Parse(s.Remove(0, 7), NumberStyles.HexNumber);
var s = code.Remove(0, 2);
result.Address = int.Parse(s.Remove(6, 5), NumberStyles.HexNumber);
result.Value = int.Parse(s.Remove(0, 7), NumberStyles.HexNumber);
return result;
}
}
}

View File

@ -3,53 +3,44 @@ using System.Globalization;
namespace BizHawk.Client.Common.cheats
{
public class SaturnGameSharkDecoder
public static class SaturnGameSharkDecoder
{
private readonly string _code;
public SaturnGameSharkDecoder(string code)
{
_code = code;
Decode();
}
public int Address { get; private set; }
public int Value { get; private set; }
public WatchSize Size { get; private set; } = WatchSize.Word;
// Sample Input for Saturn:
// 160949FC 0090
// Address: 0949FC
// Value: 90
// Note, 3XXXXXXX are Big Endian
// Remove first two octets
public void Decode()
public static IDecodeResult Decode(string code)
{
if (_code == null)
if (code == null)
{
throw new ArgumentNullException(nameof(_code));
throw new ArgumentNullException(nameof(code));
}
if (_code.IndexOf(" ") != 8)
if (code.IndexOf(" ") != 8)
{
throw new InvalidOperationException("All Saturn GameShark Codes need to contain a space after the eighth character.");
return new InvalidCheatCode("All Saturn GameShark Codes need to contain a space after the eighth character.");
}
if (_code.Length != 13)
if (code.Length != 13)
{
throw new InvalidOperationException("All Saturn GameShark Cheats need to be 13 characters in length.");
return new InvalidCheatCode("All Saturn GameShark Cheats need to be 13 characters in length.");
}
var result = new DecodeResult { Size = WatchSize.Word };
// Only the first character really matters? 16 or 36?
var test = _code.Remove(2, 11).Remove(1, 1);
var test = code.Remove(2, 11).Remove(1, 1);
if (test == "3")
{
Size = WatchSize.Byte;
result.Size = WatchSize.Byte;
}
var s = _code.Remove(0, 2);
Address = int.Parse(s.Remove(6, 5), NumberStyles.HexNumber);
Value = int.Parse(s.Remove(0, 7));
var s = code.Remove(0, 2);
result.Address = int.Parse(s.Remove(6, 5), NumberStyles.HexNumber);
result.Value = int.Parse(s.Remove(0, 7));
return result;
}
}
}

View File

@ -279,10 +279,10 @@ namespace BizHawk.Client.EmuHawk
private void Nes(string code)
{
var description = Description(code);
var result = NesGameGenieDecoder.Decode(code);
if (result.IsValid)
{
var description = Description(code);
Global.CheatList.Add(result.ToCheat(MemoryDomains.SystemBus, description));
}
else
@ -291,109 +291,34 @@ namespace BizHawk.Client.EmuHawk
}
}
private void Psx(string cheat)
private void Psx(string code)
{
// These codes, more or less work without Needing much work.
if (cheat.IndexOf(" ") != 8)
var result = PsxGameSharkDecoder.Decode(code);
if (result.IsValid)
{
MessageBox.Show("All PSX GameShark Codes need to contain a space after the eighth character", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
var description = Description(code);
Global.CheatList.Add(result.ToCheat(MemoryDomains["MainRAM"], description));
}
if (cheat.Length != 13)
else
{
MessageBox.Show("All PSX GameShark Cheats need to be 13 characters in length.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
InputError(result.Error);
}
// This determines what kind of Code we have
var test = cheat.Substring(0, 2);
switch (test)
{
case "30":
case "80":
break;
case "E0":
case "E1":
case "E2":
case "D0":
case "D1":
case "D2":
case "D3":
case "D4":
case "D5":
case "D6":
case "10":
case "11":
case "20":
case "21":
MessageBox.Show("The code you entered is not supported by BizHawk.", "Emulator Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
case "C0":
case "C1":
case "C2":
MessageBox.Show("The code you entered is not supported by BizHawk.", "Emulator Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
// Slow-Mo
case "40":
InputError("The code you entered is not needed by Bizhawk.");
return;
case "50":
MessageBox.Show("The code you entered is not supported by this tool. Please Submit the Game's Name, Cheat/Code and Purpose to the BizHawk forums.", "Tool Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
default:
MessageBox.Show("The GameShark code entered is not a recognized format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var decoder = new PsxGameSharkDecoder(cheat);
// Is Work RAM High may be incorrect?
var watch = Watch.GenerateWatch(MemoryDomains["MainRAM"], decoder.Address, decoder.Size, Common.DisplayType.Hex, false, txtDescription.Text);
Global.CheatList.Add(new Cheat(watch, decoder.Value));
}
private void Saturn(string cheat)
private void Saturn(string code)
{
if (cheat.IndexOf(" ") != 8)
var result = SaturnGameSharkDecoder.Decode(code);
if (result.IsValid)
{
InputError("All Saturn GameShark Codes need to contain a space after the eighth character.");
return;
}
var description = Description(code);
if (cheat.Length != 13)
// Is Work RAM High may be incorrect?
Global.CheatList.Add(result.ToCheat(MemoryDomains["Work Ram High"], description));
}
else
{
InputError("All Saturn GameShark Cheats need to be 13 characters in length.");
return;
InputError(result.Error);
}
// This is a special test. Only the first character really matters? 16 or 36?
var test = cheat.Remove(2, 11).Remove(1, 1);
switch (test)
{
case "1":
case "3":
break;
// 0 writes once.
case "0":
// D is RAM Equal To Activator, do Next Value
case "D":
MessageBox.Show("The code you entered is not supported by BizHawk.", "Emulator Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
case "F":
MessageBox.Show("The code you entered is not needed by Bizhawk.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
default:
InputError("The GameShark code entered is not a recognized format.");
return;
}
var decoder = new SaturnGameSharkDecoder(cheat);
// Is Work RAM High may be incorrect?
var watch = Watch.GenerateWatch(MemoryDomains["Work Ram High"], decoder.Address, decoder.Size, Common.DisplayType.Hex, true, txtDescription.Text);
Global.CheatList.Add(new Cheat(watch, decoder.Value));
}
// This also handles Game Gear due to shared hardware. Go figure.