forked from sarbian/ModuleManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchList.cs
More file actions
138 lines (122 loc) · 5.71 KB
/
Copy pathPatchList.cs
File metadata and controls
138 lines (122 loc) · 5.71 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using ModuleManager.Patches;
using ModuleManager.Patches.PassSpecifiers;
using ModuleManager.Progress;
namespace ModuleManager
{
public class PatchList : IEnumerable<IPass>
{
private class ModPass
{
public readonly string name;
public readonly Pass beforePass;
public readonly Pass forPass;
public readonly Pass afterPass;
public readonly Pass lastPass;
public ModPass(string name)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (name == string.Empty) throw new ArgumentException("can't be blank", nameof(name));
this.name = name.ToUpperInvariant();
beforePass = new Pass($":BEFORE[{this.name}]");
forPass = new Pass($":FOR[{this.name}]");
afterPass = new Pass($":AFTER[{this.name}]");
lastPass = new Pass($":LAST[{this.name}]");
}
public void AddBeforePatch(IPatch patch) => beforePass.Add(patch ?? throw new ArgumentNullException(nameof(patch)));
public void AddForPatch(IPatch patch) => forPass.Add(patch ?? throw new ArgumentNullException(nameof(patch)));
public void AddAfterPatch(IPatch patch) => afterPass.Add(patch ?? throw new ArgumentNullException(nameof(patch)));
public void AddLastPatch(IPatch patch) => lastPass.Add(patch ?? throw new ArgumentNullException(nameof(patch)));
}
private readonly Pass insertPatches = new Pass(":INSERT (initial)");
private readonly Pass firstPatches = new Pass(":FIRST");
private readonly Pass legacyPatches = new Pass(":LEGACY (default)");
private readonly Pass finalPatches = new Pass(":FINAL");
private readonly SortedDictionary<string, ModPass> modPasses = new SortedDictionary<string, ModPass>(StringComparer.InvariantCultureIgnoreCase);
private readonly SortedDictionary<string, Pass> lastPasses = new SortedDictionary<string, Pass>(StringComparer.InvariantCultureIgnoreCase);
public PatchList(IEnumerable<string> modList, IEnumerable<IPatch> patches, IPatchProgress progress)
{
if (modList == null) throw new ArgumentNullException(nameof(modList));
if (patches == null) throw new ArgumentNullException(nameof(patches));
if (progress == null) throw new ArgumentNullException(nameof(progress));
foreach (string mod in modList)
{
modPasses.Add(mod, new ModPass(mod));
}
foreach (IPatch patch in patches)
{
if (patch.PassSpecifier is InsertPassSpecifier)
{
insertPatches.Add(patch);
}
else if (patch.PassSpecifier is FirstPassSpecifier)
{
firstPatches.Add(patch);
}
else if (patch.PassSpecifier is LegacyPassSpecifier)
{
legacyPatches.Add(patch);
}
else if (patch.PassSpecifier is BeforePassSpecifier beforePassSpecifier)
{
EnsureMod(beforePassSpecifier.mod);
modPasses[beforePassSpecifier.mod].AddBeforePatch(patch);
}
else if (patch.PassSpecifier is ForPassSpecifier forPassSpecifier)
{
EnsureMod(forPassSpecifier.mod);
modPasses[forPassSpecifier.mod].AddForPatch(patch);
}
else if (patch.PassSpecifier is AfterPassSpecifier afterPassSpecifier)
{
EnsureMod(afterPassSpecifier.mod);
modPasses[afterPassSpecifier.mod].AddAfterPatch(patch);
}
else if (patch.PassSpecifier is LastPassSpecifier lastPassSpecifier)
{
if (!lastPasses.TryGetValue(lastPassSpecifier.mod, out Pass thisPass))
{
thisPass = new Pass($":LAST[{lastPassSpecifier.mod.ToUpperInvariant()}]");
lastPasses.Add(lastPassSpecifier.mod.ToLowerInvariant(), thisPass);
}
thisPass.Add(patch);
}
else if (patch.PassSpecifier is FinalPassSpecifier)
{
finalPatches.Add(patch);
}
else
{
throw new NotImplementedException("Don't know what to do with pass specifier: " + patch.PassSpecifier.Descriptor);
}
if (patch.CountsAsPatch) progress.PatchAdded();
}
}
public IEnumerator<IPass> GetEnumerator()
{
yield return insertPatches;
yield return firstPatches;
yield return legacyPatches;
foreach (ModPass modPass in modPasses.Values)
{
yield return modPass.beforePass;
yield return modPass.forPass;
yield return modPass.afterPass;
}
foreach (Pass lastPass in lastPasses.Values)
{
yield return lastPass;
}
yield return finalPatches;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private void EnsureMod(string mod)
{
if (mod == null) throw new ArgumentNullException(nameof(mod));
if (mod == string.Empty) throw new ArgumentException("can't be empty", nameof(mod));
if (!modPasses.ContainsKey(mod)) throw new KeyNotFoundException($"Mod '{mod}' not found");
}
}
}