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
88 lines (75 loc) · 3.17 KB
/
Copy pathNodeMatcher.cs
File metadata and controls
88 lines (75 loc) · 3.17 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
using System;
using ModuleManager.Extensions;
namespace ModuleManager
{
public interface INodeMatcher
{
bool IsMatch(ConfigNode node);
}
public class NodeMatcher : INodeMatcher
{
private static readonly char[] sep = { '[', ']' };
private string type;
private string[] namePatterns = null;
private string constraints = "";
public NodeMatcher(string nodeName)
{
if (nodeName == null) throw new ArgumentNullException(nameof(nodeName));
if (nodeName == "") throw new ArgumentException("can't be empty", nameof(nodeName));
if (!nodeName.IsBracketBalanced()) throw new FormatException("node name is not bracket balanced: " + nodeName);
string name = nodeName;
int indexOfHas = name.IndexOf(":HAS[", StringComparison.InvariantCultureIgnoreCase);
if (indexOfHas == 0)
{
throw new FormatException("node name cannot begin with :HAS : " + nodeName);
}
else if (indexOfHas > 0)
{
int closingBracketIndex = name.LastIndexOf(']', name.Length - 1, name.Length - indexOfHas - 1);
// Really shouldn't happen if we're bracket balanced but just in case
if (closingBracketIndex == -1) throw new FormatException("Malformed :HAS[] block detected: " + nodeName);
constraints = name.Substring(indexOfHas + 5, closingBracketIndex - indexOfHas - 5);
name = name.Substring(0, indexOfHas);
}
int bracketIndex = name.IndexOf('[');
if (bracketIndex == 0)
{
throw new FormatException("node name cannot begin with a bracket: " + nodeName);
}
else if (bracketIndex > 0)
{
int closingBracketIndex = name.LastIndexOf(']', name.Length - 1, name.Length - bracketIndex - 1);
// Really shouldn't happen if we're bracket balanced but just in case
if (closingBracketIndex == -1) throw new FormatException("Malformed brackets detected: " + nodeName);
string patterns = name.Substring(bracketIndex + 1, closingBracketIndex - bracketIndex - 1);
namePatterns = patterns.Split(',', '|');
type = name.Substring(0, bracketIndex);
}
else
{
type = name;
namePatterns = null;
}
}
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);
}
}
}