Refactor met location fetching for box report

Array length is checked before accessing
This commit is contained in:
Kurt 2017-02-23 16:39:03 -08:00
parent c8b00a183b
commit a8ba8aeeae
3 changed files with 99 additions and 45 deletions

View file

@ -20,23 +20,23 @@ namespace PKHeX.WinForms
public string Position => pkm.Identifier;
public Image Sprite => pkm.Sprite();
public string Nickname => pkm.Nickname;
public string Species => GameInfo.Strings.specieslist[pkm.Species];
public string Nature => GameInfo.Strings.natures[pkm.Nature];
public string Gender => Main.gendersymbols[pkm.Gender];
public string Species => get(GameInfo.Strings.specieslist, pkm.Species);
public string Nature => get(GameInfo.Strings.natures, pkm.Nature);
public string Gender => get(Main.gendersymbols, pkm.Gender);
public string ESV => pkm.PSV.ToString("0000");
public string HP_Type => GameInfo.Strings.types[pkm.HPType+1];
public string Ability => GameInfo.Strings.abilitylist[pkm.Ability];
public string Move1 => GameInfo.Strings.movelist[pkm.Move1];
public string Move2 => GameInfo.Strings.movelist[pkm.Move2];
public string Move3 => GameInfo.Strings.movelist[pkm.Move3];
public string Move4 => GameInfo.Strings.movelist[pkm.Move4];
public string HeldItem => GameInfo.Strings.itemlist[pkm.HeldItem];
public string HP_Type => get(GameInfo.Strings.types, pkm.HPType+1);
public string Ability => get(GameInfo.Strings.abilitylist, pkm.Ability);
public string Move1 => get(GameInfo.Strings.movelist, pkm.Move1);
public string Move2 => get(GameInfo.Strings.movelist, pkm.Move2);
public string Move3 => get(GameInfo.Strings.movelist, pkm.Move3);
public string Move4 => get(GameInfo.Strings.movelist, pkm.Move4);
public string HeldItem => get(GameInfo.Strings.itemlist, pkm.HeldItem);
public string MetLoc => pkm.getLocation(eggmet: false);
public string EggLoc => pkm.getLocation(eggmet: true);
public string Ball => GameInfo.Strings.balllist[pkm.Ball];
public string Ball => get(GameInfo.Strings.balllist, pkm.Ball);
public string OT => pkm.OT_Name;
public string Version => GameInfo.Strings.gamelist[pkm.Version];
public string OTLang => GameInfo.Strings.gamelanguages[pkm.Language] ?? $"UNK {pkm.Language}";
public string Version => get(GameInfo.Strings.gamelist, pkm.Version);
public string OTLang => get(GameInfo.Strings.gamelanguages, pkm.Language) ?? $"UNK {pkm.Language}";
public string CountryID => pkm.Format > 5 ? pkm.Country.ToString() : "N/A";
public string RegionID => pkm.Format > 5 ? pkm.Region.ToString() : "N/A";
public string DSRegionID => pkm.Format > 5 ? pkm.ConsoleRegion.ToString() : "N/A";
@ -92,10 +92,10 @@ namespace PKHeX.WinForms
public int Move2_PPUp => pkm.Move2_PPUps;
public int Move3_PPUp => pkm.Move3_PPUps;
public int Move4_PPUp => pkm.Move4_PPUps;
public string Relearn1 => GameInfo.Strings.movelist[pkm.RelearnMove1];
public string Relearn2 => GameInfo.Strings.movelist[pkm.RelearnMove2];
public string Relearn3 => GameInfo.Strings.movelist[pkm.RelearnMove3];
public string Relearn4 => GameInfo.Strings.movelist[pkm.RelearnMove4];
public string Relearn1 => get(GameInfo.Strings.movelist, pkm.RelearnMove1);
public string Relearn2 => get(GameInfo.Strings.movelist, pkm.RelearnMove2);
public string Relearn3 => get(GameInfo.Strings.movelist, pkm.RelearnMove3);
public string Relearn4 => get(GameInfo.Strings.movelist, pkm.RelearnMove4);
public ushort Checksum => pkm.Checksum;
public int mFriendship => pkm.OT_Friendship;
public int OT_Affection => pkm.OT_Affection;
@ -109,6 +109,8 @@ namespace PKHeX.WinForms
#endregion
public Preview(PKM p) { pkm = p; }
private static string get(IReadOnlyList<string> arr, int val) => arr?.Count > val ? arr[val] : null;
}
public frmReport()
{

View file

@ -525,5 +525,56 @@ namespace PKHeX.Core
}
catch { return "Illegal"; }
}
/// <summary>
/// Gets the location names array for a specified generation.
/// </summary>
/// <param name="gen">Generation to get location names for.</param>
/// <param name="bankID">BankID used to choose the text bank.</param>
/// <returns>List of location names.</returns>
public static string[] getLocationNames(int gen, int bankID)
{
switch (gen)
{
case 2: return Strings.metGSC_00000;
case 3: return Strings.metRSEFRLG_00000;
case 4:
switch (bankID)
{
case 0: return Strings.metHGSS_00000;
case 2: return Strings.metHGSS_02000;
default: return null;
}
case 5:
switch (bankID)
{
case 0: return Strings.metBW2_00000;
case 3: return Strings.metBW2_30000;
case 4: return Strings.metBW2_40000;
case 6: return Strings.metBW2_60000;
default: return null;
}
case 6:
switch (bankID)
{
case 0: return Strings.metXY_00000;
case 3: return Strings.metXY_30000;
case 4: return Strings.metXY_40000;
case 6: return Strings.metXY_60000;
default: return null;
}
case 7:
switch (bankID)
{
case 0: return Strings.metSM_00000;
case 3: return Strings.metSM_30000;
case 4: return Strings.metSM_40000;
case 6: return Strings.metSM_60000;
default: return null;
}
default:
return null;
}
}
}
}

View file

@ -2652,43 +2652,44 @@ namespace PKHeX.Core
// Extensions
public static string getLocation(this PKM pk, bool eggmet)
{
if (pk.Format <= 2)
if (pk.Format < 2)
return "";
int gen = -1;
int bankID = 0;
int locval = eggmet ? pk.Egg_Location : pk.Met_Location;
if (pk.Format == 2)
return GameInfo.Strings.metGSC_00000[locval];
if (pk.Format == 3)
return GameInfo.Strings.metRSEFRLG_00000[locval % 0x100];
if (pk.Gen4 && (eggmet || pk.Format == 4))
gen = 2;
else if (pk.Format == 3)
gen = 3;
else if (pk.Gen4 && (eggmet || pk.Format == 4)) // 4
{
if (locval < 2000) return GameInfo.Strings.metHGSS_00000[locval];
if (locval < 3000) return GameInfo.Strings.metHGSS_02000[locval % 2000];
return GameInfo.Strings.metHGSS_03000[locval % 3000];
const int size = 1000;
bankID = locval/size;
gen = 4;
locval %= size;
}
if (pk.Gen5 || pk.Format <= 5)
else // 5-7+
{
if (locval < 30000) return GameInfo.Strings.metBW2_00000[locval];
if (locval < 40000) return GameInfo.Strings.metBW2_30000[locval % 10000 - 1];
if (locval < 60000) return GameInfo.Strings.metBW2_40000[locval % 10000 - 1];
return GameInfo.Strings.metBW2_60000[locval % 10000 - 1];
const int size = 10000;
bankID = locval/size;
int g = pk.GenNumber;
if (g >= 5)
gen = g;
else if (pk.Format >= 5)
gen = pk.Format;
locval %= size;
if (bankID >= 3)
locval -= 1;
}
if (pk.Gen6 || pk.Format <= 6)
{
if (locval < 30000) return GameInfo.Strings.metXY_00000[locval];
if (locval < 40000) return GameInfo.Strings.metXY_30000[locval % 10000 - 1];
if (locval < 60000) return GameInfo.Strings.metXY_40000[locval % 10000 - 1];
return GameInfo.Strings.metXY_60000[locval % 10000 - 1];
}
if (pk.Gen7 || pk.Format <= 7)
{
if (locval < 30000) return GameInfo.Strings.metSM_00000[locval];
if (locval < 40000) return GameInfo.Strings.metSM_30000[locval % 10000 - 1];
if (locval < 60000) return GameInfo.Strings.metSM_40000[locval % 10000 - 1];
return GameInfo.Strings.metSM_60000[locval % 10000 - 1];
}
return null; // Shouldn't happen for gen 3+
var bank = GameInfo.getLocationNames(gen, bankID);
if (bank == null || bank.Length <= locval)
return "";
return bank[locval];
}
public static string[] getQRText(this PKM pkm)
{