/
EditPatch.cs
58 lines (50 loc) · 2.57 KB
/
EditPatch.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
using System;
using System.Collections.Generic;
using NodeStack = ModuleManager.Collections.ImmutableStack<ConfigNode>;
using ModuleManager.Extensions;
using ModuleManager.Logging;
using ModuleManager.Patches.PassSpecifiers;
using ModuleManager.Progress;
namespace ModuleManager.Patches
{
public class EditPatch : IPatch
{
private readonly bool loop;
public UrlDir.UrlConfig UrlConfig { get; }
public INodeMatcher NodeMatcher { get; }
public IPassSpecifier PassSpecifier { get; }
public EditPatch(UrlDir.UrlConfig urlConfig, INodeMatcher nodeMatcher, IPassSpecifier passSpecifier)
{
UrlConfig = urlConfig ?? throw new ArgumentNullException(nameof(urlConfig));
NodeMatcher = nodeMatcher ?? throw new ArgumentNullException(nameof(nodeMatcher));
PassSpecifier = passSpecifier ?? throw new ArgumentNullException(nameof(passSpecifier));
loop = urlConfig.config.HasNode("MM_PATCH_LOOP");
}
public void Apply(LinkedList<IProtoUrlConfig> databaseConfigs, IPatchProgress progress, IBasicLogger logger)
{
if (databaseConfigs == null) throw new ArgumentNullException(nameof(databaseConfigs));
if (progress == null) throw new ArgumentNullException(nameof(progress));
if (logger == null) throw new ArgumentNullException(nameof(logger));
PatchContext context = new PatchContext(UrlConfig, databaseConfigs, logger, progress);
for (LinkedListNode<IProtoUrlConfig> listNode = databaseConfigs.First; listNode != null; listNode = listNode.Next)
{
IProtoUrlConfig protoConfig = listNode.Value;
try
{
if (!NodeMatcher.IsMatch(protoConfig.Node)) continue;
if (loop) logger.Info($"Looping on {UrlConfig.SafeUrl()} to {protoConfig.FullUrl}");
do
{
progress.ApplyingUpdate(protoConfig, UrlConfig);
listNode.Value = protoConfig = new ProtoUrlConfig(protoConfig.UrlFile, MMPatchLoader.ModifyNode(new NodeStack(protoConfig.Node), UrlConfig.config, context));
} while (loop && NodeMatcher.IsMatch(protoConfig.Node));
if (loop) protoConfig.Node.RemoveNodes("MM_PATCH_LOOP");
}
catch (Exception ex)
{
progress.Exception(UrlConfig, $"Exception while applying update {UrlConfig.SafeUrl()} to {protoConfig.FullUrl}", ex);
}
}
}
}
}