-
Notifications
You must be signed in to change notification settings - Fork 96
/
NeedsChecker.cs
192 lines (174 loc) · 7.15 KB
/
NeedsChecker.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
using System;
using System.Collections.Generic;
using System.Linq;
using ModuleManager.Extensions;
using ModuleManager.Logging;
using ModuleManager.Progress;
using NodeStack = ModuleManager.Collections.ImmutableStack<ConfigNode>;
namespace ModuleManager
{
public static class NeedsChecker
{
public static void CheckNeeds(UrlDir gameDatabaseRoot, IEnumerable<string> mods, IPatchProgress progress, IBasicLogger logger)
{
foreach (UrlDir.UrlConfig mod in gameDatabaseRoot.AllConfigs.ToArray())
{
UrlDir.UrlConfig currentMod = mod;
try
{
if (mod.config.name == null)
{
progress.Error(currentMod, "Error - Node in file " + currentMod.parent.url + " subnode: " + currentMod.type +
" has config.name == null");
}
UrlDir.UrlConfig newMod;
if (currentMod.type.IndexOf(":NEEDS[", StringComparison.OrdinalIgnoreCase) >= 0)
{
string type = currentMod.type;
if (CheckNeeds(ref type, mods))
{
ConfigNode copy = new ConfigNode(type);
copy.ShallowCopyFrom(currentMod.config);
int index = mod.parent.configs.IndexOf(currentMod);
newMod = new UrlDir.UrlConfig(currentMod.parent, copy);
mod.parent.configs[index] = newMod;
}
else
{
progress.NeedsUnsatisfiedRoot(currentMod);
mod.parent.configs.Remove(currentMod);
continue;
}
}
else
{
newMod = currentMod;
}
// Recursively check the contents
PatchContext context = new PatchContext(newMod, gameDatabaseRoot, logger, progress);
CheckNeeds(new NodeStack(newMod.config), context, mods);
}
catch (Exception ex)
{
try
{
mod.parent.configs.Remove(currentMod);
}
catch(Exception ex2)
{
logger.Exception("Exception while attempting to ensure config removed" ,ex2);
}
try
{
progress.Exception(mod, "Exception while checking needs on root node :\n" + mod.PrettyPrint(), ex);
}
catch (Exception ex2)
{
progress.Exception("Exception while attempting to log an exception", ex2);
}
}
}
}
private static void CheckNeeds(NodeStack stack, PatchContext context, IEnumerable<string> mods)
{
ConfigNode original = stack.value;
for (int i = 0; i < original.values.Count; ++i)
{
ConfigNode.Value val = original.values[i];
string valname = val.name;
try
{
if (CheckNeeds(ref valname, mods))
{
val.name = valname;
}
else
{
original.values.Remove(val);
i--;
context.progress.NeedsUnsatisfiedValue(context.patchUrl, stack, val.name);
}
}
catch (ArgumentOutOfRangeException e)
{
context.progress.Exception("ArgumentOutOfRangeException in CheckNeeds for value \"" + val.name + "\"", e);
throw;
}
catch (Exception e)
{
context.progress.Exception("General Exception in CheckNeeds for value \"" + val.name + "\"", e);
throw;
}
}
for (int i = 0; i < original.nodes.Count; ++i)
{
ConfigNode node = original.nodes[i];
string nodeName = node.name;
if (nodeName == null)
{
context.progress.Error(context.patchUrl, "Error - Node in file " + context.patchUrl.SafeUrl() + " subnode: " + stack.GetPath() +
" has config.name == null");
}
try
{
if (CheckNeeds(ref nodeName, mods))
{
node.name = nodeName;
CheckNeeds(stack.Push(node), context, mods);
}
else
{
original.nodes.Remove(node);
i--;
context.progress.NeedsUnsatisfiedNode(context.patchUrl, stack.Push(node));
}
}
catch (ArgumentOutOfRangeException e)
{
context.progress.Exception("ArgumentOutOfRangeException in CheckNeeds for node \"" + node.name + "\"", e);
throw;
}
catch (Exception e)
{
context.progress.Exception("General Exception " + e.GetType().Name + " for node \"" + node.name + "\"", e);
throw;
}
}
}
/// <summary>
/// Returns true if needs are satisfied.
/// </summary>
private static bool CheckNeeds(ref string name, IEnumerable<string> mods)
{
if (name == null)
return true;
int idxStart = name.IndexOf(":NEEDS[", StringComparison.OrdinalIgnoreCase);
if (idxStart < 0)
return true;
int idxEnd = name.IndexOf(']', idxStart + 7);
string needsString = name.Substring(idxStart + 7, idxEnd - idxStart - 7).ToUpper();
name = name.Substring(0, idxStart) + name.Substring(idxEnd + 1);
// Check to see if all the needed dependencies are present.
foreach (string andDependencies in needsString.Split(',', '&'))
{
bool orMatch = false;
foreach (string orDependency in andDependencies.Split('|'))
{
if (orDependency.Length == 0)
continue;
bool not = orDependency[0] == '!';
string toFind = not ? orDependency.Substring(1) : orDependency;
bool found = mods.Contains(toFind, StringComparer.OrdinalIgnoreCase);
if (not == !found)
{
orMatch = true;
break;
}
}
if (!orMatch)
return false;
}
return true;
}
}
}