forked from sarbian/ModuleManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeMatcher.cs
More file actions
58 lines (48 loc) · 1.87 KB
/
Copy pathNodeMatcher.cs
File metadata and controls
58 lines (48 loc) · 1.87 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
using System;
using ModuleManager.Extensions;
namespace ModuleManager
{
public interface INodeMatcher
{
bool IsMatch(ConfigNode node);
}
public class NodeMatcher : INodeMatcher
{
private readonly string type;
private readonly string[] namePatterns = null;
private readonly string constraints = "";
public NodeMatcher(string type, string name, string constraints)
{
if (type == string.Empty) throw new ArgumentException("can't be empty", nameof(type));
this.type = type ?? throw new ArgumentNullException(nameof(type));
if (name == string.Empty) throw new ArgumentException("can't be empty (null allowed)", nameof(name));
if (constraints == string.Empty) throw new ArgumentException("can't be empty (null allowed)", nameof(constraints));
if (name != null) namePatterns = name.Split(',', '|');
if (constraints != null)
{
if (!constraints.IsBracketBalanced()) throw new ArgumentException("is not bracket balanced: " + constraints, nameof(constraints));
this.constraints = constraints;
}
}
public bool IsMatch(ConfigNode node)
{
if (node.name != type) return false;
if (namePatterns != null)
{
string name = node.GetValue("name");
if (name == null) return false;
bool match = false;
foreach (string pattern in namePatterns)
{
if (MMPatchLoader.WildcardMatch(name, pattern))
{
match = true;
break;
}
}
if (!match) return false;
}
return MMPatchLoader.CheckConstraints(node, constraints);
}
}
}