Skip to content

Commit 44d307b

Browse files
committed
Added custom config.
1 parent 57aca4f commit 44d307b

File tree

4 files changed

+212
-10
lines changed

4 files changed

+212
-10
lines changed

ReClass.NET/ReClass.NET.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@
324324
<Compile Include="UI\CustomToolStripProfessionalRenderer.cs" />
325325
<Compile Include="Util\CircularBuffer.cs" />
326326
<Compile Include="Util\CommandLineArgs.cs" />
327+
<Compile Include="Util\CustomConfig.cs" />
327328
<Compile Include="Util\Extension.BinaryReaderWriter.cs" />
328329
<Compile Include="Util\Extension.DataGridView.cs" />
329330
<Compile Include="Util\Extension.Dictionary.cs" />

ReClass.NET/Settings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Drawing;
3+
using ReClassNET.Util;
34

45
namespace ReClassNET
56
{
@@ -108,7 +109,7 @@ public class Settings
108109

109110
public string TypeFunctionPtr { get; set; } = "void*";
110111

111-
public Dictionary<string, string> CustomData { get; } = new Dictionary<string, string>();
112+
public CustomConfig CustomData { get; } = new CustomConfig();
112113

113114
public Settings Clone() => MemberwiseClone() as Settings;
114115
}

ReClass.NET/Util/CustomConfig.cs

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Xml.Linq;
5+
6+
namespace ReClassNET.Util
7+
{
8+
/// <summary>
9+
/// A class which stores custom config items from plugins.
10+
/// The id of an item should consist of "a-zA-z0-9.,;_-+".
11+
/// The naming convention is "Plugin.[ConfigGroup.]ConfigItem".
12+
/// </summary>
13+
public class CustomConfig
14+
{
15+
private readonly Dictionary<string, string> data = new Dictionary<string, string>();
16+
17+
internal XElement Serialize(string name)
18+
{
19+
return XElementSerializer.ToXml(name, data);
20+
}
21+
22+
internal void Deserialize(XElement element)
23+
{
24+
foreach (var kv in XElementSerializer.ToDictionary(element))
25+
{
26+
data[kv.Key] = kv.Value;
27+
}
28+
}
29+
30+
/// <summary>
31+
/// Sets a configuration item.
32+
/// </summary>
33+
/// <param name="id">The id of the item.</param>
34+
/// <param name="value">
35+
/// The value of the item.
36+
/// If the value is null the item gets removed.
37+
/// </param>
38+
public void SetString(string id, string value)
39+
{
40+
if (id == null)
41+
{
42+
throw new ArgumentNullException(nameof(id));
43+
}
44+
if (id.Length == 0)
45+
{
46+
throw new ArgumentException();
47+
}
48+
49+
if (value == null)
50+
{
51+
data.Remove(id);
52+
}
53+
else
54+
{
55+
data[id] = value;
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Sets a configuration item.
61+
/// </summary>
62+
/// <param name="id">The id of the item.</param>
63+
/// <param name="value">The value of the item.</param>
64+
public void SetBool(string id, bool value)
65+
{
66+
SetString(id, Convert.ToString(value));
67+
}
68+
69+
/// <summary>
70+
/// Sets a configuration item.
71+
/// </summary>
72+
/// <param name="id">The id of the item.</param>
73+
/// <param name="value">The value of the item.</param>
74+
public void SetLong(string id, long value)
75+
{
76+
SetString(id, value.ToString(NumberFormatInfo.InvariantInfo));
77+
}
78+
79+
/// <summary>
80+
/// Sets a configuration item.
81+
/// </summary>
82+
/// <param name="id">The id of the item.</param>
83+
/// <param name="value">The value of the item.</param>
84+
public void SetULong(string id, ulong value)
85+
{
86+
SetString(id, value.ToString(NumberFormatInfo.InvariantInfo));
87+
}
88+
89+
public void SetXElement(string id, XElement value)
90+
{
91+
SetString(id, value.ToString());
92+
}
93+
94+
/// <summary>
95+
/// Gets the value of the config item.
96+
/// </summary>
97+
/// <param name="id">The id of the item.</param>
98+
/// <returns>The value of the config item or null if the id does not exists.</returns>
99+
public string GetString(string id)
100+
{
101+
return GetString(id, null);
102+
}
103+
104+
/// <summary>
105+
/// Gets the value of the config item.
106+
/// </summary>
107+
/// <param name="id">The id of the item.</param>
108+
/// <param name="def">The default value if the id does not exists.</param>
109+
/// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns>
110+
public string GetString(string id, string def)
111+
{
112+
if (id == null)
113+
{
114+
throw new ArgumentNullException(nameof(id));
115+
}
116+
if (id.Length == 0)
117+
{
118+
throw new ArgumentException();
119+
}
120+
121+
if (data.TryGetValue(id, out var value))
122+
{
123+
return value;
124+
}
125+
126+
return def;
127+
}
128+
129+
/// <summary>
130+
/// Gets the value of the config item.
131+
/// </summary>
132+
/// <param name="id">The id of the item.</param>
133+
/// <param name="def">The default value if the id does not exists.</param>
134+
/// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns>
135+
public bool GetBool(string id, bool def)
136+
{
137+
var value = GetString(id, null);
138+
if (string.IsNullOrEmpty(value))
139+
{
140+
return def;
141+
}
142+
143+
return Convert.ToBoolean(value);
144+
}
145+
146+
/// <summary>
147+
/// Gets the value of the config item.
148+
/// </summary>
149+
/// <param name="id">The id of the item.</param>
150+
/// <param name="def">The default value if the id does not exists.</param>
151+
/// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns>
152+
public long GetLong(string id, long def)
153+
{
154+
var str = GetString(id, null);
155+
if (string.IsNullOrEmpty(str))
156+
{
157+
return def;
158+
}
159+
160+
if (long.TryParse(str, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var value))
161+
{
162+
return value;
163+
}
164+
165+
return def;
166+
}
167+
168+
/// <summary>
169+
/// Gets the value of the config item.
170+
/// </summary>
171+
/// <param name="id">The id of the item.</param>
172+
/// <param name="def">The default value if the id does not exists.</param>
173+
/// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns>
174+
public ulong GetULong(string id, ulong def)
175+
{
176+
var str = GetString(id, null);
177+
if (string.IsNullOrEmpty(str))
178+
{
179+
return def;
180+
}
181+
182+
if (ulong.TryParse(str, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var value))
183+
{
184+
return value;
185+
}
186+
187+
return def;
188+
}
189+
190+
/// <summary>
191+
/// Gets the value of the config item.
192+
/// </summary>
193+
/// <param name="id">The id of the item.</param>
194+
/// <param name="def">The default value if the id does not exists.</param>
195+
/// <returns>The value of the config item or <paramref name="def"/> if the id does not exists.</returns>
196+
public XElement GetXElement(string id, XElement def)
197+
{
198+
var str = GetString(id, null);
199+
if (string.IsNullOrEmpty(str))
200+
{
201+
return def;
202+
}
203+
204+
return XElement.Parse(str);
205+
}
206+
}
207+
}

ReClass.NET/Util/SettingsSerializer.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ public static Settings Load()
101101
var customData = root?.Element(XmlCustomDataElement);
102102
if (customData != null)
103103
{
104-
foreach (var kv in XElementSerializer.ToDictionary(customData))
105-
{
106-
settings.CustomData[kv.Key] = kv.Value;
107-
}
104+
settings.CustomData.Deserialize(customData);
108105
}
109106
}
110107
}
@@ -116,10 +113,6 @@ public static Settings Load()
116113
return settings;
117114
}
118115

119-
120-
121-
122-
123116
#endregion
124117

125118
#region Write Settings
@@ -202,7 +195,7 @@ public static void Save(Settings settings)
202195
XElementSerializer.ToXml(nameof(settings.TypeUTF32TextPtr), settings.TypeUTF32TextPtr),
203196
XElementSerializer.ToXml(nameof(settings.TypeFunctionPtr), settings.TypeFunctionPtr)
204197
),
205-
XElementSerializer.ToXml(XmlCustomDataElement, settings.CustomData)
198+
settings.CustomData.Serialize(XmlCustomDataElement)
206199
)
207200
);
208201

0 commit comments

Comments
 (0)