-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginConfig.cs
269 lines (245 loc) · 8.11 KB
/
PluginConfig.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;
namespace GeniePlugin
{
public class PluginConfig
{
private XmlDocument _configFile;
private GeniePlugin.Interfaces.IHost _host;
private GeniePlugin.Interfaces.IPlugin _plugin;
private string _configName;
private string _fileName;
private string _filePath;
private string _fullPath;
public PluginConfig(GeniePlugin.Interfaces.IHost Host, GeniePlugin.Interfaces.IPlugin PlugIn)
{
_host = Host;
_plugin = PlugIn;
_configFile = new XmlDocument();
_configName = _plugin.Name.Replace(" ", "_");
_fileName = _configName + ".xml";
_filePath = Application.StartupPath;
if (_filePath != "\\")
_filePath += "\\";
_filePath += "Plugins\\";
_fullPath = _filePath + _fileName;
}
public void SaveConfig()
{
if (_configFile == null)
return;
try
{
_configFile.Save(_fullPath);
}
catch (Exception ex)
{
_host.EchoText(_plugin.Name + ".SaveConfig: " + ex.Message + "\n");
}
}
public void OpenConfig()
{
if (_configFile == null)
_configFile = new XmlDocument();
try
{
_configFile.Load(_fullPath);
}
catch (Exception ex)
{
if (Directory.Exists(_filePath))
{
//Any number of problems, but we'll just build a new file.
_configFile = new XmlDocument();
_configFile.AppendChild(_configFile.CreateXmlDeclaration("1.0", null, null));
_configFile.AppendChild(_configFile.CreateElement(_configName));
}
else
_host.EchoText(_plugin.Name + ".OpenConfig: " + _filePath + " is not a valid path.\n");
}
} //OpenConfig
#region GetSectionList
public Dictionary<string, string> GetSectionList()
{
return GetSectionList("");
}
public Dictionary<string, string> GetSectionList(string sectionName)
{
Dictionary<string, string> retVal = new Dictionary<string, string>();
XPathNavigator nav;
XPathNodeIterator iter;
if (_configFile == null)
return null;
if (sectionName != string.Empty)
sectionName += "/";
else
sectionName = "/" + _configName + "/";
try
{
nav = ((IXPathNavigable)_configFile).CreateNavigator();
iter = nav.Select(sectionName + "*");
while (iter.MoveNext())
{
if (iter.Current.IsNode)
{
retVal.Add(iter.Current.Name, sectionName + iter.Current.Name);
}
}
}
catch (Exception ex)
{
_host.EchoText(_plugin.Name + ".GetSectionList: " + ex.Message + "\n");
return null;
}
return retVal;
}
#endregion
public XmlNode GetSection(string sectionName)
{
return GetKey("/" + _configName + "/" + sectionName);
}
#region GetKey
public XmlNode GetKey(string sectionName, string keyName)
{
return GetKey(GetSection(sectionName), keyName);
}
public XmlNode GetKey(string keyName)
{
if (_configFile == null)
return null;
return _configFile.SelectSingleNode(keyName);
}
public XmlNode GetKey(XmlNode node, string keyName)
{
return node.SelectSingleNode(keyName);
}
#endregion
#region GetKeyValue
public string GetKeyValue(string sectionName, string keyName, string defValue)
{
XmlNode node;
node = GetKey(sectionName, keyName);
if (node != null)
return GetKeyValue(node, defValue);
else
return defValue;
}
public string GetKeyValue(XmlNode Key, string defValue)
{
try
{
return Key.InnerText;
}
catch
{
return defValue;
}
}
public string GetKeyValue(XmlNode Section, string keyName, string defValue)
{
try
{
return Section.SelectSingleNode(keyName).InnerText;
}
catch
{
return defValue;
}
}
public string GetKeyValue(string Key, string defValue)
{
try
{
return _configFile.SelectSingleNode(Key).InnerText;
}
catch
{
return defValue;
}
}
#endregion
#region SetKeyValue
public void SetKeyValue(XmlNode Section, string key, string Value)
{
XmlNode node;
XmlElement element;
try
{
node = Section.SelectSingleNode(key);
node.InnerText = Value;
}
catch (Exception ex)
{
if (_configFile != null)
{
element = _configFile.CreateElement(key);
Section.AppendChild(element);
element.AppendChild(_configFile.CreateTextNode(Value));
}
}
}
public void SetKeyValue(string Key, string Value)
{
XmlNode node;
XmlElement element;
try
{
node = _configFile.SelectSingleNode(Key);
node.InnerText = Value;
}
catch (Exception ex)
{
if (_configFile != null)
{
element = _configFile.CreateElement(Key);
element.AppendChild(_configFile.CreateTextNode(Value));
}
}
}
#endregion
public void SetAttribute(XmlNode node, string attribute, string value)
{
XmlAttribute nodeAtt;
nodeAtt = _configFile.CreateAttribute(attribute);
nodeAtt.Value = value;
((XmlElement)node).SetAttributeNode(nodeAtt);
}
public string GetAttribute(XmlNode node, string attribute)
{
return node.Attributes[attribute].Value;
}
public XmlNode AddSection(string sectionName)
{
XmlNode node;
node = _configFile.CreateElement(sectionName);
_configFile.SelectSingleNode(_configName).AppendChild(node);
return node;
}
public XmlNode GetComplexKey(XmlNode Section, string key, string name)
{
return Section.SelectSingleNode(key + "[@name='" + name + "']");
}
public XmlNode AddComplexKey(XmlNode Section, string key, string name, Dictionary<string, string> attributes)
{
XmlNode node;
XmlAttribute attr;
node = _configFile.CreateElement(key);
attr = _configFile.CreateAttribute("name");
attr.Value = name;
((XmlElement)node).SetAttributeNode(attr);
foreach (string s in attributes.Keys)
{
attr = _configFile.CreateAttribute(s);
attr.Value = attributes[s];
((XmlElement)node).SetAttributeNode(attr);
}
Section.AppendChild(node);
return node;
}
}
}