Refactoring

relocate gift data storage out of legal.cs
This commit is contained in:
Kurt
2018-03-05 22:19:56 -08:00
parent 21cdf4f642
commit cbf4038a95
8 changed files with 160 additions and 195 deletions

View File

@@ -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
{
/// <summary>Event Database for Generation 3</summary>
public static MysteryGift[] MGDB_G3 { get; private set; } = new MysteryGift[0];
/// <summary>Event Database for Generation 4</summary>
public static MysteryGift[] MGDB_G4 { get; private set; } = new MysteryGift[0];
/// <summary>Event Database for Generation 5</summary>
public static MysteryGift[] MGDB_G5 { get; private set; } = new MysteryGift[0];
/// <summary>Event Database for Generation 6</summary>
public static MysteryGift[] MGDB_G6 { get; private set; } = new MysteryGift[0];
/// <summary>Event Database for Generation 7</summary>
public static MysteryGift[] MGDB_G7 { get; private set; } = new MysteryGift[0];
/// <summary>Setting to specify if an analysis should permit data sourced from the physical cartridge era of GameBoy games.</summary>
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<MysteryGift> GetPCDDB(byte[] bin)
{
var db = new HashSet<MysteryGift>();
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<MysteryGift> GetPGFDB(byte[] bin)
{
var db = new HashSet<MysteryGift>();
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<MysteryGift> GetWC6DB(byte[] wc6bin, byte[] wc6full)
{
var db = new HashSet<MysteryGift>();
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<MysteryGift> GetWC7DB(byte[] wc7bin, byte[] wc7full)
{
var db = new HashSet<MysteryGift>();
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<int> moves)

View File

@@ -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
{
/// <summary>Event Database for Generation 3</summary>
public static MysteryGift[] MGDB_G3 { get; private set; } = new MysteryGift[0];
/// <summary>Event Database for Generation 4</summary>
public static MysteryGift[] MGDB_G4 { get; private set; } = new MysteryGift[0];
/// <summary>Event Database for Generation 5</summary>
public static MysteryGift[] MGDB_G5 { get; private set; } = new MysteryGift[0];
/// <summary>Event Database for Generation 6</summary>
public static MysteryGift[] MGDB_G6 { get; private set; } = new MysteryGift[0];
/// <summary>Event Database for Generation 7</summary>
public static MysteryGift[] MGDB_G7 { get; private set; } = new MysteryGift[0];
private static IEnumerable<byte[]> 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<MysteryGift> GetPCDDB(byte[] bin) => new HashSet<MysteryGift>(GetData(bin, PCD.Size).Select(d => new PCD(d)));
private static HashSet<MysteryGift> GetPGFDB(byte[] bin) => new HashSet<MysteryGift>(GetData(bin, PGF.Size).Select(d => new PGF(d)));
private static HashSet<MysteryGift> GetWC6DB(byte[] wc6bin, byte[] wc6full) => new HashSet<MysteryGift>(
GetData(wc6bin, WC6.Size).Select(d => new WC6(d)).Concat(
GetData(wc6full, WC6.SizeFull).Select(d => new WC6(d))));
private static HashSet<MysteryGift> GetWC7DB(byte[] wc7bin, byte[] wc7full) => new HashSet<MysteryGift>(
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<MysteryGift> 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;
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using static PKHeX.Core.Legal;
using static PKHeX.Core.EncounterEvent;
namespace PKHeX.Core
{

View File

@@ -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<int>();
var levels = new List<int>();
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;
}
}
}

View File

@@ -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<int>();
var levels = new List<int>();
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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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)
{

View File

@@ -203,14 +203,7 @@ private void ResetFilters(object sender, EventArgs e)
}
private void LoadDatabase()
{
RawDB = new List<MysteryGift>();
RawDB.AddRange(Legal.MGDB_G4);
RawDB.AddRange(Legal.MGDB_G5);
RawDB.AddRange(Legal.MGDB_G6);
RawDB.AddRange(Legal.MGDB_G7);
RawDB = new List<MysteryGift>(RawDB.Where(mg => !mg.IsItem && mg.IsPokémon && mg.Species > 0).Distinct()
.Concat(Legal.MGDB_G3).OrderBy(mg => mg.Species));
RawDB = new List<MysteryGift>(EncounterEvent.GetAllEvents());
foreach (var mg in RawDB)
mg.GiftUsed = false;
BeginInvoke(new MethodInvoker(delegate