From 0798aa5a97f2b2e95f95a832836bfd6df1a535e2 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 21 Mar 2021 11:51:57 -0700 Subject: [PATCH] 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 --- NHSE.Core/Editing/Batch/BatchMutator.cs | 11 + NHSE.Core/Editing/Batch/BatchProcessor.cs | 75 ++++++ NHSE.Core/Editing/Batch/ItemMutator.cs | 215 ++++++++++++++++++ NHSE.Core/Editing/Batch/ItemProcessor.cs | 12 + NHSE.Core/Editing/Batch/ItemReflection.cs | 50 ++++ NHSE.Core/Editing/Batch/ModifyResult.cs | 28 +++ NHSE.Core/Editing/Batch/StringInstruction.cs | 101 ++++++++ .../Editing/Batch/StringInstructionSet.cs | 38 ++++ NHSE.Core/Editing/ItemRequest/ItemParser.cs | 4 +- NHSE.Core/Structures/Map/Layers/ItemLayer.cs | 45 +++- NHSE.Core/Util/ReflectUtil.cs | 3 +- NHSE.WinForms/NHSE.WinForms.csproj | 3 + .../Subforms/Map/FieldItemEditor.Designer.cs | 72 +++--- NHSE.WinForms/Subforms/Map/FieldItemEditor.cs | 8 + NHSE.WinForms/Subforms/PlayerItemEditor.cs | 2 +- .../Subforms/SysBot/BatchEditor.Designer.cs | 192 ++++++++++++++++ NHSE.WinForms/Subforms/SysBot/BatchEditor.cs | 159 +++++++++++++ .../Subforms/SysBot/BatchEditor.resx | 132 +++++++++++ 18 files changed, 1115 insertions(+), 35 deletions(-) create mode 100644 NHSE.Core/Editing/Batch/BatchMutator.cs create mode 100644 NHSE.Core/Editing/Batch/BatchProcessor.cs create mode 100644 NHSE.Core/Editing/Batch/ItemMutator.cs create mode 100644 NHSE.Core/Editing/Batch/ItemProcessor.cs create mode 100644 NHSE.Core/Editing/Batch/ItemReflection.cs create mode 100644 NHSE.Core/Editing/Batch/ModifyResult.cs create mode 100644 NHSE.Core/Editing/Batch/StringInstruction.cs create mode 100644 NHSE.Core/Editing/Batch/StringInstructionSet.cs create mode 100644 NHSE.WinForms/Subforms/SysBot/BatchEditor.Designer.cs create mode 100644 NHSE.WinForms/Subforms/SysBot/BatchEditor.cs create mode 100644 NHSE.WinForms/Subforms/SysBot/BatchEditor.resx diff --git a/NHSE.Core/Editing/Batch/BatchMutator.cs b/NHSE.Core/Editing/Batch/BatchMutator.cs new file mode 100644 index 0000000..9b47429 --- /dev/null +++ b/NHSE.Core/Editing/Batch/BatchMutator.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace NHSE.Core +{ + public abstract class BatchMutator where T : class + { + protected const string CONST_RAND = "$rand"; + + public abstract ModifyResult Modify(T item, IEnumerable filters, IEnumerable modifications); + } +} diff --git a/NHSE.Core/Editing/Batch/BatchProcessor.cs b/NHSE.Core/Editing/Batch/BatchProcessor.cs new file mode 100644 index 0000000..bd8f21c --- /dev/null +++ b/NHSE.Core/Editing/Batch/BatchProcessor.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace NHSE.Core +{ + /// + /// Carries out a batch edit and contains information summarizing the results. + /// + public abstract class BatchProcessor where T : class + { + private int Modified { get; set; } + private int Iterated { get; set; } + private int Failed { get; set; } + + protected readonly BatchMutator Mutator; + protected BatchProcessor(BatchMutator mut) => Mutator = mut; + + protected abstract bool CanModify(T item); + protected abstract bool Finalize(T item); + + /// + /// Tries to modify the . + /// + /// Object to modify. + /// Filters which must be satisfied prior to any modifications being made. + /// Modifications to perform on the item. + /// Result of the attempted modification. + public bool Process(T item, IEnumerable filters, IEnumerable modifications) + { + if (!CanModify(item)) + return false; + + var result = Mutator.Modify(item, filters, modifications); + if (result != ModifyResult.Invalid) + Iterated++; + if (result == ModifyResult.Error) + Failed++; + if (result != ModifyResult.Modified) + return false; + + Finalize(item); + Modified++; + return true; + } + + /// + /// Gets a message indicating the overall result of all modifications performed across multiple Batch Edit jobs. + /// + /// Collection of modifications. + /// Friendly (multi-line) string indicating the result of the batch edits. + public string GetEditorResults(ICollection sets) + { + if (sets.Count == 0) + return "No instructions present."; + int ctr = Modified / sets.Count; + int len = Iterated / sets.Count; + string maybe = sets.Count == 1 ? string.Empty : "~"; + string result = $"Success: {maybe}{ctr}/{len}"; + if (Failed > 0) + result += Environment.NewLine + maybe + $"Failed: {Failed} not processed."; + return result; + } + + public void Execute(IList lines, IEnumerable data) + { + var sets = StringInstructionSet.GetBatchSets(lines).ToArray(); + foreach (var pk in data) + { + foreach (var set in sets) + Process(pk, set.Filters, set.Instructions); + } + } + } +} \ No newline at end of file diff --git a/NHSE.Core/Editing/Batch/ItemMutator.cs b/NHSE.Core/Editing/Batch/ItemMutator.cs new file mode 100644 index 0000000..ebfd42f --- /dev/null +++ b/NHSE.Core/Editing/Batch/ItemMutator.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Reflection; + +namespace NHSE.Core +{ + public class ItemMutator : BatchMutator + { + public readonly ItemReflection Reflect = ItemReflection.Default; + + public override ModifyResult Modify(Item item, IEnumerable filters, IEnumerable modifications) + { + var pi = Reflect.Props[Array.IndexOf(Reflect.Types, item.GetType())]; + foreach (var cmd in filters) + { + try + { + if (!IsFilterMatch(cmd, item, pi)) + return ModifyResult.Filtered; + } +#pragma warning disable CA1031 // Do not catch general exception types + // Swallow any error because this can be malformed user input. + catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types + { + Debug.WriteLine($"Failed to compare: {ex.Message} - {cmd.PropertyName} {cmd.PropertyValue}"); + return ModifyResult.Error; + } + } + + ModifyResult result = ModifyResult.Modified; + foreach (var cmd in modifications) + { + try + { + var tmp = SetProperty(cmd, item, pi); + if (tmp != ModifyResult.Modified) + result = tmp; + } +#pragma warning disable CA1031 // Do not catch general exception types + // Swallow any error because this can be malformed user input. + catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types + { + Debug.WriteLine($"Failed to modify: {ex.Message} - {cmd.PropertyName} {cmd.PropertyValue}"); + } + } + return result; + } + + /// + /// Sets the if the should be filtered due to the provided. + /// + /// Command Filter + /// Pokémon to check. + /// PropertyInfo cache (optional) + /// True if filtered, else false. + private static ModifyResult SetProperty(StringInstruction cmd, Item item, IReadOnlyDictionary props) + { + if (SetComplexProperty(item, cmd)) + return ModifyResult.Modified; + + if (!props.TryGetValue(cmd.PropertyName, out var pi)) + return ModifyResult.Error; + + if (!pi.CanWrite) + return ModifyResult.Error; + + object val = cmd.Random ? (object)cmd.RandomValue : cmd.PropertyValue; + ReflectUtil.SetValue(pi, item, val); + return ModifyResult.Modified; + } + + private static bool SetComplexProperty(Item item, StringInstruction cmd) + { + // Zeroed out item? + if (cmd.PropertyName == nameof(Item.ItemId)) + { + if (!int.TryParse(cmd.PropertyValue, out var val)) + return false; + if (val is not 0 or 0xFFFE) + return false; + item.Delete(); + return true; + } + return false; + } + + /// + /// Tries to fetch the property from the cache of available properties. + /// + /// Pokémon to check + /// Property Name to check + /// Property Info retrieved (if any). + /// True if has property, false if does not. + public bool TryGetHasProperty(Item item, string name, [NotNullWhen(true)] out PropertyInfo? pi) + { + var type = item.GetType(); + return TryGetHasProperty(type, name, out pi); + } + + /// + /// Tries to fetch the property from the cache of available properties. + /// + /// Type to check + /// Property Name to check + /// Property Info retrieved (if any). + /// True if has property, false if does not. + public bool TryGetHasProperty(Type type, string name, [NotNullWhen(true)] out PropertyInfo? pi) + { + var index = Array.IndexOf(Reflect.Types, type); + if (index < 0) + { + pi = null; + return false; + } + var props = Reflect.Props[index]; + return props.TryGetValue(name, out pi); + } + + /// + /// Gets the type of the property using the saved cache of properties. + /// + /// Property Name to fetch the type for + /// Type index. Leave empty (0) for a nonspecific format. + /// Short name of the property's type. + public string? GetPropertyType(string propertyName, int typeIndex = 0) + { + if (typeIndex == 0) // Any + { + foreach (var p in Reflect.Props) + { + if (p.TryGetValue(propertyName, out var pi)) + return pi.PropertyType.Name; + } + return null; + } + + int index = typeIndex - 1 >= Reflect.Props.Length ? 0 : typeIndex - 1; // All vs Specific + var pr = Reflect.Props[index]; + if (!pr.TryGetValue(propertyName, out var info)) + return null; + return info.PropertyType.Name; + } + + /// + /// Checks if the object is filtered by the provided . + /// + /// Filters which must be satisfied. + /// Object to check. + /// True if matches all filters. + public bool IsFilterMatch(IEnumerable filters, Item item) => filters.All(z => IsFilterMatch(z, item, Reflect.Props[Array.IndexOf(Reflect.Types, item.GetType())])); + + /// + /// Checks if the should be filtered due to the provided. + /// + /// Command Filter + /// Pokémon to check. + /// PropertyInfo cache (optional) + /// True if filter matches, else false. + private static bool IsFilterMatch(StringInstruction cmd, Item item, IReadOnlyDictionary props) + { + return IsPropertyFiltered(cmd, item, props); + } + + /// + /// Checks if the should be filtered due to the provided. + /// + /// Command Filter + /// Pokémon to check. + /// PropertyInfo cache + /// True if filtered, else false. + private static bool IsPropertyFiltered(StringInstruction cmd, Item item, IReadOnlyDictionary props) + { + if (!props.TryGetValue(cmd.PropertyName, out var pi)) + return false; + if (!pi.CanRead) + return false; + return pi.IsValueEqual(item, cmd.PropertyValue) == cmd.Evaluator; + } + + /// + /// Checks if the object is filtered by the provided . + /// + /// Filters which must be satisfied. + /// Object to check. + /// True if matches all filters. + public static bool IsFilterMatch(IEnumerable filters, object obj) + { + foreach (var cmd in filters) + { + if (!ReflectUtil.HasProperty(obj, cmd.PropertyName, out var pi)) + return false; + try + { + if (pi.IsValueEqual(obj, cmd.PropertyValue) == cmd.Evaluator) + continue; + } +#pragma warning disable CA1031 // Do not catch general exception types + // User provided inputs can mismatch the type's required value format, and fail to be compared. + catch (Exception e) +#pragma warning restore CA1031 // Do not catch general exception types + { + Debug.WriteLine($"Unable to compare {cmd.PropertyName} to {cmd.PropertyValue}."); + Debug.WriteLine(e.Message); + } + return false; + } + return true; + } + } +} diff --git a/NHSE.Core/Editing/Batch/ItemProcessor.cs b/NHSE.Core/Editing/Batch/ItemProcessor.cs new file mode 100644 index 0000000..09bef22 --- /dev/null +++ b/NHSE.Core/Editing/Batch/ItemProcessor.cs @@ -0,0 +1,12 @@ +namespace NHSE.Core +{ + public class ItemProcessor : BatchProcessor + { + public ItemProcessor(BatchMutator mut) : base(mut) + { + } + + protected override bool CanModify(Item item) => true; + protected override bool Finalize(Item item) => true; + } +} diff --git a/NHSE.Core/Editing/Batch/ItemReflection.cs b/NHSE.Core/Editing/Batch/ItemReflection.cs new file mode 100644 index 0000000..64cbfb3 --- /dev/null +++ b/NHSE.Core/Editing/Batch/ItemReflection.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace NHSE.Core +{ + public class ItemReflection + { + public static ItemReflection Default { get; } = new(); + + public readonly Type[] Types = { typeof(Item), typeof(VillagerItem) }; + public readonly Dictionary[] Props; + public readonly string[][] Properties; + + public ItemReflection() + { + Props = Types + .Select(z => ReflectUtil.GetAllPropertyInfoPublic(z) + .GroupBy(p => p.Name) + .Select(g => g.First()) + .ToDictionary(p => p.Name)) + .ToArray(); + + Properties = GetPropArray(); + } + + public string[][] GetPropArray() + { + var p = new string[Types.Length][]; + for (int i = 0; i < p.Length; i++) + { + var pz = ReflectUtil.GetPropertiesPublic(Types[i]); + p[i] = pz.OrderBy(a => a).ToArray(); + } + + // Properties for any + var any = ReflectUtil.GetPropertiesPublic(typeof(Item)).Union(p.SelectMany(a => a)).OrderBy(a => a).ToArray(); + // Properties shared by all + var all = p.Aggregate(new HashSet(p[0]), (h, e) => { h.IntersectWith(e); return h; }).OrderBy(a => a).ToArray(); + + var p1 = new string[Types.Length + 2][]; + Array.Copy(p, 0, p1, 1, p.Length); + p1[0] = any; + p1[p1.Length - 1] = all; + + return p1; + } + } +} diff --git a/NHSE.Core/Editing/Batch/ModifyResult.cs b/NHSE.Core/Editing/Batch/ModifyResult.cs new file mode 100644 index 0000000..e3988e2 --- /dev/null +++ b/NHSE.Core/Editing/Batch/ModifyResult.cs @@ -0,0 +1,28 @@ +namespace NHSE.Core +{ + /// + /// Batch Editor Modification result for an individual item. + /// + public enum ModifyResult + { + /// + /// The data has invalid data and is not a suitable candidate for modification. + /// + Invalid, + + /// + /// An error was occurred while iterating modifications for this data. + /// + Error, + + /// + /// The data was skipped due to a matching Filter. + /// + Filtered, + + /// + /// The data was modified. + /// + Modified, + } +} diff --git a/NHSE.Core/Editing/Batch/StringInstruction.cs b/NHSE.Core/Editing/Batch/StringInstruction.cs new file mode 100644 index 0000000..e82b368 --- /dev/null +++ b/NHSE.Core/Editing/Batch/StringInstruction.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +namespace NHSE.Core +{ + /// + /// Batch Editing instruction + /// + /// + /// Can be a filter (skip), or a modification instruction (modify) + /// + /// + /// + /// + public sealed class StringInstruction + { + public string PropertyName { get; } + public string PropertyValue { get; private set; } + public bool Evaluator { get; private init; } + + public StringInstruction(string name, string value) + { + PropertyName = name; + PropertyValue = value; + } + + public void SetScreenedValue(string[] arr) + { + int index = Array.IndexOf(arr, PropertyValue); + PropertyValue = index > -1 ? index.ToString() : PropertyValue; + } + + public static readonly IReadOnlyList Prefixes = new[] { Apply, Require, Exclude }; + private const char Exclude = '!'; + private const char Require = '='; + private const char Apply = '.'; + private const char SplitRange = ','; + + /// + /// Character which divides a property and a value. + /// + /// + /// Example: + /// =Species=1 + /// The second = is the split. + /// + public const char SplitInstruction = '='; + + // Extra Functionality + private int RandomMinimum, RandomMaximum; + public bool Random { get; private set; } + public int RandomValue => RandUtil.Rand.Next(RandomMinimum, RandomMaximum + 1); + + public void SetRandRange(string pv) + { + string str = pv.Substring(1); + var split = str.Split(SplitRange); + int.TryParse(split[0], out RandomMinimum); + int.TryParse(split[1], out RandomMaximum); + + if (RandomMinimum == RandomMaximum) + { + PropertyValue = RandomMinimum.ToString(); + Debug.WriteLine(PropertyName + " randomization range Min/Max same?"); + } + else + { + Random = true; + } + } + + public static IEnumerable GetFilters(IEnumerable lines) + { + var raw = GetRelevantStrings(lines, Exclude, Require); + return from line in raw + let eval = line[0] == Require + let split = line.Substring(1).Split(SplitInstruction) + where split.Length == 2 && !string.IsNullOrWhiteSpace(split[0]) + select new StringInstruction(split[0], split[1]) { Evaluator = eval }; + } + + public static IEnumerable GetInstructions(IEnumerable lines) + { + var raw = GetRelevantStrings(lines, Apply).Select(line => line.Substring(1)); + return from line in raw + select line.Split(SplitInstruction) into split + where split.Length == 2 + select new StringInstruction(split[0], split[1]); + } + + /// + /// Weeds out invalid lines and only returns those with a valid first character. + /// + private static IEnumerable GetRelevantStrings(IEnumerable lines, params char[] pieces) + { + return lines.Where(line => !string.IsNullOrEmpty(line) && pieces.Any(z => z == line[0])); + } + } +} diff --git a/NHSE.Core/Editing/Batch/StringInstructionSet.cs b/NHSE.Core/Editing/Batch/StringInstructionSet.cs new file mode 100644 index 0000000..f903e41 --- /dev/null +++ b/NHSE.Core/Editing/Batch/StringInstructionSet.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using System.Linq; + +namespace NHSE.Core +{ + /// + /// Processes input of strings into a list of valid Filters and Instructions. + /// + public sealed class StringInstructionSet + { + public readonly IReadOnlyList Filters; + public readonly IReadOnlyList Instructions; + + private const string SetSeparator = ";"; + + public StringInstructionSet(IReadOnlyList filters, IReadOnlyList instructions) + { + Filters = filters; + Instructions = instructions; + } + + public StringInstructionSet(ICollection set) + { + Filters = StringInstruction.GetFilters(set).ToList(); + Instructions = StringInstruction.GetInstructions(set).ToList(); + } + + public static IEnumerable GetBatchSets(IList lines) + { + int start = 0; + while (start < lines.Count) + { + var list = lines.Skip(start).TakeWhile(_ => !lines[start++].StartsWith(SetSeparator)).ToList(); + yield return new StringInstructionSet(list); + } + } + } +} diff --git a/NHSE.Core/Editing/ItemRequest/ItemParser.cs b/NHSE.Core/Editing/ItemRequest/ItemParser.cs index 8e5ff08..6ae8e2c 100644 --- a/NHSE.Core/Editing/ItemRequest/ItemParser.cs +++ b/NHSE.Core/Editing/ItemRequest/ItemParser.cs @@ -264,8 +264,10 @@ public static Item GetItem(string itemName, string lang = "en") return parsedItem; if (gStrings.HasAssociatedItems(itemName, out var items)) - if (items != null && items.Count == 1) + { + if (items?.Count == 1) return new Item((ushort)items[0].Value); + } return Item.NO_ITEM; } diff --git a/NHSE.Core/Structures/Map/Layers/ItemLayer.cs b/NHSE.Core/Structures/Map/Layers/ItemLayer.cs index 0592a92..4de631c 100644 --- a/NHSE.Core/Structures/Map/Layers/ItemLayer.cs +++ b/NHSE.Core/Structures/Map/Layers/ItemLayer.cs @@ -7,7 +7,9 @@ public abstract class ItemLayer : MapGrid { public readonly Item[] Tiles; - protected ItemLayer(Item[] tiles, int w, int h) : this(tiles, w, h, w, h) { } + protected ItemLayer(Item[] tiles, int w, int h) : this(tiles, w, h, w, h) + { + } protected ItemLayer(Item[] tiles, int w, int h, int gw, int gh) : base(gw, gh, w, h) { @@ -52,6 +54,7 @@ public int RemoveAll(in int xmin, in int ymin, in int width, in int height, Func count++; } } + return count; } @@ -169,7 +172,47 @@ public int ReplaceAll(Item oldItem, Item newItem, in int xmin, in int ymin, in i count++; } } + return count; } + + public int ClearDanglingExtensions(in int xmin, in int ymin, in int width, in int height) + { + int count = 0; + for (int x = xmin; x < xmin + width; x++) + { + for (int y = ymin; y < ymin + height; y++) + { + var t = GetTile(x, y); + if (IsValidExtension(t, x, y)) + continue; + t.Delete(); + count++; + } + } + return count; + } + + private bool IsValidExtension(Item t, int x, int y) + { + if (!t.IsExtension) + return true; + var parentX = x - t.ExtensionX; + var parentY = y - t.ExtensionY; + + try + { + var parent = GetTile(parentX, parentY); + if (parent.ItemId == t.ExtensionItemId) + return true; + } +#pragma warning disable CA1031 // Do not catch general exception types + catch + { + // corrupt? + } +#pragma warning restore CA1031 // Do not catch general exception types + return false; + } } } diff --git a/NHSE.Core/Util/ReflectUtil.cs b/NHSE.Core/Util/ReflectUtil.cs index df34726..f2cfa40 100644 --- a/NHSE.Core/Util/ReflectUtil.cs +++ b/NHSE.Core/Util/ReflectUtil.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Reflection; @@ -113,7 +114,7 @@ public static IEnumerable GetAllTypeInfo(this TypeInfo? typeInfo) } } - public static bool HasProperty(object obj, string name, out PropertyInfo? pi) => (pi = GetPropertyInfo(obj.GetType().GetTypeInfo(), name)) != null; + public static bool HasProperty(object obj, string name, [NotNullWhen(true)] out PropertyInfo? pi) => (pi = GetPropertyInfo(obj.GetType().GetTypeInfo(), name)) != null; public static PropertyInfo? GetPropertyInfo(this TypeInfo typeInfo, string name) { diff --git a/NHSE.WinForms/NHSE.WinForms.csproj b/NHSE.WinForms/NHSE.WinForms.csproj index 821bb53..86aea31 100644 --- a/NHSE.WinForms/NHSE.WinForms.csproj +++ b/NHSE.WinForms/NHSE.WinForms.csproj @@ -97,6 +97,9 @@ Form + + Form + Form diff --git a/NHSE.WinForms/Subforms/Map/FieldItemEditor.Designer.cs b/NHSE.WinForms/Subforms/Map/FieldItemEditor.Designer.cs index 2be3ba2..fce1a50 100644 --- a/NHSE.WinForms/Subforms/Map/FieldItemEditor.Designer.cs +++ b/NHSE.WinForms/Subforms/Map/FieldItemEditor.Designer.cs @@ -140,13 +140,14 @@ private void InitializeComponent() this.B_SetAllTerrain = new System.Windows.Forms.ToolStripMenuItem(); this.B_SetAllRoadTiles = new System.Windows.Forms.ToolStripMenuItem(); this.B_ClearPlacedDesigns = new System.Windows.Forms.ToolStripMenuItem(); + this.B_ImportPlacedDesigns = new System.Windows.Forms.ToolStripMenuItem(); + this.B_ExportPlacedDesigns = new System.Windows.Forms.ToolStripMenuItem(); this.RB_Item = new System.Windows.Forms.RadioButton(); this.RB_Terrain = new System.Windows.Forms.RadioButton(); this.L_TileMode = new System.Windows.Forms.Label(); this.CHK_RedirectExtensionLoad = new System.Windows.Forms.CheckBox(); this.CHK_FieldItemSnap = new System.Windows.Forms.CheckBox(); - this.B_ImportPlacedDesigns = new System.Windows.Forms.ToolStripMenuItem(); - this.B_ExportPlacedDesigns = new System.Windows.Forms.ToolStripMenuItem(); + this.Menu_Batch = new System.Windows.Forms.ToolStripMenuItem(); this.CM_Click.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.PB_Map)).BeginInit(); this.CM_Picture.SuspendLayout(); @@ -472,104 +473,105 @@ private void InitializeComponent() this.B_RemoveAll, this.toolStripSeparator1, this.B_WaterFlowers, - this.Menu_Spawn}); + this.Menu_Spawn, + this.Menu_Batch}); this.CM_Remove.Name = "CM_Picture"; this.CM_Remove.ShowImageMargin = false; - this.CM_Remove.Size = new System.Drawing.Size(124, 296); + this.CM_Remove.Size = new System.Drawing.Size(156, 340); // // B_RemoveAllWeeds // this.B_RemoveAllWeeds.Name = "B_RemoveAllWeeds"; - this.B_RemoveAllWeeds.Size = new System.Drawing.Size(123, 22); + this.B_RemoveAllWeeds.Size = new System.Drawing.Size(155, 22); this.B_RemoveAllWeeds.Text = "Weeds"; this.B_RemoveAllWeeds.Click += new System.EventHandler(this.B_RemoveAllWeeds_Click); // // B_RemoveAllTrees // this.B_RemoveAllTrees.Name = "B_RemoveAllTrees"; - this.B_RemoveAllTrees.Size = new System.Drawing.Size(123, 22); + this.B_RemoveAllTrees.Size = new System.Drawing.Size(155, 22); this.B_RemoveAllTrees.Text = "Trees"; this.B_RemoveAllTrees.Click += new System.EventHandler(this.B_RemoveAllTrees_Click); // // B_RemovePlants // this.B_RemovePlants.Name = "B_RemovePlants"; - this.B_RemovePlants.Size = new System.Drawing.Size(123, 22); + this.B_RemovePlants.Size = new System.Drawing.Size(155, 22); this.B_RemovePlants.Text = "Plants"; this.B_RemovePlants.Click += new System.EventHandler(this.B_RemovePlants_Click); // // B_RemoveObjects // this.B_RemoveObjects.Name = "B_RemoveObjects"; - this.B_RemoveObjects.Size = new System.Drawing.Size(123, 22); + this.B_RemoveObjects.Size = new System.Drawing.Size(155, 22); this.B_RemoveObjects.Text = "Objects"; this.B_RemoveObjects.Click += new System.EventHandler(this.B_RemoveObjects_Click); // // B_RemovePlacedItems // this.B_RemovePlacedItems.Name = "B_RemovePlacedItems"; - this.B_RemovePlacedItems.Size = new System.Drawing.Size(123, 22); + this.B_RemovePlacedItems.Size = new System.Drawing.Size(155, 22); this.B_RemovePlacedItems.Text = "Placed Items"; this.B_RemovePlacedItems.Click += new System.EventHandler(this.B_RemovePlacedItems_Click); // // B_RemoveFences // this.B_RemoveFences.Name = "B_RemoveFences"; - this.B_RemoveFences.Size = new System.Drawing.Size(123, 22); + this.B_RemoveFences.Size = new System.Drawing.Size(155, 22); this.B_RemoveFences.Text = "Fences"; this.B_RemoveFences.Click += new System.EventHandler(this.B_RemoveFences_Click); // // B_RemoveBranches // this.B_RemoveBranches.Name = "B_RemoveBranches"; - this.B_RemoveBranches.Size = new System.Drawing.Size(123, 22); + this.B_RemoveBranches.Size = new System.Drawing.Size(155, 22); this.B_RemoveBranches.Text = "Branches"; this.B_RemoveBranches.Click += new System.EventHandler(this.B_RemoveBranches_Click); // // B_RemoveShells // this.B_RemoveShells.Name = "B_RemoveShells"; - this.B_RemoveShells.Size = new System.Drawing.Size(123, 22); + this.B_RemoveShells.Size = new System.Drawing.Size(155, 22); this.B_RemoveShells.Text = "Shells"; this.B_RemoveShells.Click += new System.EventHandler(this.B_RemoveShells_Click); // // B_RemoveFlowers // this.B_RemoveFlowers.Name = "B_RemoveFlowers"; - this.B_RemoveFlowers.Size = new System.Drawing.Size(123, 22); + this.B_RemoveFlowers.Size = new System.Drawing.Size(155, 22); this.B_RemoveFlowers.Text = "Flowers"; this.B_RemoveFlowers.Click += new System.EventHandler(this.B_RemoveFlowers_Click); // // B_FillHoles // this.B_FillHoles.Name = "B_FillHoles"; - this.B_FillHoles.Size = new System.Drawing.Size(123, 22); + this.B_FillHoles.Size = new System.Drawing.Size(155, 22); this.B_FillHoles.Text = "Holes"; this.B_FillHoles.Click += new System.EventHandler(this.B_FillHoles_Click); // // B_RemoveAll // this.B_RemoveAll.Name = "B_RemoveAll"; - this.B_RemoveAll.Size = new System.Drawing.Size(123, 22); + this.B_RemoveAll.Size = new System.Drawing.Size(155, 22); this.B_RemoveAll.Text = "All"; this.B_RemoveAll.Click += new System.EventHandler(this.B_RemoveAll_Click); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; - this.toolStripSeparator1.Size = new System.Drawing.Size(120, 6); + this.toolStripSeparator1.Size = new System.Drawing.Size(152, 6); // // B_WaterFlowers // this.B_WaterFlowers.Name = "B_WaterFlowers"; - this.B_WaterFlowers.Size = new System.Drawing.Size(123, 22); + this.B_WaterFlowers.Size = new System.Drawing.Size(155, 22); this.B_WaterFlowers.Text = "Water Flowers"; this.B_WaterFlowers.Click += new System.EventHandler(this.B_WaterFlowers_Click); // // Menu_Spawn // this.Menu_Spawn.Name = "Menu_Spawn"; - this.Menu_Spawn.Size = new System.Drawing.Size(123, 22); + this.Menu_Spawn.Size = new System.Drawing.Size(155, 22); this.Menu_Spawn.Text = "Spawn..."; this.Menu_Spawn.Click += new System.EventHandler(this.Menu_Spawn_Click); // @@ -1262,7 +1264,7 @@ private void InitializeComponent() this.B_ExportPlacedDesigns}); this.CM_Terrain.Name = "CM_Picture"; this.CM_Terrain.ShowImageMargin = false; - this.CM_Terrain.Size = new System.Drawing.Size(225, 158); + this.CM_Terrain.Size = new System.Drawing.Size(225, 136); // // B_ZeroElevation // @@ -1292,6 +1294,20 @@ private void InitializeComponent() this.B_ClearPlacedDesigns.Text = "Clear all Placed Designs"; this.B_ClearPlacedDesigns.Click += new System.EventHandler(this.B_ClearPlacedDesigns_Click); // + // B_ImportPlacedDesigns + // + this.B_ImportPlacedDesigns.Name = "B_ImportPlacedDesigns"; + this.B_ImportPlacedDesigns.Size = new System.Drawing.Size(224, 22); + this.B_ImportPlacedDesigns.Text = "Import all Placed Design Choices"; + this.B_ImportPlacedDesigns.Click += new System.EventHandler(this.B_ImportPlacedDesigns_Click); + // + // B_ExportPlacedDesigns + // + this.B_ExportPlacedDesigns.Name = "B_ExportPlacedDesigns"; + this.B_ExportPlacedDesigns.Size = new System.Drawing.Size(224, 22); + this.B_ExportPlacedDesigns.Text = "Export all Placed Design Choices"; + this.B_ExportPlacedDesigns.Click += new System.EventHandler(this.B_ExportPlacedDesigns_Click); + // // RB_Item // this.RB_Item.CheckAlign = System.Drawing.ContentAlignment.MiddleRight; @@ -1349,19 +1365,12 @@ private void InitializeComponent() this.CHK_FieldItemSnap.Text = "Snap Field Items to Grid on Set"; this.CHK_FieldItemSnap.UseVisualStyleBackColor = true; // - // B_ImportPlacedDesigns + // Menu_Batch // - this.B_ImportPlacedDesigns.Name = "B_ImportPlacedDesigns"; - this.B_ImportPlacedDesigns.Size = new System.Drawing.Size(224, 22); - this.B_ImportPlacedDesigns.Text = "Import all Placed Design Choices"; - this.B_ImportPlacedDesigns.Click += new System.EventHandler(this.B_ImportPlacedDesigns_Click); - // - // B_ExportPlacedDesigns - // - this.B_ExportPlacedDesigns.Name = "B_ExportPlacedDesigns"; - this.B_ExportPlacedDesigns.Size = new System.Drawing.Size(224, 22); - this.B_ExportPlacedDesigns.Text = "Export all Placed Design Choices"; - this.B_ExportPlacedDesigns.Click += new System.EventHandler(this.B_ExportPlacedDesigns_Click); + this.Menu_Batch.Name = "Menu_Batch"; + this.Menu_Batch.Size = new System.Drawing.Size(155, 22); + this.Menu_Batch.Text = "Batch Editor"; + this.Menu_Batch.Click += new System.EventHandler(this.Menu_Bulk_Click); // // FieldItemEditor // @@ -1554,5 +1563,6 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem B_RemoveAllTrees; private System.Windows.Forms.ToolStripMenuItem B_ImportPlacedDesigns; private System.Windows.Forms.ToolStripMenuItem B_ExportPlacedDesigns; + private System.Windows.Forms.ToolStripMenuItem Menu_Batch; } } \ No newline at end of file diff --git a/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs b/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs index 2ffd140..2ff13ae 100644 --- a/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs +++ b/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs @@ -987,6 +987,14 @@ private void B_ImportPlacedDesigns_Click(object sender, EventArgs e) } private void Menu_Spawn_Click(object sender, EventArgs e) => new BulkSpawn(this, View.X, View.Y).ShowDialog(); + + private void Menu_Bulk_Click(object sender, EventArgs e) + { + var editor = new BatchEditor(SpawnLayer.Tiles, ItemEdit.SetItem(new Item())); + editor.ShowDialog(); + SpawnLayer.ClearDanglingExtensions(0, 0, SpawnLayer.MaxWidth, SpawnLayer.MaxHeight); + LoadItemGridAcre(); + } } public interface IItemLayerEditor diff --git a/NHSE.WinForms/Subforms/PlayerItemEditor.cs b/NHSE.WinForms/Subforms/PlayerItemEditor.cs index ee1fa58..987bdca 100644 --- a/NHSE.WinForms/Subforms/PlayerItemEditor.cs +++ b/NHSE.WinForms/Subforms/PlayerItemEditor.cs @@ -159,7 +159,7 @@ private void PlayerItemEditor_DragDrop(object sender, DragEventArgs e) } } - public void EnableDragDrop(Control parent, DragEventHandler enter, DragEventHandler drop) + public static void EnableDragDrop(Control parent, DragEventHandler enter, DragEventHandler drop) { parent.AllowDrop = true; parent.DragEnter += enter; diff --git a/NHSE.WinForms/Subforms/SysBot/BatchEditor.Designer.cs b/NHSE.WinForms/Subforms/SysBot/BatchEditor.Designer.cs new file mode 100644 index 0000000..d116c3a --- /dev/null +++ b/NHSE.WinForms/Subforms/SysBot/BatchEditor.Designer.cs @@ -0,0 +1,192 @@ +namespace NHSE.WinForms +{ + partial class BatchEditor + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + 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; + } +} \ No newline at end of file diff --git a/NHSE.WinForms/Subforms/SysBot/BatchEditor.cs b/NHSE.WinForms/Subforms/SysBot/BatchEditor.cs new file mode 100644 index 0000000..8add301 --- /dev/null +++ b/NHSE.WinForms/Subforms/SysBot/BatchEditor.cs @@ -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 Items; + private readonly Item item; + private int currentFormat = -1; + + public BatchEditor(IReadOnlyList 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; + } + } +} diff --git a/NHSE.WinForms/Subforms/SysBot/BatchEditor.resx b/NHSE.WinForms/Subforms/SysBot/BatchEditor.resx new file mode 100644 index 0000000..5cfbcbc --- /dev/null +++ b/NHSE.WinForms/Subforms/SysBot/BatchEditor.resx @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 367, 17 + + + 426, 17 + + + 523, 17 + + + 620, 17 + + \ No newline at end of file