From 53b3ef6e453f2d1dc82f288a423c3666bb01c00b Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 29 Feb 2020 09:14:13 -0600 Subject: [PATCH] move Genesis action replay decoding logic to its own class --- .../cheats/GbGameSharkDecoder.cs | 1 + .../cheats/GenesisActionReplayDecoder.cs | 53 +++++++++++++++++ BizHawk.Client.EmuHawk/tools/GameShark.cs | 57 +++++-------------- 3 files changed, 69 insertions(+), 42 deletions(-) create mode 100644 BizHawk.Client.Common/cheats/GenesisActionReplayDecoder.cs diff --git a/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs b/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs index e9d05dc412..73fd993310 100644 --- a/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs +++ b/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs @@ -2,6 +2,7 @@ namespace BizHawk.Client.Common.cheats { + // TODO: validate string and throw public class GbGameSharkDecoder { private readonly string _code; diff --git a/BizHawk.Client.Common/cheats/GenesisActionReplayDecoder.cs b/BizHawk.Client.Common/cheats/GenesisActionReplayDecoder.cs new file mode 100644 index 0000000000..e24d58f674 --- /dev/null +++ b/BizHawk.Client.Common/cheats/GenesisActionReplayDecoder.cs @@ -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"); + } + } + } +} diff --git a/BizHawk.Client.EmuHawk/tools/GameShark.cs b/BizHawk.Client.EmuHawk/tools/GameShark.cs index 027b0e02c4..00caf449a7 100644 --- a/BizHawk.Client.EmuHawk/tools/GameShark.cs +++ b/BizHawk.Client.EmuHawk/tools/GameShark.cs @@ -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) {