2017-02-04 22:58:30 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-07-02 02:43:51 +00:00
|
|
|
|
using System.Diagnostics;
|
2017-02-04 22:58:30 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using PKHeX.Core;
|
2018-04-07 04:23:09 +00:00
|
|
|
|
using static PKHeX.Core.MessageStrings;
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains extension methods for use with a <see cref="SaveFile"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class SAVUtil
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dumps a folder of files to the <see cref="SaveFile"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SAV"><see cref="SaveFile"/> that is being dumped from.</param>
|
|
|
|
|
/// <param name="path">Folder to store <see cref="PKM"/> files.</param>
|
|
|
|
|
/// <param name="result">Result message from the method.</param>
|
|
|
|
|
/// <param name="boxFolders">Option to save in child folders with the Box Name as the folder name.</param>
|
|
|
|
|
/// <returns></returns>
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static bool DumpBoxes(this SaveFile SAV, string path, out string result, bool boxFolders = false)
|
2017-02-04 22:58:30 +00:00
|
|
|
|
{
|
2017-09-29 05:20:27 +00:00
|
|
|
|
var boxdata = SAV.BoxData;
|
2017-02-04 22:58:30 +00:00
|
|
|
|
if (boxdata == null)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ result = MsgSaveBoxExportInvalid; return false; }
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
|
|
|
|
int ctr = 0;
|
|
|
|
|
foreach (PKM pk in boxdata)
|
|
|
|
|
{
|
|
|
|
|
if (pk.Species == 0 || !pk.Valid)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ctr++;
|
|
|
|
|
string fileName = Util.CleanFileName(pk.FileName);
|
2018-04-07 04:23:09 +00:00
|
|
|
|
string boxfolder = string.Empty;
|
2017-02-04 22:58:30 +00:00
|
|
|
|
if (boxFolders)
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
boxfolder = SAV.GetBoxName(pk.Box - 1);
|
2017-02-04 22:58:30 +00:00
|
|
|
|
Directory.CreateDirectory(Path.Combine(path, boxfolder));
|
|
|
|
|
}
|
|
|
|
|
if (!File.Exists(Path.Combine(Path.Combine(path, boxfolder), fileName)))
|
|
|
|
|
File.WriteAllBytes(Path.Combine(Path.Combine(path, boxfolder), fileName), pk.DecryptedBoxData);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-07 04:23:09 +00:00
|
|
|
|
result = string.Format(MsgSaveBoxExportPathCount, ctr) + Environment.NewLine + path;
|
2017-02-04 22:58:30 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-23 01:51:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dumps the <see cref="SaveFile.BoxData"/> to a folder with individual decrypted files.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SAV"><see cref="SaveFile"/> that is being dumped from.</param>
|
|
|
|
|
/// <param name="path">Folder to store <see cref="PKM"/> files.</param>
|
|
|
|
|
/// <param name="result">Result message from the method.</param>
|
|
|
|
|
/// <param name="currentBox">Box contents to be dumped.</param>
|
|
|
|
|
/// <returns></returns>
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static bool DumpBox(this SaveFile SAV, string path, out string result, int currentBox)
|
2017-03-21 07:21:03 +00:00
|
|
|
|
{
|
2017-09-29 05:20:27 +00:00
|
|
|
|
var boxdata = SAV.BoxData;
|
2017-03-21 07:21:03 +00:00
|
|
|
|
if (boxdata == null)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ result = MsgSaveBoxExportInvalid; return false; }
|
2017-03-21 07:21:03 +00:00
|
|
|
|
|
|
|
|
|
int ctr = 0;
|
|
|
|
|
foreach (PKM pk in boxdata)
|
|
|
|
|
{
|
|
|
|
|
if (pk.Species == 0 || !pk.Valid || (pk.Box - 1) != currentBox)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ctr++;
|
|
|
|
|
string fileName = Util.CleanFileName(pk.FileName);
|
|
|
|
|
if (!File.Exists(Path.Combine(path, fileName)))
|
|
|
|
|
File.WriteAllBytes(Path.Combine(path, fileName), pk.DecryptedBoxData);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-07 04:23:09 +00:00
|
|
|
|
result = string.Format(MsgSaveBoxExportPathCount, ctr) + Environment.NewLine + path;
|
2017-03-21 07:21:03 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 22:58:30 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads a folder of files to the <see cref="SaveFile"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SAV"><see cref="SaveFile"/> to load folder to.</param>
|
|
|
|
|
/// <param name="path">Folder to load <see cref="PKM"/> files from. Files are only loaded from the top directory.</param>
|
|
|
|
|
/// <param name="result">Result message from the method.</param>
|
|
|
|
|
/// <param name="boxStart">First box to start loading to. All prior boxes are not modified.</param>
|
|
|
|
|
/// <param name="boxClear">Instruction to clear boxes after the starting box.</param>
|
|
|
|
|
/// <param name="noSetb">Bypass option to not modify <see cref="PKM"/> properties when setting to Save File.</param>
|
2018-02-01 07:37:01 +00:00
|
|
|
|
/// <param name="all">Enumerate all files even in sub-folders.</param>
|
|
|
|
|
/// <returns>True if any files are imported.</returns>
|
|
|
|
|
public static bool LoadBoxes(this SaveFile SAV, string path, out string result, int boxStart = 0, bool boxClear = false, bool? noSetb = null, bool all = false)
|
2017-02-04 22:58:30 +00:00
|
|
|
|
{
|
2018-02-01 07:37:01 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path))
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ result = MsgSaveBoxExportPathInvalid; return false; }
|
2018-02-01 07:37:01 +00:00
|
|
|
|
|
|
|
|
|
var opt = all ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
|
|
|
|
|
var filepaths = Directory.EnumerateFiles(path, "*.*", opt);
|
|
|
|
|
return SAV.LoadBoxes(filepaths, out result, boxStart, boxClear, noSetb);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads a folder of files to the <see cref="SaveFile"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SAV"><see cref="SaveFile"/> to load folder to.</param>
|
|
|
|
|
/// <param name="filepaths">Files to load <see cref="PKM"/> files from.</param>
|
|
|
|
|
/// <param name="result">Result message from the method.</param>
|
|
|
|
|
/// <param name="boxStart">First box to start loading to. All prior boxes are not modified.</param>
|
|
|
|
|
/// <param name="boxClear">Instruction to clear boxes after the starting box.</param>
|
|
|
|
|
/// <param name="noSetb">Bypass option to not modify <see cref="PKM"/> properties when setting to Save File.</param>
|
|
|
|
|
/// <returns>True if any files are imported.</returns>
|
|
|
|
|
public static bool LoadBoxes(this SaveFile SAV, IEnumerable<string> filepaths, out string result, int boxStart = 0, bool boxClear = false, bool? noSetb = null)
|
|
|
|
|
{
|
|
|
|
|
int generation = SAV.Generation;
|
|
|
|
|
var pks = GetPKMsFromPaths(filepaths, generation);
|
|
|
|
|
return SAV.LoadBoxes(pks, out result, boxStart, boxClear, noSetb);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads a folder of files to the <see cref="SaveFile"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SAV"><see cref="SaveFile"/> to load folder to.</param>
|
|
|
|
|
/// <param name="gifts">Gifts to load <see cref="PKM"/> files from.</param>
|
|
|
|
|
/// <param name="result">Result message from the method.</param>
|
|
|
|
|
/// <param name="boxStart">First box to start loading to. All prior boxes are not modified.</param>
|
|
|
|
|
/// <param name="boxClear">Instruction to clear boxes after the starting box.</param>
|
|
|
|
|
/// <param name="noSetb">Bypass option to not modify <see cref="PKM"/> properties when setting to Save File.</param>
|
|
|
|
|
/// <returns>True if any files are imported.</returns>
|
|
|
|
|
public static bool LoadBoxes(this SaveFile SAV, IEnumerable<MysteryGift> gifts, out string result, int boxStart = 0, bool boxClear = false, bool? noSetb = null)
|
|
|
|
|
{
|
|
|
|
|
var pks = gifts.Select(z => z.ConvertToPKM(SAV));
|
|
|
|
|
return SAV.LoadBoxes(pks, out result, boxStart, boxClear, noSetb);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads a folder of files to the <see cref="SaveFile"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SAV"><see cref="SaveFile"/> to load folder to.</param>
|
|
|
|
|
/// <param name="pks">Unconverted <see cref="PKM"/> objects to load.</param>
|
|
|
|
|
/// <param name="result">Result message from the method.</param>
|
|
|
|
|
/// <param name="boxStart">First box to start loading to. All prior boxes are not modified.</param>
|
|
|
|
|
/// <param name="boxClear">Instruction to clear boxes after the starting box.</param>
|
|
|
|
|
/// <param name="noSetb">Bypass option to not modify <see cref="PKM"/> properties when setting to Save File.</param>
|
|
|
|
|
/// <returns>True if any files are imported.</returns>
|
|
|
|
|
public static bool LoadBoxes(this SaveFile SAV, IEnumerable<PKM> pks, out string result, int boxStart = 0, bool boxClear = false, bool? noSetb = null)
|
|
|
|
|
{
|
2017-02-04 22:58:30 +00:00
|
|
|
|
if (!SAV.HasBox)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
{ result = MsgSaveBoxFailNone; return false; }
|
2018-02-01 07:37:01 +00:00
|
|
|
|
|
|
|
|
|
var compat = GetPKMForSaveFile(SAV, pks);
|
2017-02-04 22:58:30 +00:00
|
|
|
|
if (boxClear)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SAV.ClearBoxes(boxStart);
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2018-02-01 07:37:01 +00:00
|
|
|
|
int ctr = SAV.ImportPKMs(compat, boxStart, noSetb);
|
|
|
|
|
if (ctr <= 0)
|
2017-02-04 22:58:30 +00:00
|
|
|
|
{
|
2018-04-07 04:23:09 +00:00
|
|
|
|
result = MsgSaveBoxImportNoFiles;
|
2018-02-01 07:37:01 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2018-04-07 04:23:09 +00:00
|
|
|
|
result = string.Format(MsgSaveBoxImportSuccess, ctr);
|
2018-02-01 07:37:01 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2018-02-01 07:37:01 +00:00
|
|
|
|
private static int ImportPKMs(this SaveFile SAV, IEnumerable<PKM> compat, int boxStart, bool? noSetb)
|
|
|
|
|
{
|
|
|
|
|
int startCount = boxStart * SAV.BoxSlotCount;
|
|
|
|
|
int maxCount = SAV.BoxCount * SAV.BoxSlotCount;
|
|
|
|
|
int i = startCount;
|
|
|
|
|
int getbox() => i / SAV.BoxSlotCount;
|
|
|
|
|
int getslot() => i % SAV.BoxSlotCount;
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2018-02-01 07:37:01 +00:00
|
|
|
|
foreach (var pk in compat)
|
|
|
|
|
{
|
|
|
|
|
int box = getbox();
|
|
|
|
|
int slot = getslot();
|
|
|
|
|
while (SAV.IsSlotLocked(box, slot))
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
box = getbox();
|
|
|
|
|
slot = getslot();
|
|
|
|
|
}
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2018-02-01 07:37:01 +00:00
|
|
|
|
int offset = SAV.GetBoxOffset(box) + slot * SAV.SIZE_STORED;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SAV.SetStoredSlot(pk, offset, noSetb);
|
2018-02-01 07:37:01 +00:00
|
|
|
|
|
|
|
|
|
if (++i == maxCount) // Boxes full!
|
2017-02-04 22:58:30 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-02-01 07:37:01 +00:00
|
|
|
|
i -= startCount; // actual imported count
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
private static IEnumerable<PKM> GetPKMsFromPaths(IEnumerable<string> filepaths, int generation)
|
|
|
|
|
{
|
|
|
|
|
return filepaths
|
|
|
|
|
.Where(file => PKX.IsPKM(new FileInfo(file).Length))
|
|
|
|
|
.Select(File.ReadAllBytes)
|
|
|
|
|
.Select(data => PKMConverter.GetPKMfromBytes(data, prefer: generation))
|
|
|
|
|
.Where(temp => temp != null);
|
|
|
|
|
}
|
|
|
|
|
private static IEnumerable<PKM> GetPKMForSaveFile(this SaveFile SAV, IEnumerable<PKM> pks)
|
|
|
|
|
{
|
|
|
|
|
var savtype = SAV.PKMType;
|
|
|
|
|
foreach (var temp in pks)
|
|
|
|
|
{
|
|
|
|
|
PKM pk = PKMConverter.ConvertToType(temp, savtype, out string c);
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2018-02-01 07:37:01 +00:00
|
|
|
|
if (pk == null)
|
|
|
|
|
{ Debug.WriteLine(c); continue; }
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2018-02-01 07:37:01 +00:00
|
|
|
|
var compat = SAV.IsPKMCompatible(pk);
|
|
|
|
|
if (compat.Length > 0)
|
|
|
|
|
continue;
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2018-02-01 07:37:01 +00:00
|
|
|
|
yield return pk;
|
|
|
|
|
}
|
2017-02-04 22:58:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks a <see cref="PKM"/> file for compatibility to the <see cref="SaveFile"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SAV"><see cref="SaveFile"/> that is being checked.</param>
|
|
|
|
|
/// <param name="pkm"><see cref="PKM"/> that is being tested for compatibility.</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static string[] IsPKMCompatible(this SaveFile SAV, PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
// Check if PKM properties are outside of the valid range
|
|
|
|
|
List<string> errata = new List<string>();
|
|
|
|
|
if (SAV.Generation > 1)
|
|
|
|
|
{
|
|
|
|
|
ushort held = (ushort)pkm.HeldItem;
|
|
|
|
|
|
|
|
|
|
if (held > GameInfo.Strings.itemlist.Length)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexItemRange} {held}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
else if (held > SAV.MaxItemID)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexItemGame} {GameInfo.Strings.itemlist[held]}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
else if (!pkm.CanHoldItem(SAV.HeldItems))
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexItemHeld} {GameInfo.Strings.itemlist[held]}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pkm.Species > GameInfo.Strings.specieslist.Length)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexSpeciesRange} {pkm.Species}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
else if (SAV.MaxSpeciesID < pkm.Species)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexSpeciesGame} {GameInfo.Strings.specieslist[pkm.Species]}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
2017-11-22 02:31:24 +00:00
|
|
|
|
if (!SAV.Personal[pkm.Species].IsFormeWithinRange(pkm.AltForm) && !FormConverter.IsValidOutOfBoundsForme(pkm.Species, pkm.AltForm, pkm.GenNumber))
|
|
|
|
|
errata.Add(string.Format(LegalityCheckStrings.V304, Math.Max(0, SAV.Personal[pkm.Species].FormeCount - 1), pkm.AltForm));
|
2017-11-21 23:22:33 +00:00
|
|
|
|
|
2017-02-04 22:58:30 +00:00
|
|
|
|
if (pkm.Moves.Any(m => m > GameInfo.Strings.movelist.Length))
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexMoveRange} {string.Join(", ", pkm.Moves.Where(m => m > GameInfo.Strings.movelist.Length).Select(m => m.ToString()))}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
else if (pkm.Moves.Any(m => m > SAV.MaxMoveID))
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexMoveGame} {string.Join(", ", pkm.Moves.Where(m => m > SAV.MaxMoveID).Select(m => GameInfo.Strings.movelist[m]))}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
|
|
|
|
if (pkm.Ability > GameInfo.Strings.abilitylist.Length)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexAbilityRange} {pkm.Ability}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
else if (pkm.Ability > SAV.MaxAbilityID)
|
2018-04-07 04:23:09 +00:00
|
|
|
|
errata.Add($"{MsgIndexAbilityGame} {GameInfo.Strings.abilitylist[pkm.Ability]}");
|
2017-02-04 22:58:30 +00:00
|
|
|
|
|
|
|
|
|
return errata.ToArray();
|
|
|
|
|
}
|
2017-06-23 01:51:46 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the <see cref="PKM.HeldItem"/> for all <see cref="PKM"/> in the <see cref="SaveFile.BoxData"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="SAV"><see cref="SaveFile"/> that is being operated on.</param>
|
|
|
|
|
/// <param name="item"><see cref="PKM.HeldItem"/> to set. If no argument is supplied, the held item will be removed.</param>
|
|
|
|
|
public static void SetBoxDataAllHeldItems(this SaveFile SAV, int item = 0)
|
|
|
|
|
{
|
|
|
|
|
var boxdata = SAV.BoxData;
|
|
|
|
|
foreach (PKM pk in boxdata)
|
|
|
|
|
{
|
|
|
|
|
pk.HeldItem = item;
|
|
|
|
|
pk.RefreshChecksum();
|
|
|
|
|
}
|
|
|
|
|
SAV.BoxData = boxdata;
|
|
|
|
|
}
|
2017-02-04 22:58:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|