Skip to content

Commit 9bd8253

Browse files
committed
Make sure badly formed mod passes are an error
1 parent 2e5854d commit 9bd8253

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

ModuleManager/PatchExtractor.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class PatchExtractor
1111
{
1212
private static readonly Regex firstRegex = new Regex(@":FIRST", RegexOptions.IgnoreCase);
1313
private static readonly Regex finalRegex = new Regex(@":FINAL", RegexOptions.IgnoreCase);
14-
private static readonly Regex beforeRegex = new Regex(@":BEFORE\[([^\[\]]+)\]", RegexOptions.IgnoreCase);
15-
private static readonly Regex forRegex = new Regex(@":FOR\[([^\[\]]+)\]", RegexOptions.IgnoreCase);
16-
private static readonly Regex afterRegex = new Regex(@":AFTER\[([^\[\]]+)\]", RegexOptions.IgnoreCase);
14+
private static readonly Regex beforeRegex = new Regex(@":BEFORE(?:\[([^\[\]]+)\])?", RegexOptions.IgnoreCase);
15+
private static readonly Regex forRegex = new Regex(@":FOR(?:\[([^\[\]]+)\])?", RegexOptions.IgnoreCase);
16+
private static readonly Regex afterRegex = new Regex(@":AFTER(?:\[([^\[\]]+)\])?", RegexOptions.IgnoreCase);
1717

1818
public static PatchList SortAndExtractPatches(UrlDir databaseRoot, IEnumerable<string> modList, IPatchProgress progress)
1919
{
@@ -64,6 +64,21 @@ public static PatchList SortAndExtractPatches(UrlDir databaseRoot, IEnumerable<s
6464
progress.Error(url, $"Error - more than one pass specifier on a node: {url.SafeUrl()}");
6565
error = true;
6666
}
67+
if (beforeMatch.Success && !beforeMatch.Groups[1].Success)
68+
{
69+
progress.Error(url, "Error - malformed :BEFORE patch specifier detected: " + url.SafeUrl());
70+
error = true;
71+
}
72+
if (forMatch.Success && !forMatch.Groups[1].Success)
73+
{
74+
progress.Error(url, "Error - malformed :FOR patch specifier detected: " + url.SafeUrl());
75+
error = true;
76+
}
77+
if (afterMatch.Success && !afterMatch.Groups[1].Success)
78+
{
79+
progress.Error(url, "Error - malformed :AFTER patch specifier detected: " + url.SafeUrl());
80+
error = true;
81+
}
6782
if (error)
6883
{
6984
url.parent.configs.Remove(url);

ModuleManagerTests/PatchExtractorTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,33 @@ public void TestSortAndExtractPatches__NotBracketBalanced()
307307
Assert.Empty(list.modPasses["mod1"].afterPatches);
308308
}
309309

310+
[Fact]
311+
public void TestSortAndExtractPatches__BadlyFormed()
312+
{
313+
UrlDir.UrlConfig config1 = CreateConfig("@NODE:FOR[mod1]");
314+
UrlDir.UrlConfig config2 = CreateConfig("@NODE:FOR[]");
315+
UrlDir.UrlConfig config3 = CreateConfig("@NADE:FIRST:BEFORE");
316+
UrlDir.UrlConfig config4 = CreateConfig("@NADE:AFTER");
317+
318+
string[] modList = { "mod1" };
319+
PatchList list = PatchExtractor.SortAndExtractPatches(root, modList, progress);
320+
321+
Assert.Empty(root.AllConfigs);
322+
323+
progress.Received().Error(config2, "Error - malformed :FOR patch specifier detected: abc/def/@NODE:FOR[]");
324+
progress.Received().Error(config3, "Error - more than one pass specifier on a node: abc/def/@NADE:FIRST:BEFORE");
325+
progress.Received().Error(config3, "Error - malformed :BEFORE patch specifier detected: abc/def/@NADE:FIRST:BEFORE");
326+
progress.Received().Error(config4, "Error - malformed :AFTER patch specifier detected: abc/def/@NADE:AFTER");
327+
328+
Assert.Empty(list.firstPatches);
329+
Assert.Empty(list.legacyPatches);
330+
Assert.Empty(list.finalPatches);
331+
Assert.Empty(list.modPasses["mod1"].beforePatches);
332+
Assert.Equal(1, list.modPasses["mod1"].forPatches.Count);
333+
AssertUrlCorrect("@NODE", config1, list.modPasses["mod1"].forPatches[0]);
334+
Assert.Empty(list.modPasses["mod1"].afterPatches);
335+
}
336+
310337
private UrlDir.UrlConfig CreateConfig(string name)
311338
{
312339
ConfigNode node = new TestConfigNode(name)

0 commit comments

Comments
 (0)