convert more decoders

This commit is contained in:
adelikat 2020-02-29 14:14:55 -06:00
parent 6ecab3d995
commit af513a03ce
2 changed files with 29 additions and 36 deletions

View File

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

View File

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