2016-07-13 05:19:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
2017-07-02 02:43:51 +00:00
|
|
|
|
using System.Diagnostics;
|
2017-03-08 08:16:34 +00:00
|
|
|
|
using System.Drawing;
|
2016-09-16 03:54:41 +00:00
|
|
|
|
using System.Globalization;
|
2016-07-13 05:19:51 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Forms;
|
2017-01-08 07:54:09 +00:00
|
|
|
|
using PKHeX.Core;
|
2017-05-28 21:49:36 +00:00
|
|
|
|
using System.Reflection;
|
2016-07-13 05:19:51 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.WinForms
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
|
|
|
|
public partial class BatchEditor : Form
|
|
|
|
|
{
|
2017-05-23 04:55:05 +00:00
|
|
|
|
private readonly SaveFile SAV;
|
|
|
|
|
public BatchEditor(PKM pk, SaveFile sav)
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
2017-01-28 07:33:36 +00:00
|
|
|
|
pkmref = pk;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
SAV = sav;
|
|
|
|
|
InitializeComponent();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
DragDrop += TabMain_DragDrop;
|
|
|
|
|
DragEnter += TabMain_DragEnter;
|
2016-12-07 05:36:31 +00:00
|
|
|
|
|
|
|
|
|
CB_Format.Items.Clear();
|
|
|
|
|
CB_Format.Items.Add("Any");
|
2017-03-08 08:16:34 +00:00
|
|
|
|
foreach (Type t in types) CB_Format.Items.Add(t.Name.ToLower());
|
|
|
|
|
CB_Format.Items.Add("All");
|
2016-12-07 05:36:31 +00:00
|
|
|
|
|
2016-07-18 00:06:50 +00:00
|
|
|
|
CB_Format.SelectedIndex = CB_Require.SelectedIndex = 0;
|
2017-01-09 05:06:15 +00:00
|
|
|
|
new ToolTip().SetToolTip(CB_Property, "Property of a given PKM to modify.");
|
|
|
|
|
new ToolTip().SetToolTip(L_PropType, "PropertyType of the currently loaded PKM in the main window.");
|
|
|
|
|
new ToolTip().SetToolTip(L_PropValue, "PropertyValue of the currently loaded PKM in the main window.");
|
2016-07-13 05:19:51 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static string[][] GetPropArray()
|
2016-12-07 05:36:31 +00:00
|
|
|
|
{
|
|
|
|
|
var p = new string[types.Length][];
|
|
|
|
|
for (int i = 0; i < p.Length; i++)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
p[i] = ReflectFrameworkUtil.GetPropertiesCanWritePublicDeclared(types[i]).Concat(CustomProperties).OrderBy(a => a).ToArray();
|
2016-12-07 05:36:31 +00:00
|
|
|
|
|
2017-02-11 07:54:36 +00:00
|
|
|
|
// Properties for any PKM
|
2017-10-07 04:03:23 +00:00
|
|
|
|
var any = ReflectFrameworkUtil.GetPropertiesCanWritePublic(typeof(PK1)).Union(p.SelectMany(a => a)).OrderBy(a => a).ToArray();
|
2017-02-11 07:54:36 +00:00
|
|
|
|
// Properties shared by all PKM
|
2017-03-08 08:16:34 +00:00
|
|
|
|
var all = p.Aggregate(new HashSet<string>(p.First()), (h, e) => { h.IntersectWith(e); return h; }).OrderBy(a => a).ToArray();
|
2016-12-07 05:36:31 +00:00
|
|
|
|
|
|
|
|
|
var p1 = new string[types.Length + 2][];
|
|
|
|
|
Array.Copy(p, 0, p1, 1, p.Length);
|
2017-03-08 08:16:34 +00:00
|
|
|
|
p1[0] = any;
|
|
|
|
|
p1[p1.Length - 1] = all;
|
2016-12-07 05:36:31 +00:00
|
|
|
|
|
|
|
|
|
return p1;
|
|
|
|
|
}
|
2016-07-13 05:19:51 +00:00
|
|
|
|
|
2017-01-28 07:33:36 +00:00
|
|
|
|
private readonly PKM pkmref;
|
2016-07-23 18:44:37 +00:00
|
|
|
|
private const string CONST_RAND = "$rand";
|
2016-07-29 06:05:26 +00:00
|
|
|
|
private const string CONST_SHINY = "$shiny";
|
2017-02-10 06:45:58 +00:00
|
|
|
|
private const string CONST_SUGGEST = "$suggest";
|
Add ability to batch edit trashbytes
Raw editing of byte fields
.Nickname_Trash=$[]43,00,65,00,72,00,66,00,72,00,6F,00,75,00,73,00,73,00,65,00,00,00,00,00
only use cases are OT/Nickname trash bytes
not too concerned about speed re-parsing values or storing it to an auto
property; it's fast enough.
2017-07-05 05:22:04 +00:00
|
|
|
|
private const string CONST_BYTES = "$[]";
|
2017-02-10 06:45:58 +00:00
|
|
|
|
|
|
|
|
|
private const string PROP_LEGAL = "Legal";
|
|
|
|
|
private static readonly string[] CustomProperties = {PROP_LEGAL};
|
|
|
|
|
|
2016-07-18 00:06:50 +00:00
|
|
|
|
private int currentFormat = -1;
|
2017-03-03 05:00:41 +00:00
|
|
|
|
private static readonly Type[] types =
|
|
|
|
|
{
|
|
|
|
|
typeof (PK7), typeof (PK6), typeof (PK5), typeof (PK4), typeof(BK4),
|
|
|
|
|
typeof (PK3), typeof (XK3), typeof (CK3),
|
|
|
|
|
typeof (PK2), typeof (PK1),
|
|
|
|
|
};
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static readonly string[][] properties = GetPropArray();
|
2016-07-18 00:06:50 +00:00
|
|
|
|
|
2016-07-13 05:19:51 +00:00
|
|
|
|
// GUI Methods
|
|
|
|
|
private void B_Open_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!B_Go.Enabled) return;
|
|
|
|
|
var fbd = new FolderBrowserDialog();
|
|
|
|
|
if (fbd.ShowDialog() != DialogResult.OK)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
TB_Folder.Text = fbd.SelectedPath;
|
|
|
|
|
TB_Folder.Visible = true;
|
|
|
|
|
}
|
|
|
|
|
private void B_SAV_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
TB_Folder.Text = "";
|
|
|
|
|
TB_Folder.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
private void B_Go_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
RunBackgroundWorker();
|
2016-07-13 05:19:51 +00:00
|
|
|
|
}
|
2017-02-11 07:54:36 +00:00
|
|
|
|
private void B_Add_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (CB_Property.SelectedIndex < 0)
|
|
|
|
|
{ WinFormsUtil.Alert("Invalid property selected."); return; }
|
|
|
|
|
|
|
|
|
|
char[] prefix = { '.', '=', '!' };
|
|
|
|
|
string s = prefix[CB_Require.SelectedIndex] + CB_Property.Items[CB_Property.SelectedIndex].ToString() + "=";
|
|
|
|
|
if (RTB_Instructions.Lines.Length != 0 && RTB_Instructions.Lines.Last().Length > 0)
|
|
|
|
|
s = Environment.NewLine + s;
|
|
|
|
|
|
|
|
|
|
RTB_Instructions.AppendText(s);
|
|
|
|
|
}
|
|
|
|
|
private void CB_Format_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (currentFormat == CB_Format.SelectedIndex)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int format = CB_Format.SelectedIndex;
|
|
|
|
|
CB_Property.Items.Clear();
|
|
|
|
|
CB_Property.Items.AddRange(properties[format]);
|
|
|
|
|
CB_Property.SelectedIndex = 0;
|
|
|
|
|
currentFormat = format;
|
|
|
|
|
}
|
|
|
|
|
private void CB_Property_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
L_PropType.Text = GetPropertyType(CB_Property.Text);
|
2017-03-08 08:16:34 +00:00
|
|
|
|
if (pkmref.GetType().HasProperty(CB_Property.Text))
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
L_PropValue.Text = ReflectFrameworkUtil.GetValue(pkmref, CB_Property.Text).ToString();
|
2017-03-08 08:16:34 +00:00
|
|
|
|
L_PropType.ForeColor = L_PropValue.ForeColor; // reset color
|
|
|
|
|
}
|
|
|
|
|
else // no property, flag
|
|
|
|
|
{
|
|
|
|
|
L_PropValue.Text = string.Empty;
|
|
|
|
|
L_PropType.ForeColor = Color.Red;
|
|
|
|
|
}
|
2017-02-11 07:54:36 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static void TabMain_DragEnter(object sender, DragEventArgs e)
|
2017-02-11 07:54:36 +00:00
|
|
|
|
{
|
|
|
|
|
if (e.Data.GetDataPresent(DataFormats.FileDrop))
|
|
|
|
|
e.Effect = DragDropEffects.Copy;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void TabMain_DragDrop(object sender, DragEventArgs e)
|
2017-02-11 07:54:36 +00:00
|
|
|
|
{
|
|
|
|
|
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
|
|
|
|
|
if (!Directory.Exists(files[0]))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
TB_Folder.Text = files[0];
|
|
|
|
|
TB_Folder.Visible = true;
|
|
|
|
|
RB_SAV.Checked = false;
|
|
|
|
|
RB_Path.Checked = true;
|
|
|
|
|
}
|
2016-07-13 05:19:51 +00:00
|
|
|
|
|
2017-07-01 23:07:02 +00:00
|
|
|
|
private BackgroundWorker b;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void RunBackgroundWorker()
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
2017-07-01 23:07:02 +00:00
|
|
|
|
if (RTB_Instructions.Lines.Any(line => line.Length == 0))
|
|
|
|
|
{ WinFormsUtil.Error("Line length error in instruction list."); return; }
|
|
|
|
|
|
2017-10-14 00:24:26 +00:00
|
|
|
|
var sets = StringInstructionSet.GetBatchSets(RTB_Instructions.Lines).ToArray();
|
|
|
|
|
if (sets.Any(s => s.Filters.Any(z => string.IsNullOrWhiteSpace(z.PropertyValue))))
|
2017-01-08 07:54:09 +00:00
|
|
|
|
{ WinFormsUtil.Error("Empty Filter Value detected."); return; }
|
2016-07-21 05:43:26 +00:00
|
|
|
|
|
2017-10-14 00:24:26 +00:00
|
|
|
|
if (sets.Any(z => !z.Instructions.Any()))
|
|
|
|
|
{ WinFormsUtil.Error("No instructions defined for a modification set."); return; }
|
2017-07-01 23:07:02 +00:00
|
|
|
|
|
2017-10-14 00:24:26 +00:00
|
|
|
|
var emptyVal = sets.SelectMany(s => s.Instructions.Where(z => string.IsNullOrWhiteSpace(z.PropertyValue))).ToArray();
|
2016-11-27 21:32:32 +00:00
|
|
|
|
if (emptyVal.Any())
|
|
|
|
|
{
|
|
|
|
|
string props = string.Join(", ", emptyVal.Select(z => z.PropertyName));
|
2017-07-01 23:07:02 +00:00
|
|
|
|
string invalid = $"Empty Property Value{(emptyVal.Length > 1 ? "s" : "")} detected:" + Environment.NewLine + props;
|
|
|
|
|
if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, invalid, "Continue?"))
|
2016-11-27 21:32:32 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-07-21 05:43:26 +00:00
|
|
|
|
|
2017-07-01 23:07:02 +00:00
|
|
|
|
string destPath = null;
|
2016-07-31 06:03:41 +00:00
|
|
|
|
if (RB_Path.Checked)
|
|
|
|
|
{
|
2017-01-08 07:54:09 +00:00
|
|
|
|
WinFormsUtil.Alert("Please select the folder where the files will be saved to.", "This can be the same folder as the source of PKM files.");
|
2016-07-31 06:03:41 +00:00
|
|
|
|
var fbd = new FolderBrowserDialog();
|
|
|
|
|
var dr = fbd.ShowDialog();
|
|
|
|
|
if (dr != DialogResult.OK)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
destPath = fbd.SelectedPath;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 05:43:26 +00:00
|
|
|
|
FLP_RB.Enabled = RTB_Instructions.Enabled = B_Go.Enabled = false;
|
|
|
|
|
|
2017-10-14 00:24:26 +00:00
|
|
|
|
foreach (var set in sets)
|
|
|
|
|
{
|
|
|
|
|
ScreenStrings(set.Filters);
|
|
|
|
|
ScreenStrings(set.Instructions);
|
|
|
|
|
}
|
|
|
|
|
RunBatchEdit(sets, TB_Folder.Text, destPath);
|
2017-07-01 23:07:02 +00:00
|
|
|
|
}
|
2017-10-14 00:24:26 +00:00
|
|
|
|
private void RunBatchEdit(StringInstructionSet[] sets, string source, string destination)
|
2017-07-01 23:07:02 +00:00
|
|
|
|
{
|
|
|
|
|
len = err = ctr = 0;
|
|
|
|
|
b = new BackgroundWorker { WorkerReportsProgress = true };
|
|
|
|
|
b.DoWork += (sender, e) =>
|
|
|
|
|
{
|
2016-07-13 05:19:51 +00:00
|
|
|
|
if (RB_SAV.Checked)
|
2017-10-14 00:24:26 +00:00
|
|
|
|
RunBatchEditSaveFile(sets);
|
2016-07-13 05:19:51 +00:00
|
|
|
|
else
|
2017-10-14 00:24:26 +00:00
|
|
|
|
RunBatchEditFolder(sets, source, destination);
|
2016-07-13 05:19:51 +00:00
|
|
|
|
};
|
2017-07-01 23:07:02 +00:00
|
|
|
|
b.ProgressChanged += (sender, e) => SetProgressBar(e.ProgressPercentage);
|
|
|
|
|
b.RunWorkerCompleted += (sender, e) =>
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
2017-10-14 00:24:26 +00:00
|
|
|
|
ctr /= sets.Length;
|
|
|
|
|
len /= sets.Length;
|
2017-10-14 00:31:53 +00:00
|
|
|
|
string maybe = sets.Length == 1 ? string.Empty : "~";
|
2017-10-14 00:24:26 +00:00
|
|
|
|
string result = $"Modified {maybe}{ctr}/{len} files.";
|
2016-07-22 05:45:20 +00:00
|
|
|
|
if (err > 0)
|
2017-10-14 00:24:26 +00:00
|
|
|
|
result += Environment.NewLine + $"{maybe}{err} files ignored due to an internal error.";
|
2017-01-08 07:54:09 +00:00
|
|
|
|
WinFormsUtil.Alert(result);
|
2016-07-13 05:19:51 +00:00
|
|
|
|
FLP_RB.Enabled = RTB_Instructions.Enabled = B_Go.Enabled = true;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SetupProgressBar(0);
|
2016-07-13 05:19:51 +00:00
|
|
|
|
};
|
|
|
|
|
b.RunWorkerAsync();
|
|
|
|
|
}
|
2017-10-14 00:24:26 +00:00
|
|
|
|
private void RunBatchEditFolder(IList<StringInstructionSet> sets, string source, string destination)
|
2017-07-01 23:07:02 +00:00
|
|
|
|
{
|
|
|
|
|
var files = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
|
2017-10-14 00:24:26 +00:00
|
|
|
|
SetupProgressBar(files.Length * sets.Count);
|
|
|
|
|
foreach (var set in sets)
|
|
|
|
|
ProcessFolder(files, set.Filters, set.Instructions, destination);
|
2017-07-01 23:07:02 +00:00
|
|
|
|
}
|
2017-10-14 00:24:26 +00:00
|
|
|
|
private void RunBatchEditSaveFile(IList<StringInstructionSet> sets)
|
2017-07-01 23:07:02 +00:00
|
|
|
|
{
|
2017-09-29 05:20:27 +00:00
|
|
|
|
IList<PKM> data;
|
2017-07-01 23:07:02 +00:00
|
|
|
|
if (SAV.HasParty && process(data = SAV.PartyData))
|
|
|
|
|
SAV.PartyData = data;
|
|
|
|
|
if (SAV.HasBox && process(data = SAV.BoxData))
|
|
|
|
|
SAV.BoxData = data;
|
2017-09-29 05:20:27 +00:00
|
|
|
|
bool process(IList<PKM> d)
|
2017-07-01 23:07:02 +00:00
|
|
|
|
{
|
2017-10-14 00:24:26 +00:00
|
|
|
|
SetupProgressBar(d.Count * sets.Count);
|
|
|
|
|
foreach (var set in sets)
|
|
|
|
|
ProcessSAV(d, set.Filters, set.Instructions);
|
2017-09-29 05:20:27 +00:00
|
|
|
|
return d.Count != 0;
|
2017-07-01 23:07:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-13 05:19:51 +00:00
|
|
|
|
|
|
|
|
|
// Progress Bar
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void SetupProgressBar(int count)
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
|
|
|
|
MethodInvoker mi = () => { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = count; };
|
|
|
|
|
if (PB_Show.InvokeRequired)
|
|
|
|
|
PB_Show.Invoke(mi);
|
|
|
|
|
else
|
|
|
|
|
mi.Invoke();
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void SetProgressBar(int i)
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
|
|
|
|
if (PB_Show.InvokeRequired)
|
|
|
|
|
PB_Show.Invoke((MethodInvoker)(() => PB_Show.Value = i));
|
|
|
|
|
else { PB_Show.Value = i; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mass Editing
|
2016-07-22 05:45:20 +00:00
|
|
|
|
private int ctr, len, err;
|
2017-10-14 00:24:26 +00:00
|
|
|
|
private void ProcessSAV(IList<PKM> data, IList<StringInstruction> Filters, IList<StringInstruction> Instructions)
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
2017-09-29 05:20:27 +00:00
|
|
|
|
for (int i = 0; i < data.Count; i++)
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ProcessPKM(data[i], Filters, Instructions);
|
2016-07-13 05:19:51 +00:00
|
|
|
|
b.ReportProgress(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-14 00:24:26 +00:00
|
|
|
|
private void ProcessFolder(IReadOnlyList<string> files, IList<StringInstruction> Filters, IList<StringInstruction> Instructions, string destPath)
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
2017-09-29 05:20:27 +00:00
|
|
|
|
for (int i = 0; i < files.Count; i++)
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
|
|
|
|
string file = files[i];
|
2017-02-11 07:54:36 +00:00
|
|
|
|
var fi = new FileInfo(file);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (!PKX.IsPKM(fi.Length))
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
|
|
|
|
b.ReportProgress(i);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 05:20:27 +00:00
|
|
|
|
int format = fi.Extension.Length > 0 ? (fi.Extension.Last() - '0') & 0xF : SAV.Generation;
|
2016-07-13 05:19:51 +00:00
|
|
|
|
byte[] data = File.ReadAllBytes(file);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var pkm = PKMConverter.GetPKMfromBytes(data, prefer: format);
|
|
|
|
|
if (ProcessPKM(pkm, Filters, Instructions))
|
2017-02-11 07:54:36 +00:00
|
|
|
|
File.WriteAllBytes(Path.Combine(destPath, Path.GetFileName(file)), pkm.DecryptedBoxData);
|
2016-07-13 05:19:51 +00:00
|
|
|
|
|
|
|
|
|
b.ReportProgress(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private bool ProcessPKM(PKM pkm, IEnumerable<StringInstruction> Filters, IEnumerable<StringInstruction> Instructions)
|
2016-11-15 03:04:08 +00:00
|
|
|
|
{
|
2017-02-11 07:54:36 +00:00
|
|
|
|
if (!pkm.Valid || pkm.Locked)
|
2017-04-21 04:04:51 +00:00
|
|
|
|
{
|
|
|
|
|
len++;
|
2017-07-02 02:43:51 +00:00
|
|
|
|
Debug.WriteLine("Skipped a pkm due to disallowed input: " + (pkm.Locked ? "Locked." : "Not Valid."));
|
2017-02-11 07:54:36 +00:00
|
|
|
|
return false;
|
2017-04-21 04:04:51 +00:00
|
|
|
|
}
|
2017-02-11 07:54:36 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ModifyResult r = TryModifyPKM(pkm, Filters, Instructions);
|
2017-02-11 07:54:36 +00:00
|
|
|
|
if (r != ModifyResult.Invalid)
|
|
|
|
|
len++;
|
|
|
|
|
if (r == ModifyResult.Error)
|
|
|
|
|
err++;
|
|
|
|
|
if (r != ModifyResult.Modified)
|
|
|
|
|
return false;
|
|
|
|
|
if (pkm.Species <= 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
pkm.RefreshChecksum();
|
|
|
|
|
ctr++;
|
|
|
|
|
return true;
|
2016-11-15 03:04:08 +00:00
|
|
|
|
}
|
2016-07-13 05:19:51 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private string GetPropertyType(string propertyName)
|
2016-12-07 05:36:31 +00:00
|
|
|
|
{
|
2017-02-10 06:45:58 +00:00
|
|
|
|
if (CustomProperties.Contains(propertyName))
|
|
|
|
|
return "Custom";
|
|
|
|
|
|
2016-12-07 05:36:31 +00:00
|
|
|
|
int typeIndex = CB_Format.SelectedIndex;
|
|
|
|
|
|
2017-03-08 08:16:34 +00:00
|
|
|
|
if (typeIndex == properties.Length - 1) // All
|
2016-12-07 05:36:31 +00:00
|
|
|
|
return types[0].GetProperty(propertyName).PropertyType.Name;
|
|
|
|
|
|
2017-03-08 08:16:34 +00:00
|
|
|
|
if (typeIndex == 0) // Any
|
2016-12-07 05:36:31 +00:00
|
|
|
|
foreach (var p in types.Select(t => t.GetProperty(propertyName)).Where(p => p != null))
|
|
|
|
|
return p.PropertyType.Name;
|
|
|
|
|
|
|
|
|
|
return types[typeIndex - 1].GetProperty(propertyName).PropertyType.Name;
|
|
|
|
|
}
|
2016-07-13 05:19:51 +00:00
|
|
|
|
|
|
|
|
|
// Utility Methods
|
2017-02-11 07:54:36 +00:00
|
|
|
|
private enum ModifyResult
|
|
|
|
|
{
|
|
|
|
|
Invalid,
|
|
|
|
|
Error,
|
|
|
|
|
Filtered,
|
|
|
|
|
Modified,
|
|
|
|
|
}
|
2017-10-14 00:24:26 +00:00
|
|
|
|
|
|
|
|
|
public class StringInstructionSet
|
|
|
|
|
{
|
|
|
|
|
public IList<StringInstruction> Filters { get; private set; }
|
|
|
|
|
public IList<StringInstruction> Instructions { get; private set; }
|
|
|
|
|
|
|
|
|
|
private const string SetSeparator = ";";
|
|
|
|
|
public static IEnumerable<StringInstructionSet> GetBatchSets(string[] lines)
|
|
|
|
|
{
|
|
|
|
|
int start = 0;
|
|
|
|
|
while (start < lines.Length)
|
|
|
|
|
{
|
|
|
|
|
var list = lines.Skip(start).TakeWhile(z => !lines[start++].StartsWith(SetSeparator)).ToList();
|
|
|
|
|
yield return GetBatchSet(list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<StringInstructionSet> GetBatchSets(IEnumerable<IEnumerable<string>> sets)
|
|
|
|
|
{
|
|
|
|
|
return sets.Select(set => GetBatchSet(set.ToList()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static StringInstructionSet GetBatchSet(IList<string> set)
|
|
|
|
|
{
|
|
|
|
|
return new StringInstructionSet
|
|
|
|
|
{
|
|
|
|
|
Filters = StringInstruction.GetFilters(set).ToList(),
|
|
|
|
|
Instructions = StringInstruction.GetInstructions(set).ToList(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-23 08:04:55 +00:00
|
|
|
|
public class StringInstruction
|
2016-07-13 05:19:51 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public string PropertyName { get; set; }
|
|
|
|
|
public string PropertyValue { get; set; }
|
|
|
|
|
public bool Evaluator { get; set; }
|
|
|
|
|
public void SetScreenedValue(string[] arr)
|
2016-11-15 03:04:08 +00:00
|
|
|
|
{
|
|
|
|
|
int index = Array.IndexOf(arr, PropertyValue);
|
|
|
|
|
PropertyValue = index > -1 ? index.ToString() : PropertyValue;
|
|
|
|
|
}
|
2016-12-10 04:49:17 +00:00
|
|
|
|
|
|
|
|
|
// Extra Functionality
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private int Min, Max;
|
|
|
|
|
public bool Random { get; private set; }
|
|
|
|
|
public int RandomValue => Util.Rand.Next(Min, Max + 1);
|
|
|
|
|
public void SetRandRange(string pv)
|
2017-02-11 07:54:36 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string str = pv.Substring(1);
|
|
|
|
|
var split = str.Split(',');
|
|
|
|
|
int.TryParse(split[0], out Min);
|
|
|
|
|
int.TryParse(split[1], out Max);
|
|
|
|
|
|
|
|
|
|
if (Min == Max)
|
|
|
|
|
{
|
|
|
|
|
PropertyValue = Min.ToString();
|
2017-07-02 02:43:51 +00:00
|
|
|
|
Debug.WriteLine(PropertyName + " randomization range Min/Max same?");
|
2017-06-18 01:37:19 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Random = true;
|
|
|
|
|
}
|
2017-02-11 07:54:36 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<StringInstruction> GetFilters(IEnumerable<string> lines)
|
|
|
|
|
{
|
|
|
|
|
var raw = GetRelevantStrings(lines, new[] { '!', '=' });
|
2017-02-11 07:54:36 +00:00
|
|
|
|
return from line in raw
|
|
|
|
|
let eval = line[0] == '='
|
|
|
|
|
let split = line.Substring(1).Split('=')
|
|
|
|
|
where split.Length == 2 && !string.IsNullOrWhiteSpace(split[0])
|
|
|
|
|
select new StringInstruction { PropertyName = split[0], PropertyValue = split[1], Evaluator = eval };
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<StringInstruction> GetInstructions(IEnumerable<string> lines)
|
2017-02-11 07:54:36 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var raw = GetRelevantStrings(lines, new[] { '.' }).Select(line => line.Substring(1));
|
2017-02-11 07:54:36 +00:00
|
|
|
|
return from line in raw
|
|
|
|
|
select line.Split('=') into split
|
|
|
|
|
where split.Length == 2
|
|
|
|
|
select new StringInstruction { PropertyName = split[0], PropertyValue = split[1] };
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static IEnumerable<string> GetRelevantStrings(IEnumerable<string> lines, IEnumerable<char> pieces)
|
|
|
|
|
{
|
|
|
|
|
return lines
|
|
|
|
|
.Where(line => !string.IsNullOrEmpty(line))
|
|
|
|
|
.Where(line => pieces.Any(z => z == line[0]));
|
|
|
|
|
}
|
2017-02-11 07:54:36 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private sealed class PKMInfo
|
2017-02-10 06:45:58 +00:00
|
|
|
|
{
|
2017-06-24 04:19:19 +00:00
|
|
|
|
internal PKM pkm { get; }
|
2017-06-18 01:37:19 +00:00
|
|
|
|
internal PKMInfo(PKM pk) { pkm = pk; }
|
2017-02-10 06:45:58 +00:00
|
|
|
|
|
|
|
|
|
private LegalityAnalysis la;
|
|
|
|
|
private LegalityAnalysis Legality => la ?? (la = new LegalityAnalysis(pkm));
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
internal bool Legal => Legality.Valid;
|
|
|
|
|
internal int[] SuggestedRelearn => Legality.GetSuggestedRelearn();
|
|
|
|
|
internal int[] SuggestedMoves => Legality.GetSuggestedMoves(tm: true, tutor: true, reminder: false);
|
|
|
|
|
internal EncounterStatic SuggestedEncounter => Legality.GetSuggestedMetInfo();
|
2017-02-10 06:45:58 +00:00
|
|
|
|
}
|
2017-02-11 07:54:36 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static void ScreenStrings(IEnumerable<StringInstruction> il)
|
2017-02-11 07:54:36 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var i in il.Where(i => !i.PropertyValue.All(char.IsDigit)))
|
|
|
|
|
{
|
|
|
|
|
string pv = i.PropertyValue;
|
Add ability to batch edit trashbytes
Raw editing of byte fields
.Nickname_Trash=$[]43,00,65,00,72,00,66,00,72,00,6F,00,75,00,73,00,73,00,65,00,00,00,00,00
only use cases are OT/Nickname trash bytes
not too concerned about speed re-parsing values or storing it to an auto
property; it's fast enough.
2017-07-05 05:22:04 +00:00
|
|
|
|
if (pv.StartsWith("$") && !pv.StartsWith(CONST_BYTES) && pv.Contains(','))
|
2017-06-18 01:37:19 +00:00
|
|
|
|
i.SetRandRange(pv);
|
2017-02-11 07:54:36 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SetInstructionScreenedValue(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static void SetInstructionScreenedValue(StringInstruction i)
|
|
|
|
|
{
|
|
|
|
|
switch (i.PropertyName)
|
|
|
|
|
{
|
|
|
|
|
case nameof(PKM.Species): i.SetScreenedValue(GameInfo.Strings.specieslist); return;
|
|
|
|
|
case nameof(PKM.HeldItem): i.SetScreenedValue(GameInfo.Strings.itemlist); return;
|
|
|
|
|
case nameof(PKM.Ability): i.SetScreenedValue(GameInfo.Strings.abilitylist); return;
|
|
|
|
|
case nameof(PKM.Nature): i.SetScreenedValue(GameInfo.Strings.natures); return;
|
|
|
|
|
case nameof(PKM.Ball): i.SetScreenedValue(GameInfo.Strings.balllist); return;
|
|
|
|
|
case nameof(PKM.Move1):
|
|
|
|
|
case nameof(PKM.Move2):
|
|
|
|
|
case nameof(PKM.Move3):
|
|
|
|
|
case nameof(PKM.Move4):
|
|
|
|
|
case nameof(PKM.RelearnMove1):
|
|
|
|
|
case nameof(PKM.RelearnMove2):
|
|
|
|
|
case nameof(PKM.RelearnMove3):
|
|
|
|
|
case nameof(PKM.RelearnMove4):
|
|
|
|
|
i.SetScreenedValue(GameInfo.Strings.movelist); return;
|
2017-02-11 07:54:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static ModifyResult TryModifyPKM(PKM PKM, IEnumerable<StringInstruction> Filters, IEnumerable<StringInstruction> Instructions)
|
2016-07-21 05:43:26 +00:00
|
|
|
|
{
|
|
|
|
|
if (!PKM.ChecksumValid || PKM.Species == 0)
|
|
|
|
|
return ModifyResult.Invalid;
|
|
|
|
|
|
2016-07-23 08:04:55 +00:00
|
|
|
|
Type pkm = PKM.GetType();
|
2017-02-10 06:45:58 +00:00
|
|
|
|
PKMInfo info = new PKMInfo(PKM);
|
2016-07-23 08:04:55 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ModifyResult result = ModifyResult.Error;
|
2016-07-13 05:19:51 +00:00
|
|
|
|
foreach (var cmd in Filters)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (IsPKMFiltered(pkm, cmd, info, out result))
|
|
|
|
|
return result; // why it was filtered out
|
2016-07-13 05:19:51 +00:00
|
|
|
|
}
|
2017-07-02 02:43:51 +00:00
|
|
|
|
catch { Debug.WriteLine($"Unable to compare {cmd.PropertyName} to {cmd.PropertyValue}."); }
|
2016-07-13 05:19:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var cmd in Instructions)
|
|
|
|
|
{
|
2016-07-23 18:44:37 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
result = SetPKMProperty(PKM, info, cmd);
|
2016-07-23 18:44:37 +00:00
|
|
|
|
}
|
2017-07-02 02:43:51 +00:00
|
|
|
|
catch { Debug.WriteLine($"Unable to set {cmd.PropertyName} to {cmd.PropertyValue}."); }
|
2016-07-13 05:19:51 +00:00
|
|
|
|
}
|
2016-07-21 05:43:26 +00:00
|
|
|
|
return result;
|
2016-07-13 05:19:51 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static ModifyResult SetPKMProperty(PKM PKM, PKMInfo info, StringInstruction cmd)
|
|
|
|
|
{
|
Add ability to batch edit trashbytes
Raw editing of byte fields
.Nickname_Trash=$[]43,00,65,00,72,00,66,00,72,00,6F,00,75,00,73,00,73,00,65,00,00,00,00,00
only use cases are OT/Nickname trash bytes
not too concerned about speed re-parsing values or storing it to an auto
property; it's fast enough.
2017-07-05 05:22:04 +00:00
|
|
|
|
if (cmd.PropertyValue.StartsWith(CONST_BYTES))
|
|
|
|
|
return SetByteArrayProperty(PKM, cmd)
|
|
|
|
|
? ModifyResult.Modified
|
|
|
|
|
: ModifyResult.Error;
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (cmd.PropertyValue == CONST_SUGGEST)
|
|
|
|
|
return SetSuggestedPKMProperty(PKM, cmd, info)
|
|
|
|
|
? ModifyResult.Modified
|
|
|
|
|
: ModifyResult.Error;
|
|
|
|
|
|
|
|
|
|
SetProperty(PKM, cmd);
|
|
|
|
|
return ModifyResult.Modified;
|
|
|
|
|
}
|
|
|
|
|
private static bool IsPKMFiltered(Type pkm, StringInstruction cmd, PKMInfo info, out ModifyResult result)
|
|
|
|
|
{
|
|
|
|
|
result = ModifyResult.Error;
|
|
|
|
|
if (cmd.PropertyName == PROP_LEGAL)
|
|
|
|
|
{
|
|
|
|
|
if (!bool.TryParse(cmd.PropertyValue, out bool legal))
|
|
|
|
|
return true;
|
|
|
|
|
if (legal == info.Legal == cmd.Evaluator)
|
|
|
|
|
return false;
|
|
|
|
|
result = ModifyResult.Filtered;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (!pkm.HasPropertyAll(cmd.PropertyName)
|
2017-06-24 04:19:19 +00:00
|
|
|
|
|| pkm.IsValueEqual(info.pkm, cmd.PropertyName, cmd.PropertyValue) != cmd.Evaluator)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
{
|
|
|
|
|
result = ModifyResult.Filtered;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
private static bool SetSuggestedPKMProperty(PKM PKM, StringInstruction cmd, PKMInfo info)
|
2017-02-10 06:45:58 +00:00
|
|
|
|
{
|
|
|
|
|
switch (cmd.PropertyName)
|
|
|
|
|
{
|
2017-10-14 00:31:53 +00:00
|
|
|
|
case nameof(PKM.HyperTrainFlags):
|
|
|
|
|
PKM.HyperTrainFlags = GetSuggestedHyperTrainingStatus(PKM);
|
|
|
|
|
return true;
|
2017-02-10 06:45:58 +00:00
|
|
|
|
case nameof(PKM.RelearnMoves):
|
|
|
|
|
PKM.RelearnMoves = info.SuggestedRelearn;
|
|
|
|
|
return true;
|
|
|
|
|
case nameof(PKM.Met_Location):
|
|
|
|
|
var encounter = info.SuggestedEncounter;
|
|
|
|
|
if (encounter == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
int level = encounter.Level;
|
|
|
|
|
int location = encounter.Location;
|
2017-08-29 01:12:57 +00:00
|
|
|
|
int minlvl = Legal.GetLowestLevel(PKM, encounter.LevelMin);
|
2017-02-10 06:45:58 +00:00
|
|
|
|
|
|
|
|
|
PKM.Met_Level = level;
|
|
|
|
|
PKM.Met_Location = location;
|
|
|
|
|
PKM.CurrentLevel = Math.Max(minlvl, level);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case nameof(PKM.Moves):
|
|
|
|
|
var moves = info.SuggestedMoves;
|
|
|
|
|
Util.Shuffle(moves);
|
|
|
|
|
Array.Resize(ref moves, 4);
|
|
|
|
|
PKM.Moves = moves;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-14 00:31:53 +00:00
|
|
|
|
|
|
|
|
|
private static int GetSuggestedHyperTrainingStatus(PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
if (pkm.Format < 7 || pkm.CurrentLevel != 100)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
int val = 0;
|
|
|
|
|
if (pkm.IV_HP != 31)
|
|
|
|
|
val |= 1 << 0;
|
|
|
|
|
if (pkm.IV_ATK < 31 && pkm.IV_ATK > 1)
|
|
|
|
|
val |= 1 << 1;
|
|
|
|
|
if (pkm.IV_DEF != 31)
|
|
|
|
|
val |= 1 << 2;
|
|
|
|
|
if (pkm.IV_SPE < 31 && pkm.IV_SPE > 1)
|
|
|
|
|
val |= 1 << 3;
|
|
|
|
|
if (pkm.IV_SPA != 31)
|
|
|
|
|
val |= 1 << 4;
|
|
|
|
|
if (pkm.IV_SPD != 31)
|
|
|
|
|
val |= 1 << 5;
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
Add ability to batch edit trashbytes
Raw editing of byte fields
.Nickname_Trash=$[]43,00,65,00,72,00,66,00,72,00,6F,00,75,00,73,00,73,00,65,00,00,00,00,00
only use cases are OT/Nickname trash bytes
not too concerned about speed re-parsing values or storing it to an auto
property; it's fast enough.
2017-07-05 05:22:04 +00:00
|
|
|
|
private static bool SetByteArrayProperty(PKM PKM, StringInstruction cmd)
|
|
|
|
|
{
|
|
|
|
|
switch (cmd.PropertyName)
|
|
|
|
|
{
|
|
|
|
|
case nameof(PKM.Nickname_Trash):
|
|
|
|
|
PKM.Nickname_Trash = string2arr(cmd.PropertyValue);
|
|
|
|
|
return true;
|
|
|
|
|
case nameof(PKM.OT_Trash):
|
|
|
|
|
PKM.OT_Trash = string2arr(cmd.PropertyValue);
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
byte[] string2arr(string str) => str.Substring(CONST_BYTES.Length).Split(',').Select(z => Convert.ToByte(z.Trim(), 16)).ToArray();
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static void SetProperty(PKM PKM, StringInstruction cmd)
|
2017-02-10 06:45:58 +00:00
|
|
|
|
{
|
|
|
|
|
if (cmd.PropertyName == nameof(PKM.MetDate))
|
|
|
|
|
PKM.MetDate = DateTime.ParseExact(cmd.PropertyValue, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
|
|
|
|
|
else if (cmd.PropertyName == nameof(PKM.EggMetDate))
|
|
|
|
|
PKM.EggMetDate = DateTime.ParseExact(cmd.PropertyValue, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
|
|
|
|
|
else if (cmd.PropertyName == nameof(PKM.EncryptionConstant) && cmd.PropertyValue == CONST_RAND)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ReflectFrameworkUtil.SetValue(PKM, cmd.PropertyName, Util.Rand32().ToString());
|
2017-02-10 06:45:58 +00:00
|
|
|
|
else if ((cmd.PropertyName == nameof(PKM.Ability) || cmd.PropertyName == nameof(PKM.AbilityNumber)) && cmd.PropertyValue.StartsWith("$"))
|
|
|
|
|
PKM.RefreshAbility(Convert.ToInt16(cmd.PropertyValue[1]) - 0x30);
|
|
|
|
|
else if (cmd.PropertyName == nameof(PKM.PID) && cmd.PropertyValue == CONST_RAND)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
PKM.SetPIDGender(PKM.Gender);
|
2017-02-10 06:45:58 +00:00
|
|
|
|
else if (cmd.PropertyName == nameof(PKM.EncryptionConstant) && cmd.PropertyValue == nameof(PKM.PID))
|
|
|
|
|
PKM.EncryptionConstant = PKM.PID;
|
|
|
|
|
else if (cmd.PropertyName == nameof(PKM.PID) && cmd.PropertyValue == CONST_SHINY)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
PKM.SetShinyPID();
|
2017-02-10 06:45:58 +00:00
|
|
|
|
else if (cmd.PropertyName == nameof(PKM.Species) && cmd.PropertyValue == "0")
|
|
|
|
|
PKM.Data = new byte[PKM.Data.Length];
|
|
|
|
|
else if (cmd.PropertyName.StartsWith("IV") && cmd.PropertyValue == CONST_RAND)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SetRandomIVs(PKM, cmd);
|
2017-02-10 06:45:58 +00:00
|
|
|
|
else if (cmd.Random)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ReflectFrameworkUtil.SetValue(PKM, cmd.PropertyName, cmd.RandomValue);
|
2017-02-10 06:45:58 +00:00
|
|
|
|
else if (cmd.PropertyName == nameof(PKM.IsNicknamed) && cmd.PropertyValue.ToLower() == "false")
|
2017-10-25 03:59:58 +00:00
|
|
|
|
{ PKM.IsNicknamed = false; PKM.Nickname = PKX.GetSpeciesNameGeneration(PKM.Species, PKM.Language, PKM.Format); }
|
2017-02-10 06:45:58 +00:00
|
|
|
|
else
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ReflectFrameworkUtil.SetValue(PKM, cmd.PropertyName, cmd.PropertyValue);
|
2017-02-10 06:45:58 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static void SetRandomIVs(PKM PKM, StringInstruction cmd)
|
2016-11-25 23:34:47 +00:00
|
|
|
|
{
|
2017-11-02 16:05:44 +00:00
|
|
|
|
if (cmd.PropertyName == nameof(PKM.IVs))
|
2016-11-27 17:26:58 +00:00
|
|
|
|
{
|
2017-11-02 16:05:44 +00:00
|
|
|
|
PKM.SetRandomIVs();
|
|
|
|
|
return;
|
2016-11-27 17:26:58 +00:00
|
|
|
|
}
|
2017-11-02 16:05:44 +00:00
|
|
|
|
int MaxIV = PKM.Format <= 2 ? 15 : 31;
|
|
|
|
|
ReflectFrameworkUtil.SetValue(PKM, cmd.PropertyName, Util.Rand32() & MaxIV);
|
2016-11-25 23:34:47 +00:00
|
|
|
|
}
|
2016-07-13 05:19:51 +00:00
|
|
|
|
}
|
2017-05-28 21:49:36 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static class ReflectFrameworkUtil
|
2017-05-28 21:49:36 +00:00
|
|
|
|
{
|
2017-06-24 04:19:19 +00:00
|
|
|
|
public static bool IsValueEqual(this Type t, object obj, string propertyName, object value)
|
2017-05-28 21:49:36 +00:00
|
|
|
|
{
|
2017-06-24 04:19:19 +00:00
|
|
|
|
PropertyInfo pi = t.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
2017-05-28 21:49:36 +00:00
|
|
|
|
var v = pi.GetValue(obj, null);
|
|
|
|
|
var c = ConvertValue(value, pi.PropertyType);
|
|
|
|
|
return v.Equals(c);
|
|
|
|
|
}
|
|
|
|
|
public static void SetValue(object obj, string propertyName, object value)
|
|
|
|
|
{
|
|
|
|
|
PropertyInfo pi = obj.GetType().GetProperty(propertyName);
|
|
|
|
|
pi.SetValue(obj, ConvertValue(value, pi.PropertyType), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object GetValue(object obj, string propertyName)
|
|
|
|
|
{
|
|
|
|
|
PropertyInfo pi = obj.GetType().GetProperty(propertyName);
|
|
|
|
|
return pi.GetValue(obj, null);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesStartWithPrefix(Type type, string prefix, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
|
2017-05-28 21:49:36 +00:00
|
|
|
|
{
|
|
|
|
|
return type.GetProperties(flags)
|
|
|
|
|
.Where(p => p.Name.StartsWith(prefix, StringComparison.Ordinal))
|
|
|
|
|
.Select(p => p.Name);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesCanWritePublic(Type type, BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
|
2017-05-28 21:49:36 +00:00
|
|
|
|
{
|
|
|
|
|
return type.GetProperties(flags)
|
|
|
|
|
.Where(p => p.CanWrite && p.GetSetMethod(nonPublic: true).IsPublic)
|
|
|
|
|
.Select(p => p.Name);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static IEnumerable<string> GetPropertiesCanWritePublicDeclared(Type type)
|
2017-05-28 21:49:36 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return GetPropertiesCanWritePublic(type, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
|
2017-05-28 21:49:36 +00:00
|
|
|
|
}
|
|
|
|
|
public static bool HasProperty(this Type type, string name)
|
|
|
|
|
{
|
|
|
|
|
return type.GetProperty(name, BindingFlags.Public | BindingFlags.Instance) != null;
|
|
|
|
|
}
|
|
|
|
|
public static bool HasPropertyAll(this Type type, string name)
|
|
|
|
|
{
|
|
|
|
|
return type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static object ConvertValue(object value, Type type)
|
|
|
|
|
{
|
|
|
|
|
if (type == typeof(DateTime?)) // Used for PKM.MetDate and other similar properties
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return DateTime.TryParseExact(value.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateValue)
|
|
|
|
|
? new DateTime?(dateValue)
|
|
|
|
|
: null;
|
2017-05-28 21:49:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert.ChangeType is suitable for most things
|
|
|
|
|
return Convert.ChangeType(value, type);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-13 05:19:51 +00:00
|
|
|
|
}
|