/
PatchExtractor.cs
156 lines (138 loc) · 6.21 KB
/
PatchExtractor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ModuleManager.Extensions;
namespace ModuleManager
{
public static class PatchExtractor
{
private static readonly Regex firstRegex = new Regex(@":FIRST", RegexOptions.IgnoreCase);
private static readonly Regex finalRegex = new Regex(@":FINAL", RegexOptions.IgnoreCase);
private static readonly Regex beforeRegex = new Regex(@":BEFORE\[([^\[\]]+)\]", RegexOptions.IgnoreCase);
private static readonly Regex forRegex = new Regex(@":FOR\[([^\[\]]+)\]", RegexOptions.IgnoreCase);
private static readonly Regex afterRegex = new Regex(@":AFTER\[([^\[\]]+)\]", RegexOptions.IgnoreCase);
public static PatchList SortAndExtractPatches(UrlDir databaseRoot, IEnumerable<string> modList, IPatchProgress progress)
{
PatchList list = new PatchList(modList);
foreach (UrlDir.UrlConfig url in databaseRoot.AllConfigs.ToArray())
{
try
{
Command command = CommandParser.Parse(url.type, out _);;
Match firstMatch = firstRegex.Match(url.type);
Match finalMatch = finalRegex.Match(url.type);
Match beforeMatch = beforeRegex.Match(url.type);
Match forMatch = forRegex.Match(url.type);
Match afterMatch = afterRegex.Match(url.type);
int matchCount = 0;
if (firstMatch.Success) matchCount++;
if (finalMatch.Success) matchCount++;
if (beforeMatch.Success) matchCount++;
if (forMatch.Success) matchCount++;
if (afterMatch.Success) matchCount++;
if (firstMatch.NextMatch().Success) matchCount++;
if (finalMatch.NextMatch().Success) matchCount++;
if (beforeMatch.NextMatch().Success) matchCount++;
if (forMatch.NextMatch().Success) matchCount++;
if (afterMatch.NextMatch().Success) matchCount++;
bool error = false;
if (command == Command.Insert && matchCount > 0)
{
progress.Error(url, $"Error - pass specifier detected on an insert node (not a patch): {url.SafeUrl()}");
error = true;
}
if (matchCount > 1)
{
progress.Error(url, $"Error - more than one pass specifier on a node: {url.SafeUrl()}");
error = true;
}
if (error)
{
url.parent.configs.Remove(url);
continue;
}
if (command == Command.Insert) continue;
url.parent.configs.Remove(url);
Match theMatch = null;
List<UrlDir.UrlConfig> thePass = null;
bool modNotFound = false;
if (firstMatch.Success)
{
theMatch = firstMatch;
thePass = list.firstPatches;
}
else if (finalMatch.Success)
{
theMatch = finalMatch;
thePass = list.finalPatches;
}
else if (beforeMatch.Success)
{
if (CheckMod(beforeMatch, list.modPasses, out string theMod))
{
theMatch = beforeMatch;
thePass = list.modPasses[theMod].beforePatches;
}
else
{
modNotFound = true;
progress.NeedsUnsatisfiedBefore(url);
}
}
else if (forMatch.Success)
{
if (CheckMod(forMatch, list.modPasses, out string theMod))
{
theMatch = forMatch;
thePass = list.modPasses[theMod].forPatches;
}
else
{
modNotFound = true;
progress.NeedsUnsatisfiedFor(url);
}
}
else if (afterMatch.Success)
{
if (CheckMod(afterMatch, list.modPasses, out string theMod))
{
theMatch = afterMatch;
thePass = list.modPasses[theMod].afterPatches;
}
else
{
modNotFound = true;
progress.NeedsUnsatisfiedAfter(url);
}
}
else
{
thePass = list.legacyPatches;
}
if (modNotFound) continue;
UrlDir.UrlConfig newUrl = url;
if (theMatch != null)
{
string newName = url.type.Remove(theMatch.Index, theMatch.Length);
ConfigNode newNode = new ConfigNode(newName) { id = url.config.id };
newNode.ShallowCopyFrom(url.config);
newUrl = new UrlDir.UrlConfig(url.parent, newNode);
}
thePass.Add(newUrl);
}
catch(Exception e)
{
progress.Exception(url, $"Exception while parsing pass for config: {url.SafeUrl()}", e);
}
}
return list;
}
private static bool CheckMod(Match match, PatchList.ModPassCollection modPasses, out string theMod)
{
theMod = match.Groups[1].Value.Trim().ToLower();
return modPasses.HasMod(theMod);
}
}
}