/
PatchApplier.cs
196 lines (169 loc) · 7.83 KB
/
PatchApplier.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Linq;
using ModuleManager.Logging;
using ModuleManager.Extensions;
using ModuleManager.Progress;
using NodeStack = ModuleManager.Collections.ImmutableStack<ConfigNode>;
namespace ModuleManager
{
public class PatchApplier
{
private readonly IBasicLogger logger;
private readonly IPatchProgress progress;
private readonly UrlDir databaseRoot;
private readonly PatchList patchList;
private readonly UrlDir.UrlFile[] allConfigFiles;
public string Activity { get; private set; }
public PatchApplier(PatchList patchList, UrlDir databaseRoot, IPatchProgress progress, IBasicLogger logger)
{
this.patchList = patchList;
this.databaseRoot = databaseRoot;
this.progress = progress;
this.logger = logger;
allConfigFiles = databaseRoot.AllConfigFiles.ToArray();
}
public void ApplyPatches()
{
ApplyPatches(":FIRST", patchList.firstPatches);
// any node without a :pass
ApplyPatches(":LEGACY (default)", patchList.legacyPatches);
foreach (PatchList.ModPass pass in patchList.modPasses)
{
string upperModName = pass.name.ToUpper();
ApplyPatches($":BEFORE[{upperModName}]", pass.beforePatches);
ApplyPatches($":FOR[{upperModName}]", pass.forPatches);
ApplyPatches($":AFTER[{upperModName}]", pass.afterPatches);
}
// :Final node
ApplyPatches(":FINAL", patchList.finalPatches);
}
private void ApplyPatches(string stage, IEnumerable<UrlDir.UrlConfig> patches)
{
logger.Info(stage + " pass");
Activity = "ModuleManager " + stage;
foreach (UrlDir.UrlConfig mod in patches)
{
try
{
string name = mod.type.RemoveWS();
Command cmd = CommandParser.Parse(name, out string tmp);
if (cmd == Command.Insert)
{
logger.Warning("Warning - Encountered insert node that should not exist at this stage: " + mod.SafeUrl());
continue;
}
else if (cmd != Command.Edit && cmd != Command.Copy && cmd != Command.Delete)
{
logger.Warning("Invalid command encountered on a patch: " + mod.SafeUrl());
continue;
}
string upperName = name.ToUpper();
PatchContext context = new PatchContext(mod, databaseRoot, logger, progress);
char[] sep = { '[', ']' };
string condition = "";
if (upperName.Contains(":HAS["))
{
int start = upperName.IndexOf(":HAS[");
condition = name.Substring(start + 5, name.LastIndexOf(']') - start - 5);
name = name.Substring(0, start);
}
string[] splits = name.Split(sep, 3);
string[] patterns = splits.Length > 1 ? splits[1].Split(',', '|') : null;
string type = splits[0].Substring(1);
foreach (UrlDir.UrlFile file in allConfigFiles)
{
if (cmd == Command.Edit)
{
foreach (UrlDir.UrlConfig url in file.configs)
{
if (!IsMatch(url, type, patterns, condition)) continue;
bool loop = mod.config.HasNode("MM_PATCH_LOOP");
if (loop) logger.Info("Looping on " + mod.SafeUrl() + " to " + url.SafeUrl());
do
{
progress.ApplyingUpdate(url, mod);
url.config = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context);
} while (loop && IsMatch(url, type, patterns, condition));
if (loop) url.config.RemoveNodes("MM_PATCH_LOOP");
}
}
else if (cmd == Command.Copy)
{
// Avoid checking the new configs we are creating
int count = file.configs.Count;
for (int i = 0; i < count; i++)
{
UrlDir.UrlConfig url = file.configs[i];
if (!IsMatch(url, type, patterns, condition)) continue;
ConfigNode clone = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context);
if (url.config.HasValue("name") && url.config.GetValue("name") == clone.GetValue("name"))
{
progress.Error(mod, $"Error - when applying copy {mod.SafeUrl()} to {url.SafeUrl()} - the copy needs to have a different name than the parent (use @name = xxx)");
}
else
{
progress.ApplyingCopy(url, mod);
file.AddConfig(clone);
}
}
}
else if (cmd == Command.Delete)
{
int i = 0;
while (i < file.configs.Count)
{
UrlDir.UrlConfig url = file.configs[i];
if (IsMatch(url, type, patterns, condition))
{
progress.ApplyingDelete(url, mod);
file.configs.RemoveAt(i);
}
else
{
i++;
}
}
}
else
{
throw new NotImplementedException("This code should not be reachable");
}
}
progress.PatchApplied();
}
catch (Exception e)
{
progress.Exception(mod, "Exception while processing node : " + mod.SafeUrl(), e);
try
{
logger.Error("Processed node was\n" + mod.PrettyPrint());
}
catch (Exception ex2)
{
logger.Exception("Exception while attempting to print a node", ex2);
}
}
}
}
private static bool IsMatch(UrlDir.UrlConfig url, string type, string[] namePatterns, string constraints)
{
if (url.type != type) return false;
if (namePatterns != null)
{
if (url.name == url.type) return false;
bool match = false;
foreach (string pattern in namePatterns)
{
if (MMPatchLoader.WildcardMatch(url.name, pattern))
{
match = true;
break;
}
}
if (!match) return false;
}
return MMPatchLoader.CheckConstraints(url.config, constraints);
}
}
}