From 6ecab3d995f0b59a9b890dfde6fbaaf16e7fa0c2 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 29 Feb 2020 14:11:06 -0600 Subject: [PATCH] convert more decoders --- .../cheats/GbGameSharkDecoder.cs | 44 +++++++++++-------- BizHawk.Client.EmuHawk/tools/GameShark.cs | 24 +++++----- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs b/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs index 73fd993310..5d37ee2d65 100644 --- a/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs +++ b/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs @@ -1,29 +1,36 @@ -using System.Globalization; +using System; +using System.Globalization; namespace BizHawk.Client.Common.cheats { - // TODO: validate string and throw - public class GbGameSharkDecoder + public static class GbGameSharkDecoder { - private readonly string _code; - - public GbGameSharkDecoder(string code) - { - _code = code; - Decode(); - } - - public int Address { get; private set; } - public int Value { get; private set; } - // Sample Input for GB/GBC: // 010FF6C1 // Becomes: // Address C1F6 // Value 0F - public void Decode() + public static IDecodeResult Decode(string code) { - string code = _code.Remove(0, 2); + if (code == null) + { + throw new ArgumentNullException(nameof(code)); + } + + if (code.Length != 8 || code.Contains("-")) + { + return new InvalidCheatCode("GameShark codes must be 8 characters with no dashes."); + } + + var test = code.Substring(0, 2); + if (test != "00" && test != "01") + { + return new InvalidCheatCode("All GameShark Codes for GameBoy need to start with 00 or 01"); + } + + var result = new DecodeResult { Size = WatchSize.Byte }; + + code = code.Remove(0, 2); var valueStr = code.Remove(2, 4); code = code.Remove(0, 2); @@ -31,8 +38,9 @@ namespace BizHawk.Client.Common.cheats var addrStr = code.Remove(0, 2); addrStr = addrStr + code.Remove(2, 2); - Value = int.Parse(valueStr, NumberStyles.HexNumber); - Address = int.Parse(addrStr, NumberStyles.HexNumber); + result.Value = int.Parse(valueStr, NumberStyles.HexNumber); + result.Address = int.Parse(addrStr, NumberStyles.HexNumber); + return result; } } } diff --git a/BizHawk.Client.EmuHawk/tools/GameShark.cs b/BizHawk.Client.EmuHawk/tools/GameShark.cs index 0b94dc43c5..187ffdc25c 100644 --- a/BizHawk.Client.EmuHawk/tools/GameShark.cs +++ b/BizHawk.Client.EmuHawk/tools/GameShark.cs @@ -121,23 +121,21 @@ namespace BizHawk.Client.EmuHawk } // Game Shark codes - if (code.Length == 8 && code.Contains("-") == false) + if (code.Length == 8 && !code.Contains("-")) { - var test = code.Remove(2, 6); - switch (test) + var result = GbGameSharkDecoder.Decode(code); + if (result.IsValid) { - case "00": - case "01": - break; - default: - MessageBox.Show("All GameShark Codes for GameBoy need to start with 00 or 01", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; + var description = Description(code); + Global.CheatList.Add(result.ToCheat(MemoryDomains.SystemBus, description)); + } + else + { + InputError(result.Error); } - - var decoder = new GbGameSharkDecoder(code); - var watch = Watch.GenerateWatch(MemoryDomains["System Bus"], decoder.Address, WatchSize.Word, Common.DisplayType.Hex, false, txtDescription.Text); - Global.CheatList.Add(new Cheat(watch, decoder.Value)); } + + InputError($"Unknown code type: {code}"); } private void GBA(string cheat)