diff options
Diffstat (limited to 'OpenSim/Framework/General/Configuration/ConfigurationMember.cs')
-rw-r--r-- | OpenSim/Framework/General/Configuration/ConfigurationMember.cs | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/OpenSim/Framework/General/Configuration/ConfigurationMember.cs b/OpenSim/Framework/General/Configuration/ConfigurationMember.cs new file mode 100644 index 0000000..2d945b5 --- /dev/null +++ b/OpenSim/Framework/General/Configuration/ConfigurationMember.cs | |||
@@ -0,0 +1,311 @@ | |||
1 | using System; | ||
2 | using System.Collections; | ||
3 | using System.Collections.Generic; | ||
4 | using System.Text; | ||
5 | using System.Net; | ||
6 | |||
7 | using libsecondlife; | ||
8 | |||
9 | using OpenSim.Framework.Console; | ||
10 | |||
11 | namespace OpenSim.Framework.Configuration | ||
12 | { | ||
13 | public class ConfigurationMember | ||
14 | { | ||
15 | public delegate void ConfigurationOptionResult(string configuration_key, object configuration_result); | ||
16 | public delegate void ConfigurationOptionsLoad(); | ||
17 | |||
18 | private List<ConfigurationOption> configurationOptions = new List<ConfigurationOption>(); | ||
19 | private string configurationFilename = ""; | ||
20 | private string configurationDescription = ""; | ||
21 | |||
22 | private ConfigurationOptionsLoad loadFunction; | ||
23 | private ConfigurationOptionResult resultFunction; | ||
24 | |||
25 | public ConfigurationMember(string configuration_filename, string configuration_description, ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function) | ||
26 | { | ||
27 | this.configurationFilename = configuration_filename; | ||
28 | this.configurationDescription = configuration_description; | ||
29 | this.loadFunction = load_function; | ||
30 | this.resultFunction = result_function; | ||
31 | } | ||
32 | |||
33 | public void setConfigurationFilename(string filename) | ||
34 | { | ||
35 | configurationFilename = filename; | ||
36 | } | ||
37 | public void setConfigurationDescription(string desc) | ||
38 | { | ||
39 | configurationDescription = desc; | ||
40 | } | ||
41 | |||
42 | public void setConfigurationResultFunction(ConfigurationOptionResult result) | ||
43 | { | ||
44 | resultFunction = result; | ||
45 | } | ||
46 | |||
47 | public void addConfigurationOption(string configuration_key, ConfigurationOption.ConfigurationTypes configuration_type, string configuration_question, string configuration_default) | ||
48 | { | ||
49 | ConfigurationOption configOption = new ConfigurationOption(); | ||
50 | configOption.configurationKey = configuration_key; | ||
51 | configOption.configurationQuestion = configuration_question; | ||
52 | configOption.configurationDefault = configuration_default; | ||
53 | configOption.configurationType = configuration_type; | ||
54 | |||
55 | if (configuration_key != "" && configuration_question != "" && configuration_type != null) | ||
56 | { | ||
57 | if (!configurationOptions.Contains(configOption)) | ||
58 | { | ||
59 | configurationOptions.Add(configOption); | ||
60 | } | ||
61 | } | ||
62 | else | ||
63 | { | ||
64 | MainLog.Instance.Notice("Required fields for adding a configuration option is invalid. Will not add this option (" + configuration_key + ")"); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | public void performConfigurationRetrieve() | ||
69 | { | ||
70 | configurationOptions.Clear(); | ||
71 | if(loadFunction == null) | ||
72 | { | ||
73 | MainLog.Instance.Error("Load Function for '" + this.configurationDescription + "' is null. Refusing to run configuration."); | ||
74 | return; | ||
75 | } | ||
76 | |||
77 | if(resultFunction == null) | ||
78 | { | ||
79 | MainLog.Instance.Error("Result Function for '" + this.configurationDescription + "' is null. Refusing to run configuration."); | ||
80 | return; | ||
81 | } | ||
82 | |||
83 | MainLog.Instance.Verbose("Calling Configuration Load Function..."); | ||
84 | this.loadFunction(); | ||
85 | |||
86 | if(configurationOptions.Count <= 0) | ||
87 | { | ||
88 | MainLog.Instance.Error("No configuration options were specified for '" + this.configurationOptions + "'. Refusing to continue configuration."); | ||
89 | return; | ||
90 | } | ||
91 | |||
92 | bool useFile = true; | ||
93 | XmlConfiguration xmlConfig = null; | ||
94 | if (configurationFilename.Trim() != "") | ||
95 | { | ||
96 | xmlConfig = new XmlConfiguration(configurationFilename); | ||
97 | |||
98 | } | ||
99 | |||
100 | if(xmlConfig != null) | ||
101 | { | ||
102 | xmlConfig.LoadData(); | ||
103 | useFile = true; | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | MainLog.Instance.Notice("XML Configuration Filename is not valid; will not save to the file."); | ||
108 | useFile = false; | ||
109 | } | ||
110 | |||
111 | foreach (ConfigurationOption configOption in configurationOptions) | ||
112 | { | ||
113 | bool convertSuccess = false; | ||
114 | object return_result = null; | ||
115 | string errorMessage = ""; | ||
116 | bool ignoreNextFromConfig = false; | ||
117 | while (convertSuccess == false) | ||
118 | { | ||
119 | |||
120 | string attribute = null; | ||
121 | if (useFile) | ||
122 | { | ||
123 | if (!ignoreNextFromConfig) | ||
124 | { | ||
125 | attribute = xmlConfig.GetAttribute(configOption.configurationKey); | ||
126 | } | ||
127 | else | ||
128 | { | ||
129 | ignoreNextFromConfig = false; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | string console_result = ""; | ||
134 | if (attribute == null) | ||
135 | { | ||
136 | if (configurationDescription.Trim() != "") | ||
137 | { | ||
138 | console_result = MainLog.Instance.CmdPrompt(configurationDescription + ": " + configOption.configurationQuestion, configOption.configurationDefault); | ||
139 | } | ||
140 | else | ||
141 | { | ||
142 | console_result = MainLog.Instance.CmdPrompt(configOption.configurationQuestion, configOption.configurationDefault); | ||
143 | } | ||
144 | } | ||
145 | else | ||
146 | { | ||
147 | console_result = attribute; | ||
148 | } | ||
149 | |||
150 | switch (configOption.configurationType) | ||
151 | { | ||
152 | case ConfigurationOption.ConfigurationTypes.TYPE_STRING: | ||
153 | return_result = console_result; | ||
154 | convertSuccess = true; | ||
155 | break; | ||
156 | case ConfigurationOption.ConfigurationTypes.TYPE_BOOLEAN: | ||
157 | bool boolResult; | ||
158 | if (Boolean.TryParse(console_result, out boolResult)) | ||
159 | { | ||
160 | convertSuccess = true; | ||
161 | return_result = boolResult; | ||
162 | } | ||
163 | errorMessage = "'true' or 'false' (Boolean)"; | ||
164 | break; | ||
165 | case ConfigurationOption.ConfigurationTypes.TYPE_BYTE: | ||
166 | byte byteResult; | ||
167 | if (Byte.TryParse(console_result, out byteResult)) | ||
168 | { | ||
169 | convertSuccess = true; | ||
170 | return_result = byteResult; | ||
171 | } | ||
172 | errorMessage = "a byte (Byte)"; | ||
173 | break; | ||
174 | case ConfigurationOption.ConfigurationTypes.TYPE_CHARACTER: | ||
175 | char charResult; | ||
176 | if (Char.TryParse(console_result, out charResult)) | ||
177 | { | ||
178 | convertSuccess = true; | ||
179 | return_result = charResult; | ||
180 | } | ||
181 | errorMessage = "a character (Char)"; | ||
182 | break; | ||
183 | case ConfigurationOption.ConfigurationTypes.TYPE_INT16: | ||
184 | short shortResult; | ||
185 | if (Int16.TryParse(console_result, out shortResult)) | ||
186 | { | ||
187 | convertSuccess = true; | ||
188 | return_result = shortResult; | ||
189 | } | ||
190 | errorMessage = "a signed 32 bit integer (short)"; | ||
191 | break; | ||
192 | case ConfigurationOption.ConfigurationTypes.TYPE_INT32: | ||
193 | int intResult; | ||
194 | if (Int32.TryParse(console_result, out intResult)) | ||
195 | { | ||
196 | convertSuccess = true; | ||
197 | return_result = intResult; | ||
198 | |||
199 | } | ||
200 | errorMessage = "a signed 32 bit integer (int)"; | ||
201 | break; | ||
202 | case ConfigurationOption.ConfigurationTypes.TYPE_INT64: | ||
203 | long longResult; | ||
204 | if (Int64.TryParse(console_result, out longResult)) | ||
205 | { | ||
206 | convertSuccess = true; | ||
207 | return_result = longResult; | ||
208 | } | ||
209 | errorMessage = "a signed 32 bit integer (long)"; | ||
210 | break; | ||
211 | case ConfigurationOption.ConfigurationTypes.TYPE_IP_ADDRESS: | ||
212 | IPAddress ipAddressResult; | ||
213 | if (IPAddress.TryParse(console_result, out ipAddressResult)) | ||
214 | { | ||
215 | convertSuccess = true; | ||
216 | return_result = ipAddressResult; | ||
217 | } | ||
218 | errorMessage = "an IP Address (IPAddress)"; | ||
219 | break; | ||
220 | case ConfigurationOption.ConfigurationTypes.TYPE_LLUUID: | ||
221 | LLUUID uuidResult; | ||
222 | if (LLUUID.TryParse(console_result, out uuidResult)) | ||
223 | { | ||
224 | convertSuccess = true; | ||
225 | return_result = uuidResult; | ||
226 | } | ||
227 | errorMessage = "a UUID (LLUUID)"; | ||
228 | break; | ||
229 | case ConfigurationOption.ConfigurationTypes.TYPE_LLVECTOR3: | ||
230 | LLVector3 vectorResult; | ||
231 | if (LLVector3.TryParse(console_result, out vectorResult)) | ||
232 | { | ||
233 | convertSuccess = true; | ||
234 | return_result = vectorResult; | ||
235 | } | ||
236 | errorMessage = "a vector (LLVector3)"; | ||
237 | break; | ||
238 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT16: | ||
239 | ushort ushortResult; | ||
240 | if (UInt16.TryParse(console_result, out ushortResult)) | ||
241 | { | ||
242 | convertSuccess = true; | ||
243 | return_result = ushortResult; | ||
244 | } | ||
245 | errorMessage = "an unsigned 16 bit integer (ushort)"; | ||
246 | break; | ||
247 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT32: | ||
248 | uint uintResult; | ||
249 | if (UInt32.TryParse(console_result, out uintResult)) | ||
250 | { | ||
251 | convertSuccess = true; | ||
252 | return_result = uintResult; | ||
253 | |||
254 | } | ||
255 | errorMessage = "an unsigned 32 bit integer (uint)"; | ||
256 | break; | ||
257 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT64: | ||
258 | ulong ulongResult; | ||
259 | if (UInt64.TryParse(console_result, out ulongResult)) | ||
260 | { | ||
261 | convertSuccess = true; | ||
262 | return_result = ulongResult; | ||
263 | } | ||
264 | errorMessage = "an unsigned 64 bit integer (ulong)"; | ||
265 | break; | ||
266 | case ConfigurationOption.ConfigurationTypes.TYPE_FLOAT: | ||
267 | float floatResult; | ||
268 | if (float.TryParse(console_result, out floatResult)) | ||
269 | { | ||
270 | convertSuccess = true; | ||
271 | return_result = floatResult; | ||
272 | } | ||
273 | errorMessage = "a single-precision floating point number (float)"; | ||
274 | break; | ||
275 | case ConfigurationOption.ConfigurationTypes.TYPE_DOUBLE: | ||
276 | double doubleResult; | ||
277 | if (Double.TryParse(console_result, out doubleResult)) | ||
278 | { | ||
279 | convertSuccess = true; | ||
280 | return_result = doubleResult; | ||
281 | } | ||
282 | errorMessage = "an double-precision floating point number (double)"; | ||
283 | break; | ||
284 | } | ||
285 | |||
286 | if (convertSuccess) | ||
287 | { | ||
288 | if (useFile) | ||
289 | { | ||
290 | xmlConfig.SetAttribute(configOption.configurationKey, console_result); | ||
291 | } | ||
292 | |||
293 | |||
294 | this.resultFunction(configOption.configurationKey, return_result); | ||
295 | } | ||
296 | else | ||
297 | { | ||
298 | MainLog.Instance.Warn("Incorrect result given, the configuration option must be " + errorMessage + ". Prompting for same option..."); | ||
299 | ignoreNextFromConfig = true; | ||
300 | } | ||
301 | } | ||
302 | } | ||
303 | |||
304 | if(useFile) | ||
305 | { | ||
306 | xmlConfig.Commit(); | ||
307 | xmlConfig.Close(); | ||
308 | } | ||
309 | } | ||
310 | } | ||
311 | } | ||