Move saturn gameshark parsing to its own class

This commit is contained in:
adelikat 2020-02-29 11:46:45 -06:00
parent 576c11e079
commit 7771b968c9
3 changed files with 61 additions and 28 deletions

View File

@ -16,6 +16,8 @@ namespace BizHawk.Client.Common.cheats
public int Address { get; private set; }
public int Value { get; private set; }
// TODO: WatchSize
public int ByteSize { get; private set; } = 1;
// 30XXXXXX 00YY

View File

@ -0,0 +1,55 @@
using System;
using System.Globalization;
namespace BizHawk.Client.Common.cheats
{
public class SaturnGameSharkDecoder
{
private readonly string _code;
public SaturnGameSharkDecoder(string code)
{
_code = code;
Decode();
}
public int Address { get; private set; }
public int Value { get; private set; }
public WatchSize Size { get; private set; } = WatchSize.Word;
// Sample Input for Saturn:
// 160949FC 0090
// Address: 0949FC
// Value: 90
// Note, 3XXXXXXX are Big Endian
// Remove first two octets
public void Decode()
{
if (_code == null)
{
throw new ArgumentNullException(nameof(_code));
}
if (_code.IndexOf(" ") != 8)
{
throw new InvalidOperationException("All Saturn GameShark Codes need to contain a space after the eighth character.");
}
if (_code.Length != 13)
{
throw new InvalidOperationException("All Saturn GameShark Cheats need to be 13 characters in length.");
}
// Only the first character really matters? 16 or 36?
var test = _code.Remove(2, 11).Remove(1, 1);
if (test == "3")
{
Size = WatchSize.Byte;
}
var s = _code.Remove(0, 2);
Address = int.Parse(s.Remove(6, 5), NumberStyles.HexNumber);
Value = int.Parse(s.Remove(0, 7));
}
}
}

View File

@ -2461,10 +2461,7 @@ namespace BizHawk.Client.EmuHawk
switch (test)
{
case "1":
_byteSize = 16;
break;
case "3":
_byteSize = 8;
break;
// 0 writes once.
case "0":
@ -2480,34 +2477,13 @@ namespace BizHawk.Client.EmuHawk
return;
}
// Sample Input for Saturn:
// 160949FC 0090
// Address: 0949FC
// Value: 90
// Note, 3XXXXXXX are Big Endian
// Remove first two octets
var parseString = cheat.Remove(0, 2);
// Get RAM Address
_ramAddress = parseString.Remove(6, 5);
// Get RAM Value
_ramValue = parseString.Remove(0, 7);
try
{
// A Watch needs to be generated so we can make a cheat out of that. This is due to how the Cheat engine works.
// System Bus Domain, The Address to Watch, Byte size (Word), Hex Display, Description. Big Endian.
var decoder = new SaturnGameSharkDecoder(cheat);
// My Concern is that Work RAM High may be incorrect?
if (_byteSize == 8)
{
var watch = Watch.GenerateWatch(MemoryDomains["Work Ram High"], long.Parse(_ramAddress, NumberStyles.HexNumber), WatchSize.Byte, Common.DisplayType.Hex, true, txtDescription.Text);
Global.CheatList.Add(new Cheat(watch, int.Parse(_ramValue, NumberStyles.HexNumber)));
}
else if (_byteSize == 16)
{
var watch = Watch.GenerateWatch(MemoryDomains["Work Ram High"], 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 watch = Watch.GenerateWatch(MemoryDomains["Work Ram High"], decoder.Address, decoder.Size, Common.DisplayType.Hex, true, txtDescription.Text);
Global.CheatList.Add(new Cheat(watch, decoder.Value));
}
catch (Exception ex)
{