diff options
Diffstat (limited to 'OpenSim/Framework/ConfigurationMember.cs')
-rw-r--r-- | OpenSim/Framework/ConfigurationMember.cs | 440 |
1 files changed, 440 insertions, 0 deletions
diff --git a/OpenSim/Framework/ConfigurationMember.cs b/OpenSim/Framework/ConfigurationMember.cs new file mode 100644 index 0000000..b68896c --- /dev/null +++ b/OpenSim/Framework/ConfigurationMember.cs | |||
@@ -0,0 +1,440 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | using System; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Globalization; | ||
32 | using System.Net; | ||
33 | using System.Reflection; | ||
34 | using libsecondlife; | ||
35 | using OpenSim.Framework.Console; | ||
36 | |||
37 | namespace OpenSim.Framework | ||
38 | { | ||
39 | public class ConfigurationMember | ||
40 | { | ||
41 | public delegate bool ConfigurationOptionResult(string configuration_key, object configuration_result); | ||
42 | |||
43 | public delegate void ConfigurationOptionsLoad(); | ||
44 | |||
45 | private List<ConfigurationOption> configurationOptions = new List<ConfigurationOption>(); | ||
46 | private string configurationFilename = ""; | ||
47 | private string configurationDescription = ""; | ||
48 | |||
49 | private ConfigurationOptionsLoad loadFunction; | ||
50 | private ConfigurationOptionResult resultFunction; | ||
51 | |||
52 | private IGenericConfig configurationPlugin = null; | ||
53 | |||
54 | /// <summary> | ||
55 | /// This is the default configuration DLL loaded | ||
56 | /// </summary> | ||
57 | private string configurationPluginFilename = "OpenSim.Framework.Configuration.XML.dll"; | ||
58 | |||
59 | public ConfigurationMember(string configuration_filename, string configuration_description, | ||
60 | ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function) | ||
61 | { | ||
62 | configurationFilename = configuration_filename; | ||
63 | configurationDescription = configuration_description; | ||
64 | loadFunction = load_function; | ||
65 | resultFunction = result_function; | ||
66 | } | ||
67 | |||
68 | public void setConfigurationFilename(string filename) | ||
69 | { | ||
70 | configurationFilename = filename; | ||
71 | } | ||
72 | |||
73 | public void setConfigurationDescription(string desc) | ||
74 | { | ||
75 | configurationDescription = desc; | ||
76 | } | ||
77 | |||
78 | public void setConfigurationResultFunction(ConfigurationOptionResult result) | ||
79 | { | ||
80 | resultFunction = result; | ||
81 | } | ||
82 | |||
83 | public void forceConfigurationPluginLibrary(string dll_filename) | ||
84 | { | ||
85 | configurationPluginFilename = dll_filename; | ||
86 | } | ||
87 | |||
88 | public void addConfigurationOption(string configuration_key, | ||
89 | ConfigurationOption.ConfigurationTypes configuration_type, | ||
90 | string configuration_question, string configuration_default, | ||
91 | bool use_default_no_prompt) | ||
92 | { | ||
93 | ConfigurationOption configOption = new ConfigurationOption(); | ||
94 | configOption.configurationKey = configuration_key; | ||
95 | configOption.configurationQuestion = configuration_question; | ||
96 | configOption.configurationDefault = configuration_default; | ||
97 | configOption.configurationType = configuration_type; | ||
98 | configOption.configurationUseDefaultNoPrompt = use_default_no_prompt; | ||
99 | |||
100 | if ((configuration_key != "" && configuration_question != "") || | ||
101 | (configuration_key != "" && use_default_no_prompt)) | ||
102 | { | ||
103 | if (!configurationOptions.Contains(configOption)) | ||
104 | { | ||
105 | configurationOptions.Add(configOption); | ||
106 | } | ||
107 | } | ||
108 | else | ||
109 | { | ||
110 | MainLog.Instance.Notice( | ||
111 | "Required fields for adding a configuration option is invalid. Will not add this option (" + | ||
112 | configuration_key + ")"); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | public void performConfigurationRetrieve() | ||
117 | { | ||
118 | configurationPlugin = LoadConfigDll(configurationPluginFilename); | ||
119 | configurationOptions.Clear(); | ||
120 | if (loadFunction == null) | ||
121 | { | ||
122 | MainLog.Instance.Error("Load Function for '" + configurationDescription + | ||
123 | "' is null. Refusing to run configuration."); | ||
124 | return; | ||
125 | } | ||
126 | |||
127 | if (resultFunction == null) | ||
128 | { | ||
129 | MainLog.Instance.Error("Result Function for '" + configurationDescription + | ||
130 | "' is null. Refusing to run configuration."); | ||
131 | return; | ||
132 | } | ||
133 | |||
134 | MainLog.Instance.Verbose("Calling Configuration Load Function..."); | ||
135 | loadFunction(); | ||
136 | |||
137 | if (configurationOptions.Count <= 0) | ||
138 | { | ||
139 | MainLog.Instance.Error("No configuration options were specified for '" + configurationOptions + | ||
140 | "'. Refusing to continue configuration."); | ||
141 | return; | ||
142 | } | ||
143 | |||
144 | bool useFile = true; | ||
145 | if (configurationPlugin == null) | ||
146 | { | ||
147 | MainLog.Instance.Error("Configuration Plugin NOT LOADED!"); | ||
148 | return; | ||
149 | } | ||
150 | |||
151 | if (configurationFilename.Trim() != "") | ||
152 | { | ||
153 | configurationPlugin.SetFileName(configurationFilename); | ||
154 | configurationPlugin.LoadData(); | ||
155 | useFile = true; | ||
156 | } | ||
157 | else | ||
158 | { | ||
159 | MainLog.Instance.Notice("XML Configuration Filename is not valid; will not save to the file."); | ||
160 | useFile = false; | ||
161 | } | ||
162 | |||
163 | foreach (ConfigurationOption configOption in configurationOptions) | ||
164 | { | ||
165 | bool convertSuccess = false; | ||
166 | object return_result = null; | ||
167 | string errorMessage = ""; | ||
168 | bool ignoreNextFromConfig = false; | ||
169 | while (convertSuccess == false) | ||
170 | { | ||
171 | string console_result = ""; | ||
172 | string attribute = null; | ||
173 | if (useFile) | ||
174 | { | ||
175 | if (!ignoreNextFromConfig) | ||
176 | { | ||
177 | attribute = configurationPlugin.GetAttribute(configOption.configurationKey); | ||
178 | } | ||
179 | else | ||
180 | { | ||
181 | ignoreNextFromConfig = false; | ||
182 | } | ||
183 | } | ||
184 | |||
185 | if (attribute == null) | ||
186 | { | ||
187 | if (configOption.configurationUseDefaultNoPrompt) | ||
188 | { | ||
189 | console_result = configOption.configurationDefault; | ||
190 | } | ||
191 | else | ||
192 | { | ||
193 | if (configurationDescription.Trim() != "") | ||
194 | { | ||
195 | console_result = | ||
196 | MainLog.Instance.CmdPrompt( | ||
197 | configurationDescription + ": " + configOption.configurationQuestion, | ||
198 | configOption.configurationDefault); | ||
199 | } | ||
200 | else | ||
201 | { | ||
202 | console_result = | ||
203 | MainLog.Instance.CmdPrompt(configOption.configurationQuestion, | ||
204 | configOption.configurationDefault); | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | else | ||
209 | { | ||
210 | console_result = attribute; | ||
211 | } | ||
212 | |||
213 | switch (configOption.configurationType) | ||
214 | { | ||
215 | case ConfigurationOption.ConfigurationTypes.TYPE_STRING: | ||
216 | return_result = console_result; | ||
217 | convertSuccess = true; | ||
218 | break; | ||
219 | case ConfigurationOption.ConfigurationTypes.TYPE_STRING_NOT_EMPTY: | ||
220 | if (console_result.Length > 0) | ||
221 | { | ||
222 | return_result = console_result; | ||
223 | convertSuccess = true; | ||
224 | } | ||
225 | errorMessage = "a string that is not empty"; | ||
226 | break; | ||
227 | case ConfigurationOption.ConfigurationTypes.TYPE_BOOLEAN: | ||
228 | bool boolResult; | ||
229 | if (Boolean.TryParse(console_result, out boolResult)) | ||
230 | { | ||
231 | convertSuccess = true; | ||
232 | return_result = boolResult; | ||
233 | } | ||
234 | errorMessage = "'true' or 'false' (Boolean)"; | ||
235 | break; | ||
236 | case ConfigurationOption.ConfigurationTypes.TYPE_BYTE: | ||
237 | byte byteResult; | ||
238 | if (Byte.TryParse(console_result, out byteResult)) | ||
239 | { | ||
240 | convertSuccess = true; | ||
241 | return_result = byteResult; | ||
242 | } | ||
243 | errorMessage = "a byte (Byte)"; | ||
244 | break; | ||
245 | case ConfigurationOption.ConfigurationTypes.TYPE_CHARACTER: | ||
246 | char charResult; | ||
247 | if (Char.TryParse(console_result, out charResult)) | ||
248 | { | ||
249 | convertSuccess = true; | ||
250 | return_result = charResult; | ||
251 | } | ||
252 | errorMessage = "a character (Char)"; | ||
253 | break; | ||
254 | case ConfigurationOption.ConfigurationTypes.TYPE_INT16: | ||
255 | short shortResult; | ||
256 | if (Int16.TryParse(console_result, out shortResult)) | ||
257 | { | ||
258 | convertSuccess = true; | ||
259 | return_result = shortResult; | ||
260 | } | ||
261 | errorMessage = "a signed 32 bit integer (short)"; | ||
262 | break; | ||
263 | case ConfigurationOption.ConfigurationTypes.TYPE_INT32: | ||
264 | int intResult; | ||
265 | if (Int32.TryParse(console_result, out intResult)) | ||
266 | { | ||
267 | convertSuccess = true; | ||
268 | return_result = intResult; | ||
269 | } | ||
270 | errorMessage = "a signed 32 bit integer (int)"; | ||
271 | break; | ||
272 | case ConfigurationOption.ConfigurationTypes.TYPE_INT64: | ||
273 | long longResult; | ||
274 | if (Int64.TryParse(console_result, out longResult)) | ||
275 | { | ||
276 | convertSuccess = true; | ||
277 | return_result = longResult; | ||
278 | } | ||
279 | errorMessage = "a signed 32 bit integer (long)"; | ||
280 | break; | ||
281 | case ConfigurationOption.ConfigurationTypes.TYPE_IP_ADDRESS: | ||
282 | IPAddress ipAddressResult; | ||
283 | if (IPAddress.TryParse(console_result, out ipAddressResult)) | ||
284 | { | ||
285 | convertSuccess = true; | ||
286 | return_result = ipAddressResult; | ||
287 | } | ||
288 | errorMessage = "an IP Address (IPAddress)"; | ||
289 | break; | ||
290 | case ConfigurationOption.ConfigurationTypes.TYPE_LLUUID: | ||
291 | LLUUID uuidResult; | ||
292 | if (LLUUID.TryParse(console_result, out uuidResult)) | ||
293 | { | ||
294 | convertSuccess = true; | ||
295 | return_result = uuidResult; | ||
296 | } | ||
297 | errorMessage = "a UUID (LLUUID)"; | ||
298 | break; | ||
299 | case ConfigurationOption.ConfigurationTypes.TYPE_LLVECTOR3: | ||
300 | LLVector3 vectorResult; | ||
301 | if (LLVector3.TryParse(console_result, out vectorResult)) | ||
302 | { | ||
303 | convertSuccess = true; | ||
304 | return_result = vectorResult; | ||
305 | } | ||
306 | errorMessage = "a vector (LLVector3)"; | ||
307 | break; | ||
308 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT16: | ||
309 | ushort ushortResult; | ||
310 | if (UInt16.TryParse(console_result, out ushortResult)) | ||
311 | { | ||
312 | convertSuccess = true; | ||
313 | return_result = ushortResult; | ||
314 | } | ||
315 | errorMessage = "an unsigned 16 bit integer (ushort)"; | ||
316 | break; | ||
317 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT32: | ||
318 | uint uintResult; | ||
319 | if (UInt32.TryParse(console_result, out uintResult)) | ||
320 | { | ||
321 | convertSuccess = true; | ||
322 | return_result = uintResult; | ||
323 | } | ||
324 | errorMessage = "an unsigned 32 bit integer (uint)"; | ||
325 | break; | ||
326 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT64: | ||
327 | ulong ulongResult; | ||
328 | if (UInt64.TryParse(console_result, out ulongResult)) | ||
329 | { | ||
330 | convertSuccess = true; | ||
331 | return_result = ulongResult; | ||
332 | } | ||
333 | errorMessage = "an unsigned 64 bit integer (ulong)"; | ||
334 | break; | ||
335 | case ConfigurationOption.ConfigurationTypes.TYPE_FLOAT: | ||
336 | float floatResult; | ||
337 | if ( | ||
338 | float.TryParse(console_result, NumberStyles.AllowDecimalPoint, Culture.NumberFormatInfo, | ||
339 | out floatResult)) | ||
340 | { | ||
341 | convertSuccess = true; | ||
342 | return_result = floatResult; | ||
343 | } | ||
344 | errorMessage = "a single-precision floating point number (float)"; | ||
345 | break; | ||
346 | case ConfigurationOption.ConfigurationTypes.TYPE_DOUBLE: | ||
347 | double doubleResult; | ||
348 | if ( | ||
349 | Double.TryParse(console_result, NumberStyles.AllowDecimalPoint, Culture.NumberFormatInfo, | ||
350 | out doubleResult)) | ||
351 | { | ||
352 | convertSuccess = true; | ||
353 | return_result = doubleResult; | ||
354 | } | ||
355 | errorMessage = "an double-precision floating point number (double)"; | ||
356 | break; | ||
357 | } | ||
358 | |||
359 | if (convertSuccess) | ||
360 | { | ||
361 | if (useFile) | ||
362 | { | ||
363 | configurationPlugin.SetAttribute(configOption.configurationKey, console_result); | ||
364 | } | ||
365 | |||
366 | |||
367 | if (!resultFunction(configOption.configurationKey, return_result)) | ||
368 | { | ||
369 | MainLog.Instance.Notice( | ||
370 | "The handler for the last configuration option denied that input, please try again."); | ||
371 | convertSuccess = false; | ||
372 | ignoreNextFromConfig = true; | ||
373 | } | ||
374 | } | ||
375 | else | ||
376 | { | ||
377 | if (configOption.configurationUseDefaultNoPrompt) | ||
378 | { | ||
379 | MainLog.Instance.Error("CONFIG", | ||
380 | string.Format( | ||
381 | "[{3}]:[{1}] is not valid default for parameter [{0}].\nThe configuration result must be parsable to {2}.\n", | ||
382 | configOption.configurationKey, console_result, errorMessage, | ||
383 | configurationFilename)); | ||
384 | convertSuccess = true; | ||
385 | } | ||
386 | else | ||
387 | { | ||
388 | MainLog.Instance.Warn("CONFIG", | ||
389 | string.Format( | ||
390 | "[{3}]:[{1}] is not a valid value [{0}].\nThe configuration result must be parsable to {2}.\n", | ||
391 | configOption.configurationKey, console_result, errorMessage, | ||
392 | configurationFilename)); | ||
393 | ignoreNextFromConfig = true; | ||
394 | } | ||
395 | } | ||
396 | } | ||
397 | } | ||
398 | |||
399 | if (useFile) | ||
400 | { | ||
401 | configurationPlugin.Commit(); | ||
402 | configurationPlugin.Close(); | ||
403 | } | ||
404 | } | ||
405 | |||
406 | private IGenericConfig LoadConfigDll(string dllName) | ||
407 | { | ||
408 | Assembly pluginAssembly = Assembly.LoadFrom(dllName); | ||
409 | IGenericConfig plug = null; | ||
410 | |||
411 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
412 | { | ||
413 | if (pluginType.IsPublic) | ||
414 | { | ||
415 | if (!pluginType.IsAbstract) | ||
416 | { | ||
417 | Type typeInterface = pluginType.GetInterface("IGenericConfig", true); | ||
418 | |||
419 | if (typeInterface != null) | ||
420 | { | ||
421 | plug = | ||
422 | (IGenericConfig) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); | ||
423 | } | ||
424 | } | ||
425 | } | ||
426 | } | ||
427 | |||
428 | pluginAssembly = null; | ||
429 | return plug; | ||
430 | } | ||
431 | |||
432 | public void forceSetConfigurationOption(string configuration_key, string configuration_value) | ||
433 | { | ||
434 | configurationPlugin.LoadData(); | ||
435 | configurationPlugin.SetAttribute(configuration_key, configuration_value); | ||
436 | configurationPlugin.Commit(); | ||
437 | configurationPlugin.Close(); | ||
438 | } | ||
439 | } | ||
440 | } \ No newline at end of file | ||