From af513a03ced28baca1bcf2683061566acd50ab67 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 29 Feb 2020 14:14:55 -0600 Subject: [PATCH] convert more decoders --- .../cheats/SmsActionReplayDecoder.cs | 38 +++++++++---------- BizHawk.Client.EmuHawk/tools/GameShark.cs | 27 ++++++------- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/BizHawk.Client.Common/cheats/SmsActionReplayDecoder.cs b/BizHawk.Client.Common/cheats/SmsActionReplayDecoder.cs index 7cf658a832..8f6b83529f 100644 --- a/BizHawk.Client.Common/cheats/SmsActionReplayDecoder.cs +++ b/BizHawk.Client.Common/cheats/SmsActionReplayDecoder.cs @@ -3,32 +3,28 @@ using System.Globalization; namespace BizHawk.Client.Common.cheats { - // TODO: validate string and throw - public class SmsActionReplayDecoder + public static class SmsActionReplayDecoder { - private readonly string _code; - - public SmsActionReplayDecoder(string code) + public static IDecodeResult Decode(string code) { - _code = code; - Decode(); - } - - public int Address { get; private set; } - public int Value { get; private set; } - - public void Decode() - { - if (_code.IndexOf("-") != 3 && _code.Length != 9) + if (code == null) { - throw new InvalidOperationException("Invalid Action Replay Code"); + throw new ArgumentNullException(nameof(code)); } - var parseString = _code.Remove(0, 2); - var ramAddress = parseString.Remove(4, 2).Replace("-", ""); - var ramValue = parseString.Remove(0, 5); - Address = int.Parse(ramAddress, NumberStyles.HexNumber); - Value = int.Parse(ramValue, NumberStyles.HexNumber); + if (code.IndexOf("-") != 3 && code.Length != 9) + { + return new InvalidCheatCode("Action Replay Codes must be 9 characters with a dash after the third character"); + } + + var result = new DecodeResult { Size = WatchSize.Byte }; + + var s = code.Remove(0, 2); + var ramAddress = s.Remove(4, 2).Replace("-", ""); + var ramValue = s.Remove(0, 5); + result.Address = int.Parse(ramAddress, NumberStyles.HexNumber); + result.Value = int.Parse(ramValue, NumberStyles.HexNumber); + return result; } } } diff --git a/BizHawk.Client.EmuHawk/tools/GameShark.cs b/BizHawk.Client.EmuHawk/tools/GameShark.cs index 187ffdc25c..479df98d67 100644 --- a/BizHawk.Client.EmuHawk/tools/GameShark.cs +++ b/BizHawk.Client.EmuHawk/tools/GameShark.cs @@ -313,7 +313,7 @@ namespace BizHawk.Client.EmuHawk } } - // This also handles Game Gear due to shared hardware. Go figure. + // Note: this also handles Game Gear due to shared hardware private void Sms(string code) { // Game Genie @@ -334,22 +334,19 @@ namespace BizHawk.Client.EmuHawk // Action Replay else if (code.IndexOf("-") == 3 && code.Length == 9) { - var decoder = new SmsActionReplayDecoder(code); - var watch = Watch.GenerateWatch(MemoryDomains["Main RAM"], decoder.Address, WatchSize.Byte, Common.DisplayType.Hex, false, txtDescription.Text); - Global.CheatList.Add(new Cheat(watch, decoder.Value)); + var result = SmsActionReplayDecoder.Decode(code); + if (result.IsValid) + { + var description = Description(code); + Global.CheatList.Add(result.ToCheat(MemoryDomains.SystemBus, description)); + } + else + { + InputError(result.Error); + } } - // It's an Action Replay - else if (code.Length != 9 && code.LastIndexOf("-") != 7) - { - InputError("All Master System Action Replay Codes need to be nine characters in length."); - } - - // Game Genie - else if (code.LastIndexOf("-") != 7 && code.IndexOf("-") != 3) - { - InputError("All Master System Game Genie Codes need to have a dash after the third character and seventh character."); - } + InputError($"Unknown code type: {code}"); } private void Snes(string cheat)