mirror of
https://github.com/kwsch/PKHeX
synced 2025-02-17 05:48:44 +00:00
Prevent slot overwriting on bulk import
https://projectpokemon.org/home/forums/topic/48912-how-to-batch-import-pokemon-boxes-without-erasing-existing-pokemon/
This commit is contained in:
parent
3e05e9655e
commit
6f22599baf
7 changed files with 45 additions and 33 deletions
|
@ -452,8 +452,9 @@ namespace PKHeX.Core
|
|||
public virtual StorageSlotFlag GetSlotFlags(int index) => StorageSlotFlag.None;
|
||||
public StorageSlotFlag GetSlotFlags(int box, int slot) => GetSlotFlags((box * BoxSlotCount) + slot);
|
||||
public bool IsSlotLocked(int box, int slot) => GetSlotFlags(box, slot).HasFlagFast(StorageSlotFlag.Locked);
|
||||
public bool IsSlotLocked(int slot) => IsSlotLocked(slot / BoxSlotCount, slot % BoxSlotCount);
|
||||
public bool IsSlotLocked(int index) => GetSlotFlags(index).HasFlagFast(StorageSlotFlag.Locked);
|
||||
public bool IsSlotOverwriteProtected(int box, int slot) => GetSlotFlags(box, slot).IsOverwriteProtected();
|
||||
public bool IsSlotOverwriteProtected(int index) => GetSlotFlags(index).IsOverwriteProtected();
|
||||
|
||||
public bool MoveBox(int box, int insertBeforeBox)
|
||||
{
|
||||
|
|
|
@ -86,17 +86,18 @@ namespace PKHeX.Core
|
|||
/// <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="overwrite">Overwrite existing full slots. If true, will only overwrite empty slots.</param>
|
||||
/// <param name="noSetb">Bypass option to not modify <see cref="PKM"/> properties when setting to Save File.</param>
|
||||
/// <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)
|
||||
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, noSetb);
|
||||
return SAV.LoadBoxes(filepaths, out result, boxStart, boxClear, overwrite, noSetb);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -107,12 +108,13 @@ namespace PKHeX.Core
|
|||
/// <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="overwrite">Overwrite existing full slots. If true, will only overwrite empty slots.</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)
|
||||
public static bool LoadBoxes(this SaveFile SAV, IEnumerable<string> 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, noSetb);
|
||||
return SAV.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, noSetb);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -123,12 +125,13 @@ namespace PKHeX.Core
|
|||
/// <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="overwrite">Overwrite existing full slots. If true, will only overwrite empty slots.</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)
|
||||
public static bool LoadBoxes(this SaveFile SAV, IEnumerable<MysteryGift> 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, noSetb);
|
||||
return SAV.LoadBoxes(pks, out result, boxStart, boxClear, overwrite, noSetb);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -139,9 +142,10 @@ namespace PKHeX.Core
|
|||
/// <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="overwrite">Overwrite existing full slots. If true, will only overwrite empty slots.</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)
|
||||
public static bool LoadBoxes(this SaveFile SAV, IEnumerable<PKM> pks, out string result, int boxStart = 0, bool boxClear = false, bool overwrite = false, bool? noSetb = null)
|
||||
{
|
||||
if (!SAV.HasBox)
|
||||
{ result = MsgSaveBoxFailNone; return false; }
|
||||
|
@ -150,7 +154,7 @@ namespace PKHeX.Core
|
|||
if (boxClear)
|
||||
SAV.ClearBoxes(boxStart);
|
||||
|
||||
int ctr = SAV.ImportPKMs(compat, boxStart, noSetb);
|
||||
int ctr = SAV.ImportPKMs(compat, overwrite, boxStart, noSetb);
|
||||
if (ctr <= 0)
|
||||
{
|
||||
result = MsgSaveBoxImportNoFiles;
|
||||
|
|
|
@ -91,33 +91,41 @@ namespace PKHeX.Core
|
|||
return errata;
|
||||
}
|
||||
|
||||
public static int ImportPKMs(this SaveFile SAV, IEnumerable<PKM> compat, int boxStart, bool? noSetb)
|
||||
/// <summary>
|
||||
/// Imports compatible <see cref="PKM"/> data to the <see cref="SAV"/>, starting at the provided box.
|
||||
/// </summary>
|
||||
/// <param name="SAV">Save File that will receive the <see cref="compat"/> data.</param>
|
||||
/// <param name="compat">Compatible <see cref="PKM"/> data that can be set to the <see cref="SAV"/> without conversion.</param>
|
||||
/// <param name="overwrite">Overwrite existing full slots. If true, will only overwrite empty slots.</param>
|
||||
/// <param name="boxStart">First box to start loading to. All prior boxes are not modified.</param>
|
||||
/// <param name="noSetb">Bypass option to not modify <see cref="PKM"/> properties when setting to Save File.</param>
|
||||
/// <returns>Count of injected <see cref="PKM"/>.</returns>
|
||||
public static int ImportPKMs(this SaveFile SAV, IEnumerable<PKM> compat, bool overwrite = false, int boxStart = 0, bool? noSetb = null)
|
||||
{
|
||||
int startCount = boxStart * SAV.BoxSlotCount;
|
||||
int maxCount = SAV.BoxCount * SAV.BoxSlotCount;
|
||||
int i = startCount;
|
||||
int getbox() => i / SAV.BoxSlotCount;
|
||||
int getslot() => i % SAV.BoxSlotCount;
|
||||
int index = startCount;
|
||||
|
||||
foreach (var pk in compat)
|
||||
{
|
||||
int box = getbox();
|
||||
int slot = getslot();
|
||||
while (SAV.IsSlotOverwriteProtected(box, slot))
|
||||
if (overwrite)
|
||||
{
|
||||
++i;
|
||||
box = getbox();
|
||||
slot = getslot();
|
||||
while (SAV.IsSlotOverwriteProtected(index))
|
||||
++index;
|
||||
}
|
||||
else
|
||||
{
|
||||
index = SAV.NextOpenBoxSlot(index-1);
|
||||
if (index < 0) // Boxes full!
|
||||
break;
|
||||
}
|
||||
|
||||
int offset = SAV.GetBoxOffset(box) + (slot * SAV.SIZE_STORED);
|
||||
SAV.SetStoredSlot(pk, offset, noSetb);
|
||||
SAV.SetBoxSlotAtIndex(pk, index, noSetb);
|
||||
|
||||
if (++i == maxCount) // Boxes full!
|
||||
if (++index == maxCount) // Boxes full!
|
||||
break;
|
||||
}
|
||||
i -= startCount; // actual imported count
|
||||
return i;
|
||||
return index - startCount; // actual imported count
|
||||
}
|
||||
|
||||
public static IEnumerable<PKM> GetCompatible(this SaveFile SAV, IEnumerable<PKM> pks)
|
||||
|
|
|
@ -664,7 +664,6 @@ namespace PKHeX.Core
|
|||
/// <returns>A boolean indicating whether or not the save data size is valid.</returns>
|
||||
private static bool IsSizeValid(int size) => SIZES.Contains(size);
|
||||
|
||||
// SAV Manipulation
|
||||
/// <summary>Calculates the CRC16-CCITT checksum over an input byte array.</summary>
|
||||
/// <param name="data">Input byte array</param>
|
||||
/// <param name="start">Starting point for checksum</param>
|
||||
|
|
|
@ -691,9 +691,9 @@ namespace PKHeX.WinForms.Controls
|
|||
}
|
||||
|
||||
// File I/O
|
||||
public bool GetBulkImportSettings(out bool clearAll, out bool? noSetb)
|
||||
public bool GetBulkImportSettings(out bool clearAll, out bool overwrite, out bool? noSetb)
|
||||
{
|
||||
clearAll = false; noSetb = false;
|
||||
clearAll = false; noSetb = false; overwrite = false;
|
||||
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel, MsgSaveBoxImportClear, MsgSaveBoxImportClearNo);
|
||||
if (dr == DialogResult.Cancel)
|
||||
return false;
|
||||
|
@ -843,10 +843,10 @@ namespace PKHeX.WinForms.Controls
|
|||
if (!Directory.Exists(path))
|
||||
return false;
|
||||
|
||||
if (!GetBulkImportSettings(out bool clearAll, out bool? noSetb))
|
||||
if (!GetBulkImportSettings(out bool clearAll, out var overwrite, out var noSetb))
|
||||
return false;
|
||||
|
||||
SAV.LoadBoxes(path, out result, Box.CurrentBox, clearAll, noSetb);
|
||||
SAV.LoadBoxes(path, out result, Box.CurrentBox, clearAll, overwrite, noSetb);
|
||||
SetPKMBoxes();
|
||||
UpdateBoxViewers();
|
||||
return true;
|
||||
|
|
|
@ -412,11 +412,11 @@ namespace PKHeX.WinForms
|
|||
|
||||
private void Menu_Import_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!BoxView.GetBulkImportSettings(out var clearAll, out var noSetb))
|
||||
if (!BoxView.GetBulkImportSettings(out var clearAll, out var overwrite, out var noSetb))
|
||||
return;
|
||||
|
||||
int box = BoxView.Box.CurrentBox;
|
||||
if (!SAV.LoadBoxes(Results, out var result, box, clearAll, noSetb))
|
||||
if (!SAV.LoadBoxes(Results, out var result, box, clearAll, overwrite, noSetb))
|
||||
return;
|
||||
|
||||
BoxView.SetPKMBoxes();
|
||||
|
|
|
@ -357,11 +357,11 @@ namespace PKHeX.WinForms
|
|||
|
||||
private void Menu_Import_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!BoxView.GetBulkImportSettings(out var clearAll, out var noSetb))
|
||||
if (!BoxView.GetBulkImportSettings(out var clearAll, out var overwrite, out var noSetb))
|
||||
return;
|
||||
|
||||
int box = BoxView.Box.CurrentBox;
|
||||
if (!SAV.LoadBoxes(Results, out var result, box, clearAll, noSetb))
|
||||
if (!SAV.LoadBoxes(Results, out var result, box, clearAll, overwrite, noSetb))
|
||||
return;
|
||||
|
||||
BoxView.SetPKMBoxes();
|
||||
|
|
Loading…
Add table
Reference in a new issue