convert more decoders

This commit is contained in:
adelikat 2020-02-29 14:11:06 -06:00
parent ff5ce6ad29
commit 6ecab3d995
2 changed files with 37 additions and 31 deletions

View File

@ -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;
}
}
}

View File

@ -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)