Refactoring

put some string[] behind IReadOnlyList to prevent any consumer from
modifying it
This commit is contained in:
Kurt 2018-06-30 09:59:48 -07:00
parent 517441a982
commit 9f8edc89bf
15 changed files with 81 additions and 72 deletions

View file

@ -76,7 +76,7 @@ namespace PKHeX.Core
Form = ConvertFormFromShowdown(Form, Species, Ability);
// Set Form
string[] formStrings = PKX.GetFormList(Species, types, forms, genderForms);
FormIndex = Math.Max(0, Array.FindIndex(formStrings, z => z.Contains(Form ?? "")));
FormIndex = string.IsNullOrWhiteSpace(Form) ? 0 : Math.Max(0, Array.FindIndex(formStrings, z => z.Contains(Form)));
}
private void ParseLines(IEnumerable<string> lines)
{

View file

@ -45,15 +45,15 @@ namespace PKHeX.Core
}
// DataSource providing
public static List<ComboItem> ItemDataSource => Strings.ItemDataSource;
public static List<ComboItem> SpeciesDataSource => Strings.SpeciesDataSource;
public static List<ComboItem> BallDataSource => Strings.BallDataSource;
public static List<ComboItem> NatureDataSource => Strings.NatureDataSource;
public static List<ComboItem> AbilityDataSource => Strings.AbilityDataSource;
public static List<ComboItem> VersionDataSource => Strings.VersionDataSource;
public static List<ComboItem> LegalMoveDataSource => Strings.LegalMoveDataSource;
public static List<ComboItem> HaXMoveDataSource => Strings.HaXMoveDataSource;
public static List<ComboItem> MoveDataSource => Strings.MoveDataSource;
public static IReadOnlyList<ComboItem> ItemDataSource => Strings.ItemDataSource;
public static IReadOnlyList<ComboItem> SpeciesDataSource => Strings.SpeciesDataSource;
public static IReadOnlyList<ComboItem> BallDataSource => Strings.BallDataSource;
public static IReadOnlyList<ComboItem> NatureDataSource => Strings.NatureDataSource;
public static IReadOnlyList<ComboItem> AbilityDataSource => Strings.AbilityDataSource;
public static IReadOnlyList<ComboItem> VersionDataSource => Strings.VersionDataSource;
public static IReadOnlyList<ComboItem> LegalMoveDataSource => Strings.LegalMoveDataSource;
public static IReadOnlyList<ComboItem> HaXMoveDataSource => Strings.HaXMoveDataSource;
public static IReadOnlyList<ComboItem> MoveDataSource => Strings.MoveDataSource;
/// <summary>
@ -144,7 +144,7 @@ namespace PKHeX.Core
/// <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>
private static string[] GetLocationNames(int gen, int bankID)
private static IReadOnlyList<string> GetLocationNames(int gen, int bankID)
{
switch (gen)
{
@ -231,7 +231,7 @@ namespace PKHeX.Core
}
var bank = GetLocationNames(gen, bankID);
if (bank == null || bank.Length <= locval)
if (bank == null || bank.Count <= locval)
return string.Empty;
return bank[locval];
}
@ -243,7 +243,7 @@ namespace PKHeX.Core
/// <param name="pkmFormat">Generation to retrieve for</param>
/// <param name="egg">Egg Locations are to be retrieved instead of regular Met Locations</param>
/// <returns>Consumable list of met locations</returns>
public static List<ComboItem> GetLocationList(GameVersion version, int pkmFormat, bool egg = false)
public static IReadOnlyList<ComboItem> GetLocationList(GameVersion version, int pkmFormat, bool egg = false)
{
return Strings.GetLocationList(version, pkmFormat, egg);
}

View file

@ -26,12 +26,12 @@ namespace PKHeX.Core
private readonly string lang;
public string EggName => eggname;
public string[] Species => specieslist;
public string[] Item => itemlist;
public string[] Move => movelist;
public string[] Ability => abilitylist;
public string[] Types => types;
public string[] Natures => natures;
public IReadOnlyList<string> Species => specieslist;
public IReadOnlyList<string> Item => itemlist;
public IReadOnlyList<string> Move => movelist;
public IReadOnlyList<string> Ability => abilitylist;
public IReadOnlyList<string> Types => types;
public IReadOnlyList<string> Natures => natures;
private string[] Get(string ident) => GameInfo.GetStrings(ident, lang);
@ -240,7 +240,7 @@ namespace PKHeX.Core
metSM_30000[i] += " (-)";
}
public string[] GetItemStrings(int generation, GameVersion game = GameVersion.Any)
public IReadOnlyList<string> GetItemStrings(int generation, GameVersion game = GameVersion.Any)
{
switch (generation)
{
@ -271,23 +271,23 @@ namespace PKHeX.Core
}
// DataSource providing
public List<ComboItem> ItemDataSource { get; private set; }
public List<ComboItem> SpeciesDataSource { get; private set; }
public List<ComboItem> BallDataSource { get; private set; }
public List<ComboItem> NatureDataSource { get; private set; }
public List<ComboItem> AbilityDataSource { get; private set; }
public List<ComboItem> VersionDataSource { get; private set; }
public List<ComboItem> LegalMoveDataSource { get; private set; }
public List<ComboItem> HaXMoveDataSource { get; private set; }
public List<ComboItem> MoveDataSource { get; set; }
public IReadOnlyList<ComboItem> ItemDataSource { get; private set; }
public IReadOnlyList<ComboItem> SpeciesDataSource { get; private set; }
public IReadOnlyList<ComboItem> BallDataSource { get; private set; }
public IReadOnlyList<ComboItem> NatureDataSource { get; private set; }
public IReadOnlyList<ComboItem> AbilityDataSource { get; private set; }
public IReadOnlyList<ComboItem> VersionDataSource { get; private set; }
public IReadOnlyList<ComboItem> LegalMoveDataSource { get; private set; }
public IReadOnlyList<ComboItem> HaXMoveDataSource { get; private set; }
public IReadOnlyList<ComboItem> MoveDataSource { get; set; }
private List<ComboItem> MetGen2 { get; set; }
private List<ComboItem> MetGen3 { get; set; }
private List<ComboItem> MetGen3CXD { get; set; }
private List<ComboItem> MetGen4 { get; set; }
private List<ComboItem> MetGen5 { get; set; }
private List<ComboItem> MetGen6 { get; set; }
private List<ComboItem> MetGen7 { get; set; }
private IReadOnlyList<ComboItem> MetGen2 { get; set; }
private IReadOnlyList<ComboItem> MetGen3 { get; set; }
private IReadOnlyList<ComboItem> MetGen3CXD { get; set; }
private IReadOnlyList<ComboItem> MetGen4 { get; set; }
private IReadOnlyList<ComboItem> MetGen5 { get; set; }
private IReadOnlyList<ComboItem> MetGen6 { get; set; }
private IReadOnlyList<ComboItem> MetGen7 { get; set; }
public MemoryStrings Memories { get; private set; }
@ -299,10 +299,7 @@ namespace PKHeX.Core
SpeciesDataSource = Util.GetCBList(specieslist, null);
NatureDataSource = Util.GetCBList(natures, null);
AbilityDataSource = Util.GetCBList(abilitylist, null);
VersionDataSource = Util.GetCBList(gamelist, Legal.Games_7usum, Legal.Games_7sm, Legal.Games_6oras, Legal.Games_6xy, Legal.Games_5, Legal.Games_4, Legal.Games_4e, Legal.Games_4r, Legal.Games_3, Legal.Games_3e, Legal.Games_3r, Legal.Games_3s);
VersionDataSource.AddRange(Util.GetCBList(gamelist, Legal.Games_7vc1).OrderBy(g => g.Value)); // stuff to end unsorted
VersionDataSource.AddRange(Util.GetCBList(gamelist, Legal.Games_7vc2).OrderBy(g => g.Value)); // stuff to end unsorted
VersionDataSource.AddRange(Util.GetCBList(gamelist, Legal.Games_7go).OrderBy(g => g.Value)); // stuff to end unsorted
VersionDataSource = GetVersionList();
HaXMoveDataSource = Util.GetCBList(movelist, null);
MoveDataSource = LegalMoveDataSource = HaXMoveDataSource.Where(m => !Legal.Z_Moves.Contains(m.Value)).ToList();
@ -310,6 +307,19 @@ namespace PKHeX.Core
Memories = new MemoryStrings(this);
}
private IReadOnlyList<ComboItem> GetVersionList()
{
var ver = Util.GetCBList(gamelist,
Legal.Games_7usum, Legal.Games_7sm,
Legal.Games_6oras, Legal.Games_6xy,
Legal.Games_5, Legal.Games_4, Legal.Games_4e, Legal.Games_4r,
Legal.Games_3, Legal.Games_3e, Legal.Games_3r, Legal.Games_3s);
ver.AddRange(Util.GetCBList(gamelist, Legal.Games_7vc1).OrderBy(g => g.Value)); // stuff to end unsorted
ver.AddRange(Util.GetCBList(gamelist, Legal.Games_7vc2).OrderBy(g => g.Value)); // stuff to end unsorted
ver.AddRange(Util.GetCBList(gamelist, Legal.Games_7go).OrderBy(g => g.Value)); // stuff to end unsorted
return ver;
}
private void InitializeMetSources()
{
// Gen 2
@ -374,10 +384,10 @@ namespace PKHeX.Core
public void SetItemDataSource(GameVersion game, int generation, int MaxItemID, IEnumerable<ushort> allowed = null, bool HaX = false)
{
string[] items = GetItemStrings(generation, game);
var items = GetItemStrings(generation, game);
ItemDataSource = Util.GetCBList(items, (allowed == null || HaX ? Enumerable.Range(0, MaxItemID) : allowed.Select(i => (int)i)).ToArray());
}
public List<ComboItem> GetLocationList(GameVersion Version, int SaveFormat, bool egg)
public IReadOnlyList<ComboItem> GetLocationList(GameVersion Version, int SaveFormat, bool egg)
{
if (SaveFormat == 2)
return MetGen2;

View file

@ -1,13 +1,15 @@
namespace PKHeX.Core
using System.Collections.Generic;
namespace PKHeX.Core
{
public interface IBasicStrings
{
string[] Species { get; }
string[] Item { get; }
string[] Move { get; }
string[] Ability { get; }
string[] Types { get; }
string[] Natures { get; }
IReadOnlyList<string> Species { get; }
IReadOnlyList<string> Item { get; }
IReadOnlyList<string> Move { get; }
IReadOnlyList<string> Ability { get; }
IReadOnlyList<string> Types { get; }
IReadOnlyList<string> Natures { get; }
string EggName { get; }
}
}

View file

@ -42,7 +42,7 @@ namespace PKHeX.Core
if (pkm.HeldItem > 0)
{
var str = s.GetItemStrings(pkm.Format);
if (pkm.HeldItem < str.Length)
if (pkm.HeldItem < str.Count)
yield return $" @ {str[pkm.HeldItem]}";
}

View file

@ -88,9 +88,9 @@ namespace PKHeX.Core
/// <param name="list">Source list to sort</param>
/// <param name="speciesNames">Names of each species</param>
/// <returns>Enumerable list that is sorted</returns>
public static IEnumerable<PKM> OrderBySpeciesName(this IEnumerable<PKM> list, string[] speciesNames)
public static IEnumerable<PKM> OrderBySpeciesName(this IEnumerable<PKM> list, IReadOnlyList<string> speciesNames)
{
int max = speciesNames.Length - 1;
int max = speciesNames.Count - 1;
string SpeciesName(int s) => s > max ? string.Empty : speciesNames[s];
return list.InitialSortBy()

View file

@ -248,20 +248,20 @@ namespace PKHeX.Core
.Select(data => new ComboItem { Text = data[1], Value = Convert.ToInt32(data[0]) })
.ToList();
}
public static List<ComboItem> GetCBList(string[] inStrings, params int[][] allowed)
public static List<ComboItem> GetCBList(IReadOnlyList<string> inStrings, params int[][] allowed)
{
if (allowed?[0] == null)
allowed = new[] { Enumerable.Range(0, inStrings.Length).ToArray() };
allowed = new[] { Enumerable.Range(0, inStrings.Count).ToArray() };
return allowed.SelectMany(list => list
.Select(z => new ComboItem { Text = inStrings[z], Value = z })
.OrderBy(z => z.Text))
.ToList();
}
public static List<ComboItem> GetOffsetCBList(List<ComboItem> cbList, string[] inStrings, int offset, int[] allowed)
public static List<ComboItem> GetOffsetCBList(List<ComboItem> cbList, IReadOnlyList<string> inStrings, int offset, IEnumerable<int> allowed)
{
if (allowed == null)
allowed = Enumerable.Range(0, inStrings.Length).ToArray();
allowed = Enumerable.Range(0, inStrings.Count);
var list = allowed
.Select(z => new ComboItem {Text = inStrings[z - offset], Value = z})

View file

@ -1689,7 +1689,7 @@ namespace PKHeX.WinForms.Controls
if (pkm.Format >= 3)
{
var met_list = GameInfo.GetLocationList((GameVersion)pkm.Version, pkm.Format, egg: false);
var locstr = met_list.Find(loc => loc.Value == location).Text;
var locstr = met_list.First(loc => loc.Value == location).Text;
suggestion.Add($"{MsgPKMSuggestionMetLocation} {locstr}");
suggestion.Add($"{MsgPKMSuggestionMetLevel} {level}");
}

View file

@ -586,7 +586,7 @@ namespace PKHeX.WinForms.Controls
new SAV_SimpleTrainer(SAV).ShowDialog();
else if (SAV.Generation == 6)
new SAV_Trainer(SAV).ShowDialog();
else if (SAV.Generation == 7)
else if (SAV is SAV7)
new SAV_Trainer7(SAV).ShowDialog();
// Refresh conversion info
PKMConverter.UpdateConfig(SAV.SubRegion, SAV.Country, SAV.ConsoleRegion, SAV.OT, SAV.Gender, SAV.Language);
@ -728,7 +728,7 @@ namespace PKHeX.WinForms.Controls
{
if (SAV.Generation == 6)
new SAV_HallOfFame(SAV).ShowDialog();
else if (SAV.Generation == 7)
else if (SAV is SAV7)
new SAV_HallOfFame7(SAV).ShowDialog();
}
private void B_CGearSkin_Click(object sender, EventArgs e)
@ -795,8 +795,7 @@ namespace PKHeX.WinForms.Controls
{
if (!SAV.Exportable)
return false;
SaveFileDialog sfd = new SaveFileDialog
{ FileName = Util.CleanFileName(SAV.BAKName) };
var sfd = new SaveFileDialog {FileName = Util.CleanFileName(SAV.BAKName)};
if (sfd.ShowDialog() != DialogResult.OK)
return false;
@ -1028,8 +1027,6 @@ namespace PKHeX.WinForms.Controls
GB_Daycare.Visible = sav.HasDaycare;
B_OpenSecretBase.Enabled = sav.HasSecretBase;
B_OpenPokepuffs.Enabled = sav.HasPuff;
B_OpenPokeBeans.Enabled = sav.Generation == 7;
B_CellsStickers.Enabled = sav.Generation == 7;
B_OUTPasserby.Enabled = sav.HasPSS;
B_OpenBoxLayout.Enabled = sav.HasNamableBoxes;
B_OpenWondercards.Enabled = sav.HasWondercards;
@ -1044,6 +1041,7 @@ namespace PKHeX.WinForms.Controls
B_OpenEventFlags.Enabled = sav.HasEvents;
B_OpenLinkInfo.Enabled = sav.HasLink;
B_CGearSkin.Enabled = sav.Generation == 5;
B_OpenPokeBeans.Enabled = B_CellsStickers.Enabled = B_FestivalPlaza.Enabled = sav is SAV7;
B_OpenTrainerInfo.Enabled = B_OpenItemPouch.Enabled = sav.HasParty && !(SAV is SAV4BR); // Box RS & Battle Revolution
B_OpenMiscEditor.Enabled = sav is SAV3 || sav is SAV4 || sav is SAV5;
@ -1053,12 +1051,11 @@ namespace PKHeX.WinForms.Controls
B_OpenApricorn.Enabled = sav.HGSS;
B_OpenRTCEditor.Enabled = sav.RS || sav.E || sav.Generation == 2;
B_OpenUGSEditor.Enabled = sav.DP || sav.Pt;
B_FestivalPlaza.Enabled = sav.Generation == 7;
B_MailBox.Enabled = sav is SAV2 || sav is SAV3 || sav is SAV4 || sav is SAV5;
SL_Extra.Initialize(sav.GetExtraSlots(HaX), InitializeDragDrop);
}
GB_SAVtools.Visible = sav.Exportable && FLP_SAVtools.Controls.Cast<Control>().Any(c => c.Enabled);
GB_SAVtools.Visible = sav.Exportable && FLP_SAVtools.Controls.OfType<Control>().Any(c => c.Enabled);
foreach (Control c in FLP_SAVtools.Controls.Cast<Control>())
c.Visible = c.Enabled;
}
@ -1163,7 +1160,7 @@ namespace PKHeX.WinForms.Controls
}
private void B_FestivalPlaza_Click(object sender, EventArgs e)
{
if (SAV.Generation == 7)
if (SAV is SAV7)
new SAV_FestivalPlaza(SAV).ShowDialog();
}
private void B_MailBox_Click(object sender, EventArgs e)

View file

@ -21,7 +21,7 @@ namespace PKHeX.WinForms
InitializeComponent();
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
SAV = sav;
alolanOnly = SAV.Generation == 7 && DialogResult.Yes == WinFormsUtil.Prompt(MessageBoxButtons.YesNo, "Alolan Dex only?");
alolanOnly = SAV is SAV7 && DialogResult.Yes == WinFormsUtil.Prompt(MessageBoxButtons.YesNo, "Alolan Dex only?");
Array.Resize(ref species, SAV.Personal.TableLength);

View file

@ -141,7 +141,7 @@ namespace PKHeX.WinForms
private void B_GetTickets_Click(object sender, EventArgs e)
{
var Pouches = SAV.Inventory;
string[] itemlist = GameInfo.Strings.GetItemStrings(SAV.Generation, SAV.Version);
var itemlist = GameInfo.Strings.GetItemStrings(SAV.Generation, SAV.Version).ToArray();
for (int i = 0; i < itemlist.Length; i++)
if (string.IsNullOrEmpty(itemlist[i]))
itemlist[i] = $"(Item #{i:000})";

View file

@ -103,7 +103,7 @@ namespace PKHeX.WinForms
NUD_Coin.Value = SAV.Coin;
NUD_Coin.Maximum = SAV.MaxCoins;
int[] FlyDestD;
List<ComboItem> metLocationList;
IReadOnlyList<ComboItem> metLocationList;
switch (SAV.Version)
{
case GameVersion.D:

View file

@ -259,7 +259,7 @@ namespace PKHeX.WinForms
private void LoadMapFlyToData()
{
List<ComboItem> metLocationList = GameInfo.GetLocationList(GameVersion.US, 7, false);
IReadOnlyList<ComboItem> metLocationList = GameInfo.GetLocationList(GameVersion.US, 7, false);
int[] FlyDestNameIndex = {
-1,24,34,8,20,38,12,46,40,30,//Melemele
70,68,78,86,74,104,82,58,90,72,76,92,62,//Akala

View file

@ -16,7 +16,7 @@ namespace PKHeX.WinForms
InitializeComponent();
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
SAV = (Origin = sav).Clone();
itemlist = GameInfo.Strings.GetItemStrings(SAV.Generation, SAV.Version);
itemlist = GameInfo.Strings.GetItemStrings(SAV.Generation, SAV.Version).ToArray();
for (int i = 0; i < itemlist.Length; i++)
if (itemlist[i]?.Length == 0)

View file

@ -148,7 +148,7 @@ namespace PKHeX.WinForms
}.ToList(), null);
}
string[] ItemList = GameInfo.Strings.GetItemStrings(Gen, SAV.Version);
var ItemList = GameInfo.Strings.GetItemStrings(Gen, SAV.Version);
CB_MailType.Items.Clear();
CB_MailType.Items.Add(ItemList[0]);
foreach (int item in MailItemID)