Skip to content

Commit 69f66c5

Browse files
committed
More performance for nodes that have repeated names. Also fixed the replace nodes and naming conventions
1 parent 1f6e184 commit 69f66c5

File tree

12 files changed

+155
-222
lines changed

12 files changed

+155
-222
lines changed

LunaConfigNode/CfgNode/CfgNodeValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
namespace LunaConfigNode
3+
namespace LunaConfigNode.CfgNode
44
{
55
public class CfgNodeValue<T1, T2>
66
{

LunaConfigNode/CfgNode/ConfigNode.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public partial class ConfigNode
88
public string Name { get; set; } = string.Empty;
99
public ConfigNode Parent { get; set; }
1010

11-
public MixedCollection<string, string> Values { get; } = new MixedCollection<string, string>();
12-
public MixedCollection<string, ConfigNode> Nodes { get; } = new MixedCollection<string, ConfigNode>();
11+
public MixedCollection<string, string> Values { get; private set; } = new MixedCollection<string, string>();
12+
public MixedCollection<string, ConfigNode> Nodes { get; private set; } = new MixedCollection<string, ConfigNode>();
1313

1414
public int Depth => Parent?.Depth + 1 ?? 0;
1515

@@ -26,14 +26,16 @@ public ConfigNode(string contents)
2626
{
2727
if (line.Contains(CfgNodeConstants.ValueSeparator))
2828
{
29-
currentNode.CreateValue(line.Substring(0, line.IndexOf(CfgNodeConstants.ValueSeparator, StringComparison.Ordinal)).Trim(),
30-
line.Substring(line.LastIndexOf(CfgNodeConstants.ValueSeparator, StringComparison.Ordinal) + CfgNodeConstants.ValueSeparator.Length).Trim());
29+
currentNode.CreateValue(new CfgNodeValue<string, string>(line.Substring(0, line.IndexOf(CfgNodeConstants.ValueSeparator, StringComparison.Ordinal)).Trim(),
30+
line.Substring(line.LastIndexOf(CfgNodeConstants.ValueSeparator, StringComparison.Ordinal) + CfgNodeConstants.ValueSeparator.Length).Trim()));
3131

3232
continue;
3333
}
3434
if (line.TrimEnd().Equals(CfgNodeConstants.OpenNodeSymbol))
3535
{
36-
currentNode = currentNode.CreateNode(previousLine);
36+
var newNode = new ConfigNode(previousLine, this);
37+
currentNode.AddNode(newNode);
38+
currentNode = newNode;
3739
continue;
3840
}
3941
if (line.TrimEnd().Equals(CfgNodeConstants.CloseNodeSymbol))
@@ -58,9 +60,9 @@ public bool IsEmpty()
5860
{
5961
return Values.IsEmpty() && Nodes.IsEmpty();
6062
}
61-
62-
#region Base overrides
6363

64+
#region Base overrides
65+
6466
public override string ToString()
6567
{
6668
return CfgNodeWriter.WriteConfigNode(this);

LunaConfigNode/CfgNode/ConfigNodeAdd.cs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
{
33
public partial class ConfigNode
44
{
5-
/// <summary>
6-
/// Create the specified name and value
7-
/// </summary>
8-
public void CreateValue(string name, string value)
9-
{
10-
Values.Create(name, value);
11-
}
12-
135
/// <summary>
146
/// Adds the specified name-value
157
/// </summary>
@@ -18,55 +10,13 @@ public void CreateValue(CfgNodeValue<string,string> value)
1810
Values.Add(value);
1911
}
2012

21-
/// <summary>
22-
/// Create or updates the given value
23-
/// </summary>
24-
public void CreateOrUpdateValue(string name, string value)
25-
{
26-
if (Values.Exists(name))
27-
{
28-
Values.Update(name, value);
29-
}
30-
else
31-
{
32-
Values.Create(name, value);
33-
}
34-
}
35-
36-
/// <summary>
37-
/// Creates a new empty config node with the given name
38-
/// </summary>
39-
public ConfigNode CreateNode(string name)
40-
{
41-
var newConfigNode = new ConfigNode(name, this);
42-
43-
Nodes.Create(name, newConfigNode);
44-
45-
return newConfigNode;
46-
}
47-
4813
/// <summary>
4914
/// Adds the given node as a child
5015
/// </summary>
5116
public void AddNode(ConfigNode value)
5217
{
5318
value.Parent = this;
54-
Nodes.Create(value.Name, value);
55-
}
56-
57-
/// <summary>
58-
/// Adds or updates the matching node names
59-
/// </summary>
60-
public void CreateOrReplaceNode(ConfigNode value)
61-
{
62-
if (Nodes.Exists(value.Name))
63-
{
64-
Nodes.Update(value.Name, value);
65-
}
66-
else
67-
{
68-
Nodes.Create(value.Name, value);
69-
}
19+
Nodes.Add(new CfgNodeValue<string, ConfigNode>(value.Name, value));
7020
}
7121
}
7222
}

LunaConfigNode/CfgNode/ConfigNodeRemove.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void RemoveNode(ConfigNode configNode)
1919
}
2020

2121
/// <summary>
22-
/// Removes the nodes that have the given name
22+
/// Removes ALL the nodes that have the given name
2323
/// </summary>
2424
public void RemoveNode(string name)
2525
{

LunaConfigNode/CfgNode/ConfigNodeUpdater.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@ public void UpdateValue(string name, string value)
1010
Values.Update(name, value);
1111
}
1212

13-
/// <summary>
14-
/// Replaces ALL the nodes with the matching node name if they exist for the one given as parameter
15-
/// </summary>
16-
public void ReplaceNode(string name, ConfigNode updatedNode)
17-
{
18-
Nodes.Update(name, updatedNode);
19-
}
20-
2113
/// <summary>
2214
/// Replaces the nodes with the matching node name and configNode if it exists for the one given as parameter
2315
/// </summary>
2416
public void ReplaceNode(ConfigNode oldNode, ConfigNode updatedNode)
2517
{
18+
updatedNode.Parent = this;
2619
Nodes.Replace(new CfgNodeValue<string, ConfigNode>(oldNode.Name, oldNode), new CfgNodeValue<string, ConfigNode>(updatedNode.Name, updatedNode));
2720
}
2821
}

0 commit comments

Comments
 (0)