mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-05-05 21:17:14 -05:00
Database improvements
Reduce linq usage (toarray) and just use the actual index Implement GetHashCode for Mystery Gifts to only add unique data arrays #1071
This commit is contained in:
parent
ceaf7547ad
commit
757cf11f52
|
|
@ -145,10 +145,7 @@ public Main()
|
|||
};
|
||||
|
||||
// Load Event Databases
|
||||
refreshPCDDB();
|
||||
refreshPGFDB();
|
||||
refreshWC6DB();
|
||||
refreshWC7DB();
|
||||
refreshMGDB();
|
||||
|
||||
#endregion
|
||||
#region Localize & Populate Fields
|
||||
|
|
@ -1363,99 +1360,89 @@ private void openSAV(SaveFile sav, string path)
|
|||
SystemSounds.Beep.Play();
|
||||
}
|
||||
|
||||
private static void refreshPCDDB()
|
||||
private static void refreshMGDB()
|
||||
{
|
||||
List<MysteryGift> db = new List<MysteryGift>();
|
||||
byte[] bin = Resources.pcd;
|
||||
var g4 = getPCDDB(Resources.pcd);
|
||||
var g5 = getPGFDB(Resources.pgf);
|
||||
var g6 = getWC6DB(Resources.wc6, Resources.wc6full);
|
||||
var g7 = getWC7DB(Resources.wc7, Resources.wc7full);
|
||||
|
||||
foreach (var file in Directory.GetFiles(MGDatabasePath, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
var fi = new FileInfo(file);
|
||||
if (!MysteryGift.getIsMysteryGift(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;
|
||||
}
|
||||
}
|
||||
|
||||
Legal.MGDB_G4 = g4.ToArray();
|
||||
Legal.MGDB_G5 = g5.ToArray();
|
||||
Legal.MGDB_G6 = g6.ToArray();
|
||||
Legal.MGDB_G7 = g7.ToArray();
|
||||
}
|
||||
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));
|
||||
}
|
||||
if (Directory.Exists(MGDatabasePath))
|
||||
{
|
||||
foreach (var file in Directory.GetFiles(MGDatabasePath, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
var fi = new FileInfo(file);
|
||||
if (fi.Length == PCD.Size && fi.Extension == ".pcd")
|
||||
db.Add(new PCD(File.ReadAllBytes(file)));
|
||||
else if (fi.Length == PGT.Size && fi.Extension == ".pgt")
|
||||
db.Add(new PCD {Gift = new PGT(File.ReadAllBytes(file)), CardTitle = "MGDB PGT"});
|
||||
}
|
||||
}
|
||||
|
||||
Legal.MGDB_G4 = db.Distinct().ToArray();
|
||||
return db;
|
||||
}
|
||||
private static void refreshPGFDB()
|
||||
private static HashSet<MysteryGift> getPGFDB(byte[] bin)
|
||||
{
|
||||
List<MysteryGift> db = new List<MysteryGift>();
|
||||
byte[] bin = Resources.pgf;
|
||||
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));
|
||||
}
|
||||
if (Directory.Exists(MGDatabasePath))
|
||||
db.AddRange(from file in Directory.GetFiles(MGDatabasePath, "*", SearchOption.AllDirectories)
|
||||
let fi = new FileInfo(file)
|
||||
where ".pgf" == fi.Extension && PGF.Size == fi.Length
|
||||
select new PGF(File.ReadAllBytes(file)));
|
||||
|
||||
Legal.MGDB_G5 = db.Distinct().ToArray();
|
||||
return db;
|
||||
}
|
||||
private static void refreshWC6DB()
|
||||
private static HashSet<MysteryGift> getWC6DB(byte[] wc6bin, byte[] wc6full)
|
||||
{
|
||||
List<MysteryGift> wc6db = new List<MysteryGift>();
|
||||
byte[] wc6bin = Resources.wc6;
|
||||
var db = new HashSet<MysteryGift>();
|
||||
for (int i = 0; i < wc6bin.Length; i += WC6.Size)
|
||||
{
|
||||
byte[] data = new byte[WC6.Size];
|
||||
Array.Copy(wc6bin, i, data, 0, WC6.Size);
|
||||
wc6db.Add(new WC6(data));
|
||||
Buffer.BlockCopy(wc6bin, i, data, 0, WC6.Size);
|
||||
db.Add(new WC6(data));
|
||||
}
|
||||
byte[] wc6full = Resources.wc6full;
|
||||
for (int i = 0; i < wc6full.Length; i += WC6.SizeFull)
|
||||
{
|
||||
byte[] data = new byte[WC6.SizeFull];
|
||||
Array.Copy(wc6full, i, data, 0, WC6.SizeFull);
|
||||
wc6db.Add(new WC6(data));
|
||||
Buffer.BlockCopy(wc6full, i, data, 0, WC6.SizeFull);
|
||||
db.Add(new WC6(data));
|
||||
}
|
||||
|
||||
if (Directory.Exists(MGDatabasePath))
|
||||
wc6db.AddRange(from file in Directory.GetFiles(MGDatabasePath, "*", SearchOption.AllDirectories)
|
||||
let fi = new FileInfo(file)
|
||||
where new[] {".wc6full", ".wc6"}.Contains(fi.Extension) && new[] {WC6.Size, WC6.SizeFull}.Contains((int)fi.Length)
|
||||
select new WC6(File.ReadAllBytes(file)));
|
||||
|
||||
Legal.MGDB_G6 = wc6db.Distinct().ToArray();
|
||||
return db;
|
||||
}
|
||||
private static void refreshWC7DB()
|
||||
private static HashSet<MysteryGift> getWC7DB(byte[] wc7bin, byte[] wc7full)
|
||||
{
|
||||
List<MysteryGift> wc7db = new List<MysteryGift>();
|
||||
byte[] wc7bin = Resources.wc7;
|
||||
var db = new HashSet<MysteryGift>();
|
||||
for (int i = 0; i < wc7bin.Length; i += WC7.Size)
|
||||
{
|
||||
byte[] data = new byte[WC7.Size];
|
||||
Array.Copy(wc7bin, i, data, 0, WC7.Size);
|
||||
wc7db.Add(new WC7(data));
|
||||
Buffer.BlockCopy(wc7bin, i, data, 0, WC7.Size);
|
||||
db.Add(new WC7(data));
|
||||
}
|
||||
byte[] wc7full = Resources.wc7full;
|
||||
for (int i = 0; i < wc7full.Length; i += WC7.SizeFull)
|
||||
{
|
||||
byte[] data = new byte[WC7.SizeFull];
|
||||
Array.Copy(wc7full, i, data, 0, WC7.SizeFull);
|
||||
wc7db.Add(new WC7(data));
|
||||
Buffer.BlockCopy(wc7full, i, data, 0, WC7.SizeFull);
|
||||
db.Add(new WC7(data));
|
||||
}
|
||||
|
||||
if (Directory.Exists(MGDatabasePath))
|
||||
wc7db.AddRange(from file in Directory.GetFiles(MGDatabasePath, "*", SearchOption.AllDirectories)
|
||||
let fi = new FileInfo(file)
|
||||
where new[] { ".wc7full", ".wc7" }.Contains(fi.Extension) && new[] { WC7.Size, WC7.SizeFull }.Contains((int)fi.Length)
|
||||
select new WC7(File.ReadAllBytes(file)));
|
||||
|
||||
Legal.MGDB_G7 = wc7db.Distinct().ToArray();
|
||||
return db;
|
||||
}
|
||||
|
||||
// Language Translation
|
||||
|
|
|
|||
|
|
@ -135,65 +135,75 @@ private void clickView(object sender, EventArgs e)
|
|||
{
|
||||
sender = ((sender as ToolStripItem)?.Owner as ContextMenuStrip)?.SourceControl ?? sender as PictureBox;
|
||||
int index = Array.IndexOf(PKXBOXES, sender);
|
||||
|
||||
var dataArr = Results.Skip(SCR_Box.Value * RES_MIN).Take(RES_MAX).ToArray();
|
||||
if (index >= dataArr.Length)
|
||||
System.Media.SystemSounds.Exclamation.Play();
|
||||
else
|
||||
if (index >= RES_MAX)
|
||||
{
|
||||
m_parent.populateFields(dataArr[index], false);
|
||||
slotSelected = index + SCR_Box.Value * RES_MIN;
|
||||
slotColor = Core.Properties.Resources.slotView;
|
||||
FillPKXBoxes(SCR_Box.Value);
|
||||
L_Viewed.Text = string.Format(Viewed, dataArr[index].Identifier);
|
||||
System.Media.SystemSounds.Exclamation.Play();
|
||||
return;
|
||||
}
|
||||
index += SCR_Box.Value * RES_MIN;
|
||||
if (index >= Results.Count)
|
||||
{
|
||||
System.Media.SystemSounds.Exclamation.Play();
|
||||
return;
|
||||
}
|
||||
|
||||
m_parent.populateFields(Results[index], false);
|
||||
slotSelected = index;
|
||||
slotColor = Core.Properties.Resources.slotView;
|
||||
FillPKXBoxes(SCR_Box.Value);
|
||||
L_Viewed.Text = string.Format(Viewed, Results[index].Identifier);
|
||||
}
|
||||
private void clickDelete(object sender, EventArgs e)
|
||||
{
|
||||
sender = ((sender as ToolStripItem)?.Owner as ContextMenuStrip)?.SourceControl ?? sender as PictureBox;
|
||||
int index = Array.IndexOf(PKXBOXES, sender);
|
||||
|
||||
var dataArr = Results.Skip(SCR_Box.Value * RES_MIN).Take(RES_MAX).ToArray();
|
||||
if (index >= dataArr.Length)
|
||||
if (index >= RES_MAX)
|
||||
{
|
||||
System.Media.SystemSounds.Exclamation.Play();
|
||||
return;
|
||||
}
|
||||
index += SCR_Box.Value * RES_MIN;
|
||||
if (index >= Results.Count)
|
||||
{
|
||||
System.Media.SystemSounds.Exclamation.Play();
|
||||
return;
|
||||
}
|
||||
|
||||
var pk = Results[index];
|
||||
string path = pk.Identifier;
|
||||
|
||||
if (path.Contains(Path.DirectorySeparatorChar))
|
||||
{
|
||||
// Data from Database: Delete file from disk
|
||||
File.Delete(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
var pk = dataArr[index];
|
||||
string path = pk.Identifier;
|
||||
// Data from Box: Delete from save file
|
||||
int box = pk.Box-1;
|
||||
int slot = pk.Slot-1;
|
||||
int offset = Main.SAV.getBoxOffset(box) + slot*Main.SAV.SIZE_STORED;
|
||||
PKM pkSAV = Main.SAV.getStoredSlot(offset);
|
||||
|
||||
if (path.Contains(Path.DirectorySeparatorChar))
|
||||
if (pkSAV.Data.SequenceEqual(pk.Data))
|
||||
{
|
||||
// Data from Database: Delete file from disk
|
||||
File.Delete(path);
|
||||
Main.SAV.setStoredSlot(Main.SAV.BlankPKM, offset);
|
||||
m_parent.setPKXBoxes();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Data from Box: Delete from save file
|
||||
int box = pk.Box-1;
|
||||
int slot = pk.Slot-1;
|
||||
int offset = Main.SAV.getBoxOffset(box) + slot*Main.SAV.SIZE_STORED;
|
||||
PKM pkSAV = Main.SAV.getStoredSlot(offset);
|
||||
|
||||
if (pkSAV.Data.SequenceEqual(pk.Data))
|
||||
{
|
||||
Main.SAV.setStoredSlot(Main.SAV.BlankPKM, offset);
|
||||
m_parent.setPKXBoxes();
|
||||
}
|
||||
else
|
||||
{
|
||||
WinFormsUtil.Error("Database slot data does not match save data!", "Don't move Pokémon after initializing the Database, please re-open the Database viewer.");
|
||||
return;
|
||||
}
|
||||
WinFormsUtil.Error("Database slot data does not match save data!", "Don't move Pokémon after initializing the Database, please re-open the Database viewer.");
|
||||
return;
|
||||
}
|
||||
// Remove from database.
|
||||
RawDB.Remove(pk);
|
||||
Results.Remove(pk);
|
||||
// Refresh database view.
|
||||
L_Count.Text = string.Format(Counter, Results.Count);
|
||||
slotSelected = -1;
|
||||
FillPKXBoxes(SCR_Box.Value);
|
||||
System.Media.SystemSounds.Asterisk.Play();
|
||||
}
|
||||
// Remove from database.
|
||||
RawDB.Remove(pk);
|
||||
Results.Remove(pk);
|
||||
// Refresh database view.
|
||||
L_Count.Text = string.Format(Counter, Results.Count);
|
||||
slotSelected = -1;
|
||||
FillPKXBoxes(SCR_Box.Value);
|
||||
System.Media.SystemSounds.Asterisk.Play();
|
||||
}
|
||||
private void clickSet(object sender, EventArgs e)
|
||||
{
|
||||
|
|
@ -218,7 +228,7 @@ private void clickSet(object sender, EventArgs e)
|
|||
|
||||
int pre = RawDB.Count;
|
||||
RawDB.Add(pk);
|
||||
RawDB = new List<PKM>(RawDB.Distinct()); // just in case
|
||||
RawDB = new List<PKM>(RawDB);
|
||||
int post = RawDB.Count;
|
||||
if (pre == post)
|
||||
{ WinFormsUtil.Alert("Pokémon already exists in database."); return; }
|
||||
|
|
|
|||
|
|
@ -80,16 +80,6 @@ public SAV_MysteryGiftDB(Main f1)
|
|||
RawDB.AddRange(Legal.MGDB_G6);
|
||||
RawDB.AddRange(Legal.MGDB_G7);
|
||||
|
||||
if (Directory.Exists(DatabasePath))
|
||||
foreach (string file in Directory.GetFiles(DatabasePath, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
FileInfo fi = new FileInfo(file);
|
||||
if (!MysteryGift.getIsMysteryGift(fi.Length)) continue;
|
||||
var mg = MysteryGift.getMysteryGift(File.ReadAllBytes(file), fi.Extension);
|
||||
if (mg != null)
|
||||
RawDB.Add(mg);
|
||||
}
|
||||
|
||||
RawDB = new List<MysteryGift>(RawDB.Where(mg => !mg.IsItem && mg.IsPokémon && mg.Species > 0).Distinct().OrderBy(mg => mg.Species));
|
||||
foreach (var mg in RawDB)
|
||||
mg.GiftUsed = false;
|
||||
|
|
@ -122,18 +112,23 @@ private void clickView(object sender, EventArgs e)
|
|||
{
|
||||
sender = ((sender as ToolStripItem)?.Owner as ContextMenuStrip)?.SourceControl ?? sender as PictureBox;
|
||||
int index = Array.IndexOf(PKXBOXES, sender);
|
||||
|
||||
var dataArr = Results.Skip(SCR_Box.Value * RES_MIN).Take(RES_MAX).ToArray();
|
||||
if (index >= dataArr.Length)
|
||||
System.Media.SystemSounds.Exclamation.Play();
|
||||
else
|
||||
if (index >= RES_MAX)
|
||||
{
|
||||
m_parent.populateFields(dataArr[index].convertToPKM(Main.SAV), false);
|
||||
slotSelected = index + SCR_Box.Value * RES_MIN;
|
||||
slotColor = Core.Properties.Resources.slotView;
|
||||
FillPKXBoxes(SCR_Box.Value);
|
||||
L_Viewed.Text = string.Format(Viewed, dataArr[index].FileName);
|
||||
System.Media.SystemSounds.Exclamation.Play();
|
||||
return;
|
||||
}
|
||||
index += SCR_Box.Value*RES_MIN;
|
||||
if (index >= Results.Count)
|
||||
{
|
||||
System.Media.SystemSounds.Exclamation.Play();
|
||||
return;
|
||||
}
|
||||
|
||||
m_parent.populateFields(Results[index].convertToPKM(Main.SAV), false);
|
||||
slotSelected = index;
|
||||
slotColor = Core.Properties.Resources.slotView;
|
||||
FillPKXBoxes(SCR_Box.Value);
|
||||
L_Viewed.Text = string.Format(Viewed, Results[index].FileName);
|
||||
}
|
||||
private void populateComboBoxes()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ public static MysteryGift getMysteryGift(byte[] data)
|
|||
|
||||
public string Extension => GetType().Name.ToLower();
|
||||
public string FileName => getCardHeader() + "." + Extension;
|
||||
public virtual byte[] Data { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
public abstract PKM convertToPKM(SaveFile SAV);
|
||||
public abstract int Format { get; }
|
||||
|
||||
|
|
@ -115,6 +115,14 @@ public MysteryGift Clone()
|
|||
|
||||
public string getCardHeader() => (CardID > 0 ? $"Card #: {CardID:0000}" : "N/A") + $" - {CardTitle.Replace('\u3000',' ').Trim()}";
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int hash = 17;
|
||||
foreach (var b in Data)
|
||||
hash = hash*31 + b;
|
||||
return hash;
|
||||
}
|
||||
|
||||
// Search Properties
|
||||
public virtual int[] Moves { get { return new int[4]; } set { } }
|
||||
public virtual int[] RelearnMoves { get { return new int[4]; } set { } }
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user