Add comment support for all rule types

This commit is contained in:
azakhi 2018-08-31 11:21:43 +03:00
parent a86ab3ec8d
commit 65d3e07156
4 changed files with 201 additions and 93 deletions

View File

@ -17,6 +17,7 @@ namespace Filtration.ObjectModel.BlockItemBaseTypes
public abstract Color SummaryBackgroundColor { get; } public abstract Color SummaryBackgroundColor { get; }
public abstract Color SummaryTextColor { get; } public abstract Color SummaryTextColor { get; }
public abstract int SortOrder { get; } public abstract int SortOrder { get; }
public string Comment { get; set; }
public bool IsDirty public bool IsDirty
{ {

View File

@ -4,6 +4,6 @@ namespace Filtration.ObjectModel
{ {
public interface IBlockItemWithTheme : IItemFilterBlockItem public interface IBlockItemWithTheme : IItemFilterBlockItem
{ {
ThemeComponent ThemeComponent { get; } ThemeComponent ThemeComponent { get; set; }
} }
} }

View File

@ -14,5 +14,6 @@ namespace Filtration.ObjectModel
int MaximumAllowed { get; } int MaximumAllowed { get; }
int SortOrder { get; } int SortOrder { get; }
bool IsDirty { get; } bool IsDirty { get; }
string Comment { get; set; }
} }
} }

View File

@ -63,13 +63,36 @@ namespace Filtration.Parser.Services
foreach (var line in new LineReader(() => new StringReader(inputString))) foreach (var line in new LineReader(() => new StringReader(inputString)))
{ {
if (line.StartsWith(@"#") && !showHideFound) if (line.StartsWith(@"#"))
{
if(!showHideFound)
{ {
block.Description = line.TrimStart('#').TrimStart(' '); 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; 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 spaceOrEndOfLinePos = trimmedLine.IndexOf(" ", StringComparison.Ordinal) > 0 ? trimmedLine.IndexOf(" ", StringComparison.Ordinal) : trimmedLine.Length;
var lineOption = trimmedLine.Substring(0, spaceOrEndOfLinePos); 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. // group hierarchy, if block groups are disabled it is preserved as a simple text comment.
if (parentItemFilterScript.ItemFilterScriptSettings.BlockGroupsEnabled) if (parentItemFilterScript.ItemFilterScriptSettings.BlockGroupsEnabled)
{ {
AddBlockGroupToBlock(block, trimmedLine); AddBlockGroupToBlock(block, fullLine);
} }
else else
{ {
block.ActionBlockItem.Comment = GetTextAfterFirstComment(trimmedLine); block.ActionBlockItem.Comment = GetTextAfterFirstComment(fullLine);
} }
break; break;
} }
@ -193,7 +216,12 @@ namespace Filtration.Parser.Services
// Only ever use the last SetTextColor item encountered as multiples aren't valid. // Only ever use the last SetTextColor item encountered as multiples aren't valid.
RemoveExistingBlockItemsOfType<TextColorBlockItem>(block); RemoveExistingBlockItemsOfType<TextColorBlockItem>(block);
AddColorItemToBlockItems<TextColorBlockItem>(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; break;
} }
case "SetBackgroundColor": case "SetBackgroundColor":
@ -201,7 +229,12 @@ namespace Filtration.Parser.Services
// Only ever use the last SetBackgroundColor item encountered as multiples aren't valid. // Only ever use the last SetBackgroundColor item encountered as multiples aren't valid.
RemoveExistingBlockItemsOfType<BackgroundColorBlockItem>(block); RemoveExistingBlockItemsOfType<BackgroundColorBlockItem>(block);
AddColorItemToBlockItems<BackgroundColorBlockItem>(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; break;
} }
case "SetBorderColor": case "SetBorderColor":
@ -209,7 +242,12 @@ namespace Filtration.Parser.Services
// Only ever use the last SetBorderColor item encountered as multiples aren't valid. // Only ever use the last SetBorderColor item encountered as multiples aren't valid.
RemoveExistingBlockItemsOfType<BorderColorBlockItem>(block); RemoveExistingBlockItemsOfType<BorderColorBlockItem>(block);
AddColorItemToBlockItems<BorderColorBlockItem>(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; break;
} }
case "SetFontSize": case "SetFontSize":
@ -217,16 +255,13 @@ namespace Filtration.Parser.Services
// Only ever use the last SetFontSize item encountered as multiples aren't valid. // Only ever use the last SetFontSize item encountered as multiples aren't valid.
RemoveExistingBlockItemsOfType<FontSizeBlockItem>(block); RemoveExistingBlockItemsOfType<FontSizeBlockItem>(block);
var match = Regex.Matches(trimmedLine, @"(\s+(\d+)\s*)([#]?)(.*)"); var match = Regex.Matches(trimmedLine, @"(\s+(\d+)\s*)");
if (match.Count > 0) if (match.Count > 0)
{ {
var blockItem = new FontSizeBlockItem(Convert.ToInt16(match[0].Groups[2].Value)); 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); block.BlockItems.Add(blockItem);
} }
themeComponentType = ThemeComponentType.FontSize;
break; break;
} }
case "PlayAlertSound": case "PlayAlertSound":
@ -237,7 +272,7 @@ namespace Filtration.Parser.Services
RemoveExistingBlockItemsOfType<PositionalSoundBlockItem>(block); RemoveExistingBlockItemsOfType<PositionalSoundBlockItem>(block);
RemoveExistingBlockItemsOfType<CustomSoundBlockItem>(block); RemoveExistingBlockItemsOfType<CustomSoundBlockItem>(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) if (match.Success)
{ {
@ -253,12 +288,6 @@ namespace Filtration.Parser.Services
secondValue = 79; 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") if (lineOption == "PlayAlertSound")
{ {
var blockItemValue = new SoundBlockItem var blockItemValue = new SoundBlockItem
@ -266,7 +295,6 @@ namespace Filtration.Parser.Services
Value = firstValue, Value = firstValue,
SecondValue = secondValue SecondValue = secondValue
}; };
blockItemValue.ThemeComponent = themeComponent;
block.BlockItems.Add(blockItemValue); block.BlockItems.Add(blockItemValue);
} }
else else
@ -276,10 +304,10 @@ namespace Filtration.Parser.Services
Value = firstValue, Value = firstValue,
SecondValue = secondValue SecondValue = secondValue
}; };
blockItemValue.ThemeComponent = themeComponent;
block.BlockItems.Add(blockItemValue); block.BlockItems.Add(blockItemValue);
} }
} }
themeComponentType = ThemeComponentType.AlertSound;
break; break;
} }
case "GemLevel": case "GemLevel":
@ -337,6 +365,7 @@ namespace Filtration.Parser.Services
} }
block.BlockItems.Add(blockItemValue); block.BlockItems.Add(blockItemValue);
} }
themeComponentType = ThemeComponentType.Icon;
break; break;
} }
case "PlayEffect": case "PlayEffect":
@ -345,7 +374,7 @@ namespace Filtration.Parser.Services
RemoveExistingBlockItemsOfType<PlayEffectBlockItem>(block); RemoveExistingBlockItemsOfType<PlayEffectBlockItem>(block);
// TODO: Get colors programmatically // 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) if (match.Success)
{ {
@ -354,15 +383,9 @@ namespace Filtration.Parser.Services
Color = EnumHelper.GetEnumValueFromDescription<EffectColor>(match.Groups[1].Value), Color = EnumHelper.GetEnumValueFromDescription<EffectColor>(match.Groups[1].Value),
Temporary = match.Groups[2].Value.Trim().ToLower() == "temp" 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); block.BlockItems.Add(blockItemValue);
} }
themeComponentType = ThemeComponentType.Effect;
break; break;
} }
case "CustomAlertSound": case "CustomAlertSound":
@ -372,7 +395,7 @@ namespace Filtration.Parser.Services
RemoveExistingBlockItemsOfType<SoundBlockItem>(block); RemoveExistingBlockItemsOfType<SoundBlockItem>(block);
RemoveExistingBlockItemsOfType<PositionalSoundBlockItem>(block); RemoveExistingBlockItemsOfType<PositionalSoundBlockItem>(block);
var match = Regex.Match(trimmedLine, @"\S+\s+""(\S+)""\s*([#]?)(.*)"); var match = Regex.Match(trimmedLine, @"\S+\s+""(\S+)""");
if (match.Success) if (match.Success)
{ {
@ -380,14 +403,9 @@ namespace Filtration.Parser.Services
{ {
Value = match.Groups[1].Value 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); block.BlockItems.Add(blockItemValue);
} }
themeComponentType = ThemeComponentType.CustomSound;
break; break;
} }
case "MapTier": case "MapTier":
@ -396,8 +414,87 @@ namespace Filtration.Parser.Services
break; 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; block.IsEdited = false;
return block; return block;
} }
@ -460,48 +557,6 @@ namespace Filtration.Parser.Services
} }
} }
private void AddColorItemToBlockItems<T>(IItemFilterBlock block, string inputString) where T : ColorBlockItem
{
block.BlockItems.Add(GetColorBlockItemFromString<T>(inputString));
}
private T GetColorBlockItemFromString<T>(string inputString) where T: ColorBlockItem
{
var blockItem = Activator.CreateInstance<T>();
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<IItemFilterBlockItem> blockItems, string inputString) public void ReplaceAudioVisualBlockItemsFromString(ObservableCollection<IItemFilterBlockItem> blockItems, string inputString)
{ {
// Reverse iterate to remove existing IAudioVisualBlockItems // Reverse iterate to remove existing IAudioVisualBlockItems
@ -516,36 +571,87 @@ namespace Filtration.Parser.Services
foreach (var line in new LineReader(() => new StringReader(inputString))) foreach (var line in new LineReader(() => new StringReader(inputString)))
{ {
var matches = Regex.Match(line, @"(\w+)"); 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) switch (matches.Value)
{ {
case "PlayAlertSound": case "PlayAlertSound":
{ {
var match = Regex.Match(line, @"\s+(\S+) (\d+)"); var match = Regex.Match(trimmedLine, @"\s+(\S+) (\d+)");
if (!match.Success) break; 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; break;
} }
case "SetTextColor": case "SetTextColor":
{ {
blockItems.Add(GetColorBlockItemFromString<TextColorBlockItem>(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; break;
} }
case "SetBackgroundColor": case "SetBackgroundColor":
{ {
blockItems.Add(GetColorBlockItemFromString<BackgroundColorBlockItem>(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; break;
} }
case "SetBorderColor": case "SetBorderColor":
{ {
blockItems.Add(GetColorBlockItemFromString<BorderColorBlockItem>(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; break;
} }
case "SetFontSize": case "SetFontSize":
{ {
var match = Regex.Match(line, @"\s+(\d+)"); var match = Regex.Match(trimmedLine, @"\s+(\d+)");
if (!match.Success) break; 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; break;
} }
} }