move Genesis action replay decoding logic to its own class

This commit is contained in:
adelikat 2020-02-29 09:14:13 -06:00
parent de1bd8c849
commit 53b3ef6e45
3 changed files with 69 additions and 42 deletions

View File

@ -2,6 +2,7 @@
namespace BizHawk.Client.Common.cheats
{
// TODO: validate string and throw
public class GbGameSharkDecoder
{
private readonly string _code;

View File

@ -0,0 +1,53 @@
using System;
using System.Globalization;
namespace BizHawk.Client.Common.cheats
{
// TODO: validate string and throw
public class GenesisActionReplayDecoder
{
private readonly string _code;
public GenesisActionReplayDecoder(string code)
{
_code = code;
Decode();
}
public int Address { get; private set; }
public int Value { get; private set; }
public int ByteSize { get; private set; }
public void Decode()
{
var parseString = _code.Remove(0, 2);
switch (_code.Length)
{
case 9:
// Sample Code of 1-Byte:
// FFF761:64
// Becomes:
// Address: F761
// Value: 64
Address = int.Parse(parseString.Remove(4, 3), NumberStyles.HexNumber);
Value = int.Parse(parseString.Remove(0, 5), NumberStyles.HexNumber);
ByteSize = 1;
break;
case 11:
// Sample Code of 2-Byte:
// FFF761:6411
// Becomes:
// Address: F761
// Value: 6411
Address = int.Parse(parseString.Remove(4, 5), NumberStyles.HexNumber);
Value = int.Parse(parseString.Remove(0, 5), NumberStyles.HexNumber);
ByteSize = 2;
break;
default:
// We could have checked above but here is fine, since it's a quick check due to one of three possibilities.
throw new InvalidOperationException(
"All Genesis Action Replay/Pro Action Replay Codes need to be either 9 or 11 characters in length");
}
}
}
}

View File

@ -2193,7 +2193,9 @@ namespace BizHawk.Client.EmuHawk
// We start from Zero.
if (_singleCheat.IndexOf(":") != 6)
{
MessageBox.Show("All Genesis Action Replay/Pro Action Replay Codes need to contain a colon after the sixth character", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(
"All Genesis Action Replay/Pro Action Replay Codes need to contain a colon after the sixth character"
, "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
@ -2201,53 +2203,24 @@ namespace BizHawk.Client.EmuHawk
// TODO: Fix that.
if (_singleCheat.StartsWith("FF") == false)
{
MessageBox.Show("This Action Replay Code, is not understood by this tool.", "Tool Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
MessageBox.Show("This Action Replay Code, is not understood by this tool.", "Tool Error"
, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
// Now to do some work.
// Determine Length, to Determine Byte Size
_parseString = _singleCheat.Remove(0, 2);
switch (_singleCheat.Length)
{
case 9:
// Sample Code of 1-Byte:
// FFF761:64
// Becomes:
// Address: F761
// Value: 64
_ramAddress = _parseString.Remove(4, 3);
_ramValue = _parseString.Remove(0, 5);
_byteSize = 1;
break;
case 11:
// Sample Code of 2-Byte:
// FFF761:6411
// Becomes:
// Address: F761
// Value: 6411
_ramAddress = _parseString.Remove(4, 5);
_ramValue = _parseString.Remove(0, 5);
_byteSize = 2;
break;
default:
// We could have checked above but here is fine, since it's a quick check due to one of three possibilities.
MessageBox.Show("All Genesis Action Replay/Pro Action Replay Codes need to be either 9 or 11 characters in length", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var decoder = new GenesisActionReplayDecoder(_singleCheat);
try
{
if (_byteSize == 1)
{
var watch = Watch.GenerateWatch(MemoryDomains["68K RAM"], long.Parse(_ramAddress, NumberStyles.HexNumber), WatchSize.Byte, Common.DisplayType.Hex, false, txtDescription.Text);
Global.CheatList.Add(new Cheat(watch, int.Parse(_ramValue, NumberStyles.HexNumber)));
}
else if (_byteSize == 2)
{
var watch = Watch.GenerateWatch(MemoryDomains["68K RAM"], long.Parse(_ramAddress, NumberStyles.HexNumber), WatchSize.Word, Common.DisplayType.Hex, true, txtDescription.Text);
Global.CheatList.Add(new Cheat(watch, int.Parse(_ramValue, NumberStyles.HexNumber)));
}
var size = decoder.ByteSize == 1 ? WatchSize.Byte : WatchSize.Word;
var watch = Watch.GenerateWatch(
MemoryDomains["68K RAM"],
decoder.Address,
size,
Common.DisplayType.Hex,
false,
txtDescription.Text);
Global.CheatList.Add(new Cheat(watch, decoder.Value));
}
catch (Exception ex)
{