FIL-4 Refactored script parsing to retain isolated comments as a new ItemFilterBlockComment type - UI still needs reworking with new templates and such.

This commit is contained in:
Ben Wallis 2017-05-20 18:14:28 +01:00
parent 797c911bb5
commit 4def27c49d
18 changed files with 273 additions and 108 deletions

View File

@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
@ -21,6 +22,7 @@ namespace Filtration.Common.Utilities
/// the stream into text.
/// </summary>
/// <param name="streamSource">Data source</param>
[DebuggerStepThrough]
public LineReader(Func<Stream> streamSource)
: this(streamSource, Encoding.UTF8)
{
@ -33,6 +35,7 @@ namespace Filtration.Common.Utilities
/// <param name="streamSource">Data source</param>
/// <param name="encoding">Encoding to use to decode the stream
/// into text</param>
[DebuggerStepThrough]
public LineReader(Func<Stream> streamSource, Encoding encoding)
: this(() => new StreamReader(streamSource(), encoding))
{
@ -44,6 +47,7 @@ namespace Filtration.Common.Utilities
/// UTF8 is used to decode the file into text.
/// </summary>
/// <param name="filename">File to read from</param>
[DebuggerStepThrough]
public LineReader(string filename)
: this(filename, Encoding.UTF8)
{
@ -56,6 +60,7 @@ namespace Filtration.Common.Utilities
/// <param name="filename">File to read from</param>
/// <param name="encoding">Encoding to use to decode the file
/// into text</param>
[DebuggerStepThrough]
public LineReader(string filename, Encoding encoding)
: this(() => new StreamReader(filename, encoding))
{
@ -66,6 +71,7 @@ namespace Filtration.Common.Utilities
/// is only called when the enumerator is fetched
/// </summary>
/// <param name="dataSource">Data source</param>
[DebuggerStepThrough]
public LineReader(Func<TextReader> dataSource)
{
_dataSource = dataSource;
@ -74,6 +80,7 @@ namespace Filtration.Common.Utilities
/// <summary>
/// Enumerates the data source line by line.
/// </summary>
[DebuggerStepThrough]
public IEnumerator<string> GetEnumerator()
{
using (TextReader reader = _dataSource())
@ -89,6 +96,7 @@ namespace Filtration.Common.Utilities
/// <summary>
/// Enumerates the data source line by line.
/// </summary>
[DebuggerStepThrough]
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();

View File

@ -29,7 +29,7 @@ namespace Filtration.ItemFilterPreview.Tests.Services
//Arrange
var testInputItem = Mock.Of<IItem>();
var testInputBlock = Mock.Of<IItemFilterBlock>();
var testInputScript = Mock.Of<IItemFilterScript>(s => s.ItemFilterBlocks == new ObservableCollection<IItemFilterBlock> {testInputBlock});
var testInputScript = Mock.Of<IItemFilterScript>(s => s.ItemFilterBlocks == new ObservableCollection<IItemFilterBlockBase> {testInputBlock});
_testUtility.MockBlockItemMatcher
.Setup(b => b.ItemBlockMatch(testInputBlock, testInputItem))
@ -50,7 +50,7 @@ namespace Filtration.ItemFilterPreview.Tests.Services
//Arrange
var testInputItem = Mock.Of<IItem>();
var testInputBlock = Mock.Of<IItemFilterBlock>();
var testInputScript = Mock.Of<IItemFilterScript>(s => s.ItemFilterBlocks == new ObservableCollection<IItemFilterBlock> { testInputBlock });
var testInputScript = Mock.Of<IItemFilterScript>(s => s.ItemFilterBlocks == new ObservableCollection<IItemFilterBlockBase> { testInputBlock });
_testUtility.MockBlockItemMatcher
.Setup(b => b.ItemBlockMatch(testInputBlock, testInputItem))

View File

@ -31,6 +31,7 @@ namespace Filtration.ItemFilterPreview.Services
sw.Restart();
var matchedBlock = itemFilterScript.ItemFilterBlocks
.OfType<IItemFilterBlock>()
.Where(b => !(b is ItemFilterSection))
.FirstOrDefault(block => _blockItemMatcher.ItemBlockMatch(block, item));

View File

@ -6,23 +6,6 @@ namespace Filtration.ObjectModel.Tests
[TestFixture]
public class TestItemFilterBlock
{
[Test]
public void ItemFilterBlock_BlockCount_ReturnsCorrectNumber()
{
// Arrange
var block = new ItemFilterBlock();
block.BlockItems.Add(new ItemLevelBlockItem());
block.BlockItems.Add(new ItemLevelBlockItem());
block.BlockItems.Add(new ItemLevelBlockItem());
block.BlockItems.Add(new ItemLevelBlockItem());
// Act
var count = block.BlockCount(typeof (ItemLevelBlockItem));
// Assert
Assert.AreEqual(4, count);
}
[Test]
public void ItemFilterBlock_AddBlockItemAllowed_LessThanMaximum_ReturnsTrue()
{

View File

@ -9,7 +9,7 @@ using Filtration.ObjectModel.Extensions;
namespace Filtration.ObjectModel
{
public interface IItemFilterBlock
public interface IItemFilterBlock : IItemFilterBlockBase
{
bool Enabled { get; set; }
string Description { get; set; }
@ -21,13 +21,32 @@ namespace Filtration.ObjectModel
Color DisplayTextColor { get; }
Color DisplayBorderColor { get; }
double DisplayFontSize { get; }
int BlockCount(Type type);
bool AddBlockItemAllowed(Type type);
bool HasBlockItemOfType<T>();
bool HasBlockGroupInParentHierarchy(ItemFilterBlockGroup targetBlockGroup, ItemFilterBlockGroup startingBlockGroup);
}
public class ItemFilterBlock : IItemFilterBlock
public interface IItemFilterBlockBase
{
}
public class ItemFilterBlockBase : IItemFilterBlockBase
{
}
public interface IItemFilterCommentBlock : IItemFilterBlockBase
{
string Comment { get; set; }
bool IsSection { get; set; }
}
public class ItemFilterCommentBlock : ItemFilterBlockBase, IItemFilterCommentBlock
{
public string Comment { get; set; }
public bool IsSection { get; set; }
}
public class ItemFilterBlock : ItemFilterBlockBase, IItemFilterBlock
{
private ItemFilterBlockGroup _blockGroup;
@ -43,7 +62,7 @@ namespace Filtration.ObjectModel
public ItemFilterBlockGroup BlockGroup
{
get { return _blockGroup; }
get => _blockGroup;
set
{
var oldBlockGroup = _blockGroup;
@ -85,15 +104,15 @@ namespace Filtration.ObjectModel
public ObservableCollection<IItemFilterBlockItem> BlockItems { get; }
public int BlockCount(Type type)
public bool AddBlockItemAllowed(Type type)
{
int BlockCount()
{
return BlockItems?.Count(b => b.GetType() == type) ?? 0;
}
public bool AddBlockItemAllowed(Type type)
{
var blockItem = (IItemFilterBlockItem)Activator.CreateInstance(type);
return BlockCount(type) < blockItem.MaximumAllowed;
return BlockCount() < blockItem.MaximumAllowed;
}
public bool HasBlockItemOfType<T>()

View File

@ -9,7 +9,7 @@ namespace Filtration.ObjectModel
{
public interface IItemFilterScript
{
ObservableCollection<IItemFilterBlock> ItemFilterBlocks { get; }
ObservableCollection<IItemFilterBlockBase> ItemFilterBlocks { get; }
ObservableCollection<ItemFilterBlockGroup> ItemFilterBlockGroups { get; }
ThemeComponentCollection ThemeComponents { get; set; }
string FilePath { get; set; }
@ -25,7 +25,7 @@ namespace Filtration.ObjectModel
{
public ItemFilterScript()
{
ItemFilterBlocks = new ObservableCollection<IItemFilterBlock>();
ItemFilterBlocks = new ObservableCollection<IItemFilterBlockBase>();
ItemFilterBlockGroups = new ObservableCollection<ItemFilterBlockGroup>
{
new ItemFilterBlockGroup("Root", null)
@ -34,7 +34,7 @@ namespace Filtration.ObjectModel
ItemFilterScriptSettings = new ItemFilterScriptSettings(ThemeComponents);
}
public ObservableCollection<IItemFilterBlock> ItemFilterBlocks { get; }
public ObservableCollection<IItemFilterBlockBase> ItemFilterBlocks { get; }
public ObservableCollection<ItemFilterBlockGroup> ItemFilterBlockGroups { get; }
public ThemeComponentCollection ThemeComponents { get; set; }
@ -59,9 +59,7 @@ namespace Filtration.ObjectModel
public void ReplaceColors(ReplaceColorsParameterSet replaceColorsParameterSet)
{
foreach (
var block in
ItemFilterBlocks.Where(b => BlockIsColorReplacementCandidate(replaceColorsParameterSet, b)))
foreach (var block in ItemFilterBlocks.OfType<ItemFilterBlock>().Where(b => BlockIsColorReplacementCandidate(replaceColorsParameterSet, b)))
{
if (replaceColorsParameterSet.ReplaceTextColor)
{

View File

@ -8,5 +8,6 @@ namespace Filtration.Parser.Interface.Services
IItemFilterBlock TranslateStringToItemFilterBlock(string inputString, IItemFilterScriptSettings itemFilterScriptSettings);
string TranslateItemFilterBlockToString(IItemFilterBlock block);
void ReplaceAudioVisualBlockItemsFromString(ObservableCollection<IItemFilterBlockItem> blockItems, string inputString);
IItemFilterCommentBlock TranslateStringToItemFilterCommentBlock(string inputString);
}
}

View File

@ -109,6 +109,9 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\testscript2.txt" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>

View File

@ -91,6 +91,37 @@ namespace Filtration.Parser.Tests.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to #Script description
///#Script description
///#Script description
///#Script description
///
///#Comment
///#SomeComment
///
///#Blockdescription
///Show #Flasks - Endgame - Life/Mana - Divine/Eternal - Q10+ - Normal
/// Class &quot;Life Flasks&quot; &quot;Mana Flasks&quot;
/// Rarity Normal
/// DropLevel &gt;= 60
/// Quality &gt;= 10
/// SetFontSize 28
/// SetTextColor 240 240 240 #Normal Item Highlight
///
///#commentymccommentface
///
///Show #Flasks - Endgame - Life/Mana - Divine/Eternal - Q10+ - Normal
/// Class &quot;Life Flasks&quot; &quot;Mana Flasks&quot;
/// Rarity Normal
/// DropL [rest of string was truncated]&quot;;.
/// </summary>
public static string testscript2 {
get {
return ResourceManager.GetString("testscript2", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to ###BETA VERSION 1.0.0.1-8
///#Please test and let me know via pm on reddit /u/brute_force or @Thiole in game if i am online

View File

@ -118,10 +118,13 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="ThioleItemFilter" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ThioleItemFilter.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="testscript" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\testscript.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="testscript2" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\testscript2.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="ThioleItemFilter" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ThioleItemFilter.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
</root>

View File

@ -0,0 +1,31 @@
#Script description
#Script description
#Script description
#Script description
#Blockdescription
Show #Flasks - Endgame - Life/Mana - Divine/Eternal - Q10+ - Normal
Class "Life Flasks" "Mana Flasks"
Rarity Normal
SetFontSize 28
# commentymccommentface
Show
Class "Life Flasks" "Mana Flasks"
Rarity Normal
DropLevel >= 60
#commment
#morecomment
#blah
#anothercomment
#notpartofblockdescription
#blockdescription2
Show #TestBlock
Class "Life Flasks" "Mana Flasks"
Rarity Normal

View File

@ -26,6 +26,19 @@ namespace Filtration.Parser.Tests.Services
_testUtility = new ItemFilterBlockTranslatorTestUtility();
}
[Test]
public void TranslateStringToItemFilterBlockComment_ReturnsItemFilterBlockCommentWithSpacesNotRemoved()
{
//Arrange
var testInputString = "# This is a comment\r\n# Line 2 \r\n # Test";
//Act
var result = _testUtility.Translator.TranslateStringToItemFilterCommentBlock(testInputString);
//Assert
Assert.AreEqual(" This is a comment\r\n Line 2 \r\n Test", result.Comment);
}
[Test]
public void TranslateStringToItemFilterBlock_BlockGroupsEnabled_ActionBlockItemCommentIsNull()
{
@ -704,21 +717,6 @@ namespace Filtration.Parser.Tests.Services
Assert.AreEqual(95, blockItem.SecondValue);
}
[Test]
public void TranslateStringToItemFilterBlock_SectionComment_ReturnsItemFilterSectionObjectWithCorrectDescription()
{
// Arrange
const string testInputSectionDescription = "Wonderful items that you definitely won't want to miss!";
var inputString = "# Section: " + testInputSectionDescription;
// Act
var result = _testUtility.Translator.TranslateStringToItemFilterBlock(inputString, Mock.Of<IItemFilterScriptSettings>());
// Assert
Assert.IsInstanceOf<ItemFilterSection>(result);
Assert.AreEqual(testInputSectionDescription, result.Description);
}
[Test]
public void TranslateStringToItemFilterBlock_Everything_ReturnsCorrectObject()
{
@ -1571,22 +1569,6 @@ namespace Filtration.Parser.Tests.Services
Assert.AreEqual(expectedResult, result);
}
[Test]
public void TranslateItemFilterBlockToString_Section_ReturnsCorrectString()
{
// Arrange
const string testInputSectionText = "Ermagerd it's a section!";
var expectedResult = "# Section: " + testInputSectionText;
_testUtility.TestBlock = new ItemFilterSection { Description = testInputSectionText };
// Act
var result = _testUtility.Translator.TranslateItemFilterBlockToString(_testUtility.TestBlock);
// Assert
Assert.AreEqual(expectedResult, result);
}
[Test]
public void TranslateItemFilterBlockToString_DisabledBlock_ReturnsCorrectString()
{

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Filtration.ObjectModel;
using Filtration.ObjectModel.BlockItemTypes;
@ -9,6 +10,7 @@ using Filtration.Parser.Interface.Services;
using Filtration.Parser.Services;
using Filtration.Parser.Tests.Properties;
using Filtration.Properties;
using FluentAssertions;
using Moq;
using NUnit.Framework;
@ -75,6 +77,36 @@ namespace Filtration.Parser.Tests.Services
// Not crashing out when loading a huge script means this integration test has passed!
}
[Test]
public void TranslateStringToItemFilterScript_Blah()
{
// Arrange
var testInput = Resources.testscript2;
var blockTranslator = new ItemFilterBlockTranslator(_testUtility.MockBlockGroupHierarchyBuilder.Object);
var translator = new ItemFilterScriptTranslator(blockTranslator, _testUtility.MockBlockGroupHierarchyBuilder.Object);
// Act
var result = translator.TranslateStringToItemFilterScript(testInput);
// Assert
var expectedResult = Mock.Of<IItemFilterScript>(s => s.ItemFilterBlocks == new ObservableCollection<IItemFilterBlockBase>
{
Mock.Of<IItemFilterBlock>(c => c.Description == "Blockdescription"),
Mock.Of<IItemFilterCommentBlock>(c => c.Comment == " commentymccommentface"),
Mock.Of<IItemFilterBlock>(),
Mock.Of<IItemFilterCommentBlock>(c => c.Comment == "commment\r\nmorecomment\r\nblah"),
Mock.Of<IItemFilterCommentBlock>(c => c.Comment == "anothercomment"),
Mock.Of<IItemFilterCommentBlock>(c => c.Comment == "notpartofblockdescription "),
Mock.Of<IItemFilterBlock>(c => c.Description == "blockdescription2")
} && s.ItemFilterBlockGroups == new ObservableCollection<ItemFilterBlockGroup> { new ItemFilterBlockGroup("Root", null, false) }
&& s.ThemeComponents == new ThemeComponentCollection()
&& s.ItemFilterScriptSettings == new ItemFilterScriptSettings(new ThemeComponentCollection())
&& s.Description == "Script description\r\nScript description\r\nScript description\r\nScript description");
result.ShouldBeEquivalentTo(expectedResult);
}
[Test]
public void TranslateItemFilterScriptToString_OneBlock_CallsTranslator()
{
@ -221,7 +253,7 @@ namespace Filtration.Parser.Tests.Services
// Assert
Assert.AreEqual(2, result.ItemFilterBlocks.Count);
var block = result.ItemFilterBlocks.First(l => l.GetType() != typeof(ItemFilterSection));
var block = result.ItemFilterBlocks.OfType<ItemFilterBlock>().First(l => l.GetType() != typeof(ItemFilterSection));
Assert.AreEqual(4, block.BlockItems.Count);
var baseTypeItem = block.BlockItems.OfType<BaseTypeBlockItem>().First();
Assert.AreEqual(2, baseTypeItem.Items.Count);
@ -244,7 +276,7 @@ namespace Filtration.Parser.Tests.Services
// Assert
Assert.AreEqual("Script edited with Filtration - https://github.com/ben-wallis/Filtration", result.Description);
var firstBlock = result.ItemFilterBlocks.First();
var firstBlock = result.ItemFilterBlocks.OfType<ItemFilterBlock>().First();
Assert.IsNull(firstBlock.Description);
}
@ -311,9 +343,9 @@ namespace Filtration.Parser.Tests.Services
// Assert
Assert.AreEqual(3, result.ItemFilterBlocks.Count);
var firstBlock = result.ItemFilterBlocks.First();
var secondBlock = result.ItemFilterBlocks.Skip(1).First();
var thirdBlock = result.ItemFilterBlocks.Skip(2).First();
var firstBlock = result.ItemFilterBlocks.OfType<ItemFilterBlock>().First();
var secondBlock = result.ItemFilterBlocks.OfType<ItemFilterBlock>().Skip(1).First();
var thirdBlock = result.ItemFilterBlocks.OfType<ItemFilterBlock>().Skip(2).First();
Assert.AreEqual(3, firstBlock.BlockItems.Count);
Assert.AreEqual(5, secondBlock.BlockItems.Count);
@ -344,7 +376,7 @@ namespace Filtration.Parser.Tests.Services
// Assert
Assert.AreEqual(2, result.ItemFilterBlocks.Count);
var secondBlock = result.ItemFilterBlocks.Skip(1).First();
var secondBlock = result.ItemFilterBlocks.OfType<ItemFilterBlock>().Skip(1).First();
Assert.AreEqual("This is a disabled block", secondBlock.Description);
}
@ -379,7 +411,7 @@ namespace Filtration.Parser.Tests.Services
// Assert
Assert.AreEqual(2, result.ItemFilterBlocks.Count);
var secondBlock = result.ItemFilterBlocks.Skip(1).First();
var secondBlock = result.ItemFilterBlocks.OfType<ItemFilterBlock>().Skip(1).First();
Assert.AreEqual("This is a disabled block", secondBlock.Description);
Assert.AreEqual("My Block Group", secondBlock.BlockGroup.GroupName);
}

View File

@ -29,6 +29,22 @@ namespace Filtration.Parser.Services
_blockGroupHierarchyBuilder = blockGroupHierarchyBuilder;
}
// Converts a string into an ItemFilterCommentBlock maintaining newlines and spaces but removing # characters
public IItemFilterCommentBlock TranslateStringToItemFilterCommentBlock(string inputString)
{
var itemFilterCommentBlock = new ItemFilterCommentBlock();
foreach (var line in new LineReader(() => new StringReader(inputString)))
{
var trimmedLine = line.TrimStart(' ').TrimStart('#');
itemFilterCommentBlock.Comment += trimmedLine + Environment.NewLine;
}
itemFilterCommentBlock.Comment = itemFilterCommentBlock.Comment.TrimEnd('\r', '\n');
return itemFilterCommentBlock;
}
// This method converts a string into a ItemFilterBlock. This is used for pasting ItemFilterBlocks
// and reading ItemFilterScripts from a file.
public IItemFilterBlock TranslateStringToItemFilterBlock(string inputString, IItemFilterScriptSettings itemFilterScriptSettings)
@ -39,16 +55,6 @@ namespace Filtration.Parser.Services
foreach (var line in new LineReader(() => new StringReader(inputString)))
{
if (line.StartsWith(@"# Section:"))
{
var section = new ItemFilterSection
{
Description = line.Substring(line.IndexOf(":", StringComparison.Ordinal) + 1).Trim()
};
return section;
}
if (line.StartsWith(@"#") && !showHideFound)
{
block.Description = line.TrimStart('#').TrimStart(' ');
@ -205,6 +211,7 @@ namespace Filtration.Parser.Services
switch (matches.Count)
{
case 1:
{
if (matches[0].Success)
{
var blockItemValue = new SoundBlockItem
@ -215,7 +222,9 @@ namespace Filtration.Parser.Services
block.BlockItems.Add(blockItemValue);
}
break;
}
case 2:
{
if (matches[0].Success && matches[1].Success)
{
var blockItemValue = new SoundBlockItem
@ -227,6 +236,7 @@ namespace Filtration.Parser.Services
}
break;
}
}
break;
}
}
@ -436,7 +446,8 @@ namespace Filtration.Parser.Services
// to the clipboard, and when saving a ItemFilterScript.
public string TranslateItemFilterBlockToString(IItemFilterBlock block)
{
if (block.GetType() == typeof (ItemFilterSection))
// TODO: fix
if (block.GetType() == typeof (ItemFilterCommentBlock))
{
return "# Section: " + block.Description;
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Filtration.Common.Utilities;
using Filtration.ObjectModel;
@ -10,6 +11,26 @@ using Filtration.Properties;
namespace Filtration.Parser.Services
{
internal class ItemFilterBlockBoundary
{
public ItemFilterBlockBoundary(int startLine, ItemFilterBlockBoundaryType itemFilterBlockBoundaryType)
{
StartLine = startLine;
BoundaryType = itemFilterBlockBoundaryType;
}
public int StartLine { get; set; }
public ItemFilterBlockBoundaryType BoundaryType { get; set; }
}
internal enum ItemFilterBlockBoundaryType
{
ItemFilterBlock,
CommentBlock
}
internal class ItemFilterScriptTranslator : IItemFilterScriptTranslator
{
private readonly IItemFilterBlockTranslator _blockTranslator;
@ -98,7 +119,7 @@ namespace Filtration.Parser.Services
var lines = Regex.Split(inputString, "\r\n|\r|\n");
// Process the script header
for (var i = 0; i < conditionBoundaries.First.Value; i++)
for (var i = 0; i < conditionBoundaries.First.Value.StartLine; i++)
{
if (lines[i].StartsWith("#"))
{
@ -115,43 +136,85 @@ namespace Filtration.Parser.Services
// and add that object to the ItemFilterBlocks list
for (var boundary = conditionBoundaries.First; boundary != null; boundary = boundary.Next)
{
var begin = boundary.Value;
var end = boundary.Next?.Value ?? lines.Length;
var begin = boundary.Value.StartLine;
var end = boundary.Next?.Value.StartLine ?? lines.Length;
var block = new string[end - begin];
Array.Copy(lines, begin, block, 0, end - begin);
var blockString = string.Join("\r\n", block);
if (boundary.Value.BoundaryType == ItemFilterBlockBoundaryType.ItemFilterBlock)
{
script.ItemFilterBlocks.Add(_blockTranslator.TranslateStringToItemFilterBlock(blockString, script.ItemFilterScriptSettings));
}
else
{
script.ItemFilterBlocks.Add(_blockTranslator.TranslateStringToItemFilterCommentBlock(blockString));
}
}
_blockGroupHierarchyBuilder.Cleanup();
return script;
}
private static LinkedList<int> IdentifyBlockBoundaries(string inputString)
private static LinkedList<ItemFilterBlockBoundary> IdentifyBlockBoundaries(string inputString)
{
var blockBoundaries = new LinkedList<int>();
var blockBoundaries = new LinkedList<ItemFilterBlockBoundary>();
var previousLine = string.Empty;
var currentLine = 0;
var currentLine = -1;
var currentItemFilterBlockBoundary = new ItemFilterBlockBoundary(1, ItemFilterBlockBoundaryType.CommentBlock);
foreach (var line in new LineReader(() => new StringReader(inputString)))
{
currentLine++;
var trimmedLine = line.Trim(' ');
if (trimmedLine.StartsWith("Show") || trimmedLine.StartsWith("Hide") ||
trimmedLine.StartsWith("# Section:"))
if (string.IsNullOrWhiteSpace(trimmedLine))
{
previousLine = line;
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)
{
blockBoundaries.AddLast(currentItemFilterBlockBoundary);
currentItemFilterBlockBoundary = new ItemFilterBlockBoundary(currentLine, ItemFilterBlockBoundaryType.CommentBlock);
}
// A line starting with a comment where the previous line was null represents the start of a new comment (unless we're on the first
// line in which case it's not a new comment).
else if (trimmedLine.StartsWith("#") && string.IsNullOrWhiteSpace(previousLine) && currentLine > 0)
{
if (blockBoundaries.Count > 0)
{
blockBoundaries.AddLast(currentItemFilterBlockBoundary);
}
currentItemFilterBlockBoundary = new ItemFilterBlockBoundary(currentLine, ItemFilterBlockBoundaryType.CommentBlock);
}
else if (trimmedLine.StartsWith("Show") || trimmedLine.StartsWith("Hide"))
{
// If the line previous to the Show or Hide line is a comment then we should include that in the block
// as it represents the block description.
// currentLine > 2 caters for an edge case where the script description is a single line and the first
// block has no description. This prevents the script description from being assigned to the first block's description.
blockBoundaries.AddLast(previousLine.StartsWith("#") && !previousLine.StartsWith("# Section:") &&
currentLine > 2
? currentLine - 2
: currentLine - 1);
if (!(currentItemFilterBlockBoundary.StartLine == currentLine - 1 && currentItemFilterBlockBoundary.BoundaryType == ItemFilterBlockBoundaryType.CommentBlock))
{
blockBoundaries.AddLast(currentItemFilterBlockBoundary);
}
currentItemFilterBlockBoundary = new ItemFilterBlockBoundary(previousLine.StartsWith("#") && currentLine > 2 ? currentLine - 1 : currentLine,
ItemFilterBlockBoundaryType.ItemFilterBlock);
}
previousLine = line;
}
if (blockBoundaries.Last.Value != currentItemFilterBlockBoundary)
{
blockBoundaries.AddLast(currentItemFilterBlockBoundary);
}
return blockBoundaries;
}
@ -178,7 +241,7 @@ namespace Filtration.Parser.Services
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var block in script.ItemFilterBlocks)
{
outputString += _blockTranslator.TranslateItemFilterBlockToString(block) + Environment.NewLine;
outputString += _blockTranslator.TranslateItemFilterBlockToString(block as ItemFilterBlock) + Environment.NewLine;
if (Settings.Default.ExtraLineBetweenBlocks)
{

View File

@ -44,7 +44,7 @@ namespace Filtration.ThemeEditor.Services
break;
}
foreach (var block in script.ItemFilterBlocks)
foreach (var block in script.ItemFilterBlocks.OfType<ItemFilterBlock>())
{
foreach (var blockItem in block.BlockItems.Where(i => i.GetType() == targetType))
{

View File

@ -4,7 +4,6 @@ using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Media;
using Filtration.Common.ViewModels;
using Filtration.ObjectModel;
using Filtration.ObjectModel.BlockItemBaseTypes;
using Filtration.ObjectModel.BlockItemTypes;

View File

@ -320,7 +320,7 @@ namespace Filtration.ViewModels
ItemFilterBlockViewModels.Clear();
Script = itemFilterScript;
foreach (var block in Script.ItemFilterBlocks)
foreach (var block in Script.ItemFilterBlocks.OfType<ItemFilterBlock>())
{
var vm = _itemFilterBlockViewModelFactory.Create();
vm.Initialise(block, this);
@ -426,7 +426,7 @@ namespace Filtration.ViewModels
var unusedThemeComponents =
Script.ThemeComponents.Where(
t =>
Script.ItemFilterBlocks.Count(
Script.ItemFilterBlocks.OfType<ItemFilterBlock>().Count(
b => b.BlockItems.OfType<ColorBlockItem>().Count(i => i.ThemeComponent == t) > 0) == 0).ToList();
if (unusedThemeComponents.Count <= 0) return true;