Skip to content

Commit

Permalink
ArgumentOutOfRangeException
Browse files Browse the repository at this point in the history
Use the new NET8 API
  • Loading branch information
kwsch committed Dec 9, 2023
1 parent edf28f7 commit 1fe2b4f
Show file tree
Hide file tree
Showing 82 changed files with 281 additions and 299 deletions.
3 changes: 1 addition & 2 deletions PKHeX.Core/Editing/Bulk/StringInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public static bool IsRandomRange(ReadOnlySpan<char> str)
public void SetRandomRange(ReadOnlySpan<char> str)
{
var index = str.IndexOf(SplitRange);
if (index <= 0)
throw new ArgumentException($"Invalid Random Range: {str.ToString()}", nameof(str));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(index);

var min = str[..index];
var max = str[(index + 1)..];
Expand Down
68 changes: 68 additions & 0 deletions PKHeX.Core/Editing/HiddenPower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ public static int GetType(ReadOnlySpan<int> IVs)
return SixBitType[hp];
}

/// <summary>
/// Gets the current Hidden Power Type of the input IVs for Generations 3+
/// </summary>
/// <param name="u32">32-bit value of the IVs</param>
/// <returns>Hidden Power Type of the IVs</returns>
public static int GetType(uint u32)
{
uint hp = 0;
for (int i = 0; i < 6; i++)
{
hp |= (u32 & 1) << i;
u32 >>= 5;
}
return SixBitType[(int)hp];
}

/// <summary>
/// Gets the current Hidden Power Type of the input IVs for Generations 3+
/// </summary>
/// <param name="u32">32-bit value of the IVs</param>
/// <remarks>IVs are stored in reverse order in the 32-bit value</remarks>
/// <returns>Hidden Power Type of the IVs</returns>
public static int GetTypeBigEndian(uint u32)
{
uint hp = 0;
for (int i = 0; i < 6; i++)
{
hp |= (u32 & 1) << (5 - i);
u32 >>= 5;
}
return SixBitType[(int)hp];
}

private static ReadOnlySpan<byte> SixBitType =>
[
// (low-bit mash) * 15 / 63
Expand Down Expand Up @@ -193,6 +226,41 @@ private static void ForceLowBits(Span<int> ivs, byte bits)
ivs[i] = (ivs[i] & 0b11110) | ((bits >> i) & 1);
}

/// <inheritdoc cref="SetIVs(int,Span{int},EntityContext)"/>
public static uint SetIVs(int type, uint ivs)
{
var bits = DefaultLowBits[type];
for (int i = 0; i < 6; i++)
{
var bit = (bits >> i) & 1;
var bitIndex = i * 5;
var mask = (1u << bitIndex);
if (bit == 0)
ivs &= ~mask;
else
ivs |= mask;
}
return ivs;
}

/// <inheritdoc cref="SetIVs(int,uint)"/>
/// <remarks>IVs are stored in reverse order in the 32-bit value</remarks>
public static uint SetIVsBigEndian(int type, uint ivs)
{
var bits = DefaultLowBits[type];
for (int i = 0; i < 6; i++)
{
var bit = (bits >> i) & 1;
var bitIndex = (5 - i) * 5;
var mask = (1u << bitIndex);
if (bit == 0)
ivs &= ~mask;
else
ivs |= mask;
}
return ivs;
}

/// <summary>
/// Hidden Power IV values (even or odd) to achieve a specified Hidden Power Type
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Save()
FR or LG or FRLG => "frlg",
C => "c",
GD or SI or GS => "gs",
_ => throw new ArgumentOutOfRangeException(nameof(GameVersion)),
_ => throw new ArgumentOutOfRangeException(nameof(ver), ver, null),
};

private static GameVersion GetVersion(TSave ver)
Expand Down
3 changes: 1 addition & 2 deletions PKHeX.Core/Editing/Saves/Slots/BoxEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public sealed class BoxEdit(SaveFile SAV)

public void LoadBox(int box)
{
if ((uint)box >= SAV.BoxCount)
throw new ArgumentOutOfRangeException(nameof(box));
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)box, (uint)SAV.BoxCount);

SAV.AddBoxData(CurrentContents, box, 0);
CurrentBox = box;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ private static GameVersion GetOtherGamePair(GameVersion version)
// 32 -> 30 (US -> SN)
// 33 -> 31 (UM -> MN)
// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
#pragma warning disable RCS1130 // Bitwise operation on enum without Flags attribute.
return version ^ (GameVersion)0b111110;
#pragma warning restore RCS1130 // Bitwise operation on enum without Flags attribute.
}

private static EncounterEgg CreateEggEncounter(ushort species, byte form, GameVersion version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private void SetPINGA(PK9 pk, EncounterCriteria criteria, PersonalInfo9SV pi)
const byte undefinedSize = 0;
var param = new GenerateParam9(Species, pi.Gender, FlawlessIVCount, rollCount,
undefinedSize, undefinedSize, ScaleType, Scale,
Ability, Shiny, IVs: IVs, Nature: Nature);
Ability, Shiny, Nature, IVs: IVs);

var init = Util.Rand.Rand64();
var success = this.TryApply32(pk, init, param, criteria);
Expand Down Expand Up @@ -326,9 +326,9 @@ public EncounterMatchRating GetMatchRating(PKM pk)
return IsMatchDeferred(pk);
}

private bool IsMatchLocationExact(PKM pk) => pk.Met_Location == Location;
private static bool IsMatchLocationExact(PKM pk) => pk.Met_Location == Location;

private bool IsMatchLocationRemapped(PKM pk)
private static bool IsMatchLocationRemapped(PKM pk)
{
var met = (ushort)pk.Met_Location;
var version = pk.Version;
Expand Down Expand Up @@ -380,7 +380,7 @@ private bool IsMatchPartial(PKM pk)
return true;

var pi = PersonalTable.SV.GetFormEntry(Species, Form);
var param = new GenerateParam9(Species, pi.Gender, FlawlessIVCount, 1, 0, 0, ScaleType, Scale, Ability, Shiny, IVs: IVs, Nature: Nature);
var param = new GenerateParam9(Species, pi.Gender, FlawlessIVCount, 1, 0, 0, ScaleType, Scale, Ability, Shiny, Nature, IVs: IVs);
if (!Encounter9RNG.IsMatch(pk, param, seed))
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ public EncounterMatchRating GetMatchRating(PKM pk)
return IsMatchDeferred(pk);
}

private bool IsMatchLocationExact(PKM pk) => pk.Met_Location == Location;
private static bool IsMatchLocationExact(PKM pk) => pk.Met_Location == Location;

private bool IsMatchLocationRemapped(PKM pk)
private static bool IsMatchLocationRemapped(PKM pk)
{
var met = (ushort)pk.Met_Location;
var version = pk.Version;
Expand Down
8 changes: 4 additions & 4 deletions PKHeX.Core/Legality/RNG/Methods/Gen4/PokewalkerRNG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static class PokewalkerRNG

/// <summary> Species slots per course. </summary>
public const int SlotsPerCourse = 6;
public const int GroupsPerCourse = 3;
public const int SlotsPerGroup = 2;

/// <summary>
/// All species for all Pokéwalker courses.
Expand Down Expand Up @@ -170,10 +172,8 @@ public static bool IsValidStrollSeed(uint seed, ushort species, PokewalkerCourse
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static ushort GetSpecies(PokewalkerCourse4 course, int group, int rare)
{
if ((uint)group > 2)
throw new ArgumentOutOfRangeException(nameof(group));
if ((uint)rare > 1)
throw new ArgumentOutOfRangeException(nameof(rare));
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>((uint)group, GroupsPerCourse);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>((uint)rare, SlotsPerGroup);
var span = GetSpecies(course);
return span[(group * 2) + rare];
}
Expand Down
1 change: 1 addition & 0 deletions PKHeX.Core/Legality/Verifiers/Ball/BallContextUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace PKHeX.Core;

#if DEBUG
// ReSharper disable once UnusedType.Global
public static class BallContextUtil
{
/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions PKHeX.Core/Legality/Verifiers/MiscVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ private static void DisallowLevelUpMove(byte level, ushort move, PK9 pk, Legalit
if (m.Info.Method != LearnMethod.LevelUp || m.Info.Argument != level)
return;
var flagIndex = pk.Permit.RecordPermitIndexes.IndexOf(move);
if (flagIndex == -1)
throw new ArgumentOutOfRangeException(nameof(move), move, "Expected a valid TM index.");
ArgumentOutOfRangeException.ThrowIfNegative(flagIndex, nameof(move)); // Always expect it to match.
if (pk.GetMoveRecordFlag(flagIndex))
return;
m = new MoveResult(LearnMethod.None);
Expand Down
38 changes: 20 additions & 18 deletions PKHeX.Core/Legality/Verifiers/PIDVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void Verify(LegalityAnalysis data)
if (enc.Species == (int)Species.Wurmple)
VerifyECPIDWurmple(data);
else if (enc.Species is (int)Species.Tandemaus or (int)Species.Dunsparce)
VerifyEC100(data);
VerifyEC100(data, enc.Species);

if (pk.PID == 0)
data.AddLine(Get(LPIDZero, Severity.Fishy));
Expand Down Expand Up @@ -96,27 +96,29 @@ private static void VerifyECPIDWurmple(LegalityAnalysis data)
}
}

private static void VerifyEC100(LegalityAnalysis data)
private static void VerifyEC100(LegalityAnalysis data, ushort encSpecies)
{
var pk = data.Entity;
var enc = data.EncounterMatch;
if (pk.Species == enc.Species)
{
uint evoVal = pk.EncryptionConstant % 100;
bool rare = evoVal == 0;
var (species, form) = enc.Species switch
{
(int)Species.Tandemaus => ((ushort)Species.Maushold, rare ? 0 : 1),
(int)Species.Dunsparce => ((ushort)Species.Dudunsparce, rare ? 1 : 0),
_ => throw new ArgumentOutOfRangeException(nameof(enc.Species), "Incorrect EC%100 species."),
};
var str = GameInfo.Strings;
var forms = FormConverter.GetFormList(species, str.Types, str.forms, GameInfo.GenderSymbolASCII, EntityContext.Gen9);
var msg = string.Format(L_XRareFormEvo_0_1, forms[form], rare);
data.AddLine(GetValid(msg, CheckIdentifier.EC));
}
if (pk.Species != encSpecies)
return; // Evolved, don't need to calculate the final evolution for the verbose report.

// Indicate the evolution for the user.
uint evoVal = pk.EncryptionConstant % 100;
bool rare = evoVal == 0;
var (species, form) = GetEvolvedSpeciesForm(encSpecies, rare);
var str = GameInfo.Strings;
var forms = FormConverter.GetFormList(species, str.Types, str.forms, GameInfo.GenderSymbolASCII, EntityContext.Gen9);
var msg = string.Format(L_XRareFormEvo_0_1, forms[form], rare);
data.AddLine(GetValid(msg, CheckIdentifier.EC));
}

private static (ushort, int) GetEvolvedSpeciesForm(ushort species, bool rare) => species switch
{
(int)Species.Tandemaus => ((ushort)Species.Maushold, rare ? 0 : 1),
(int)Species.Dunsparce => ((ushort)Species.Dudunsparce, rare ? 1 : 0),
_ => throw new ArgumentOutOfRangeException(nameof(species), species, "Incorrect EC%100 species."),
};

private static void VerifyEC(LegalityAnalysis data)
{
var pk = data.Entity;
Expand Down
13 changes: 4 additions & 9 deletions PKHeX.Core/MysteryGifts/WA8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,13 @@ public bool HasMarkEncounter8

public byte GetRibbonAtIndex(int byteIndex)
{
if ((uint)byteIndex >= RibbonBytesCount)
throw new ArgumentOutOfRangeException(nameof(byteIndex));
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>((uint)byteIndex, RibbonBytesCount);
return Data[RibbonBytesOffset + byteIndex];
}

public void SetRibbonAtIndex(int byteIndex, byte ribbonIndex)
{
if ((uint)byteIndex >= RibbonBytesCount)
throw new ArgumentOutOfRangeException(nameof(byteIndex));
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>((uint)byteIndex, RibbonBytesCount);
Data[RibbonBytesOffset + byteIndex] = ribbonIndex;
}

Expand Down Expand Up @@ -815,16 +813,13 @@ public override bool IsMatchExact(PKM pk, EvoCriteria evo)

public void SetRibbon(int index, bool value = true)
{
if ((uint)index > (uint)MarkSlump)
throw new ArgumentOutOfRangeException(nameof(index));

ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)index, (uint)MarkSlump);
if (value)
{
if (GetRibbon(index))
return;
var openIndex = Array.IndexOf(Data, RibbonByteNone, RibbonBytesOffset, RibbonBytesCount);
if (openIndex == -1) // Full?
throw new ArgumentOutOfRangeException(nameof(index));
ArgumentOutOfRangeException.ThrowIfNegative(openIndex, nameof(openIndex)); // Full?
SetRibbonAtIndex(openIndex, (byte)index);
}
else
Expand Down
13 changes: 4 additions & 9 deletions PKHeX.Core/MysteryGifts/WB8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,13 @@ public bool HasMarkEncounter8

public byte GetRibbonAtIndex(int byteIndex)
{
if ((uint)byteIndex >= RibbonBytesCount)
throw new ArgumentOutOfRangeException(nameof(byteIndex));
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>((uint)byteIndex, RibbonBytesCount);
return Data[RibbonBytesOffset + byteIndex];
}

public void SetRibbonAtIndex(int byteIndex, byte ribbonIndex)
{
if ((uint)byteIndex >= RibbonBytesCount)
throw new ArgumentOutOfRangeException(nameof(byteIndex));
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>((uint)byteIndex, RibbonBytesCount);
Data[RibbonBytesOffset + byteIndex] = ribbonIndex;
}

Expand Down Expand Up @@ -832,16 +830,13 @@ private bool IsMatchLocationRemapped(PKM pk)

public void SetRibbon(int index, bool value = true)
{
if ((uint)index > (uint)MarkSlump)
throw new ArgumentOutOfRangeException(nameof(index));

ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)index, (uint)MarkSlump);
if (value)
{
if (GetRibbon(index))
return;
var openIndex = Array.IndexOf(Data, RibbonByteNone, RibbonBytesOffset, RibbonBytesCount);
if (openIndex == -1) // Full?
throw new ArgumentOutOfRangeException(nameof(index));
ArgumentOutOfRangeException.ThrowIfNegative(openIndex, nameof(openIndex)); // Full?
SetRibbonAtIndex(openIndex, (byte)index);
}
else
Expand Down
13 changes: 4 additions & 9 deletions PKHeX.Core/MysteryGifts/WC8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,13 @@ public bool HasMarkEncounter8

public byte GetRibbonAtIndex(int byteIndex)
{
if ((uint)byteIndex >= RibbonBytesCount)
throw new ArgumentOutOfRangeException(nameof(byteIndex));
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>((uint)byteIndex, RibbonBytesCount);
return Data[RibbonBytesOffset + byteIndex];
}

public void SetRibbonAtIndex(int byteIndex, byte ribbonIndex)
{
if ((uint)byteIndex >= RibbonBytesCount)
throw new ArgumentOutOfRangeException(nameof(byteIndex));
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>((uint)byteIndex, RibbonBytesCount);
Data[RibbonBytesOffset + byteIndex] = ribbonIndex;
}

Expand Down Expand Up @@ -889,16 +887,13 @@ private bool IsHOMEShinyPossible()

public void SetRibbon(int index, bool value = true)
{
if ((uint)index > (uint)MarkSlump)
throw new ArgumentOutOfRangeException(nameof(index));

ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)index, (uint)MarkSlump);
if (value)
{
if (GetRibbon(index))
return;
var openIndex = Array.IndexOf(Data, RibbonByteNone, RibbonBytesOffset, RibbonBytesCount);
if (openIndex == -1) // Full?
throw new ArgumentOutOfRangeException(nameof(index));
ArgumentOutOfRangeException.ThrowIfNegative(openIndex, nameof(openIndex)); // Full?
SetRibbonAtIndex(openIndex, (byte)index);
}
else
Expand Down
Loading

0 comments on commit 1fe2b4f

Please sign in to comment.