Use type checks for DecodeResult/InvalidCheatCode

This commit is contained in:
YoshiRulz 2021-01-06 17:25:05 +10:00
parent 966b8a974a
commit ab3846906f
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
3 changed files with 13 additions and 18 deletions

View File

@ -7,11 +7,6 @@ namespace BizHawk.Client.Common.cheats
/// </summary>
public interface IDecodeResult
{
int Address { get; }
int Value { get; }
int? Compare { get; }
WatchSize Size { get; }
bool IsValid { get; }
string Error { get; }
}
@ -21,7 +16,6 @@ namespace BizHawk.Client.Common.cheats
public int Value { get; internal set; }
public int? Compare { get; internal set; }
public WatchSize Size { get; internal set; }
public bool IsValid => true;
public string Error => "";
}
@ -32,17 +26,18 @@ namespace BizHawk.Client.Common.cheats
Error = error;
}
public int Address => int.MaxValue;
public int Value => int.MaxValue;
public int? Compare => null;
public WatchSize Size => WatchSize.Separator;
public bool IsValid => false;
public string Error { get; }
}
public static class DecodeResultExtensions
{
public static Cheat ToCheat(this IDecodeResult result, MemoryDomain domain, string description)
public static bool IsValid(this IDecodeResult result, out DecodeResult valid)
{
valid = result as DecodeResult;
return result is DecodeResult;
}
public static Cheat ToCheat(this DecodeResult result, MemoryDomain domain, string description)
{
var watch = Watch.GenerateWatch(
domain,

View File

@ -334,10 +334,10 @@ namespace BizHawk.Client.Common
var decoder = new GameSharkDecoder(MainForm.Emulator.AsMemoryDomains(), MainForm.Emulator.SystemId);
var result = decoder.Decode(code);
if (result.IsValid)
if (result.IsValid(out var valid))
{
var domain = decoder.CheatDomain();
MainForm.CheatList.Add(result.ToCheat(domain, code));
MainForm.CheatList.Add(valid.ToCheat(domain, code));
}
else
{
@ -363,10 +363,10 @@ namespace BizHawk.Client.Common
var decoder = new GameSharkDecoder(MainForm.Emulator.AsMemoryDomains(), MainForm.Emulator.SystemId);
var result = decoder.Decode(code);
if (result.IsValid)
if (result.IsValid(out var valid))
{
MainForm.CheatList.RemoveRange(
MainForm.CheatList.Where(c => c.Address == result.Address));
MainForm.CheatList.Where(c => c.Address == valid.Address));
}
else
{

View File

@ -41,12 +41,12 @@ namespace BizHawk.Client.EmuHawk
var result = decoder.Decode(code);
var domain = decoder.CheatDomain();
if (result.IsValid)
if (result.IsValid(out var valid))
{
var description = !string.IsNullOrWhiteSpace(txtDescription.Text)
? txtDescription.Text
: code;
MainForm.CheatList.Add(result.ToCheat(domain, description));
MainForm.CheatList.Add(valid.ToCheat(domain, description));
}
else
{