From 270d9841301b3249f30c03f8c420b2d4e766f106 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 4 Apr 2020 19:30:50 -0700 Subject: [PATCH] Minor clean Remove unnecessary null checks Mark some methods with nullable type --- PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs | 2 -- PKHeX.Core/Editing/WurmpleUtil.cs | 2 +- PKHeX.Core/Game/GameStrings/GameInfo.cs | 2 +- PKHeX.Core/Game/GameStrings/GameLanguage.cs | 2 +- PKHeX.Core/Game/GameStrings/GeoLocation.cs | 8 +------- .../Encounters/Generator/EncounterMovesetGenerator.cs | 2 +- .../Encounters/Generator/EncounterSlotGenerator.cs | 2 +- .../Encounters/Generator/EncounterStaticGenerator.cs | 2 +- .../Legality/Encounters/Information/EncounterLearn.cs | 2 -- .../Encounters/Information/EncounterSuggestion.cs | 1 - .../Legality/Encounters/Verifiers/MysteryGiftVerifier.cs | 6 +++--- PKHeX.Core/PKM/Util/PokeCrypto.cs | 2 +- PKHeX.Core/Saves/SAV3.cs | 4 ++-- PKHeX.Core/Saves/SAV3Colosseum.cs | 2 +- PKHeX.Core/Saves/Substructures/Gen3/StrategyMemo.cs | 2 +- PKHeX.Core/Saves/Util/SaveDetection.cs | 6 +++--- PKHeX.WinForms/Controls/PKM Editor/LoadSave.cs | 3 ++- PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs | 6 +++--- PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs | 2 +- PKHeX.WinForms/MainWindow/Main.cs | 2 -- PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Pokedex4.cs | 2 +- .../Subforms/Save Editors/Gen6/SAV_BoxLayout.cs | 2 +- PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs | 2 -- 23 files changed, 26 insertions(+), 40 deletions(-) diff --git a/PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs b/PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs index c17f7877f..e769758d3 100644 --- a/PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs +++ b/PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs @@ -38,8 +38,6 @@ public static int[] GetMoveSet(this PKM pk, bool random = false) public static int[] GetMoveSet(this PKM pk, LegalityAnalysis la, bool random = false) { int[] m = la.GetSuggestedMoves(tm: random, tutor: random, reminder: random); - if (m == null) - return pk.Moves; if (!m.All(z => la.AllSuggestedMovesAndRelearn().Contains(z))) m = m.Intersect(la.AllSuggestedMovesAndRelearn()).ToArray(); diff --git a/PKHeX.Core/Editing/WurmpleUtil.cs b/PKHeX.Core/Editing/WurmpleUtil.cs index 942aeda3e..e13277a14 100644 --- a/PKHeX.Core/Editing/WurmpleUtil.cs +++ b/PKHeX.Core/Editing/WurmpleUtil.cs @@ -41,7 +41,7 @@ public static uint GetWurmpleEC(int evoVal) } /// - /// Checks to see if the input , with species being that of Wurmple's evo chain, is valid. + /// Checks to see if the input , with species being that of Wurmple's evo chain, is valid. /// /// Pokémon data /// True if valid, false if invalid diff --git a/PKHeX.Core/Game/GameStrings/GameInfo.cs b/PKHeX.Core/Game/GameStrings/GameInfo.cs index b475594b3..75dc54455 100644 --- a/PKHeX.Core/Game/GameStrings/GameInfo.cs +++ b/PKHeX.Core/Game/GameStrings/GameInfo.cs @@ -7,7 +7,7 @@ namespace PKHeX.Core /// public static class GameInfo { - private static readonly GameStrings[] Languages = new GameStrings[GameLanguage.LanguageCount]; + private static readonly GameStrings?[] Languages = new GameStrings[GameLanguage.LanguageCount]; public static string CurrentLanguage { get; set; } = GameLanguage.DefaultLanguage; public static readonly IReadOnlyList GenderSymbolUnicode = new[] {"♂", "♀", "-"}; diff --git a/PKHeX.Core/Game/GameStrings/GameLanguage.cs b/PKHeX.Core/Game/GameStrings/GameLanguage.cs index 24c0b15c3..8cd4bfa79 100644 --- a/PKHeX.Core/Game/GameStrings/GameLanguage.cs +++ b/PKHeX.Core/Game/GameStrings/GameLanguage.cs @@ -42,7 +42,7 @@ public static string GetTransporterName(int index) public static string[] GetStrings(string ident, string lang, string type = "text") { string[] data = Util.GetStringList(ident, lang, type); - if (data == null || data.Length == 0) + if (data.Length == 0) data = Util.GetStringList(ident, DefaultLanguage, type); return data; diff --git a/PKHeX.Core/Game/GameStrings/GeoLocation.cs b/PKHeX.Core/Game/GameStrings/GeoLocation.cs index 835064a83..6dd707617 100644 --- a/PKHeX.Core/Game/GameStrings/GeoLocation.cs +++ b/PKHeX.Core/Game/GameStrings/GeoLocation.cs @@ -61,13 +61,7 @@ private static string GetRegionName(int countryID, int regionID, int l) return INVALID; if (countryID >= RegionList.Length) return INVALID; - var regionstrs = RegionList[countryID]; - if (regionstrs == null) - { - regionstrs = RegionList[countryID] = GetRegionList(countryID); - if (regionstrs == null) - return INVALID; - } + var regionstrs = RegionList[countryID] ?? (RegionList[countryID] = GetRegionList(countryID)); if (regionID >= regionstrs.Length) return INVALID; var localized = regionstrs[regionID]; diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterMovesetGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterMovesetGenerator.cs index 6cae7110b..40f75d777 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterMovesetGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterMovesetGenerator.cs @@ -267,7 +267,7 @@ private static IEnumerable GetTrades(PKM pk, IReadOnlyCollection continue; } var em = trade.Moves; - if (em != null && !needs.Except(em).Any()) + if (!needs.Except(em).Any()) yield return trade; } } diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterSlotGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterSlotGenerator.cs index b1de37838..b2fa5487f 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterSlotGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterSlotGenerator.cs @@ -164,7 +164,7 @@ private static bool IsHiddenAbilitySlot(this EncounterSlot slot) return slot.Permissions.DexNav || slot.Type == SlotType.FriendSafari || slot.Type == SlotType.Horde || slot.Type == SlotType.SOS; } - internal static EncounterArea GetCaptureLocation(PKM pkm) + internal static EncounterArea? GetCaptureLocation(PKM pkm) { var vs = EvolutionChain.GetValidPreEvolutions(pkm); return (from area in GetEncounterSlots(pkm) diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterStaticGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterStaticGenerator.cs index 79563ff9c..9d487c4ed 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterStaticGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterStaticGenerator.cs @@ -146,7 +146,7 @@ private static EncounterStatic GetGSStaticTransfer(int species, int pkmMetLevel) }; } - internal static EncounterStatic GetStaticLocation(PKM pkm, int species = -1) + internal static EncounterStatic? GetStaticLocation(PKM pkm, int species = -1) { switch (pkm.GenNumber) { diff --git a/PKHeX.Core/Legality/Encounters/Information/EncounterLearn.cs b/PKHeX.Core/Legality/Encounters/Information/EncounterLearn.cs index 21bbd7305..4e7eb902a 100644 --- a/PKHeX.Core/Legality/Encounters/Information/EncounterLearn.cs +++ b/PKHeX.Core/Legality/Encounters/Information/EncounterLearn.cs @@ -44,8 +44,6 @@ public static IEnumerable GetLearnSummary(string species, IEnumerable GetLearn(string species, IEnumerable moves, string lang = GameLanguage.DefaultLanguage) { var str = GameInfo.GetStrings(lang); - if (str == null) - return Array.Empty(); var spec = StringUtil.FindIndexIgnoreCase(str.specieslist, species); var moveIDs = StringUtil.GetIndexes(str.movelist, moves.ToList()); diff --git a/PKHeX.Core/Legality/Encounters/Information/EncounterSuggestion.cs b/PKHeX.Core/Legality/Encounters/Information/EncounterSuggestion.cs index eded38b00..f9abc76c6 100644 --- a/PKHeX.Core/Legality/Encounters/Information/EncounterSuggestion.cs +++ b/PKHeX.Core/Legality/Encounters/Information/EncounterSuggestion.cs @@ -184,7 +184,6 @@ public static int GetLowestLevel(PKM pkm, int startLevel) return startLevel; } - public static int GetSuggestedMetLevel(PKM pkm, int minLevel) { var clone = pkm.Clone(); diff --git a/PKHeX.Core/Legality/Encounters/Verifiers/MysteryGiftVerifier.cs b/PKHeX.Core/Legality/Encounters/Verifiers/MysteryGiftVerifier.cs index 3b22adf5e..fb49f73fc 100644 --- a/PKHeX.Core/Legality/Encounters/Verifiers/MysteryGiftVerifier.cs +++ b/PKHeX.Core/Legality/Encounters/Verifiers/MysteryGiftVerifier.cs @@ -6,11 +6,11 @@ namespace PKHeX.Core { public static class MysteryGiftVerifier { - private static readonly Dictionary[] RestrictionSet = Get(); + private static readonly Dictionary?[] RestrictionSet = Get(); - private static Dictionary[] Get() + private static Dictionary?[] Get() { - var s = new Dictionary[PKX.Generation + 1]; + var s = new Dictionary?[PKX.Generation + 1]; for (int i = 3; i < s.Length; i++) s[i] = GetRestriction(i); return s; diff --git a/PKHeX.Core/PKM/Util/PokeCrypto.cs b/PKHeX.Core/PKM/Util/PokeCrypto.cs index 02a594b85..0cf90fb59 100644 --- a/PKHeX.Core/PKM/Util/PokeCrypto.cs +++ b/PKHeX.Core/PKM/Util/PokeCrypto.cs @@ -239,7 +239,7 @@ public static void CryptArray(byte[] data, uint seed, int start, int end) public static void CryptArray(byte[] data, uint seed) => CryptArray(data, seed, 0, data.Length); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void Crypt(in byte[] data, ref uint seed, in int i) + private static void Crypt(byte[] data, ref uint seed, in int i) { seed = (0x41C64E6D * seed) + 0x00006073; data[i] ^= (byte)(seed >> 16); diff --git a/PKHeX.Core/Saves/SAV3.cs b/PKHeX.Core/Saves/SAV3.cs index 9c74ed58e..2670094b7 100644 --- a/PKHeX.Core/Saves/SAV3.cs +++ b/PKHeX.Core/Saves/SAV3.cs @@ -81,7 +81,7 @@ public SAV3(GameVersion version = GameVersion.FRLG, bool japanese = false) : bas Version = GameVersion.RS; else Version = version; - _personal = SaveUtil.GetG3Personal(Version) ?? PersonalTable.RS; + _personal = SaveUtil.GetG3Personal(Version); Japanese = japanese; LoadBlocks(out BlockOrder, out BlockOfs); @@ -105,7 +105,7 @@ public SAV3(byte[] data, GameVersion versionOverride = GameVersion.Any) : base(d { LoadBlocks(out BlockOrder, out BlockOfs); Version = versionOverride != GameVersion.Any ? versionOverride : GetVersion(Data, BlockOfs[0]); - _personal = SaveUtil.GetG3Personal(Version) ?? PersonalTable.RS; + _personal = SaveUtil.GetG3Personal(Version); // Japanese games are limited to 5 character OT names; any unused characters are 0xFF. // 5 for JP, 7 for INT. There's always 1 terminator, thus we can check 0x6-0x7 being 0xFFFF = INT diff --git a/PKHeX.Core/Saves/SAV3Colosseum.cs b/PKHeX.Core/Saves/SAV3Colosseum.cs index ba5e6dc60..d37574523 100644 --- a/PKHeX.Core/Saves/SAV3Colosseum.cs +++ b/PKHeX.Core/Saves/SAV3Colosseum.cs @@ -158,7 +158,7 @@ public override SaveFile Clone() // Checksums private readonly SHA1 sha1 = SHA1.Create(); - public void Dispose() => sha1?.Dispose(); + public void Dispose() => sha1.Dispose(); private byte[] EncryptColosseum(byte[] input, byte[] digest) { diff --git a/PKHeX.Core/Saves/Substructures/Gen3/StrategyMemo.cs b/PKHeX.Core/Saves/Substructures/Gen3/StrategyMemo.cs index 627e05380..237fee670 100644 --- a/PKHeX.Core/Saves/Substructures/Gen3/StrategyMemo.cs +++ b/PKHeX.Core/Saves/Substructures/Gen3/StrategyMemo.cs @@ -11,7 +11,7 @@ public sealed class StrategyMemo private readonly List Entries = new List(); public const int MAX_COUNT = 500; public const int MAX_SIZE = MAX_COUNT * SIZE_ENTRY; - private StrategyMemoEntry this[int Species] => Entries.Find(e => e.Species == Species); + private StrategyMemoEntry? this[int Species] => Entries.Find(e => e.Species == Species); private readonly byte[] _unk; public StrategyMemo(bool xd = true) : this(new byte[MAX_SIZE], 0, xd) { } diff --git a/PKHeX.Core/Saves/Util/SaveDetection.cs b/PKHeX.Core/Saves/Util/SaveDetection.cs index cc13f766b..85d9b8375 100644 --- a/PKHeX.Core/Saves/Util/SaveDetection.cs +++ b/PKHeX.Core/Saves/Util/SaveDetection.cs @@ -17,7 +17,7 @@ public static class SaveDetection /// Optional parameter to skip the first drive. /// The first drive is usually the system hard drive, or can be a floppy disk drive (slower to check, never has expected data). /// Folder path pointing to the Nintendo 3DS folder. - public static string Get3DSLocation(IEnumerable drives, bool skipFirstDrive = true) => + public static string? Get3DSLocation(IEnumerable drives, bool skipFirstDrive = true) => FindConsoleRootFolder(drives, "Nintendo 3DS", skipFirstDrive); /// @@ -27,10 +27,10 @@ public static class SaveDetection /// Optional parameter to skip the first drive. /// The first drive is usually the system hard drive, or can be a floppy disk drive (slower to check, never has expected data). /// Folder path pointing to the Nintendo folder. - public static string GetSwitchLocation(IEnumerable drives, bool skipFirstDrive = true) => + public static string? GetSwitchLocation(IEnumerable drives, bool skipFirstDrive = true) => FindConsoleRootFolder(drives, "Nintendo", skipFirstDrive); - private static string FindConsoleRootFolder(IEnumerable drives, string path, bool skipFirstDrive) + private static string? FindConsoleRootFolder(IEnumerable drives, string path, bool skipFirstDrive) { if (skipFirstDrive) drives = drives.Skip(1); diff --git a/PKHeX.WinForms/Controls/PKM Editor/LoadSave.cs b/PKHeX.WinForms/Controls/PKM Editor/LoadSave.cs index ac5017c4a..6e22b4a30 100644 --- a/PKHeX.WinForms/Controls/PKM Editor/LoadSave.cs +++ b/PKHeX.WinForms/Controls/PKM Editor/LoadSave.cs @@ -347,7 +347,8 @@ private void SaveHandlingTrainer(PKM pk) // Misc private static void CheckTransferPIDValid(PKM pk) { - if (pk.Version >= 24 && pk.Version != 0) + var ver = pk.Version; + if (ver >= (int)GameVersion.X || ver == 0) return; uint EC = pk.EncryptionConstant; diff --git a/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs b/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs index 2f029fbe9..33e44434f 100644 --- a/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs +++ b/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs @@ -288,7 +288,7 @@ public void UpdateLegality(LegalityAnalysis la = null, bool skipMoveRepop = fals var moves = Entity.Moves; for (int i = 0; i < 4; i++) { - bool invalid = !Legality.Info?.Moves[i]?.Valid ?? false; + bool invalid = !Legality.Info?.Moves[i].Valid ?? false; Bitmap img; if (invalid) @@ -304,7 +304,7 @@ public void UpdateLegality(LegalityAnalysis la = null, bool skipMoveRepop = fals if (Entity.Format >= 6) { for (int i = 0; i < 4; i++) - relearnPB[i].Visible = !Legality.Info?.Relearn[i]?.Valid ?? false; + relearnPB[i].Visible = !Legality.Info?.Relearn[i].Valid ?? false; } if (skipMoveRepop) @@ -704,7 +704,7 @@ private void ClickMoves(object sender, EventArgs e) private bool SetSuggestedMoves(bool random = false, bool silent = false) { int[] m = Entity.GetMoveSet(random); - if (m?.Any(z => z != 0) != true) + if (m.Any(z => z != 0) != true) { if (!silent) WinFormsUtil.Alert(MsgPKMSuggestionFormat); diff --git a/PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs b/PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs index 0630cb41c..f763d89ad 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/BoxMenuStrip.cs @@ -95,7 +95,7 @@ public ItemVisibility(ToolStripItem toolStripItem, IBoxManip visible) Manip = visible; } - public void SetVisibility(SaveFile s) => Item.Visible = Manip.Usable?.Invoke(s) ?? true; + public void SetVisibility(SaveFile s) => Item.Visible = Manip.Usable(s); } public void ToggleVisibility() diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index fde433b72..694b5eb3c 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -992,8 +992,6 @@ private void ExportQRFromTabs() } var qr = QREncode.GenerateQRCode(pk); - if (qr == null) - return; var sprite = dragout.Image; var la = new LegalityAnalysis(pk, C_SAV.SAV.Personal); diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Pokedex4.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Pokedex4.cs index 28313c87a..973d51c8a 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Pokedex4.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Pokedex4.cs @@ -221,7 +221,7 @@ private void SetEntry() } var forms = SAV.GetForms(species); - if (forms != null) + if (forms.Length > 0) { int[] arr = new int[LB_Form.Items.Count]; string[] formNames = GetFormNames4Dex(species); diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_BoxLayout.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_BoxLayout.cs index abf2ffbb3..cf06fde28 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_BoxLayout.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_BoxLayout.cs @@ -80,7 +80,7 @@ private void LoadUnlockedCount() private void LoadFlags() { byte[] flags = SAV.BoxFlags; - if (flags == null || flags.Length == 0) + if (flags.Length == 0) { FLP_Flags.Visible = false; return; diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs index 3e5b4849b..8f08502c4 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs @@ -332,8 +332,6 @@ private void ExportQRFromView() } Image qr = QREncode.GenerateQRCode(mg); - if (qr == null) - return; string desc = $"({mg.Type}) {string.Join(Environment.NewLine, mg.GetDescription())}";