Add batch editor

Similar to PKHeX's batch editor, probably with some stubbed functionality.

Example to change Oak Trees to apple trees:
=ItemId=60000
.ItemId=60001
;
=ExtensionItemId=60000
.ExtensionItemId=60001

Example to unbury all items:
=IsBuried=True
.IsBuried=False
.IsDropped=True
This commit is contained in:
Kurt
2021-03-21 11:51:57 -07:00
parent c9a4554beb
commit 0798aa5a97
18 changed files with 1115 additions and 35 deletions

View File

@@ -0,0 +1,192 @@
namespace NHSE.WinForms
{
partial class BatchEditor
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.L_PropValue = new System.Windows.Forms.Label();
this.L_PropType = new System.Windows.Forms.Label();
this.B_Add = new System.Windows.Forms.Button();
this.CB_Require = new System.Windows.Forms.ComboBox();
this.CB_Property = new System.Windows.Forms.ComboBox();
this.CB_Format = new System.Windows.Forms.ComboBox();
this.PB_Show = new System.Windows.Forms.ProgressBar();
this.B_Go = new System.Windows.Forms.Button();
this.RTB_Instructions = new System.Windows.Forms.RichTextBox();
this.b = new System.ComponentModel.BackgroundWorker();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.toolTip2 = new System.Windows.Forms.ToolTip(this.components);
this.toolTip3 = new System.Windows.Forms.ToolTip(this.components);
this.SuspendLayout();
//
// L_PropValue
//
this.L_PropValue.AutoSize = true;
this.L_PropValue.Location = new System.Drawing.Point(205, 36);
this.L_PropValue.Name = "L_PropValue";
this.L_PropValue.Size = new System.Drawing.Size(73, 13);
this.L_PropValue.TabIndex = 22;
this.L_PropValue.Text = "PropertyValue";
//
// L_PropType
//
this.L_PropType.AutoSize = true;
this.L_PropType.Location = new System.Drawing.Point(59, 36);
this.L_PropType.Name = "L_PropType";
this.L_PropType.Size = new System.Drawing.Size(70, 13);
this.L_PropType.TabIndex = 21;
this.L_PropType.Text = "PropertyType";
//
// B_Add
//
this.B_Add.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.B_Add.Location = new System.Drawing.Point(325, 11);
this.B_Add.Name = "B_Add";
this.B_Add.Size = new System.Drawing.Size(57, 23);
this.B_Add.TabIndex = 20;
this.B_Add.Text = "Add";
this.B_Add.UseVisualStyleBackColor = true;
this.B_Add.Click += new System.EventHandler(this.B_Add_Click);
//
// CB_Require
//
this.CB_Require.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.CB_Require.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CB_Require.FormattingEnabled = true;
this.CB_Require.Items.AddRange(new object[] {
"Set Equal To",
"Require Equals",
"Require Not Equals"});
this.CB_Require.Location = new System.Drawing.Point(208, 12);
this.CB_Require.Name = "CB_Require";
this.CB_Require.Size = new System.Drawing.Size(111, 21);
this.CB_Require.TabIndex = 19;
//
// CB_Property
//
this.CB_Property.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.CB_Property.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.CB_Property.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.CB_Property.DropDownWidth = 200;
this.CB_Property.FormattingEnabled = true;
this.CB_Property.Location = new System.Drawing.Point(62, 12);
this.CB_Property.Name = "CB_Property";
this.CB_Property.Size = new System.Drawing.Size(140, 21);
this.CB_Property.TabIndex = 18;
this.CB_Property.SelectedIndexChanged += new System.EventHandler(this.CB_Property_SelectedIndexChanged);
//
// CB_Format
//
this.CB_Format.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CB_Format.FormattingEnabled = true;
this.CB_Format.Location = new System.Drawing.Point(12, 12);
this.CB_Format.Name = "CB_Format";
this.CB_Format.Size = new System.Drawing.Size(44, 21);
this.CB_Format.TabIndex = 17;
this.CB_Format.SelectedIndexChanged += new System.EventHandler(this.CB_Format_SelectedIndexChanged);
//
// PB_Show
//
this.PB_Show.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.PB_Show.Location = new System.Drawing.Point(13, 220);
this.PB_Show.Name = "PB_Show";
this.PB_Show.Size = new System.Drawing.Size(307, 21);
this.PB_Show.TabIndex = 16;
//
// B_Go
//
this.B_Go.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.B_Go.Location = new System.Drawing.Point(326, 219);
this.B_Go.Name = "B_Go";
this.B_Go.Size = new System.Drawing.Size(57, 23);
this.B_Go.TabIndex = 15;
this.B_Go.Text = "Run";
this.B_Go.UseVisualStyleBackColor = true;
this.B_Go.Click += new System.EventHandler(this.B_Go_Click);
//
// RTB_Instructions
//
this.RTB_Instructions.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.RTB_Instructions.Location = new System.Drawing.Point(13, 56);
this.RTB_Instructions.Name = "RTB_Instructions";
this.RTB_Instructions.Size = new System.Drawing.Size(370, 158);
this.RTB_Instructions.TabIndex = 14;
this.RTB_Instructions.Text = "";
//
// b
//
this.b.WorkerReportsProgress = true;
//
// BatchEditor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(394, 254);
this.Controls.Add(this.L_PropValue);
this.Controls.Add(this.L_PropType);
this.Controls.Add(this.B_Add);
this.Controls.Add(this.CB_Require);
this.Controls.Add(this.CB_Property);
this.Controls.Add(this.CB_Format);
this.Controls.Add(this.PB_Show);
this.Controls.Add(this.B_Go);
this.Controls.Add(this.RTB_Instructions);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = global::NHSE.WinForms.Properties.Resources.icon;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "BatchEditor";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Batch Editor";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SysBotRAMEdit_FormClosing);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label L_PropValue;
private System.Windows.Forms.Label L_PropType;
private System.Windows.Forms.Button B_Add;
private System.Windows.Forms.ComboBox CB_Require;
private System.Windows.Forms.ComboBox CB_Property;
private System.Windows.Forms.ComboBox CB_Format;
private System.Windows.Forms.ProgressBar PB_Show;
private System.Windows.Forms.Button B_Go;
private System.Windows.Forms.RichTextBox RTB_Instructions;
private System.ComponentModel.BackgroundWorker b;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.ToolTip toolTip2;
private System.Windows.Forms.ToolTip toolTip3;
}
}

View File

@@ -0,0 +1,159 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using NHSE.Core;
namespace NHSE.WinForms
{
public partial class BatchEditor : Form
{
private readonly ItemMutator Mutator;
private readonly ItemProcessor Processor;
private readonly IReadOnlyList<Item> Items;
private readonly Item item;
private int currentFormat = -1;
public BatchEditor(IReadOnlyList<Item> items, Item item)
{
InitializeComponent();
Items = items;
this.item = item;
Mutator = new ItemMutator();
Processor = new ItemProcessor(Mutator);
CB_Format.Items.Clear();
CB_Format.Items.Add("Any");
foreach (Type t in Mutator.Reflect.Types)
CB_Format.Items.Add(t.Name.ToLower());
CB_Format.Items.Add("All");
CB_Format.SelectedIndex = CB_Require.SelectedIndex = 0;
toolTip1.SetToolTip(CB_Property, "Name");
toolTip2.SetToolTip(L_PropType, "Type");
toolTip3.SetToolTip(L_PropValue, "Value");
}
private void SysBotRAMEdit_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void B_Go_Click(object sender, EventArgs e)
{
RunBackgroundWorker();
}
private void B_Add_Click(object sender, EventArgs e)
{
if (CB_Property.SelectedIndex < 0)
{ WinFormsUtil.Alert("Invalid Property"); return; }
var prefix = StringInstruction.Prefixes;
string s = prefix[CB_Require.SelectedIndex] + CB_Property.Items[CB_Property.SelectedIndex].ToString() + StringInstruction.SplitInstruction;
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(Mutator.Reflect.Properties[format]);
CB_Property.SelectedIndex = 0;
currentFormat = format;
}
private void CB_Property_SelectedIndexChanged(object sender, EventArgs e)
{
L_PropType.Text = Mutator.GetPropertyType(CB_Property.Text, CB_Format.SelectedIndex);
if (Mutator.TryGetHasProperty(item, CB_Property.Text, out var pi))
{
L_PropValue.Text = pi.GetValue(item)?.ToString();
L_PropType.ForeColor = L_PropValue.ForeColor; // reset color
}
else // no property, flag
{
L_PropValue.Text = string.Empty;
L_PropType.ForeColor = Color.Red;
}
}
private void RunBackgroundWorker()
{
if (RTB_Instructions.Lines.Any(line => line.Length == 0))
{ WinFormsUtil.Error("Invalid instruction detected."); return; }
var sets = StringInstructionSet.GetBatchSets(RTB_Instructions.Lines).ToArray();
if (sets.Any(s => s.Filters.Any(z => string.IsNullOrWhiteSpace(z.PropertyValue))))
{ WinFormsUtil.Error("Filter empty."); return; }
if (sets.Any(z => z.Instructions.Count == 0))
{ WinFormsUtil.Error("No instructions."); return; }
var emptyVal = sets.SelectMany(s => s.Instructions.Where(z => string.IsNullOrWhiteSpace(z.PropertyValue))).ToArray();
if (emptyVal.Length > 0)
{
string props = string.Join(", ", emptyVal.Select(z => z.PropertyName));
string invalid = "Property empty." + Environment.NewLine + props;
if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, invalid, "Continue?"))
return;
}
RTB_Instructions.Enabled = B_Go.Enabled = false;
RunBatchEdit(sets);
}
private void RunBatchEdit(StringInstructionSet[] sets)
{
bool finished = false, displayed = false; // hack cuz DoWork event isn't cleared after completion
b.DoWork += (sender, e) =>
{
if (finished)
return;
foreach (var s in sets)
{
foreach (var i in Items)
Processor.Process(i, s.Filters, s.Instructions);
}
finished = true;
};
b.ProgressChanged += (sender, e) => SetProgressBar(e.ProgressPercentage);
b.RunWorkerCompleted += (sender, e) =>
{
string result = Processor.GetEditorResults(sets);
if (!displayed) WinFormsUtil.Alert(result);
displayed = true;
RTB_Instructions.Enabled = B_Go.Enabled = true;
SetupProgressBar(0);
};
b.RunWorkerAsync();
}
// Progress Bar
private void SetupProgressBar(int count)
{
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();
}
private void SetProgressBar(int i)
{
if (PB_Show.InvokeRequired)
PB_Show.Invoke((MethodInvoker)(() => PB_Show.Value = i));
else
PB_Show.Value = i;
}
}
}

View File

@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="b.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>367, 17</value>
</metadata>
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>426, 17</value>
</metadata>
<metadata name="toolTip2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>523, 17</value>
</metadata>
<metadata name="toolTip3.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>620, 17</value>
</metadata>
</root>