struct CheckResult

tiny object, eliminate allocation for them since they're 10-16 bytes at most.
This commit is contained in:
Kurt 2023-04-23 16:04:04 -07:00
parent b417964193
commit 907ed0b32f
11 changed files with 24 additions and 23 deletions

View file

@ -81,7 +81,7 @@ public sealed class BulkAnalysis
public void AddLine(SlotCache first, SlotCache second, string msg, CheckIdentifier i, Severity s = Severity.Invalid)
{
var c = $"{msg}{Environment.NewLine}{GetSummary(first)}{Environment.NewLine}{GetSummary(second)}{Environment.NewLine}";
var chk = new CheckResult(s, c, i);
var chk = new CheckResult(s, i, c);
Parse.Add(chk);
}
@ -91,7 +91,7 @@ public sealed class BulkAnalysis
public void AddLine(SlotCache first, string msg, CheckIdentifier i, Severity s = Severity.Invalid)
{
var c = $"{msg}{Environment.NewLine}{GetSummary(first)}{Environment.NewLine}";
var chk = new CheckResult(s, c, i);
var chk = new CheckResult(s, i, c);
Parse.Add(chk);
}

View file

@ -62,14 +62,14 @@ public static class EncounterFinder
continue;
// We ran out of possible encounters without finding a suitable match; add a message indicating that the encounter is not a complete match.
info.Parse.Add(new CheckResult(Severity.Invalid, LEncInvalid, CheckIdentifier.Encounter));
info.Parse.Add(new CheckResult(Severity.Invalid, CheckIdentifier.Encounter, LEncInvalid));
break;
}
if (info is { FrameMatches: false, EncounterMatch: EncounterSlot }) // if false, all valid RNG frame matches have already been consumed
info.Parse.Add(new CheckResult(ParseSettings.RNGFrameNotFound, LEncConditionBadRNGFrame, CheckIdentifier.PID)); // todo for further confirmation
info.Parse.Add(new CheckResult(ParseSettings.RNGFrameNotFound, CheckIdentifier.PID, LEncConditionBadRNGFrame)); // todo for further confirmation
if (!info.PIDIVMatches) // if false, all valid PIDIV matches have already been consumed
info.Parse.Add(new CheckResult(Severity.Invalid, LPIDTypeMismatch, CheckIdentifier.PID));
info.Parse.Add(new CheckResult(Severity.Invalid, CheckIdentifier.PID, LPIDTypeMismatch));
}
/// <summary>
@ -155,7 +155,7 @@ public static class EncounterFinder
info.EncounterMatch = new EncounterInvalid(pk);
string hint = GetHintWhyNotFound(pk, info.EncounterMatch.Generation);
info.Parse.Add(new CheckResult(Severity.Invalid, hint, CheckIdentifier.Encounter));
info.Parse.Add(new CheckResult(Severity.Invalid, CheckIdentifier.Encounter, hint));
LearnVerifierRelearn.Verify(info.Relearn, info.EncounterOriginal, pk);
LearnVerifier.Verify(info.Moves, pk, info.EncounterMatch, info.EvoChainsAllGens);
}

View file

@ -121,8 +121,8 @@ public static class EncounterVerifier
return GetInvalid(LEggLocationInvalid);
}
private static CheckResult GetInvalid(string message, CheckIdentifier ident = CheckIdentifier.Encounter) => new(Severity.Invalid, message, ident);
private static CheckResult GetValid(string message) => new(Severity.Valid, message, CheckIdentifier.Encounter);
private static CheckResult GetInvalid(string message, CheckIdentifier ident = CheckIdentifier.Encounter) => new(Severity.Invalid, ident, message);
private static CheckResult GetValid(string message) => new(Severity.Valid, CheckIdentifier.Encounter, message);
private static CheckResult VerifyEncounterEgg3Transfer(PKM pk)
{

View file

@ -17,11 +17,11 @@ public static class EvolutionVerifier
{
// Check if basic evolution methods are satisfiable with this encounter.
if (!IsValidEvolution(pk, info))
return new CheckResult(Severity.Invalid, LEvoInvalid, CheckIdentifier.Evolution);
return new CheckResult(Severity.Invalid, CheckIdentifier.Evolution, LEvoInvalid);
// Check if complex evolution methods are satisfiable with this encounter.
if (!IsValidEvolutionWithMove(pk, info))
return new CheckResult(Severity.Invalid, string.Format(LMoveEvoFCombination_0, ParseSettings.SpeciesStrings[pk.Species]), CheckIdentifier.Evolution);
return new CheckResult(Severity.Invalid, CheckIdentifier.Evolution, string.Format(LMoveEvoFCombination_0, ParseSettings.SpeciesStrings[pk.Species]));
return VALID;
}

View file

@ -42,17 +42,17 @@ public static class MysteryGiftVerifier
var ver = (int)value >> 16;
if (ver != 0 && !CanVersionReceiveGift(g.Generation, ver, pk.Version))
return new CheckResult(Severity.Invalid, LEncGiftVersionNotDistributed, CheckIdentifier.GameOrigin);
return new CheckResult(Severity.Invalid, CheckIdentifier.GameOrigin, LEncGiftVersionNotDistributed);
var lang = value & MysteryGiftRestriction.LangRestrict;
if (lang != 0 && !lang.HasFlag((MysteryGiftRestriction) (1 << pk.Language)))
return new CheckResult(Severity.Invalid, string.Format(LOTLanguage, lang.GetSuggestedLanguage(), pk.Language), CheckIdentifier.GameOrigin);
return new CheckResult(Severity.Invalid, CheckIdentifier.GameOrigin, string.Format(LOTLanguage, lang.GetSuggestedLanguage(), pk.Language));
if (pk is IRegionOrigin tr)
{
var region = value & MysteryGiftRestriction.RegionRestrict;
if (region != 0 && !region.HasFlag((MysteryGiftRestriction)((int)MysteryGiftRestriction.RegionBase << tr.ConsoleRegion)))
return new CheckResult(Severity.Invalid, LGeoHardwareRange, CheckIdentifier.GameOrigin);
return new CheckResult(Severity.Invalid, CheckIdentifier.GameOrigin, LGeoHardwareRange);
}
return new CheckResult(CheckIdentifier.GameOrigin);

View file

@ -264,7 +264,7 @@ public sealed class LegalityAnalysis
/// <param name="s">Check severity</param>
/// <param name="c">Check comment</param>
/// <param name="i">Check type</param>
internal void AddLine(Severity s, string c, CheckIdentifier i) => AddLine(new CheckResult(s, c, i));
internal void AddLine(Severity s, string c, CheckIdentifier i) => AddLine(new CheckResult(s, i, c));
/// <summary>
/// Adds a new Check parse value.

View file

@ -5,12 +5,12 @@ namespace PKHeX.Core;
/// </summary>
[System.Diagnostics.DebuggerDisplay($"{{{nameof(Identifier)}}}: {{{nameof(Comment)}}}")]
// ReSharper disable once NotAccessedPositionalProperty.Global
public sealed record CheckResult(Severity Judgement, string Comment, CheckIdentifier Identifier)
public readonly record struct CheckResult(Severity Judgement, CheckIdentifier Identifier, string Comment)
{
public bool Valid => Judgement != Severity.Invalid;
public string Rating => Judgement.Description();
internal CheckResult(CheckIdentifier i) : this(Severity.Valid, LegalityCheckStrings.L_AValid, i) { }
internal CheckResult(CheckIdentifier i) : this(Severity.Valid, i, LegalityCheckStrings.L_AValid) { }
public string Format(string format) => string.Format(format, Rating, Comment);
}

View file

@ -34,16 +34,16 @@ public sealed class ItemVerifier : Verifier
{
var status = EReaderBerrySettings.GetStatus();
var chk = GetEReaderCheckResult(status);
if (chk != null)
if (chk != default)
data.AddLine(chk);
}
private CheckResult? GetEReaderCheckResult(EReaderBerryMatch status) => status switch
private CheckResult GetEReaderCheckResult(EReaderBerryMatch status) => status switch
{
EReaderBerryMatch.NoMatch => GetInvalid(LEReaderInvalid),
EReaderBerryMatch.NoData => GetInvalid(LItemUnreleased),
EReaderBerryMatch.InvalidUSA => GetInvalid(LEReaderAmerica),
EReaderBerryMatch.InvalidJPN => GetInvalid(LEReaderJapan),
_ => null,
_ => default,
};
}

View file

@ -245,7 +245,7 @@ public sealed class MiscVerifier : Verifier
var time = t.Met_TimeOfDay;
bool valid = data.EncounterOriginal is EncounterTrade2 ? time == 0 : time is 1 or 2 or 3;
if (!valid)
data.AddLine(new CheckResult(Severity.Invalid, LMetDetailTimeOfDay, Encounter));
data.AddLine(new CheckResult(Severity.Invalid, Encounter, LMetDetailTimeOfDay));
}
return;
}

View file

@ -1,4 +1,4 @@
namespace PKHeX.Core;
namespace PKHeX.Core;
/// <summary>
/// Verification that provides new <see cref="CheckResult"/> values for a <see cref="LegalityAnalysis"/>.
@ -18,9 +18,9 @@ public abstract class Verifier
protected CheckResult GetInvalid(string msg) => Get(msg, Severity.Invalid);
protected CheckResult GetValid(string msg) => Get(msg, Severity.Valid);
protected CheckResult Get(string msg, Severity s) => new(s, msg, Identifier);
protected CheckResult Get(string msg, Severity s) => new(s, Identifier, msg);
protected static CheckResult GetInvalid(string msg, CheckIdentifier c) => Get(msg, Severity.Invalid, c);
protected static CheckResult GetValid(string msg, CheckIdentifier c) => Get(msg, Severity.Valid, c);
protected static CheckResult Get(string msg, Severity s, CheckIdentifier c) => new(s, msg, c);
protected static CheckResult Get(string msg, Severity s, CheckIdentifier c) => new(s, c, msg);
}

View file

@ -18,6 +18,7 @@ public class MarshalTests
[InlineData( 8, typeof(NPCLock))]
[InlineData( 8, typeof(IndividualValueSet))]
[InlineData(16, typeof(DreamWorldEntry))]
[InlineData(16, typeof(CheckResult))]
[InlineData(24, typeof(GenerateParam9))]
public void MarshalSizeLessThanEqual(int expect, Type t) => Marshal.SizeOf(t).Should().BeLessOrEqualTo(expect);
}