forked from sarbian/ModuleManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigNodeExtensions.cs
More file actions
107 lines (97 loc) · 3.7 KB
/
Copy pathConfigNodeExtensions.cs
File metadata and controls
107 lines (97 loc) · 3.7 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
using System;
using System.Text;
namespace ModuleManager.Extensions
{
public static class ConfigNodeExtensions
{
public static void ShallowCopyFrom(this ConfigNode toNode, ConfigNode fromeNode)
{
toNode.ClearData();
foreach (ConfigNode.Value value in fromeNode.values)
toNode.values.Add(value);
foreach (ConfigNode node in fromeNode.nodes)
toNode.nodes.Add(node);
}
// KSP implementation of ConfigNode.CreateCopy breaks with badly formed nodes (nodes with a blank name)
public static ConfigNode DeepCopy(this ConfigNode from)
{
ConfigNode to = new ConfigNode(from.name);
foreach (ConfigNode.Value value in from.values)
to.AddValueSafe(value.name, value.value);
foreach (ConfigNode node in from.nodes)
{
ConfigNode newNode = DeepCopy(node);
to.nodes.Add(newNode);
}
return to;
}
public static void PrettyPrint(this ConfigNode node, ref StringBuilder sb, string indent)
{
if (sb == null) throw new ArgumentNullException(nameof(sb));
if (indent == null) indent = string.Empty;
if (node == null)
{
sb.Append(indent + "<null node>\n");
return;
}
sb.AppendFormat("{0}{1}\n{2}{{\n", indent, node.name ?? "<null>", indent);
string newindent = indent + " ";
if (node.values == null)
{
sb.AppendFormat("{0}<null value list>\n", newindent);
}
else
{
foreach (ConfigNode.Value value in node.values)
{
if (value == null)
sb.AppendFormat("{0}<null value>\n", newindent);
else
sb.AppendFormat("{0}{1} = {2}\n", newindent, value.name ?? "<null>", value.value ?? "<null>");
}
}
if (node.nodes == null)
{
sb.AppendFormat("{0}<null node list>\n", newindent);
}
else
{
foreach (ConfigNode subnode in node.nodes)
{
subnode.PrettyPrint(ref sb, newindent);
}
}
sb.AppendFormat("{0}}}\n", indent);
}
public static void AddValueSafe(this ConfigNode node, string name, string value)
{
node.values.Add(new ConfigNode.Value(name, value));
}
public static void EscapeValuesRecursive(this ConfigNode theNode)
{
foreach (ConfigNode subNode in theNode.nodes)
{
subNode.EscapeValuesRecursive();
}
foreach (ConfigNode.Value value in theNode.values)
{
value.value = value.value.Replace("\n", "\\n");
value.value = value.value.Replace("\r", "\\r");
value.value = value.value.Replace("\t", "\\t");
}
}
public static void UnescapeValuesRecursive(this ConfigNode theNode)
{
foreach (ConfigNode subNode in theNode.nodes)
{
subNode.UnescapeValuesRecursive();
}
foreach (ConfigNode.Value value in theNode.values)
{
value.value = value.value.Replace("\\n", "\n");
value.value = value.value.Replace("\\r", "\r");
value.value = value.value.Replace("\\t", "\t");
}
}
}
}