Skip to content

Commit

Permalink
Fix #38
Browse files Browse the repository at this point in the history
Switch to beta tag
Add version into in app title
  • Loading branch information
bcssov committed Apr 26, 2020
1 parent ebbdec6 commit 0260a4e
Show file tree
Hide file tree
Showing 37 changed files with 764 additions and 516 deletions.
114 changes: 92 additions & 22 deletions src/IronyModManager.Parser.Common/Parsers/BaseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 02-17-2020
//
// Last Modified By : Mario
// Last Modified On : 04-25-2020
// Last Modified On : 04-26-2020
// ***********************************************************************
// <copyright file="BaseParser.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -43,6 +43,11 @@ public abstract class BaseParser : IDefaultParser
/// </summary>
protected readonly ICodeParser codeParser;

/// <summary>
/// The logger
/// </summary>
protected readonly ILogger logger;

#endregion Fields

#region Constructors
Expand All @@ -51,9 +56,11 @@ public abstract class BaseParser : IDefaultParser
/// Initializes a new instance of the <see cref="BaseParser" /> class.
/// </summary>
/// <param name="codeParser">The code parser.</param>
public BaseParser(ICodeParser codeParser)
/// <param name="logger">The logger.</param>
public BaseParser(ICodeParser codeParser, ILogger logger)
{
this.codeParser = codeParser;
this.logger = logger;
}

#endregion Constructors
Expand Down Expand Up @@ -162,13 +169,26 @@ protected virtual string EvalComplexParseKeyValueForId(IScriptKeyValue value)
/// Evals for errors only.
/// </summary>
/// <param name="args">The arguments.</param>
/// <param name="fallbackToSimpleParser">if set to <c>true</c> [fallback to simple parser].</param>
/// <returns>IEnumerable&lt;IDefinition&gt;.</returns>
protected virtual IEnumerable<IDefinition> EvalForErrorsOnly(ParserArgs args)
protected virtual IEnumerable<IDefinition> EvalForErrorsOnly(ParserArgs args, bool fallbackToSimpleParser = true)
{
if (HasPassedComplexThreshold(args.Lines))
{
return EvalSimpleParseForErrorsOnly(args);
}
if (fallbackToSimpleParser)
{
try
{
return EvalComplexParseForErrorsOnly(args);
}
catch (Exception ex)
{
logger.Error(ex, $"CWTools has encountered an error, switching to simple parser. ModName: {args.ModName} File: {args.File}");
return EvalSimpleParseForErrorsOnly(args);
}
}
return EvalComplexParseForErrorsOnly(args);
}

Expand Down Expand Up @@ -299,48 +319,98 @@ protected virtual void OnSimpleParseReadObjectLine(ParsingArgs args)
/// Parses the complex first level.
/// </summary>
/// <param name="args">The arguments.</param>
/// <param name="fallbackToSimpleParser">if set to <c>true</c> [fallback to simple parser].</param>
/// <returns>IEnumerable&lt;IDefinition&gt;.</returns>
protected virtual IEnumerable<IDefinition> ParseComplexFirstLevel(ParserArgs args)
protected virtual IEnumerable<IDefinition> ParseComplexFirstLevel(ParserArgs args, bool fallbackToSimpleParser = true)
{
var result = new List<IDefinition>();
var value = codeParser.ParseScript(args.Lines, args.File);
if (value.Error != null)
List<IDefinition> parse()
{
result.Add(ParseScriptError(value.Error, args));
var result = new List<IDefinition>();
var value = codeParser.ParseScript(args.Lines, args.File);
if (value.Error != null)
{
result.Add(ParseScriptError(value.Error, args));
}
else
{
if (value.Value.Nodes?.Count() > 0)
{
foreach (var item in value.Value.Nodes)
{
result.AddRange(ParseComplexScriptKeyValues(item.KeyValues, args, parent: item.Key));
result.AddRange(ParseComplexScriptNodes(item.Nodes, args, parent: item.Key));
}
}
}
return result;
}
else
if (fallbackToSimpleParser)
{
if (value.Value.Nodes?.Count() > 0)
try
{
foreach (var item in value.Value.Nodes)
return parse();
}
catch (Exception ex)
{
logger.Error(ex, $"CWTools has encountered an error, switching to simple parser. ModName: {args.ModName} File: {args.File}");
var error = EvalSimpleParseForErrorsOnly(args);
if (error != null)
{
result.AddRange(ParseComplexScriptKeyValues(item.KeyValues, args, parent: item.Key));
result.AddRange(ParseComplexScriptNodes(item.Nodes, args, parent: item.Key));
return error;
}
return ParseSimple(args);
}
}
return result;
else
{
return parse();
}
}

/// <summary>
/// Parses the complex root.
/// </summary>
/// <param name="args">The arguments.</param>
/// <param name="fallbackToSimpleParser">if set to <c>true</c> [fallback to simple parser].</param>
/// <returns>IEnumerable&lt;IDefinition&gt;.</returns>
protected virtual IEnumerable<IDefinition> ParseComplexRoot(ParserArgs args)
protected virtual IEnumerable<IDefinition> ParseComplexRoot(ParserArgs args, bool fallbackToSimpleParser = true)
{
var result = new List<IDefinition>();
var values = codeParser.ParseScript(args.Lines, args.File);
if (values.Error != null)
List<IDefinition> parse()
{
result.Add(ParseScriptError(values.Error, args));
var result = new List<IDefinition>();
var values = codeParser.ParseScript(args.Lines, args.File);
if (values.Error != null)
{
result.Add(ParseScriptError(values.Error, args));
}
else
{
result.AddRange(ParseComplexScriptKeyValues(values.Value.KeyValues, args));
result.AddRange(ParseComplexScriptNodes(values.Value.Nodes, args));
}
return result;
}
if (fallbackToSimpleParser)
{
try
{
return parse();
}
catch (Exception ex)
{
logger.Error(ex, $"CWTools has encountered an error, switching to simple parser. ModName: {args.ModName} File: {args.File}");
var error = EvalSimpleParseForErrorsOnly(args);
if (error != null)
{
return error;
}
return ParseSimple(args);
}
}
else
{
result.AddRange(ParseComplexScriptKeyValues(values.Value.KeyValues, args));
result.AddRange(ParseComplexScriptNodes(values.Value.Nodes, args));
return parse();
}
return result;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/IronyModManager.Parser.Tests/DefaultParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void Parse_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new DefaultParser(new CodeParser());
var parser = new DefaultParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(4);
Expand Down Expand Up @@ -218,7 +218,7 @@ public void Parse_ending_edge_case_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new DefaultParser(new CodeParser());
var parser = new DefaultParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(4);
Expand Down
4 changes: 2 additions & 2 deletions src/IronyModManager.Parser.Tests/GenericBinaryParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void CanParse_should_be_false_then_true()
File = "common\\gamerules\\test.txt",
GameType = "Stellaris"
};
var parser = new BinaryParser(new CodeParser());
var parser = new BinaryParser(new CodeParser(), null);
parser.CanParse(args).Should().BeFalse();
args.File = "common\\gfx\\test.png";
parser.CanParse(args).Should().BeTrue();
Expand All @@ -60,7 +60,7 @@ public void Parse_should_yield_results()
File = "common\\gfx\\test.png",
ModName = "fake"
};
var parser = new BinaryParser(new CodeParser());
var parser = new BinaryParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(1);
Expand Down
12 changes: 6 additions & 6 deletions src/IronyModManager.Parser.Tests/GenericDefinesParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void CanParse_should_be_false_then_true()
File = "common\\gamerules\\test.txt",
GameType = "Stellaris"
};
var parser = new Generic.DefinesParser(new CodeParser());
var parser = new Generic.DefinesParser(new CodeParser(), null);
parser.CanParse(args).Should().BeFalse();
args.File = "common\\defines\\t.txt";
parser.CanParse(args).Should().BeTrue();
Expand Down Expand Up @@ -90,7 +90,7 @@ public void Parse_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new Generic.DefinesParser(new CodeParser());
var parser = new Generic.DefinesParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(3);
Expand Down Expand Up @@ -159,7 +159,7 @@ public void Parse_complex_type_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new Generic.DefinesParser(new CodeParser());
var parser = new Generic.DefinesParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(1);
Expand Down Expand Up @@ -215,7 +215,7 @@ public void Parse_complex_type_edge_case_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new Generic.DefinesParser(new CodeParser());
var parser = new Generic.DefinesParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(1);
Expand Down Expand Up @@ -266,7 +266,7 @@ public void Parse_ending_edge_case_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new Generic.DefinesParser(new CodeParser());
var parser = new Generic.DefinesParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(1);
Expand Down Expand Up @@ -318,7 +318,7 @@ public void Parse_multiple_types_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new Generic.DefinesParser(new CodeParser());
var parser = new Generic.DefinesParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(3);
Expand Down
18 changes: 9 additions & 9 deletions src/IronyModManager.Parser.Tests/GenericGraphicsParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void CanParse_gui_should_be_false_then_true()
{
File = "common\\gamerules\\test.txt",
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
parser.CanParse(args).Should().BeFalse();
args.File = "gui\\gui.gui";
parser.CanParse(args).Should().BeTrue();
Expand Down Expand Up @@ -88,7 +88,7 @@ public void Parse_gui_edge_case_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(2);
Expand Down Expand Up @@ -145,7 +145,7 @@ public void Parse_gui_ending_edge_case_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(1);
Expand Down Expand Up @@ -195,7 +195,7 @@ public void Parse_gui_inline_edge_case_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(1);
Expand Down Expand Up @@ -263,7 +263,7 @@ public void Parse_gui_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(2);
Expand Down Expand Up @@ -300,7 +300,7 @@ public void CanParse_gfx_should_be_false_then_true()
{
File = "common\\gamerules\\test.txt",
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
parser.CanParse(args).Should().BeFalse();
args.File = "gfx\\gfx.gfx";
parser.CanParse(args).Should().BeTrue();
Expand Down Expand Up @@ -358,7 +358,7 @@ public void Parse_gfx_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(2);
Expand Down Expand Up @@ -419,7 +419,7 @@ public void Parse_gfx_ending_edge_case_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(1);
Expand Down Expand Up @@ -491,7 +491,7 @@ public void Parse_gfx_bitmap_type_override_should_yield_results()
Lines = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
ModName = "fake"
};
var parser = new GraphicsParser(new CodeParser());
var parser = new GraphicsParser(new CodeParser(), null);
var result = parser.Parse(args).ToList();
result.Should().NotBeNullOrEmpty();
result.Count().Should().Be(2);
Expand Down
Loading

0 comments on commit 0260a4e

Please sign in to comment.