From 57c19d9db448025bcef80dca13d3d55417aa2498 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 29 Feb 2020 08:58:50 -0600 Subject: [PATCH] move Gb GameShark decoding to its own class --- .../cheats/GbGameSharkDecoder.cs | 37 +++++++++++++++++++ BizHawk.Client.EmuHawk/tools/GameShark.cs | 32 ++-------------- 2 files changed, 40 insertions(+), 29 deletions(-) create mode 100644 BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs diff --git a/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs b/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs new file mode 100644 index 0000000000..e9d05dc412 --- /dev/null +++ b/BizHawk.Client.Common/cheats/GbGameSharkDecoder.cs @@ -0,0 +1,37 @@ +using System.Globalization; + +namespace BizHawk.Client.Common.cheats +{ + public 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() + { + string code = _code.Remove(0, 2); + + var valueStr = code.Remove(2, 4); + code = code.Remove(0, 2); + + var addrStr = code.Remove(0, 2); + addrStr = addrStr + code.Remove(2, 2); + + Value = int.Parse(valueStr, NumberStyles.HexNumber); + Address = int.Parse(addrStr, NumberStyles.HexNumber); + } + } +} diff --git a/BizHawk.Client.EmuHawk/tools/GameShark.cs b/BizHawk.Client.EmuHawk/tools/GameShark.cs index ce3ddc6a5b..b512815858 100644 --- a/BizHawk.Client.EmuHawk/tools/GameShark.cs +++ b/BizHawk.Client.EmuHawk/tools/GameShark.cs @@ -118,8 +118,6 @@ namespace BizHawk.Client.EmuHawk private void GameBoy() { - string ramCompare = null; - // Game Genie if (_singleCheat.LastIndexOf("-") == 7 && _singleCheat.IndexOf("-") == 3) { @@ -155,33 +153,9 @@ namespace BizHawk.Client.EmuHawk return; } - // Sample Input for GB/GBC: - // 010FF6C1 - // Becomes: - // Address C1F6 - // Value 0F - _parseString = _singleCheat.Remove(0, 2); - - // Now we need to break it down a little more. - _ramValue = _parseString.Remove(2, 4); - _parseString = _parseString.Remove(0, 2); - - // The issue is Endian... Time to get ultra clever. And Regret it. - // First Half - _ramAddress = _parseString.Remove(0, 2); - _ramAddress = _ramAddress + _parseString.Remove(2, 2); - } - - try - { - var watch = Watch.GenerateWatch(MemoryDomains["System Bus"], long.Parse(_ramAddress, NumberStyles.HexNumber), WatchSize.Word, Common.DisplayType.Hex, false, txtDescription.Text); - Global.CheatList.Add(ramCompare == null - ? new Cheat(watch, int.Parse(_ramValue, NumberStyles.HexNumber)) - : new Cheat(watch, int.Parse(_ramValue, NumberStyles.HexNumber), int.Parse(ramCompare, NumberStyles.HexNumber))); - } - catch (Exception ex) - { - MessageBox.Show($"An Error occured: {ex.GetType()}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + var decoder = new GbGameSharkDecoder(_singleCheat); + 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)); } }