convert more decoders

This commit is contained in:
adelikat 2020-02-29 14:04:42 -06:00
parent 8f00024361
commit ff5ce6ad29
3 changed files with 55 additions and 54 deletions

View File

@ -6,11 +6,9 @@ namespace BizHawk.Client.Common.cheats
/// <summary>
/// Decodes Gameboy and Game Gear Game Genie codes
/// </summary>
public class GbGgGameGenieDecoder
public static class GbGgGameGenieDecoder
{
private readonly string _code;
private readonly Dictionary<char, int> _gbGgGameGenieTable = new Dictionary<char, int>
private static readonly Dictionary<char, int> _gbGgGameGenieTable = new Dictionary<char, int>
{
['0'] = 0,
['1'] = 1,
@ -30,65 +28,67 @@ namespace BizHawk.Client.Common.cheats
['F'] = 15
};
public GbGgGameGenieDecoder(string code)
public static IDecodeResult Decode(string _code)
{
_code = code;
Decode();
}
if (_code == null)
{
throw new ArgumentNullException(nameof(_code));
}
public int Address { get; private set; }
public int Value { get; private set; }
public int? Compare { get; private set; }
if (_code.LastIndexOf("-") != 7 || _code.IndexOf("-") != 3)
{
return new InvalidCheatCode("All Master System Game Genie Codes need to have a dash after the third character and seventh character.");
}
public void Decode()
{
// No cypher on value
// Char # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
// Bit # |3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|3|2|1|0|
// maps to| Value |A|B|C|D|E|F|G|H|I|J|K|L|XOR 0xF|a|b|c|c|NotUsed|e|f|g|h|
// proper | Value |XOR 0xF|A|B|C|D|E|F|G|H|I|J|K|L|g|h|a|b|Nothing|c|d|e|f|
var result = new DecodeResult { Size = WatchSize.Byte };
int x;
// Getting Value
if (_code.Length > 0)
{
_gbGgGameGenieTable.TryGetValue(_code[0], out x);
Value = x << 4;
result.Value = x << 4;
}
if (_code.Length > 1)
{
_gbGgGameGenieTable.TryGetValue(_code[1], out x);
Value |= x;
result.Value |= x;
}
// Address
if (_code.Length > 2)
{
_gbGgGameGenieTable.TryGetValue(_code[2], out x);
Value = x << 8;
result.Value = x << 8;
}
else
{
Value = -1;
result.Value = -1;
}
if (_code.Length > 3)
{
_gbGgGameGenieTable.TryGetValue(_code[3], out x);
Address |= x << 4;
result.Address |= x << 4;
}
if (_code.Length > 4)
{
_gbGgGameGenieTable.TryGetValue(_code[4], out x);
Address |= x;
result.Address |= x;
}
if (_code.Length > 5)
{
_gbGgGameGenieTable.TryGetValue(_code[5], out x);
Address |= (x ^ 0xF) << 12;
result.Address |= (x ^ 0xF) << 12;
}
// compare need to be full
@ -101,8 +101,10 @@ namespace BizHawk.Client.Common.cheats
_gbGgGameGenieTable.TryGetValue(_code[8], out x);
comp |= (x & 0xC) >> 2;
comp |= (x & 0x3) << 6;
Compare = comp ^ 0xBA;
result.Compare = comp ^ 0xBA;
}
return result;
}
}
}

View File

@ -34,7 +34,7 @@ namespace BizHawk.Client.Common.cheats
if (code.Length != 6 && code.Length != 8)
{
return new InvalidResult("Game Genie codes need to be six or eight characters in length.");
return new InvalidCheatCode("Game Genie codes need to be six or eight characters in length.");
}
var result = new DecodeResult { Size = WatchSize.Byte };

View File

@ -103,33 +103,27 @@ namespace BizHawk.Client.EmuHawk
txtDescription.Clear();
}
private void GameBoy(string cheat)
private void GameBoy(string code)
{
// Game Genie
if (cheat.LastIndexOf("-") == 7 && cheat.IndexOf("-") == 3)
if (code.LastIndexOf("-") == 7 && code.IndexOf("-") == 3)
{
var decoder = new GbGgGameGenieDecoder(cheat);
var watch = Watch.GenerateWatch(MemoryDomains["System Bus"], decoder.Address, WatchSize.Word, Common.DisplayType.Hex, false, txtDescription.Text);
Global.CheatList.Add(decoder.Compare.HasValue
? new Cheat(watch, decoder.Value, decoder.Compare)
: new Cheat(watch, decoder.Value));
}
else if (cheat.Contains("-") && cheat.LastIndexOf("-") != 7 && cheat.IndexOf("-") != 3)
{
MessageBox.Show("All GameBoy Game Genie Codes need to have a dash after the third character and seventh character.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
var result = GbGgGameGenieDecoder.Decode(code);
if (result.IsValid)
{
var description = Description(code);
Global.CheatList.Add(result.ToCheat(MemoryDomains.SystemBus, description));
}
else
{
InputError(result.Error);
}
}
// Game Shark codes
if (cheat.Length != 8 && cheat.Contains("-") == false)
if (code.Length == 8 && code.Contains("-") == false)
{
MessageBox.Show("All GameShark Codes need to be Eight characters in Length", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (cheat.Length == 8 && cheat.Contains("-") == false)
{
var test = cheat.Remove(2, 6);
var test = code.Remove(2, 6);
switch (test)
{
case "00":
@ -140,7 +134,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
var decoder = new GbGameSharkDecoder(cheat);
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));
}
@ -322,34 +316,39 @@ namespace BizHawk.Client.EmuHawk
}
// This also handles Game Gear due to shared hardware. Go figure.
private void Sms(string cheat)
private void Sms(string code)
{
// Game Genie
if (cheat.LastIndexOf("-") == 7 && cheat.IndexOf("-") == 3)
if (code.LastIndexOf("-") == 7 && code.IndexOf("-") == 3)
{
var decoder = new GbGgGameGenieDecoder(cheat);
var watch = Watch.GenerateWatch(MemoryDomains["System Bus"], decoder.Address, WatchSize.Word, Common.DisplayType.Hex, false, txtDescription.Text);
Global.CheatList.Add(decoder.Compare.HasValue
? new Cheat(watch, decoder.Value, decoder.Compare)
: new Cheat(watch, decoder.Value));
var result = GbGgGameGenieDecoder.Decode(code);
if (result.IsValid)
{
var description = Description(code);
Global.CheatList.Add(result.ToCheat(MemoryDomains.SystemBus, description));
}
else
{
InputError(result.Error);
}
}
// Action Replay
else if (cheat.IndexOf("-") == 3 && cheat.Length == 9)
else if (code.IndexOf("-") == 3 && code.Length == 9)
{
var decoder = new SmsActionReplayDecoder(cheat);
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));
}
// It's an Action Replay
else if (cheat.Length != 9 && cheat.LastIndexOf("-") != 7)
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 (cheat.LastIndexOf("-") != 7 && cheat.IndexOf("-") != 3)
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.");
}