From 3cd45cf5c4c526c9af2fe07e054ec75b59a81b6d Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 16 Sep 2018 12:44:00 -0700 Subject: [PATCH] Continue extracting logic --- PKHeX.Core/Saves/SaveFile.cs | 2 +- PKHeX.Core/Util/FileUtil.cs | 37 ++++++++++++ .../Controls/SAV Editor/SlotChangeManager.cs | 60 ++++++++++--------- PKHeX.WinForms/MainWindow/Main.cs | 2 +- 4 files changed, 70 insertions(+), 31 deletions(-) diff --git a/PKHeX.Core/Saves/SaveFile.cs b/PKHeX.Core/Saves/SaveFile.cs index 78cb0ff50..45e9aab2d 100644 --- a/PKHeX.Core/Saves/SaveFile.cs +++ b/PKHeX.Core/Saves/SaveFile.cs @@ -735,7 +735,7 @@ public void ModifyBoxes(Action action, int BoxStart = 0, int BoxEnd = -1) } public byte[] PCBinary => BoxData.SelectMany(pk => pk.EncryptedBoxData).ToArray(); - public byte[] GetBoxBinary(int box) => BoxData.Skip(box*BoxSlotCount).Take(BoxSlotCount).SelectMany(pk => pk.EncryptedBoxData).ToArray(); + public byte[] GetBoxBinary(int box) => GetBoxData(box).SelectMany(pk => pk.EncryptedBoxData).ToArray(); public bool SetPCBinary(byte[] data) { diff --git a/PKHeX.Core/Util/FileUtil.cs b/PKHeX.Core/Util/FileUtil.cs index 8bf7edb9d..666f6d83f 100644 --- a/PKHeX.Core/Util/FileUtil.cs +++ b/PKHeX.Core/Util/FileUtil.cs @@ -179,5 +179,42 @@ public static bool TryGetMysteryGift(byte[] data, out MysteryGift mg, string ext mg = MysteryGift.GetMysteryGift(data, ext); return mg != null; } + + /// + /// Gets a Temp location File Name for the . + /// + /// Data to be exported + /// Data is to be encrypted + /// Path to temporary file location to write to. + public static string GetPKMTempFileName(PKM pk, bool encrypt) + { + string fn = pk.FileNameWithoutExtension; + string filename = fn + (encrypt ? $".ek{pk.Format}" : $".{pk.Extension}"); + + return Path.Combine(Path.GetTempPath(), Util.CleanFileName(filename)); + } + + /// + /// Gets a from the provided path, which is to be loaded to the . + /// + /// or file path. + /// Generation Info + /// New reference from the file. + public static PKM GetSingleFromPath(string file, ITrainerInfo SAV) + { + var fi = new FileInfo(file); + if (!fi.Exists) + return null; + if (!PKX.IsPKM(fi.Length) && !MysteryGift.IsMysteryGift(fi.Length)) + return null; + var data = File.ReadAllBytes(file); + var ext = fi.Extension; + var mg = MysteryGift.GetMysteryGift(data, ext); + var gift = mg?.ConvertToPKM(SAV); + if (gift != null) + return gift; + int prefer = PKX.GetPKMFormatFromExtension(ext, SAV.Generation); + return PKMConverter.GetPKMfromBytes(data, prefer: prefer); + } } } diff --git a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs index 73ff72c00..d19adb0f0 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Drawing; using System.IO; +using System.Linq; using System.Media; using System.Threading.Tasks; using System.Windows.Forms; @@ -217,10 +218,8 @@ private void ShowSimulatorSetTooltip(Control pb, PKM pk) ShowSet.RemoveAll(); return; } - var set = new ShowdownSet(pk); - if (pk.Format <= 2) // Nature preview from IVs - set.Nature = Experience.GetNatureVC(pk.EXP); - ShowSet.SetToolTip(pb, set.LocalizedText(Settings.Default.Language)); + var text = GetLocalizedPreviewText(pk); + ShowSet.SetToolTip(pb, text); } private void PlayCry(PKM pk) @@ -279,12 +278,10 @@ private string CreateDragDropPKM(PictureBox pb, bool encrypt, out bool external) { byte[] dragdata = SAV.DecryptPKM(DragInfo.Source.OriginalData); Array.Resize(ref dragdata, SAV.SIZE_STORED); - PKM pkx = SAV.GetPKM(dragdata); - string fn = pkx.FileName; fn = fn.Substring(0, fn.LastIndexOf('.')); - string filename = fn + (encrypt ? $".ek{pkx.Format}" : $".{pkx.Extension}"); // Make File - string newfile = Path.Combine(Path.GetTempPath(), Util.CleanFileName(filename)); + PKM pkx = SAV.GetPKM(dragdata); + string newfile = FileUtil.GetPKMTempFileName(pkx, encrypt); try { TryMakeDragDropPKM(pb, encrypt, pkx, newfile, out external); @@ -369,26 +366,18 @@ private void AlertInvalidate(string msg) WinFormsUtil.Alert(msg); } - private bool TryLoadFiles(string[] files, DragEventArgs e, bool noEgg) + private bool TryLoadFiles(IReadOnlyList files, DragEventArgs e, bool noEgg) { - if (files.Length <= 0) + if (files.Count == 0) return false; - string file = files[0]; - FileInfo fi = new FileInfo(file); - if (!fi.Exists) - return false; - if (!PKX.IsPKM(fi.Length) && !MysteryGift.IsMysteryGift(fi.Length)) + + var temp = FileUtil.GetSingleFromPath(files[0], SAV); + if (temp == null) { RequestExternalDragDrop?.Invoke(this, e); // pass thru return true; // treat as handled } - byte[] data = File.ReadAllBytes(file); - MysteryGift mg = MysteryGift.GetMysteryGift(data, fi.Extension); - if (fi.Extension.Length > 0 || !int.TryParse(fi.Extension[fi.Extension.Length - 1].ToString(), out var prefer)) - prefer = SAV.Generation; - PKM temp = mg?.ConvertToPKM(SAV) ?? PKMConverter.GetPKMfromBytes(data, prefer: prefer); - PKM pk = PKMConverter.ConvertToType(temp, SAV.PKMType, out string c); if (pk == null) { @@ -565,14 +554,7 @@ public void SwapBoxes(int index, int other) if (index == other) return; SAV.SwapBox(index, other); - - foreach (var box in Boxes) - { - if (box.CurrentBox != index && box.CurrentBox != other) - continue; - box.ResetSlots(); - box.ResetBoxNames(box.CurrentBox); - } + UpdateBoxViewAtBoxIndexes(index, other); } public void Dispose() @@ -583,5 +565,25 @@ public void Dispose() CurrentBackground?.Dispose(); ColorizedColor?.Dispose(); } + + private void UpdateBoxViewAtBoxIndexes(params int[] boxIndexes) + { + foreach (var box in Boxes) + { + var current = box.CurrentBox; + if (!boxIndexes.Contains(current)) + continue; + box.ResetSlots(); + box.ResetBoxNames(current); + } + } + + private static string GetLocalizedPreviewText(PKM pk) + { + var set = new ShowdownSet(pk); + if (pk.Format <= 2) // Nature preview from IVs + set.Nature = Experience.GetNatureVC(pk.EXP); + return set.LocalizedText(Settings.Default.Language); + } } } diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 8fce32a54..825cbbb3f 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -1089,7 +1089,7 @@ private void Dragout_MouseDown(object sender, MouseEventArgs e) // Create Temp File to Drag PKM pkx = PreparePKM(); bool encrypt = ModifierKeys == Keys.Control; - string fn = pkx.FileName; fn = fn.Substring(0, fn.LastIndexOf('.')); + string fn = pkx.FileNameWithoutExtension; string filename = fn + (encrypt ? $".ek{pkx.Format}" : $".{pkx.Extension}"); byte[] dragdata = encrypt ? pkx.EncryptedBoxData : pkx.DecryptedBoxData; // Make file