Misc tweaks

more annotations - all bool->out ? are annotated now
fix gender symbol inversion (forgot to set text)
extract some methods to reduce nesting
This commit is contained in:
Kurt 2021-07-06 08:42:15 -07:00
parent de8e855116
commit 3fa311f6ed
6 changed files with 97 additions and 65 deletions

View file

@ -9,7 +9,7 @@ namespace PKHeX.Core
/// </summary>
public sealed class LegalMoveSource
{
public readonly IList<bool> IsMoveBoxOrdered = new bool[4];
public readonly bool[] IsMoveBoxOrdered = new bool[4];
public IReadOnlyList<ComboItem> DataSource => (ComboItem[])MoveDataAllowed.Clone();
public bool CanLearn(int move) => AllowedMoves.Contains(move);
@ -29,8 +29,7 @@ namespace PKHeX.Core
// MoveDataAllowed = MoveDataAllowed.OrderByDescending(m => AllowedMoves.Contains(m.Value)).ToArray();
// defer re-population until dropdown is opened; handled by dropdown event
for (int i = 0; i < IsMoveBoxOrdered.Count; i++)
IsMoveBoxOrdered[i] = false;
Array.Clear(IsMoveBoxOrdered, 0, IsMoveBoxOrdered.Length);
}
private int Compare(ComboItem i1, ComboItem i2)

View file

@ -10,9 +10,9 @@ namespace PKHeX.Core
internal static readonly Learnset[] LevelUpY = LearnsetReader.GetArray(Util.GetBinaryResource("lvlmove_y.pkl"), MaxSpeciesID_1);
// Gen 2
internal static readonly EggMoves[] EggMovesGS = EggMoves2.GetArray(Util.GetBinaryResource("eggmove_gs.pkl"), MaxSpeciesID_2);
internal static readonly EggMoves2[] EggMovesGS = EggMoves2.GetArray(Util.GetBinaryResource("eggmove_gs.pkl"), MaxSpeciesID_2);
internal static readonly Learnset[] LevelUpGS = LearnsetReader.GetArray(Util.GetBinaryResource("lvlmove_gs.pkl"), MaxSpeciesID_2);
internal static readonly EggMoves[] EggMovesC = EggMoves2.GetArray(Util.GetBinaryResource("eggmove_c.pkl"), MaxSpeciesID_2);
internal static readonly EggMoves2[] EggMovesC = EggMoves2.GetArray(Util.GetBinaryResource("eggmove_c.pkl"), MaxSpeciesID_2);
internal static readonly Learnset[] LevelUpC = LearnsetReader.GetArray(Util.GetBinaryResource("lvlmove_c.pkl"), MaxSpeciesID_2);
// Gen 3

View file

@ -8,6 +8,8 @@ namespace PKHeX.Core
/// <remarks>
/// Used in Generation 4 games, this value is set depending on what type of overworld tile the player is standing on when the <see cref="PKM"/> is obtained.
/// </remarks>
#pragma warning disable RCS1234 // Duplicate enum value.
#pragma warning disable CA1027 // Mark enums with FlagsAttribute
public enum GroundTileType : byte
{
None = 00, // No animation for the tile
@ -41,6 +43,8 @@ namespace PKHeX.Core
Distortion = 23,
Max_Pt = 24, // Unspecific, catch-all for Pt undefined tiles.
}
#pragma warning restore CA1027 // Mark enums with FlagsAttribute
#pragma warning restore RCS1234 // Duplicate enum value.
public static class GroundTileTypeExtensions
{

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
@ -160,9 +161,9 @@ namespace PKHeX.Core
return true;
}
public static bool DetectSaveFile(out string path, out SaveFile? sav) => DetectSaveFile(out path, out sav, Environment.GetLogicalDrives());
public static bool DetectSaveFile(out string path, [NotNullWhen(true)] out SaveFile? sav) => DetectSaveFile(out path, out sav, Environment.GetLogicalDrives());
public static bool DetectSaveFile(out string path, out SaveFile? sav, IReadOnlyList<string> drives)
public static bool DetectSaveFile(out string path, [NotNullWhen(true)] out SaveFile? sav, IReadOnlyList<string> drives)
{
string errorMsg = string.Empty;
var result = FindMostRecentSaveFile(drives, ref errorMsg);

View file

@ -636,9 +636,7 @@ namespace PKHeX.WinForms.Controls
if (string.IsNullOrWhiteSpace(lbl.Text))
return;
int gender = PKX.GetGenderFromString(lbl.Text) ^ 1;
lbl.Text = gendersymbols[gender];
lbl.ForeColor = Draw.GetGenderColor(gender);
InvertGenderLabel(lbl);
}
private void ClickBall(object sender, EventArgs e)
@ -834,9 +832,19 @@ namespace PKHeX.WinForms.Controls
return true;
}
private void InvertGenderLabel(Label lbl)
{
int gender = (PKX.GetGenderFromString(lbl.Text) & 1) ^ 1;
UpdateGenderLabel(lbl, gender);
}
private void UpdateGenderLabel(Label c, int gender)
{
ReloadGender(c, gendersymbols);
var symbols = gendersymbols;
if ((uint) gender >= symbols.Count)
gender = 0;
c.Text = gendersymbols[gender];
c.ForeColor = Draw.GetGenderColor(gender);
}

View file

@ -166,54 +166,72 @@ namespace PKHeX.WinForms
}
if (C_SAV.SAV is FakeSaveFile) // No SAV loaded from exe args
{
#if !DEBUG
bool savLoaded = false;
try
#endif
{
var startup = Settings.Startup;
string path = string.Empty;
SaveFile? sav = null;
if (startup.AutoLoadSaveOnStartup == AutoLoadSetting.RecentBackup)
{
if (!SaveFinder.DetectSaveFile(out path, out sav))
{
if (!string.IsNullOrWhiteSpace(path))
WinFormsUtil.Error(path); // `path` contains the error message
}
}
else if (startup.AutoLoadSaveOnStartup == AutoLoadSetting.LastLoaded)
{
if (startup.RecentlyLoaded.Count != 0)
{
path = startup.RecentlyLoaded[0];
if (File.Exists(path))
sav = SaveUtil.GetVariantSAV(path);
}
}
bool savLoaded = false;
if (sav != null && path.Length != 0)
{
savLoaded = OpenSAV(sav, path);
}
if (!savLoaded)
LoadBlankSaveFile(startup.DefaultSaveVersion);
savLoaded = LoadAutoDetectedSAV();
}
#if !DEBUG
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
ErrorWindow.ShowErrorDialog(MsgFileLoadFailAuto, ex, true);
}
#endif
finally
{
if (!savLoaded)
LoadBlankSaveFile(Settings.Startup.DefaultSaveVersion);
}
}
if (!string.IsNullOrWhiteSpace(pkmArg) && File.Exists(pkmArg))
{
byte[] data = File.ReadAllBytes(pkmArg);
var pk = PKMConverter.GetPKMfromBytes(data);
if (pk != null)
OpenPKM(pk);
}
LoadPKMFromPath(pkmArg);
}
private bool LoadAutoDetectedSAV()
{
var startup = Settings.Startup;
if (startup.AutoLoadSaveOnStartup == AutoLoadSetting.RecentBackup)
return LoadMostRecentBackup();
if (startup.AutoLoadSaveOnStartup == AutoLoadSetting.LastLoaded)
return LoadMostRecentlyLoaded(startup.RecentlyLoaded);
return false;
}
private bool LoadMostRecentlyLoaded(IReadOnlyList<string> paths)
{
if (paths.Count == 0)
return false;
string path = paths[0];
if (!File.Exists(path))
return false;
var sav = SaveUtil.GetVariantSAV(path);
if (sav is null)
return false;
return OpenSAV(sav, path);
}
private bool LoadMostRecentBackup()
{
if (SaveFinder.DetectSaveFile(out string path, out var sav))
return OpenSAV(sav, path);
if (path.Length != 0)
WinFormsUtil.Error(path); // `path` contains the error message
return false;
}
private void LoadPKMFromPath(string pkmArg)
{
if (string.IsNullOrWhiteSpace(pkmArg) || !File.Exists(pkmArg))
return;
byte[] data = File.ReadAllBytes(pkmArg);
var pk = PKMConverter.GetPKMfromBytes(data);
if (pk != null)
OpenPKM(pk);
}
private void LoadBlankSaveFile(GameVersion ver)
@ -487,7 +505,7 @@ namespace PKHeX.WinForms
{
if (this.OpenWindowExists<SAV_FolderList>())
return;
var form = new SAV_FolderList(s => OpenSAV(SaveUtil.GetVariantSAV(s.Metadata.FilePath!), s.Metadata.FilePath!));
var form = new SAV_FolderList(s => OpenSAV(s.Clone(), s.Metadata.FilePath!));
form.Show();
}
@ -503,8 +521,9 @@ namespace PKHeX.WinForms
if (Set.Species < 0)
{ WinFormsUtil.Alert(MsgSimulatorFailClipboard); return; }
if (Set.Nickname.Length > C_SAV.SAV.NickLength)
Set.Nickname = Set.Nickname[..C_SAV.SAV.NickLength];
var maxLength = C_SAV.SAV.NickLength;
if (Set.Nickname.Length > maxLength)
Set.Nickname = Set.Nickname[..maxLength];
if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgSimulatorLoad, Set.Text))
return;
@ -577,11 +596,11 @@ namespace PKHeX.WinForms
string ext = fi.Extension;
#if DEBUG
OpenFile(input, path, ext);
OpenFile(input, path, ext);
#else
try { OpenFile(input, path, ext); }
try { OpenFile(input, path, ext); }
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e) { WinFormsUtil.Error(MsgFileLoadFail + "\nPath: " + path, e); }
catch (Exception e) { WinFormsUtil.Error(MsgFileLoadFail + "\nPath: " + path, e); }
#pragma warning restore CA1031 // Do not catch general exception types
#endif
}
@ -617,6 +636,8 @@ namespace PKHeX.WinForms
if (!CheckGCMemoryCard(gc, path))
return true;
var mcsav = SaveUtil.GetVariantSAV(gc);
if (mcsav is null)
return false;
return OpenSAV(mcsav, path);
}
return false;
@ -707,13 +728,12 @@ namespace PKHeX.WinForms
return false;
case GCMemoryCardState.MultipleSaveGame:
{
GameVersion game = SelectMemoryCardSaveGame(memCard);
if (game == GameVersion.Invalid) //Cancel
return false;
memCard.SelectSaveGame(game);
break;
}
GameVersion game = SelectMemoryCardSaveGame(memCard);
if (game == GameVersion.Invalid) //Cancel
return false;
memCard.SelectSaveGame(game);
break;
case GCMemoryCardState.SaveGameCOLO: memCard.SelectSaveGame(GameVersion.COLO); break;
case GCMemoryCardState.SaveGameXD: memCard.SelectSaveGame(GameVersion.XD); break;
case GCMemoryCardState.SaveGameRSBOX: memCard.SelectSaveGame(GameVersion.RSBOX); break;
@ -731,9 +751,9 @@ namespace PKHeX.WinForms
EReaderBerrySettings.LoadFrom(sav3);
}
private bool OpenSAV(SaveFile? sav, string path)
private bool OpenSAV(SaveFile sav, string path)
{
if (sav == null || sav.Version == GameVersion.Invalid)
if (sav.Version == GameVersion.Invalid)
{
WinFormsUtil.Error(MsgFileLoadSaveLoadFail, path);
return true;