using System.Collections.Generic;
using System.IO;
using System.Linq;
using static PKHeX.Core.MessageStrings;
namespace PKHeX.Core
{
///
/// Contains extension methods for use with a .
///
public static class BoxUtil
{
///
/// Dumps a folder of files to the .
///
/// that is being dumped from.
/// Folder to store files.
/// Option to save in child folders with the Box Name as the folder name.
/// -1 if aborted, otherwise the amount of files dumped.
public static int DumpBoxes(this SaveFile SAV, string path, bool boxFolders = false)
{
if (!SAV.HasBox)
return -1;
var boxdata = SAV.BoxData;
int ctr = 0;
foreach (PKM pk in boxdata)
{
if (pk.Species == 0 || !pk.Valid)
continue;
ctr++;
string fileName = Util.CleanFileName(pk.FileName);
string boxfolder = string.Empty;
if (boxFolders)
{
boxfolder = SAV.GetBoxName(pk.Box - 1);
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);
}
return ctr;
}
///
/// Dumps the to a folder with individual decrypted files.
///
/// that is being dumped from.
/// Folder to store files.
/// Box contents to be dumped.
/// -1 if aborted, otherwise the amount of files dumped.
public static int DumpBox(this SaveFile SAV, string path, int currentBox)
{
if (!SAV.HasBox)
return -1;
var boxdata = SAV.BoxData;
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);
}
return ctr;
}
///
/// Loads a folder of files to the .
///
/// to load folder to.
/// Folder to load files from. Files are only loaded from the top directory.
/// Result message from the method.
/// First box to start loading to. All prior boxes are not modified.
/// Instruction to clear boxes after the starting box.
/// Overwrite existing full slots. If true, will only overwrite empty slots.
/// Bypass option to not modify properties when setting to Save File.
/// Enumerate all files even in sub-folders.
/// True if any files are imported.
public static bool LoadBoxes(this SaveFile SAV, string path, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, bool? noSetb = null, bool all = false)
{
if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path))
{ result = MsgSaveBoxExportPathInvalid; return false; }
var opt = all ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var filepaths = Directory.EnumerateFiles(path, "*.*", opt);
return SAV.LoadBoxes(filepaths, out result, boxStart, boxClear, overwrite, noSetb);
}
///
/// Loads a folder of files to the .
///
/// to load folder to.
/// Files to load files from.
/// Result message from the method.
/// First box to start loading to. All prior boxes are not modified.
/// Instruction to clear boxes after the starting box.
/// Overwrite existing full slots. If true, will only overwrite empty slots.
/// Bypass option to not modify properties when setting to Save File.
/// True if any files are imported.
public static bool LoadBoxes(this SaveFile SAV, IEnumerable filepaths, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, bool? noSetb = null)
{
var pks = GetPossiblePKMsFromPaths(SAV, filepaths);
return SAV.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, noSetb);
}
///
/// Loads a folder of files to the .
///
/// to load folder to.
/// Gifts to load files from.
/// Result message from the method.
/// First box to start loading to. All prior boxes are not modified.
/// Instruction to clear boxes after the starting box.
/// Overwrite existing full slots. If true, will only overwrite empty slots.
/// Bypass option to not modify properties when setting to Save File.
/// True if any files are imported.
public static bool LoadBoxes(this SaveFile SAV, IEnumerable gifts, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, bool? noSetb = null)
{
var pks = gifts.Select(z => z.ConvertToPKM(SAV));
return SAV.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, noSetb);
}
///
/// Loads a folder of files to the .
///
/// to load folder to.
/// Unconverted objects to load.
/// Result message from the method.
/// First box to start loading to. All prior boxes are not modified.
/// Instruction to clear boxes after the starting box.
/// Overwrite existing full slots. If true, will only overwrite empty slots.
/// Bypass option to not modify properties when setting to Save File.
/// True if any files are imported.
public static bool LoadBoxes(this SaveFile SAV, IEnumerable pks, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, bool? noSetb = null)
{
if (!SAV.HasBox)
{ result = MsgSaveBoxFailNone; return false; }
var compat = SAV.GetCompatible(pks);
if (boxClear)
SAV.ClearBoxes(boxStart);
int ctr = SAV.ImportPKMs(compat, overwrite, boxStart, noSetb);
if (ctr <= 0)
{
result = MsgSaveBoxImportNoFiles;
return false;
}
result = string.Format(MsgSaveBoxImportSuccess, ctr);
return true;
}
public static IEnumerable GetPKMsFromPaths(IEnumerable 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 GetPossiblePKMsFromPaths(SaveFile sav, IEnumerable filepaths)
{
foreach (var f in filepaths)
{
var obj = FileUtil.GetSupportedFile(f, sav);
switch (obj)
{
case PKM pk:
yield return pk;
break;
case MysteryGift g when g.IsPokémon:
yield return g.ConvertToPKM(sav);
break;
case GP1 g when g.Species != 0:
yield return g.ConvertToPB7(sav);
break;
}
}
}
}
}