From 3851ad51e126153b8530cf0598deceab04f471fd Mon Sep 17 00:00:00 2001 From: azakhi Date: Fri, 31 Aug 2018 14:22:26 +0300 Subject: [PATCH] Add support for comments between rules --- .../BlockItemBaseTypes/ActionBlockItem.cs | 2 - .../TestItemFilterScriptTranslator.cs | 12 +-- Filtration.Parser/Filtration.Parser.csproj | 1 + .../Services/ItemFilterScriptTranslator.cs | 78 ++++++++++++++++--- 4 files changed, 69 insertions(+), 24 deletions(-) diff --git a/Filtration.ObjectModel/BlockItemBaseTypes/ActionBlockItem.cs b/Filtration.ObjectModel/BlockItemBaseTypes/ActionBlockItem.cs index 8660377..5dd20c2 100644 --- a/Filtration.ObjectModel/BlockItemBaseTypes/ActionBlockItem.cs +++ b/Filtration.ObjectModel/BlockItemBaseTypes/ActionBlockItem.cs @@ -27,8 +27,6 @@ namespace Filtration.ObjectModel.BlockItemBaseTypes } } - public string Comment { get; set; } - public override string OutputText => Action.GetAttributeDescription(); public override string PrefixText => string.Empty; diff --git a/Filtration.Parser.Tests/Services/TestItemFilterScriptTranslator.cs b/Filtration.Parser.Tests/Services/TestItemFilterScriptTranslator.cs index 31290a8..a859cc2 100644 --- a/Filtration.Parser.Tests/Services/TestItemFilterScriptTranslator.cs +++ b/Filtration.Parser.Tests/Services/TestItemFilterScriptTranslator.cs @@ -285,13 +285,11 @@ namespace Filtration.Parser.Tests.Services " ItemLevel > 2" + Environment.NewLine + " SetTextColor 255 40 0" + Environment.NewLine + Environment.NewLine + - "#Disabled Block Start" + Environment.NewLine + "#Show" + Environment.NewLine + "# ItemLevel > 2" + Environment.NewLine + "# SetTextColor 255 215 0" + Environment.NewLine + "# SetBorderColor 255 105 180" + Environment.NewLine + "# SetFontSize 32" + Environment.NewLine + - "#Disabled Block End" + Environment.NewLine + Environment.NewLine + "Show" + Environment.NewLine + " ItemLevel > 20" + Environment.NewLine + @@ -316,13 +314,11 @@ namespace Filtration.Parser.Tests.Services " ItemLevel > 2" + Environment.NewLine + " SetTextColor 255 40 0" + Environment.NewLine + Environment.NewLine + - "#Disabled Block Start" + Environment.NewLine + "#Show" + Environment.NewLine + "# ItemLevel > 2" + Environment.NewLine + "# SetTextColor 255 215 0" + Environment.NewLine + "# SetBorderColor 255 105 180" + Environment.NewLine + "# SetFontSize 32" + Environment.NewLine + - "#Disabled Block End" + Environment.NewLine + Environment.NewLine + "Show" + Environment.NewLine + " ItemLevel > 20" + Environment.NewLine + @@ -355,11 +351,9 @@ namespace Filtration.Parser.Tests.Services " ItemLevel > 2" + Environment.NewLine + " SetTextColor 255 40 0" + Environment.NewLine + Environment.NewLine + - "#Disabled Block Start" + Environment.NewLine + "# This is a disabled block" + Environment.NewLine + "#Show" + Environment.NewLine + - "# ItemLevel > 2" + Environment.NewLine + - "#Disabled Block End"; + "# ItemLevel > 2"; var blockTranslator = new ItemFilterBlockTranslator(Mock.Of()); @@ -384,11 +378,9 @@ namespace Filtration.Parser.Tests.Services " ItemLevel > 2" + Environment.NewLine + " SetTextColor 255 40 0" + Environment.NewLine + Environment.NewLine + - "#Disabled Block Start" + Environment.NewLine + "# This is a disabled block" + Environment.NewLine + "#Show#My Block Group" + Environment.NewLine + - "# ItemLevel > 2" + Environment.NewLine + - "#Disabled Block End"; + "# ItemLevel > 2"; var mockBlockGroupHierarchyBuilder = new Mock(); diff --git a/Filtration.Parser/Filtration.Parser.csproj b/Filtration.Parser/Filtration.Parser.csproj index 2a8d160..31c62ba 100644 --- a/Filtration.Parser/Filtration.Parser.csproj +++ b/Filtration.Parser/Filtration.Parser.csproj @@ -39,6 +39,7 @@ ..\packages\Castle.Windsor.3.4.0\lib\net45\Castle.Windsor.dll + diff --git a/Filtration.Parser/Services/ItemFilterScriptTranslator.cs b/Filtration.Parser/Services/ItemFilterScriptTranslator.cs index 6407226..e386963 100644 --- a/Filtration.Parser/Services/ItemFilterScriptTranslator.cs +++ b/Filtration.Parser/Services/ItemFilterScriptTranslator.cs @@ -2,8 +2,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Runtime.InteropServices; using System.Text.RegularExpressions; +using System.Windows; using Filtration.Common.Utilities; using Filtration.ObjectModel; using Filtration.ObjectModel.Factories; @@ -48,18 +48,58 @@ namespace Filtration.Parser.Services _itemFilterScriptFactory = itemFilterScriptFactory; } - public static string PreprocessDisabledBlocks(string inputString) + public static string PreprocessDisabledBlocks(string inputString, out List inBlock) { bool inDisabledBlock = false; + inBlock = new List(); var lines = Regex.Split(inputString, "\r\n|\r|\n").ToList(); + // find first show/hide and check script + for (var i = 0; i < lines.Count; i++) + { + inBlock.Add(false); + lines[i] = lines[i].Trim(); + if(!lines[i].StartsWith("#")) + { + string curLine = Regex.Replace(lines[i], @"\s+", ""); + if ((curLine.StartsWith("Show") || curLine.StartsWith("Hide")) && (curLine.Length == 4 || curLine[4] == '#')) // found + { + inBlock[i] = true; + break; + } + else // This means script has wrong syntax, just replace those lines with empty string + { + lines[i] = ""; + } + } + } + + // find remaining boundaries + var lastInBlock = inBlock.Count - 1; + for (var i = inBlock.Count; i < lines.Count; i++) + { + inBlock.Add(false); + lines[i] = lines[i].Trim(); + if (!lines[i].StartsWith("#") && lines[i].Length > 0) + { + if (!lines[i].StartsWith("Show") && !lines[i].StartsWith("Hide")) // Continuing inline + { + for(int j = lastInBlock + 1; j < i; j++) + { + inBlock[j] = true; + } + } + lastInBlock = i; + inBlock[i] = true; + } + } for (var i = 0; i < lines.Count; i++) { if (!inDisabledBlock && lines[i].StartsWith("#")) { string curLine = Regex.Replace(lines[i].Substring(1), @"\s+", ""); - if ((curLine.StartsWith("Show") || curLine.StartsWith("Hide")) && (curLine.Length == 4 || curLine[4] == '#')) + if ((curLine.StartsWith("Show") || curLine.StartsWith("Hide")) && (curLine.Length == 4 || curLine[4] == '#') && !inBlock[i]) { inDisabledBlock = true; lines[i] = lines[i].Substring(1).TrimStart(' '); @@ -88,16 +128,29 @@ namespace Filtration.Parser.Services var script = _itemFilterScriptFactory.Create(); _blockGroupHierarchyBuilder.Initialise(script.ItemFilterBlockGroups.First()); - //Remove old disabled tags - inputString = Regex.Replace(inputString, @"#Disabled\sBlock\s(Start|End).*?\n", ""); - inputString = (inputString.EndsWith("\n#Disabled Block End")) ? inputString.Substring(0, inputString.Length - 19) : inputString; + if(Regex.Matches(inputString, @"#Disabled\sBlock\s(Start|End).*?\n").Count > 0) + { + if (MessageBox.Show( + "Loaded script contains special '#Disabled Block Start' lines." + + " These may be coming from old versions of Filtration or Greengroove's filter." + + "It is suggested to remove them however this may cause problems with original source" + + Environment.NewLine + "(There is no in game effect of those lines)" + + Environment.NewLine + Environment.NewLine + "Would you like to remove them?", "Special Comment Lines Found", + MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) + { + //Remove old disabled tags + inputString = Regex.Replace(inputString, @"#Disabled\sBlock\s(Start|End).*?\n", ""); + inputString = (inputString.EndsWith("\n#Disabled Block End")) ? inputString.Substring(0, inputString.Length - 19) : inputString; + } + } var originalLines = Regex.Split(inputString, "\r\n|\r|\n"); inputString = inputString.Replace("\t", ""); - inputString = PreprocessDisabledBlocks(inputString); + List inBlock; + inputString = PreprocessDisabledBlocks(inputString, out inBlock); - var conditionBoundaries = IdentifyBlockBoundaries(inputString); + var conditionBoundaries = IdentifyBlockBoundaries(inputString, inBlock); var lines = Regex.Split(inputString, "\r\n|\r|\n"); @@ -158,7 +211,7 @@ namespace Filtration.Parser.Services return script; } - private static LinkedList IdentifyBlockBoundaries(string inputString) + private static LinkedList IdentifyBlockBoundaries(string inputString, List inBlock) { var blockBoundaries = new LinkedList(); var previousLine = string.Empty; @@ -177,9 +230,10 @@ namespace Filtration.Parser.Services continue; } - // A line starting with a comment when we're inside a ItemFilterBlock boundary represents the end of that block - // as ItemFilterBlocks cannot have comment lines after the block description - if (trimmedLine.StartsWith("#") && currentItemFilterBlockBoundary.BoundaryType == ItemFilterBlockBoundaryType.ItemFilterBlock) + // A line starting with a comment when we're inside a ItemFilterBlock boundary may represent the end of that block + // or a block item comment + if (trimmedLine.StartsWith("#") && !inBlock[currentLine] && + currentItemFilterBlockBoundary.BoundaryType == ItemFilterBlockBoundaryType.ItemFilterBlock) { blockBoundaries.AddLast(currentItemFilterBlockBoundary); currentItemFilterBlockBoundary = new ItemFilterBlockBoundary(currentLine, ItemFilterBlockBoundaryType.CommentBlock);