Skip to content

Commit

Permalink
Fix parsing curly braces in quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Dec 4, 2022
1 parent dca9233 commit 882ad24
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
57 changes: 44 additions & 13 deletions src/IronyModManager.Parser/CodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 02-22-2020
//
// Last Modified By : Mario
// Last Modified On : 03-06-2022
// Last Modified On : 12-04-2022
// ***********************************************************************
// <copyright file="CodeParser.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -54,14 +54,8 @@ public class CodeParser : ICodeParser
};

/// <summary>
/// The code terminator map
/// The quotes regex
/// </summary>
protected static readonly Dictionary<string, string> codeTerminatorMap = new()
{
{ $"{Common.Constants.Scripts.OpenObject}", $" {Common.Constants.Scripts.OpenObject} " },
{ $"{Common.Constants.Scripts.CloseObject}", $" {Common.Constants.Scripts.CloseObject} " }
};

protected static readonly Regex quotesRegex = new("\".*?\"", RegexOptions.Compiled | RegexOptions.IgnoreCase);

/// <summary>
Expand Down Expand Up @@ -95,7 +89,8 @@ public CodeParser(ILogger logger)
public virtual IEnumerable<string> CleanCode(string file, IEnumerable<string> lines)
{
var commentId = IsLua(file) ? Common.Constants.Scripts.LuaScriptCommentId : Common.Constants.Scripts.ScriptCommentId.ToString();
return lines.Where(p => !string.IsNullOrWhiteSpace(p) && !p.Trim().StartsWith(commentId))
var cleaned = FormatCurlyBraces(string.Join(Environment.NewLine, lines)).Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
return cleaned.Where(p => !string.IsNullOrWhiteSpace(p) && !p.Trim().StartsWith(commentId))
.Select(p => FormatCodeTerminators(RemoveInlineComments(commentId, p)));
}

Expand Down Expand Up @@ -300,11 +295,47 @@ protected string FormatCodeTerminators(string line)
cleaned = cleaned.Remove(cleanedRegexHits[i].Index, cleanedRegexHits[i].Length).Insert(cleanedRegexHits[i].Index, insert);
}
}
foreach (var item in codeTerminatorMap)
return cleaned.Trim();
}

/// <summary>
/// Formats the curly braces.
/// </summary>
/// <param name="code">The code.</param>
/// <returns>System.String.</returns>
protected string FormatCurlyBraces(string code)
{
var sb = new StringBuilder();
var quoteOpened = false;
for (int i = 0; i < code.Length; i++)
{
cleaned = cleaned.Replace(item.Key, item.Value).Trim();
var c = code[i];
if (c == Common.Constants.Scripts.Quote)
{
if (quoteOpened)
{
quoteOpened = false;
}
else
{
quoteOpened = true;
}
}
var addLine = true;
if (c == Common.Constants.Scripts.OpenObject || c == Common.Constants.Scripts.CloseObject)
{
if (!quoteOpened)
{
sb.Append($" {c} ");
addLine = false;
}
}
if (addLine)
{
sb.Append(c);
}
}
return cleaned;
return sb.ToString();
}

/// <summary>
Expand Down Expand Up @@ -599,7 +630,7 @@ protected ElementValue GetElementValue(List<char> code, ref int index, bool brea
}
}
}
else if (Common.Constants.Scripts.CodeTerminators.Any(p => p == character.GetValueOrDefault()))
else if (Common.Constants.Scripts.CodeTerminators.Any(p => p == character.GetValueOrDefault()) && !openQuote)
{
terminator = character;
index = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 02-12-2020
//
// Last Modified By : Mario
// Last Modified On : 10-29-2022
// Last Modified On : 12-04-2022
// ***********************************************************************
// <copyright file="GameRegistration.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -184,7 +184,7 @@ private IGameType GetHOI4(string baseUserDir)
game.ParadoxGameId = Shared.Constants.GamesTypes.HeartsOfIron4.ParadoxGameId;
game.SupportedMergeTypes = IronyModManager.Models.Common.SupportedMergeTypes.Zip | IronyModManager.Models.Common.SupportedMergeTypes.Basic;
game.ModDescriptorType = IronyModManager.Models.Common.ModDescriptorType.DescriptorMod;
game.GameIndexCacheVersion = 4;
game.GameIndexCacheVersion = 5;
MapGameSettings(game, GetExecutableSettings(game));
return game;
}
Expand Down Expand Up @@ -245,7 +245,7 @@ private IGameType GetStellaris(string baseUserDir)
game.GogAppId = Shared.Constants.GamesTypes.Stellaris.GogId;
game.SupportedMergeTypes = IronyModManager.Models.Common.SupportedMergeTypes.Zip | IronyModManager.Models.Common.SupportedMergeTypes.Basic;
game.ModDescriptorType = IronyModManager.Models.Common.ModDescriptorType.DescriptorMod;
game.GameIndexCacheVersion = 9;
game.GameIndexCacheVersion = 10;
MapGameSettings(game, GetExecutableSettings(game));
return game;
}
Expand Down

0 comments on commit 882ad24

Please sign in to comment.