From 65d3e0715676ac9c5f7764a84a92e537278e966c Mon Sep 17 00:00:00 2001 From: azakhi Date: Fri, 31 Aug 2018 11:21:43 +0300 Subject: [PATCH] Add comment support for all rule types --- .../BlockItemBaseTypes/BlockItembase.cs | 1 + Filtration.ObjectModel/IBlockItemWithTheme.cs | 2 +- .../IItemFilterBlockItem.cs | 1 + .../Services/ItemFilterBlockTranslator.cs | 290 ++++++++++++------ 4 files changed, 201 insertions(+), 93 deletions(-) diff --git a/Filtration.ObjectModel/BlockItemBaseTypes/BlockItembase.cs b/Filtration.ObjectModel/BlockItemBaseTypes/BlockItembase.cs index b6ce2d9..d7c76f2 100644 --- a/Filtration.ObjectModel/BlockItemBaseTypes/BlockItembase.cs +++ b/Filtration.ObjectModel/BlockItemBaseTypes/BlockItembase.cs @@ -17,6 +17,7 @@ namespace Filtration.ObjectModel.BlockItemBaseTypes public abstract Color SummaryBackgroundColor { get; } public abstract Color SummaryTextColor { get; } public abstract int SortOrder { get; } + public string Comment { get; set; } public bool IsDirty { diff --git a/Filtration.ObjectModel/IBlockItemWithTheme.cs b/Filtration.ObjectModel/IBlockItemWithTheme.cs index 5971eb0..8d8dd81 100644 --- a/Filtration.ObjectModel/IBlockItemWithTheme.cs +++ b/Filtration.ObjectModel/IBlockItemWithTheme.cs @@ -4,6 +4,6 @@ namespace Filtration.ObjectModel { public interface IBlockItemWithTheme : IItemFilterBlockItem { - ThemeComponent ThemeComponent { get; } + ThemeComponent ThemeComponent { get; set; } } } diff --git a/Filtration.ObjectModel/IItemFilterBlockItem.cs b/Filtration.ObjectModel/IItemFilterBlockItem.cs index ec693d0..ce9f880 100644 --- a/Filtration.ObjectModel/IItemFilterBlockItem.cs +++ b/Filtration.ObjectModel/IItemFilterBlockItem.cs @@ -14,5 +14,6 @@ namespace Filtration.ObjectModel int MaximumAllowed { get; } int SortOrder { get; } bool IsDirty { get; } + string Comment { get; set; } } } diff --git a/Filtration.Parser/Services/ItemFilterBlockTranslator.cs b/Filtration.Parser/Services/ItemFilterBlockTranslator.cs index a48148e..385a311 100644 --- a/Filtration.Parser/Services/ItemFilterBlockTranslator.cs +++ b/Filtration.Parser/Services/ItemFilterBlockTranslator.cs @@ -63,13 +63,36 @@ namespace Filtration.Parser.Services foreach (var line in new LineReader(() => new StringReader(inputString))) { - if (line.StartsWith(@"#") && !showHideFound) + if (line.StartsWith(@"#")) { - block.Description = line.TrimStart('#').TrimStart(' '); + if(!showHideFound) + { + block.Description = line.TrimStart('#').TrimStart(' '); + } + else + { + if(block.BlockItems.Count > 1) + { + block.BlockItems.Last().Comment += Environment.NewLine + line.TrimStart('#'); + } + else + { + block.ActionBlockItem.Comment += Environment.NewLine + line.TrimStart('#'); + } + } continue; } - var trimmedLine = line.Trim(); + var fullLine = line.Trim(); + var trimmedLine = fullLine; + var blockComment = ""; + var nextBlockIndex = block.BlockItems.Count; + ThemeComponentType themeComponentType = ThemeComponentType.AlertSound; + if(trimmedLine.IndexOf('#') > 0) + { + blockComment = trimmedLine.Substring(trimmedLine.IndexOf('#') + 1); + trimmedLine = trimmedLine.Substring(0, trimmedLine.IndexOf('#')).Trim(); + } var spaceOrEndOfLinePos = trimmedLine.IndexOf(" ", StringComparison.Ordinal) > 0 ? trimmedLine.IndexOf(" ", StringComparison.Ordinal) : trimmedLine.Length; var lineOption = trimmedLine.Substring(0, spaceOrEndOfLinePos); @@ -88,11 +111,11 @@ namespace Filtration.Parser.Services // group hierarchy, if block groups are disabled it is preserved as a simple text comment. if (parentItemFilterScript.ItemFilterScriptSettings.BlockGroupsEnabled) { - AddBlockGroupToBlock(block, trimmedLine); + AddBlockGroupToBlock(block, fullLine); } else { - block.ActionBlockItem.Comment = GetTextAfterFirstComment(trimmedLine); + block.ActionBlockItem.Comment = GetTextAfterFirstComment(fullLine); } break; } @@ -193,7 +216,12 @@ namespace Filtration.Parser.Services // Only ever use the last SetTextColor item encountered as multiples aren't valid. RemoveExistingBlockItemsOfType(block); - AddColorItemToBlockItems(block, trimmedLine); + var result = Regex.Matches(trimmedLine, @"([\w\s]*)"); + + var blockItem = new TextColorBlockItem(); + blockItem.Color = GetColorFromString(result[0].Groups[1].Value); + block.BlockItems.Add(blockItem); + themeComponentType = ThemeComponentType.TextColor; break; } case "SetBackgroundColor": @@ -201,7 +229,12 @@ namespace Filtration.Parser.Services // Only ever use the last SetBackgroundColor item encountered as multiples aren't valid. RemoveExistingBlockItemsOfType(block); - AddColorItemToBlockItems(block, trimmedLine); + var result = Regex.Matches(trimmedLine, @"([\w\s]*)"); + + var blockItem = new BackgroundColorBlockItem(); + blockItem.Color = GetColorFromString(result[0].Groups[1].Value); + block.BlockItems.Add(blockItem); + themeComponentType = ThemeComponentType.BackgroundColor; break; } case "SetBorderColor": @@ -209,7 +242,12 @@ namespace Filtration.Parser.Services // Only ever use the last SetBorderColor item encountered as multiples aren't valid. RemoveExistingBlockItemsOfType(block); - AddColorItemToBlockItems(block, trimmedLine); + var result = Regex.Matches(trimmedLine, @"([\w\s]*)"); + + var blockItem = new BorderColorBlockItem(); + blockItem.Color = GetColorFromString(result[0].Groups[1].Value); + block.BlockItems.Add(blockItem); + themeComponentType = ThemeComponentType.BorderColor; break; } case "SetFontSize": @@ -217,16 +255,13 @@ namespace Filtration.Parser.Services // Only ever use the last SetFontSize item encountered as multiples aren't valid. RemoveExistingBlockItemsOfType(block); - var match = Regex.Matches(trimmedLine, @"(\s+(\d+)\s*)([#]?)(.*)"); + var match = Regex.Matches(trimmedLine, @"(\s+(\d+)\s*)"); if (match.Count > 0) { var blockItem = new FontSizeBlockItem(Convert.ToInt16(match[0].Groups[2].Value)); - if(match[0].Groups[3].Value == "#" && !string.IsNullOrWhiteSpace(match[0].Groups[4].Value)) - { - blockItem.ThemeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.FontSize, match[0].Groups[4].Value.Trim(), blockItem.Value); - } block.BlockItems.Add(blockItem); } + themeComponentType = ThemeComponentType.FontSize; break; } case "PlayAlertSound": @@ -237,7 +272,7 @@ namespace Filtration.Parser.Services RemoveExistingBlockItemsOfType(block); RemoveExistingBlockItemsOfType(block); - var match = Regex.Match(trimmedLine, @"\S+\s+(\S+)\s?(\d+)?\s*([#]?)(.*)"); + var match = Regex.Match(trimmedLine, @"\S+\s+(\S+)\s?(\d+)?"); if (match.Success) { @@ -251,12 +286,6 @@ namespace Filtration.Parser.Services else { secondValue = 79; - } - - ThemeComponent themeComponent = null; - if(match.Groups[3].Value == "#" && !string.IsNullOrWhiteSpace(match.Groups[4].Value)) - { - themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.AlertSound, match.Groups[4].Value.Trim(), firstValue, secondValue); } if (lineOption == "PlayAlertSound") @@ -266,7 +295,6 @@ namespace Filtration.Parser.Services Value = firstValue, SecondValue = secondValue }; - blockItemValue.ThemeComponent = themeComponent; block.BlockItems.Add(blockItemValue); } else @@ -276,10 +304,10 @@ namespace Filtration.Parser.Services Value = firstValue, SecondValue = secondValue }; - blockItemValue.ThemeComponent = themeComponent; block.BlockItems.Add(blockItemValue); } - } + } + themeComponentType = ThemeComponentType.AlertSound; break; } case "GemLevel": @@ -337,6 +365,7 @@ namespace Filtration.Parser.Services } block.BlockItems.Add(blockItemValue); } + themeComponentType = ThemeComponentType.Icon; break; } case "PlayEffect": @@ -345,7 +374,7 @@ namespace Filtration.Parser.Services RemoveExistingBlockItemsOfType(block); // TODO: Get colors programmatically - var match = Regex.Match(trimmedLine, @"\S+\s+(Red|Green|Blue|Brown|White|Yellow)\s*(Temp)?\s*([#]?)(.*)", RegexOptions.IgnoreCase); + var match = Regex.Match(trimmedLine, @"\S+\s+(Red|Green|Blue|Brown|White|Yellow)\s*(Temp)?", RegexOptions.IgnoreCase); if (match.Success) { @@ -353,16 +382,10 @@ namespace Filtration.Parser.Services { Color = EnumHelper.GetEnumValueFromDescription(match.Groups[1].Value), Temporary = match.Groups[2].Value.Trim().ToLower() == "temp" - }; - - if(match.Groups[3].Value == "#" && !string.IsNullOrWhiteSpace(match.Groups[4].Value)) - { - ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.Effect, match.Groups[4].Value.Trim(), - blockItemValue.Color, blockItemValue.Temporary); - blockItemValue.ThemeComponent = themeComponent; - } + }; block.BlockItems.Add(blockItemValue); } + themeComponentType = ThemeComponentType.Effect; break; } case "CustomAlertSound": @@ -372,22 +395,17 @@ namespace Filtration.Parser.Services RemoveExistingBlockItemsOfType(block); RemoveExistingBlockItemsOfType(block); - var match = Regex.Match(trimmedLine, @"\S+\s+""(\S+)""\s*([#]?)(.*)"); + var match = Regex.Match(trimmedLine, @"\S+\s+""(\S+)"""); if (match.Success) { var blockItemValue = new CustomSoundBlockItem { Value = match.Groups[1].Value - }; - - if(match.Groups[2].Value == "#" && !string.IsNullOrWhiteSpace(match.Groups[3].Value)) - { - ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.CustomSound, match.Groups[3].Value.Trim(), blockItemValue.Value); - blockItemValue.ThemeComponent = themeComponent; - } + }; block.BlockItems.Add(blockItemValue); - } + } + themeComponentType = ThemeComponentType.CustomSound; break; } case "MapTier": @@ -395,9 +413,88 @@ namespace Filtration.Parser.Services AddNumericFilterPredicateItemToBlockItems(block, trimmedLine); break; } + } + + if (!string.IsNullOrWhiteSpace(blockComment) && block.BlockItems.Count > 1) + { + var blockItemWithTheme = block.BlockItems.Last() as IBlockItemWithTheme; + if(blockItemWithTheme == null) + { + block.BlockItems.Last().Comment = blockComment; + } + else if(nextBlockIndex == block.BlockItems.Count - 1) // If these 2 are not equal, this theme doesn't belong to last block + { + switch(themeComponentType) + { + case ThemeComponentType.AlertSound: + { + ThemeComponent themeComponent = null; + if(blockItemWithTheme is SoundBlockItem) + { + themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.AlertSound, blockComment.Trim(), + ((SoundBlockItem)blockItemWithTheme).Value, ((SoundBlockItem)blockItemWithTheme).SecondValue); + } + else + { + themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.AlertSound, blockComment.Trim(), + ((PositionalSoundBlockItem)blockItemWithTheme).Value, ((PositionalSoundBlockItem)blockItemWithTheme).SecondValue); + } + blockItemWithTheme.ThemeComponent = themeComponent; + break; + } + case ThemeComponentType.BackgroundColor: + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.BackgroundColor, + blockComment.Trim(), ((BackgroundColorBlockItem)blockItemWithTheme).Color); + blockItemWithTheme.ThemeComponent = themeComponent; + break; + } + case ThemeComponentType.BorderColor: + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.BorderColor, + blockComment.Trim(), ((BorderColorBlockItem)blockItemWithTheme).Color); + blockItemWithTheme.ThemeComponent = themeComponent; + break; + } + case ThemeComponentType.CustomSound: + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.CustomSound, + blockComment.Trim(), ((CustomSoundBlockItem)blockItemWithTheme).Value); + blockItemWithTheme.ThemeComponent = themeComponent; + break; + } + case ThemeComponentType.Effect: + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.Effect, + blockComment.Trim(), ((EffectColorBlockItem)blockItemWithTheme).Color, ((EffectColorBlockItem)blockItemWithTheme).Temporary); + blockItemWithTheme.ThemeComponent = themeComponent; + break; + } + case ThemeComponentType.FontSize: + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.FontSize, + blockComment.Trim(), ((FontSizeBlockItem)blockItemWithTheme).Value); + blockItemWithTheme.ThemeComponent = themeComponent; + break; + } + case ThemeComponentType.Icon: + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.Icon, blockComment.Trim(), + ((IconBlockItem)blockItemWithTheme).Size, ((IconBlockItem)blockItemWithTheme).Color, ((IconBlockItem)blockItemWithTheme).Shape); + blockItemWithTheme.ThemeComponent = themeComponent; + break; + } + case ThemeComponentType.TextColor: + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.TextColor, + blockComment.Trim(), ((TextColorBlockItem)blockItemWithTheme).Color); + blockItemWithTheme.ThemeComponent = themeComponent; + break; + } + } + } } } - block.IsEdited = false; return block; } @@ -460,48 +557,6 @@ namespace Filtration.Parser.Services } } - private void AddColorItemToBlockItems(IItemFilterBlock block, string inputString) where T : ColorBlockItem - { - block.BlockItems.Add(GetColorBlockItemFromString(inputString)); - } - - private T GetColorBlockItemFromString(string inputString) where T: ColorBlockItem - { - var blockItem = Activator.CreateInstance(); - var result = Regex.Matches(inputString, @"([\w\s]*)[#]?(.*)"); - - blockItem.Color = GetColorFromString(result[0].Groups[1].Value); - - var componentName = result[0].Groups[2].Value.Trim(); - if (!string.IsNullOrEmpty(componentName)) - { - ThemeComponentType componentType; - if (typeof(T) == typeof(TextColorBlockItem)) - { - componentType = ThemeComponentType.TextColor; - } - else if (typeof(T) == typeof(BackgroundColorBlockItem)) - { - componentType = ThemeComponentType.BackgroundColor; - } - else if (typeof(T) == typeof(BorderColorBlockItem)) - { - componentType = ThemeComponentType.BorderColor; - } - else - { - throw new Exception("Parsing error - unknown theme component type"); - } - if (_masterComponentCollection != null) - { - blockItem.ThemeComponent = _masterComponentCollection.AddComponent(componentType, componentName, - blockItem.Color); - } - } - - return blockItem; - } - public void ReplaceAudioVisualBlockItemsFromString(ObservableCollection blockItems, string inputString) { // Reverse iterate to remove existing IAudioVisualBlockItems @@ -516,36 +571,87 @@ namespace Filtration.Parser.Services foreach (var line in new LineReader(() => new StringReader(inputString))) { var matches = Regex.Match(line, @"(\w+)"); - + var blockComment = ""; + var trimmedLine = line.Trim(); + if (trimmedLine.IndexOf('#') > 0) + { + blockComment = trimmedLine.Substring(trimmedLine.IndexOf('#') + 1); + trimmedLine = trimmedLine.Substring(0, trimmedLine.IndexOf('#')).Trim(); + } + switch (matches.Value) { case "PlayAlertSound": { - var match = Regex.Match(line, @"\s+(\S+) (\d+)"); + var match = Regex.Match(trimmedLine, @"\s+(\S+) (\d+)"); if (!match.Success) break; - blockItems.Add(new SoundBlockItem(match.Groups[1].Value, Convert.ToInt16(match.Groups[2].Value))); + var blockItem = new SoundBlockItem(match.Groups[1].Value, Convert.ToInt16(match.Groups[2].Value)); + if(_masterComponentCollection != null && !string.IsNullOrWhiteSpace(blockComment)) + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.AlertSound, + blockComment, blockItem.Value, blockItem.SecondValue); + blockItem.ThemeComponent = themeComponent; + } + blockItems.Add(blockItem); break; } case "SetTextColor": { - blockItems.Add(GetColorBlockItemFromString(line)); + var result = Regex.Matches(trimmedLine, @"([\w\s]*)"); + + var blockItem = new TextColorBlockItem(); + blockItem.Color = GetColorFromString(result[0].Groups[1].Value); + if(_masterComponentCollection != null && !string.IsNullOrWhiteSpace(blockComment)) + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.TextColor, + blockComment, blockItem.Color); + blockItem.ThemeComponent = themeComponent; + } + blockItems.Add(blockItem); break; } case "SetBackgroundColor": { - blockItems.Add(GetColorBlockItemFromString(line)); + var result = Regex.Matches(trimmedLine, @"([\w\s]*)"); + + var blockItem = new BackgroundColorBlockItem(); + blockItem.Color = GetColorFromString(result[0].Groups[1].Value); + if(_masterComponentCollection != null && !string.IsNullOrWhiteSpace(blockComment)) + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.BackgroundColor, + blockComment, blockItem.Color); + blockItem.ThemeComponent = themeComponent; + } + blockItems.Add(blockItem); break; } case "SetBorderColor": { - blockItems.Add(GetColorBlockItemFromString(line)); + var result = Regex.Matches(trimmedLine, @"([\w\s]*)"); + + var blockItem = new BorderColorBlockItem(); + blockItem.Color = GetColorFromString(result[0].Groups[1].Value); + if(_masterComponentCollection != null && !string.IsNullOrWhiteSpace(blockComment)) + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.BorderColor, + blockComment, blockItem.Color); + blockItem.ThemeComponent = themeComponent; + } + blockItems.Add(blockItem); break; } case "SetFontSize": { - var match = Regex.Match(line, @"\s+(\d+)"); + var match = Regex.Match(trimmedLine, @"\s+(\d+)"); if (!match.Success) break; - blockItems.Add(new FontSizeBlockItem(Convert.ToInt16(match.Value))); + var blockItem = new FontSizeBlockItem(Convert.ToInt16(match.Value)); + if (_masterComponentCollection != null && !string.IsNullOrWhiteSpace(blockComment)) + { + ThemeComponent themeComponent = _masterComponentCollection.AddComponent(ThemeComponentType.FontSize, + blockComment, blockItem.Value); + blockItem.ThemeComponent = themeComponent; + } + blockItems.Add(blockItem); break; } }