From 6970c3a5fb4e0eb4df14cc939d362258f054d3bd Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 19 May 2020 21:46:05 -0700 Subject: [PATCH] Remove IVersion.set property, as separate interface I don't want people to mutate IEncounterable.Version; it's only intended to be set on-load --- .../Legality/Encounters/Data/EncounterUtil.cs | 6 +++--- .../Encounters/EncounterMisc/EncounterEgg.cs | 7 ++++--- .../Encounters/EncounterMisc/EncounterInvalid.cs | 4 ++-- .../Encounters/EncounterMisc/EncounterRejected.cs | 7 +------ .../Encounters/EncounterSlot/EncounterSlot.cs | 2 +- .../Encounters/EncounterStatic/EncounterStatic.cs | 2 +- .../Encounters/EncounterTrade/EncounterTrade.cs | 2 +- .../Encounters/Generator/EncounterEggGenerator.cs | 8 ++++---- .../Encounters/Generator/EncounterGenerator.cs | 4 ++-- PKHeX.Core/Legality/Structures/IVersion.cs | 13 +++++++++---- 10 files changed, 28 insertions(+), 27 deletions(-) diff --git a/PKHeX.Core/Legality/Encounters/Data/EncounterUtil.cs b/PKHeX.Core/Legality/Encounters/Data/EncounterUtil.cs index f8bd9add0..a6773183e 100644 --- a/PKHeX.Core/Legality/Encounters/Data/EncounterUtil.cs +++ b/PKHeX.Core/Legality/Encounters/Data/EncounterUtil.cs @@ -264,10 +264,10 @@ internal static void MarkEncounterTradeStrings(EncounterTrade[] table, string[][ string[] getNames(int i, IEnumerable names) => names.Select(z => z.Length > i ? z[i] : string.Empty).ToArray(); } - internal static void MarkEncounterGame(IEnumerable table, GameVersion version) + internal static void MarkEncounterGame(IEnumerable table, GameVersion version) where T: IVersion, IVersionSet { - foreach (var t in table.Where(z => z.Version == GameVersion.Any)) - t.Version = version; + foreach (var t in table.Where(z => ((IVersion)z).Version == GameVersion.Any)) + ((IVersionSet)t).Version = version; } } } diff --git a/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterEgg.cs b/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterEgg.cs index 10730244e..8af0aace9 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterEgg.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterEgg.cs @@ -17,16 +17,17 @@ public class EncounterEgg : IEncounterable public int LevelMax => Level; public readonly int Level; public int Generation { get; } + public GameVersion Version { get; } - public EncounterEgg(int species, int form, int level, int gen) + public EncounterEgg(int species, int form, int level, int gen, GameVersion game) { Species = species; Form = form; Level = level; Generation = gen; + Version = game; } - public GameVersion Version { get; set; } public PKM ConvertToPKM(ITrainerInfo SAV) => ConvertToPKM(SAV, EncounterCriteria.Unrestricted); @@ -149,6 +150,6 @@ private int[] GetCurrentEggMoves(PKM pk, GameVersion version) public sealed class EncounterEggSplit : EncounterEgg { public int OtherSpecies { get; } - public EncounterEggSplit(int species, int form, int level, int gen, int otherSpecies) : base(species, form, level, gen) => OtherSpecies = otherSpecies; + public EncounterEggSplit(int species, int form, int level, int gen, GameVersion game, int otherSpecies) : base(species, form, level, gen, game) => OtherSpecies = otherSpecies; } } diff --git a/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterInvalid.cs b/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterInvalid.cs index 0c83c2897..e91125fe7 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterInvalid.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterInvalid.cs @@ -14,8 +14,8 @@ public sealed class EncounterInvalid : IEncounterable public int LevelMin { get; } public int LevelMax { get; } public bool EggEncounter { get; } - public int Generation { get; set; } - public GameVersion Version { get; set; } + public int Generation { get; } + public GameVersion Version { get; } public string Name => "Invalid"; public string LongName => "Invalid"; diff --git a/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterRejected.cs b/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterRejected.cs index 810c7c46d..b2a405355 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterRejected.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterMisc/EncounterRejected.cs @@ -19,12 +19,7 @@ public sealed class EncounterRejected : IEncounterable public int LevelMin => Encounter.LevelMin; public int LevelMax => Encounter.LevelMax; public int Generation => Encounter.Generation; - - public GameVersion Version - { - get => Encounter.Version; - set => Encounter.Version = value; - } + public GameVersion Version => Encounter.Version; public EncounterRejected(IEncounterable encounter, CheckResult check) { diff --git a/PKHeX.Core/Legality/Encounters/EncounterSlot/EncounterSlot.cs b/PKHeX.Core/Legality/Encounters/EncounterSlot/EncounterSlot.cs index c954a8270..2b21f641f 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterSlot/EncounterSlot.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterSlot/EncounterSlot.cs @@ -5,7 +5,7 @@ namespace PKHeX.Core /// /// Wild Encounter Slot data /// - public class EncounterSlot : IEncounterable, IGenerationSet, ILocation + public class EncounterSlot : IEncounterable, IGenerationSet, ILocation, IVersionSet { public int Species { get; set; } public int Form { get; set; } diff --git a/PKHeX.Core/Legality/Encounters/EncounterStatic/EncounterStatic.cs b/PKHeX.Core/Legality/Encounters/EncounterStatic/EncounterStatic.cs index 90c8e8b7f..4ef7b5424 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterStatic/EncounterStatic.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterStatic/EncounterStatic.cs @@ -10,7 +10,7 @@ namespace PKHeX.Core /// /// Static Encounters are fixed position encounters with properties that are not subject to Wild Encounter conditions. /// - public class EncounterStatic : IEncounterable, IMoveset, IGenerationSet, ILocation, IContestStats, IRelearn + public class EncounterStatic : IEncounterable, IMoveset, IGenerationSet, ILocation, IContestStats, IRelearn, IVersionSet { public int Species { get; set; } public IReadOnlyList Moves { get; set; } = Array.Empty(); diff --git a/PKHeX.Core/Legality/Encounters/EncounterTrade/EncounterTrade.cs b/PKHeX.Core/Legality/Encounters/EncounterTrade/EncounterTrade.cs index 4b3704634..6bb2ba56e 100644 --- a/PKHeX.Core/Legality/Encounters/EncounterTrade/EncounterTrade.cs +++ b/PKHeX.Core/Legality/Encounters/EncounterTrade/EncounterTrade.cs @@ -10,7 +10,7 @@ namespace PKHeX.Core /// /// Trade data is fixed level in all cases except for the first few generations of games. /// - public class EncounterTrade : IEncounterable, IGenerationSet, IMoveset, ILocation, IContestStats + public class EncounterTrade : IEncounterable, IGenerationSet, IMoveset, ILocation, IContestStats, IVersionSet { public int Species { get; set; } public IReadOnlyList Moves { get; set; } = Array.Empty(); diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterEggGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterEggGenerator.cs index 8d01bc764..0e979d7a7 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterEggGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterEggGenerator.cs @@ -36,9 +36,9 @@ public static IEnumerable GenerateEggs(PKM pkm, IReadOnlyList 5 && (pkm.WasTradedEgg || all) && HasOtherGamePair(ver)) - yield return new EncounterEgg(e.Species, e.Form, lvl, gen) { Version = GetOtherTradePair(ver) }; + yield return new EncounterEgg(e.Species, e.Form, lvl, gen, GetOtherTradePair(ver)); } if (!GetSplitBreedGeneration(gen).Contains(species)) @@ -47,9 +47,9 @@ public static IEnumerable GenerateEggs(PKM pkm, IReadOnlyList 5 && (pkm.WasTradedEgg || all) && HasOtherGamePair(ver)) - yield return new EncounterEggSplit(o.Species, o.Form, lvl, gen, e.Species) { Version = GetOtherTradePair(ver) }; + yield return new EncounterEggSplit(o.Species, o.Form, lvl, gen, GetOtherTradePair(ver), e.Species); } } diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs index 847a15704..230b217e3 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs @@ -191,8 +191,8 @@ private static IEnumerable GenerateRawEncounters12(PKM pkm, Game { int eggspec = GetBaseEggSpecies(pkm).Species; if (ParseSettings.AllowGen2Crystal(pkm)) - yield return new EncounterEgg(eggspec, 0, 5, 2) { Version = GameVersion.C }; // gen2 egg - yield return new EncounterEgg(eggspec, 0, 5, 2) { Version = GameVersion.GS }; // gen2 egg + yield return new EncounterEgg(eggspec, 0, 5, 2, GameVersion.C); // gen2 egg + yield return new EncounterEgg(eggspec, 0, 5, 2, GameVersion.GS); // gen2 egg } } diff --git a/PKHeX.Core/Legality/Structures/IVersion.cs b/PKHeX.Core/Legality/Structures/IVersion.cs index cc419a848..7654d29ce 100644 --- a/PKHeX.Core/Legality/Structures/IVersion.cs +++ b/PKHeX.Core/Legality/Structures/IVersion.cs @@ -4,7 +4,12 @@ namespace PKHeX.Core { public interface IVersion { - GameVersion Version { get; set; } + GameVersion Version { get; } + } + + internal interface IVersionSet + { + GameVersion Version { set; } } public static partial class Extensions @@ -18,12 +23,12 @@ public static GameVersion GetCompatibleVersion(this IVersion ver, GameVersion pr return ver.GetSingleVersion(); } - internal static void SetVersion(this IEnumerable arr, GameVersion game) + internal static void SetVersion(this IEnumerable arr, GameVersion game) where T : IVersion, IVersionSet { foreach (var z in arr) { - if (z.Version <= 0) - z.Version = game; + if (((IVersion)z).Version <= 0) + ((IVersionSet)z).Version = game; } }