From cbf4038a950628bfc9ea5905ca4543da192aebd0 Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 5 Mar 2018 22:19:56 -0800 Subject: [PATCH] Refactoring relocate gift data storage out of legal.cs --- PKHeX.Core/Legality/Core.cs | 107 +----------------- .../Encounters/Data/EncounterEvent.cs | 92 +++++++++++++++ .../Generator/EncounterGenerator.cs | 1 + PKHeX.Core/Legality/Learnset/Learnset.cs | 78 ------------- PKHeX.Core/Legality/Learnset/Learnset1.cs | 33 ++++++ PKHeX.Core/Legality/Learnset/Learnset6.cs | 29 +++++ PKHeX.WinForms/MainWindow/Main.cs | 6 +- PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs | 9 +- 8 files changed, 160 insertions(+), 195 deletions(-) create mode 100644 PKHeX.Core/Legality/Encounters/Data/EncounterEvent.cs create mode 100644 PKHeX.Core/Legality/Learnset/Learnset1.cs create mode 100644 PKHeX.Core/Legality/Learnset/Learnset6.cs diff --git a/PKHeX.Core/Legality/Core.cs b/PKHeX.Core/Legality/Core.cs index cadeef957..2b6ea59dc 100644 --- a/PKHeX.Core/Legality/Core.cs +++ b/PKHeX.Core/Legality/Core.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using static PKHeX.Core.Encounters1; using static PKHeX.Core.Encounters2; @@ -9,28 +8,12 @@ using static PKHeX.Core.Encounters5; using static PKHeX.Core.Encounters6; using static PKHeX.Core.Encounters7; -using static PKHeX.Core.EncountersWC3; using static PKHeX.Core.LegalityCheckStrings; namespace PKHeX.Core { public static partial class Legal { - /// Event Database for Generation 3 - public static MysteryGift[] MGDB_G3 { get; private set; } = new MysteryGift[0]; - - /// Event Database for Generation 4 - public static MysteryGift[] MGDB_G4 { get; private set; } = new MysteryGift[0]; - - /// Event Database for Generation 5 - public static MysteryGift[] MGDB_G5 { get; private set; } = new MysteryGift[0]; - - /// Event Database for Generation 6 - public static MysteryGift[] MGDB_G6 { get; private set; } = new MysteryGift[0]; - - /// Event Database for Generation 7 - public static MysteryGift[] MGDB_G7 { get; private set; } = new MysteryGift[0]; - /// Setting to specify if an analysis should permit data sourced from the physical cartridge era of GameBoy games. public static bool AllowGBCartEra { get; set; } public static bool AllowGen1Tradeback { get; set; } @@ -93,9 +76,9 @@ public static partial class Legal // Gen 7 private static readonly EggMoves[] EggMovesSM = EggMoves7.GetArray(Data.UnpackMini(Util.GetBinaryResource("eggmove_sm.pkl"), "sm")); - private static readonly Learnset[] LevelUpSM = Learnset7.GetArray(Data.UnpackMini(Util.GetBinaryResource("lvlmove_sm.pkl"), "sm")); + private static readonly Learnset[] LevelUpSM = Learnset6.GetArray(Data.UnpackMini(Util.GetBinaryResource("lvlmove_sm.pkl"), "sm")); private static readonly EggMoves[] EggMovesUSUM = EggMoves7.GetArray(Data.UnpackMini(Util.GetBinaryResource("eggmove_uu.pkl"), "uu")); - private static readonly Learnset[] LevelUpUSUM = Learnset7.GetArray(Data.UnpackMini(Util.GetBinaryResource("lvlmove_uu.pkl"), "uu")); + private static readonly Learnset[] LevelUpUSUM = Learnset6.GetArray(Data.UnpackMini(Util.GetBinaryResource("lvlmove_uu.pkl"), "uu")); // Setup Help static Legal() @@ -103,92 +86,8 @@ static Legal() // Misc Fixes to Data pertaining to legality constraints EggMovesUSUM[198].Moves = EggMovesUSUM[198].Moves.Take(15).ToArray(); // Remove Punishment from USUM Murkrow (no species can pass it #1829) } - private static HashSet GetPCDDB(byte[] bin) - { - var db = new HashSet(); - for (int i = 0; i < bin.Length; i += PCD.Size) - { - byte[] data = new byte[PCD.Size]; - Buffer.BlockCopy(bin, i, data, 0, PCD.Size); - db.Add(new PCD(data)); - } - return db; - } - private static HashSet GetPGFDB(byte[] bin) - { - var db = new HashSet(); - for (int i = 0; i < bin.Length; i += PGF.Size) - { - byte[] data = new byte[PGF.Size]; - Buffer.BlockCopy(bin, i, data, 0, PGF.Size); - db.Add(new PGF(data)); - } - return db; - } - private static HashSet GetWC6DB(byte[] wc6bin, byte[] wc6full) - { - var db = new HashSet(); - for (int i = 0; i < wc6bin.Length; i += WC6.Size) - { - byte[] data = new byte[WC6.Size]; - Buffer.BlockCopy(wc6bin, i, data, 0, WC6.Size); - db.Add(new WC6(data)); - } - for (int i = 0; i < wc6full.Length; i += WC6.SizeFull) - { - byte[] data = new byte[WC6.SizeFull]; - Buffer.BlockCopy(wc6full, i, data, 0, WC6.SizeFull); - db.Add(new WC6(data)); - } - return db; - } - private static HashSet GetWC7DB(byte[] wc7bin, byte[] wc7full) - { - var db = new HashSet(); - for (int i = 0; i < wc7bin.Length; i += WC7.Size) - { - byte[] data = new byte[WC7.Size]; - Buffer.BlockCopy(wc7bin, i, data, 0, WC7.Size); - db.Add(new WC7(data)); - } - for (int i = 0; i < wc7full.Length; i += WC7.SizeFull) - { - byte[] data = new byte[WC7.SizeFull]; - Buffer.BlockCopy(wc7full, i, data, 0, WC7.SizeFull); - db.Add(new WC7(data)); - } - return db; - } - public static void RefreshMGDB(string localDbPath) - { - var g4 = GetPCDDB(Util.GetBinaryResource("wc4.pkl")); - var g5 = GetPGFDB(Util.GetBinaryResource("pgf.pkl")); - var g6 = GetWC6DB(Util.GetBinaryResource("wc6.pkl"), Util.GetBinaryResource("wc6full.pkl")); - var g7 = GetWC7DB(Util.GetBinaryResource("wc7.pkl"), Util.GetBinaryResource("wc7full.pkl")); - if (Directory.Exists(localDbPath)) - foreach (var file in Directory.EnumerateFiles(localDbPath, "*", SearchOption.AllDirectories)) - { - var fi = new FileInfo(file); - if (!MysteryGift.IsMysteryGift(fi.Length)) - continue; - - var gift = MysteryGift.GetMysteryGift(File.ReadAllBytes(file), fi.Extension); - switch (gift?.Format) - { - case 4: g4.Add(gift); continue; - case 5: g5.Add(gift); continue; - case 6: g6.Add(gift); continue; - case 7: g7.Add(gift); continue; - } - } - - MGDB_G3 = Encounter_WC3; // hardcoded - MGDB_G4 = g4.ToArray(); - MGDB_G5 = g5.ToArray(); - MGDB_G6 = g6.ToArray(); - MGDB_G7 = g7.ToArray(); - } + public static void RefreshMGDB(string localDbPath) => EncounterEvent.RefreshMGDB(localDbPath); // Moves internal static int[] GetMinLevelLearnMove(int species, int Generation, List moves) diff --git a/PKHeX.Core/Legality/Encounters/Data/EncounterEvent.cs b/PKHeX.Core/Legality/Encounters/Data/EncounterEvent.cs new file mode 100644 index 000000000..f2cbdf1bb --- /dev/null +++ b/PKHeX.Core/Legality/Encounters/Data/EncounterEvent.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +using static PKHeX.Core.EncountersWC3; + +namespace PKHeX.Core +{ + public static class EncounterEvent + { + /// Event Database for Generation 3 + public static MysteryGift[] MGDB_G3 { get; private set; } = new MysteryGift[0]; + + /// Event Database for Generation 4 + public static MysteryGift[] MGDB_G4 { get; private set; } = new MysteryGift[0]; + + /// Event Database for Generation 5 + public static MysteryGift[] MGDB_G5 { get; private set; } = new MysteryGift[0]; + + /// Event Database for Generation 6 + public static MysteryGift[] MGDB_G6 { get; private set; } = new MysteryGift[0]; + + /// Event Database for Generation 7 + public static MysteryGift[] MGDB_G7 { get; private set; } = new MysteryGift[0]; + + private static IEnumerable GetData(byte[] bin, int size) + { + for (int i = 0; i < bin.Length; i += size) + { + byte[] data = new byte[size]; + Buffer.BlockCopy(bin, i, data, 0, size); + yield return data; + } + } + private static HashSet GetPCDDB(byte[] bin) => new HashSet(GetData(bin, PCD.Size).Select(d => new PCD(d))); + private static HashSet GetPGFDB(byte[] bin) => new HashSet(GetData(bin, PGF.Size).Select(d => new PGF(d))); + private static HashSet GetWC6DB(byte[] wc6bin, byte[] wc6full) => new HashSet( + GetData(wc6bin, WC6.Size).Select(d => new WC6(d)).Concat( + GetData(wc6full, WC6.SizeFull).Select(d => new WC6(d)))); + private static HashSet GetWC7DB(byte[] wc7bin, byte[] wc7full) => new HashSet( + GetData(wc7bin, WC7.Size).Select(d => new WC7(d)).Concat( + GetData(wc7full, WC7.SizeFull).Select(d => new WC7(d)))); + + public static void RefreshMGDB(params string[] paths) + { + var g4 = GetPCDDB(Util.GetBinaryResource("wc4.pkl")); + var g5 = GetPGFDB(Util.GetBinaryResource("pgf.pkl")); + var g6 = GetWC6DB(Util.GetBinaryResource("wc6.pkl"), Util.GetBinaryResource("wc6full.pkl")); + var g7 = GetWC7DB(Util.GetBinaryResource("wc7.pkl"), Util.GetBinaryResource("wc7full.pkl")); + + foreach (var path in paths.Where(Directory.Exists)) + foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)) + { + var fi = new FileInfo(file); + if (!MysteryGift.IsMysteryGift(fi.Length)) + continue; + + var gift = MysteryGift.GetMysteryGift(File.ReadAllBytes(file), fi.Extension); + switch (gift?.Format) + { + case 4: g4.Add(gift); continue; + case 5: g5.Add(gift); continue; + case 6: g6.Add(gift); continue; + case 7: g7.Add(gift); continue; + } + } + + MGDB_G3 = Encounter_WC3; // hardcoded + MGDB_G4 = g4.ToArray(); + MGDB_G5 = g5.ToArray(); + MGDB_G6 = g6.ToArray(); + MGDB_G7 = g7.ToArray(); + } + + public static IEnumerable GetAllEvents(bool sorted = true) + { + var regular = new[] + { + MGDB_G4, + MGDB_G5, + MGDB_G6, + MGDB_G7, + }.SelectMany(z => z); + regular = regular.Where(mg => !mg.IsItem && mg.IsPokémon && mg.Species > 0); + var result = MGDB_G3.Concat(regular.Distinct()); + if (sorted) + result = result.OrderBy(mg => mg.Species); + return result; + } + } +} diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs index f457878d5..01bac91ee 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using static PKHeX.Core.Legal; +using static PKHeX.Core.EncounterEvent; namespace PKHeX.Core { diff --git a/PKHeX.Core/Legality/Learnset/Learnset.cs b/PKHeX.Core/Legality/Learnset/Learnset.cs index 496acf4cd..a603e11c5 100644 --- a/PKHeX.Core/Legality/Learnset/Learnset.cs +++ b/PKHeX.Core/Legality/Learnset/Learnset.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; namespace PKHeX.Core { @@ -76,81 +75,4 @@ public int GetLevelLearnMove(int move) return index < 0 ? 0 : Levels[index]; } } - - public class Learnset1 : Learnset - { - private Learnset1(byte[] data, ref int offset) - { - var moves = new List(); - var levels = new List(); - while (data[offset] != 0) - { - levels.Add(data[offset++]); - moves.Add(data[offset++]); - } - ++offset; - - Moves = moves.ToArray(); - Levels = levels.ToArray(); - Count = Moves.Length; - } - public static Learnset[] GetArray(byte[] input, int maxSpecies) - { - var data = new Learnset[maxSpecies + 1]; - - int offset = 0; - for (int s = 0; s < data.Length; s++) - data[s] = new Learnset1(input, ref offset); - - return data; - } - } - public class Learnset6 : Learnset - { - private Learnset6(byte[] data) - { - if (data.Length < 4 || data.Length % 4 != 0) - { Count = 0; Levels = new int[0]; Moves = new int[0]; return; } - Count = data.Length / 4 - 1; - Moves = new int[Count]; - Levels = new int[Count]; - using (BinaryReader br = new BinaryReader(new MemoryStream(data))) - for (int i = 0; i < Count; i++) - { - Moves[i] = br.ReadInt16(); - Levels[i] = br.ReadInt16(); - } - } - public static Learnset[] GetArray(byte[][] entries) - { - Learnset[] data = new Learnset[entries.Length]; - for (int i = 0; i < data.Length; i++) - data[i] = new Learnset6(entries[i]); - return data; - } - } - public class Learnset7 : Learnset - { - private Learnset7(byte[] data) - { - if (data.Length < 4 || data.Length % 4 != 0) - { Count = 0; Levels = new int[0]; Moves = new int[0]; return; } - Count = data.Length / 4 - 1; - Moves = new int[Count]; - Levels = new int[Count]; - using (BinaryReader br = new BinaryReader(new MemoryStream(data))) - for (int i = 0; i < Count; i++) - { - Moves[i] = br.ReadInt16(); - Levels[i] = br.ReadInt16(); - } - } - public static Learnset[] GetArray(byte[][] entries) - { - Learnset[] data = new Learnset[entries.Length]; - for (int i = 0; i < data.Length; i++) - data[i] = new Learnset7(entries[i]); - return data; - } - } } diff --git a/PKHeX.Core/Legality/Learnset/Learnset1.cs b/PKHeX.Core/Legality/Learnset/Learnset1.cs new file mode 100644 index 000000000..baadf63d2 --- /dev/null +++ b/PKHeX.Core/Legality/Learnset/Learnset1.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; + +namespace PKHeX.Core +{ + public class Learnset1 : Learnset + { + private Learnset1(byte[] data, ref int offset) + { + var moves = new List(); + var levels = new List(); + while (data[offset] != 0) + { + levels.Add(data[offset++]); + moves.Add(data[offset++]); + } + ++offset; + + Moves = moves.ToArray(); + Levels = levels.ToArray(); + Count = Moves.Length; + } + public static Learnset[] GetArray(byte[] input, int maxSpecies) + { + var data = new Learnset[maxSpecies + 1]; + + int offset = 0; + for (int s = 0; s < data.Length; s++) + data[s] = new Learnset1(input, ref offset); + + return data; + } + } +} \ No newline at end of file diff --git a/PKHeX.Core/Legality/Learnset/Learnset6.cs b/PKHeX.Core/Legality/Learnset/Learnset6.cs new file mode 100644 index 000000000..531a77fc1 --- /dev/null +++ b/PKHeX.Core/Legality/Learnset/Learnset6.cs @@ -0,0 +1,29 @@ +using System.IO; + +namespace PKHeX.Core +{ + public class Learnset6 : Learnset + { + private Learnset6(byte[] data) + { + if (data.Length < 4 || data.Length % 4 != 0) + { Count = 0; Levels = new int[0]; Moves = new int[0]; return; } + Count = data.Length / 4 - 1; + Moves = new int[Count]; + Levels = new int[Count]; + using (BinaryReader br = new BinaryReader(new MemoryStream(data))) + for (int i = 0; i < Count; i++) + { + Moves[i] = br.ReadInt16(); + Levels[i] = br.ReadInt16(); + } + } + public static Learnset[] GetArray(byte[][] entries) + { + Learnset[] data = new Learnset[entries.Length]; + for (int i = 0; i < data.Length; i++) + data[i] = new Learnset6(entries[i]); + return data; + } + } +} \ No newline at end of file diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 8f7d1f325..e505b0012 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -22,7 +22,7 @@ public partial class Main : Form public Main() { new Task(() => new SplashScreen().ShowDialog()).Start(); - new Task(RefreshMGDB).Start(); + new Task(() => Legal.RefreshMGDB(MGDatabasePath)).Start(); InitializeComponent(); FormLoadCheckForUpdates(); @@ -731,10 +731,6 @@ private static PKM LoadTemplate(SaveFile sav) var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(path), prefer: blank.Format); return PKMConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out path); // no sneaky plz; reuse string } - private static void RefreshMGDB() - { - Legal.RefreshMGDB(MGDatabasePath); - } private void OpenSAV(SaveFile sav, string path) { diff --git a/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs b/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs index 7166e1654..48d7d3469 100644 --- a/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs +++ b/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs @@ -203,14 +203,7 @@ private void ResetFilters(object sender, EventArgs e) } private void LoadDatabase() { - RawDB = new List(); - RawDB.AddRange(Legal.MGDB_G4); - RawDB.AddRange(Legal.MGDB_G5); - RawDB.AddRange(Legal.MGDB_G6); - RawDB.AddRange(Legal.MGDB_G7); - - RawDB = new List(RawDB.Where(mg => !mg.IsItem && mg.IsPokémon && mg.Species > 0).Distinct() - .Concat(Legal.MGDB_G3).OrderBy(mg => mg.Species)); + RawDB = new List(EncounterEvent.GetAllEvents()); foreach (var mg in RawDB) mg.GiftUsed = false; BeginInvoke(new MethodInvoker(delegate