Skip to content

Commit 1f6e184

Browse files
committed
Added several helper methods
1 parent 3434637 commit 1f6e184

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

LunaConfigNode/CfgNode/ConfigNodeRemove.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ public void RemoveValue(string name)
1515
/// </summary>
1616
public void RemoveNode(ConfigNode configNode)
1717
{
18-
Nodes.Remove(configNode.Name, configNode);
18+
Nodes.Remove(new CfgNodeValue<string, ConfigNode>(configNode.Name, configNode));
19+
}
20+
21+
/// <summary>
22+
/// Removes the nodes that have the given name
23+
/// </summary>
24+
public void RemoveNode(string name)
25+
{
26+
Nodes.Remove(name);
1927
}
2028
}
2129
}

LunaConfigNode/CfgNode/ConfigNodeUpdater.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ public void UpdateValue(string name, string value)
1111
}
1212

1313
/// <summary>
14-
/// Replaces all the nodes with the matching node name if they exist for the one given as parameter
14+
/// Replaces ALL the nodes with the matching node name if they exist for the one given as parameter
1515
/// </summary>
16-
public void ReplaceNode(ConfigNode updatedNode)
16+
public void ReplaceNode(string name, ConfigNode updatedNode)
1717
{
18-
Nodes.Update(updatedNode.Name, updatedNode);
18+
Nodes.Update(name, updatedNode);
19+
}
20+
21+
/// <summary>
22+
/// Replaces the nodes with the matching node name and configNode if it exists for the one given as parameter
23+
/// </summary>
24+
public void ReplaceNode(ConfigNode oldNode, ConfigNode updatedNode)
25+
{
26+
Nodes.Replace(new CfgNodeValue<string, ConfigNode>(oldNode.Name, oldNode), new CfgNodeValue<string, ConfigNode>(updatedNode.Name, updatedNode));
1927
}
2028
}
2129
}

LunaConfigNode/MixedCollection.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ public List<TV> GetAllValues()
128128
}
129129
}
130130

131+
/// <summary>
132+
/// Sets a new value for all the elements that matches in KEY
133+
/// </summary>
131134
public void Update(TK key, TV value)
132135
{
133136
lock (_lock)
@@ -146,6 +149,30 @@ public void Update(TK key, TV value)
146149
}
147150
}
148151

152+
/// <summary>
153+
/// Replaces all the elements that match in KEY and VALUE for the new one
154+
/// </summary>
155+
public void Replace(CfgNodeValue<TK, TV> oldValue, CfgNodeValue<TK, TV> newValue)
156+
{
157+
lock (_lock)
158+
{
159+
if (Dictionary.ContainsKey(oldValue.Key))
160+
{
161+
Dictionary[oldValue.Key] = newValue;
162+
}
163+
else
164+
{
165+
for (var i = 0; i < List.Count; i++)
166+
{
167+
if (List[i].Equals(oldValue))
168+
{
169+
List[i] = newValue;
170+
}
171+
}
172+
}
173+
}
174+
}
175+
149176
public void Remove(TK key)
150177
{
151178
lock (_lock)

LunaConfigNodeTest/UpdateTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void TestUpdateNode()
3030
{
3131
var configNode = new ConfigNode(Resources.Simple);
3232
var newNode = new ConfigNode("Node2", null);
33-
configNode.ReplaceNode(newNode);
33+
configNode.ReplaceNode("Node2", newNode);
3434

3535
Assert.AreEqual(newNode, configNode.GetNodes("Node2")[0].Value);
3636
}
@@ -40,7 +40,7 @@ public void TestUpdateSeveralNode()
4040
{
4141
var configNode = new ConfigNode(Resources.Simple);
4242
var newNode = new ConfigNode("RepeatedNode3", configNode);
43-
configNode.ReplaceNode(newNode);
43+
configNode.ReplaceNode("RepeatedNode3", newNode);
4444

4545
Assert.AreEqual(newNode, configNode.GetNodes("RepeatedNode3")[0].Value);
4646
Assert.AreEqual(newNode, configNode.GetNodes("RepeatedNode3")[1].Value);

0 commit comments

Comments
 (0)