2018-05-18 05:43:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2018-05-22 00:04:02 +00:00
|
|
|
|
using System.Linq;
|
2018-05-18 05:43:07 +00:00
|
|
|
|
using static PKHeX.Core.MessageStrings;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Carries out a batch edit and contains information summarizing the results.
|
|
|
|
|
/// </summary>
|
2019-10-04 02:09:02 +00:00
|
|
|
|
public sealed class BatchEditor
|
2018-05-18 05:43:07 +00:00
|
|
|
|
{
|
|
|
|
|
private int Modified { get; set; }
|
|
|
|
|
private int Iterated { get; set; }
|
2020-06-17 02:46:22 +00:00
|
|
|
|
private int Failed { get; set; }
|
2018-05-18 05:43:07 +00:00
|
|
|
|
|
2018-05-19 02:19:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tries to modify the <see cref="PKM"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pkm">Object to modify.</param>
|
|
|
|
|
/// <param name="filters">Filters which must be satisfied prior to any modifications being made.</param>
|
|
|
|
|
/// <param name="modifications">Modifications to perform on the <see cref="pkm"/>.</param>
|
|
|
|
|
/// <returns>Result of the attempted modification.</returns>
|
2020-06-17 02:46:22 +00:00
|
|
|
|
public bool Process(PKM pkm, IEnumerable<StringInstruction> filters, IEnumerable<StringInstruction> modifications)
|
2018-05-18 05:43:07 +00:00
|
|
|
|
{
|
|
|
|
|
if (pkm.Species <= 0)
|
|
|
|
|
return false;
|
|
|
|
|
if (!pkm.Valid || pkm.Locked)
|
|
|
|
|
{
|
|
|
|
|
Iterated++;
|
|
|
|
|
var reason = pkm.Locked ? "Locked." : "Not Valid.";
|
|
|
|
|
Debug.WriteLine($"{MsgBEModifyFailBlocked} {reason}");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-07 07:28:02 +00:00
|
|
|
|
var result = BatchEditing.TryModifyPKM(pkm, filters, modifications);
|
|
|
|
|
if (result != ModifyResult.Invalid)
|
2018-05-18 05:43:07 +00:00
|
|
|
|
Iterated++;
|
2019-02-07 07:28:02 +00:00
|
|
|
|
if (result == ModifyResult.Error)
|
2020-06-17 02:46:22 +00:00
|
|
|
|
Failed++;
|
2019-02-07 07:28:02 +00:00
|
|
|
|
if (result != ModifyResult.Modified)
|
2018-05-18 05:43:07 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
pkm.RefreshChecksum();
|
|
|
|
|
Modified++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-19 02:19:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a message indicating the overall result of all modifications performed across multiple Batch Edit jobs.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sets">Collection of modifications.</param>
|
|
|
|
|
/// <returns>Friendly (multi-line) string indicating the result of the batch edits.</returns>
|
2018-05-18 05:43:07 +00:00
|
|
|
|
public string GetEditorResults(ICollection<StringInstructionSet> sets)
|
|
|
|
|
{
|
2018-05-21 03:05:27 +00:00
|
|
|
|
if (sets.Count == 0)
|
|
|
|
|
return MsgBEInstructionNone;
|
2018-05-18 05:43:07 +00:00
|
|
|
|
int ctr = Modified / sets.Count;
|
|
|
|
|
int len = Iterated / sets.Count;
|
|
|
|
|
string maybe = sets.Count == 1 ? string.Empty : "~";
|
|
|
|
|
string result = string.Format(MsgBEModifySuccess, maybe, ctr, len);
|
2020-06-17 02:46:22 +00:00
|
|
|
|
if (Failed > 0)
|
|
|
|
|
result += Environment.NewLine + maybe + string.Format(MsgBEModifyFailError, Failed);
|
2018-05-18 05:43:07 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2018-05-22 00:04:02 +00:00
|
|
|
|
|
|
|
|
|
public static BatchEditor Execute(IList<string> lines, IEnumerable<PKM> data)
|
|
|
|
|
{
|
|
|
|
|
var editor = new BatchEditor();
|
2018-12-17 19:17:19 +00:00
|
|
|
|
var sets = StringInstructionSet.GetBatchSets(lines).ToArray();
|
2018-05-22 00:04:02 +00:00
|
|
|
|
foreach (var pk in data)
|
2018-07-29 20:27:48 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var set in sets)
|
2020-06-17 02:46:22 +00:00
|
|
|
|
editor.Process(pk, set.Filters, set.Instructions);
|
2018-07-29 20:27:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-22 00:04:02 +00:00
|
|
|
|
return editor;
|
|
|
|
|
}
|
2018-05-18 05:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|