diff options
Diffstat (limited to 'OpenSim/Framework/General/Configuration/ConfigurationMember.cs')
-rw-r--r-- | OpenSim/Framework/General/Configuration/ConfigurationMember.cs | 415 |
1 files changed, 0 insertions, 415 deletions
diff --git a/OpenSim/Framework/General/Configuration/ConfigurationMember.cs b/OpenSim/Framework/General/Configuration/ConfigurationMember.cs deleted file mode 100644 index af9a0f6..0000000 --- a/OpenSim/Framework/General/Configuration/ConfigurationMember.cs +++ /dev/null | |||
@@ -1,415 +0,0 @@ | |||
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.Reflection; | ||
31 | using System.Collections; | ||
32 | using System.Collections.Generic; | ||
33 | using System.Text; | ||
34 | using System.Net; | ||
35 | |||
36 | using libsecondlife; | ||
37 | |||
38 | using OpenSim.Framework.Console; | ||
39 | using OpenSim.Framework.Configuration.Interfaces; | ||
40 | using System.Globalization; | ||
41 | |||
42 | namespace OpenSim.Framework.Configuration | ||
43 | { | ||
44 | public class ConfigurationMember | ||
45 | { | ||
46 | public delegate bool ConfigurationOptionResult(string configuration_key, object configuration_result); | ||
47 | public delegate void ConfigurationOptionsLoad(); | ||
48 | |||
49 | private List<ConfigurationOption> configurationOptions = new List<ConfigurationOption>(); | ||
50 | private string configurationFilename = ""; | ||
51 | private string configurationDescription = ""; | ||
52 | |||
53 | private ConfigurationOptionsLoad loadFunction; | ||
54 | private ConfigurationOptionResult resultFunction; | ||
55 | |||
56 | private IGenericConfig configurationPlugin = null; | ||
57 | /// <summary> | ||
58 | /// This is the default configuration DLL loaded | ||
59 | /// </summary> | ||
60 | private string configurationPluginFilename = "OpenSim.Framework.Configuration.XML.dll"; | ||
61 | public ConfigurationMember(string configuration_filename, string configuration_description, ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function) | ||
62 | { | ||
63 | this.configurationFilename = configuration_filename; | ||
64 | this.configurationDescription = configuration_description; | ||
65 | this.loadFunction = load_function; | ||
66 | this.resultFunction = result_function; | ||
67 | } | ||
68 | |||
69 | public void setConfigurationFilename(string filename) | ||
70 | { | ||
71 | configurationFilename = filename; | ||
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 | public void addConfigurationOption(string configuration_key, ConfigurationOption.ConfigurationTypes configuration_type, string configuration_question, string configuration_default, bool use_default_no_prompt) | ||
88 | { | ||
89 | ConfigurationOption configOption = new ConfigurationOption(); | ||
90 | configOption.configurationKey = configuration_key; | ||
91 | configOption.configurationQuestion = configuration_question; | ||
92 | configOption.configurationDefault = configuration_default; | ||
93 | configOption.configurationType = configuration_type; | ||
94 | configOption.configurationUseDefaultNoPrompt = use_default_no_prompt; | ||
95 | |||
96 | if ((configuration_key != "" && configuration_question != "") || (configuration_key != "" && use_default_no_prompt)) | ||
97 | { | ||
98 | if (!configurationOptions.Contains(configOption)) | ||
99 | { | ||
100 | configurationOptions.Add(configOption); | ||
101 | } | ||
102 | } | ||
103 | else | ||
104 | { | ||
105 | MainLog.Instance.Notice("Required fields for adding a configuration option is invalid. Will not add this option (" + configuration_key + ")"); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | public void performConfigurationRetrieve() | ||
110 | { | ||
111 | configurationPlugin = this.LoadConfigDll(configurationPluginFilename); | ||
112 | configurationOptions.Clear(); | ||
113 | if(loadFunction == null) | ||
114 | { | ||
115 | MainLog.Instance.Error("Load Function for '" + this.configurationDescription + "' is null. Refusing to run configuration."); | ||
116 | return; | ||
117 | } | ||
118 | |||
119 | if(resultFunction == null) | ||
120 | { | ||
121 | MainLog.Instance.Error("Result Function for '" + this.configurationDescription + "' is null. Refusing to run configuration."); | ||
122 | return; | ||
123 | } | ||
124 | |||
125 | MainLog.Instance.Verbose("Calling Configuration Load Function..."); | ||
126 | this.loadFunction(); | ||
127 | |||
128 | if(configurationOptions.Count <= 0) | ||
129 | { | ||
130 | MainLog.Instance.Error("No configuration options were specified for '" + this.configurationOptions + "'. Refusing to continue configuration."); | ||
131 | return; | ||
132 | } | ||
133 | |||
134 | bool useFile = true; | ||
135 | if (configurationPlugin == null) | ||
136 | { | ||
137 | MainLog.Instance.Error("Configuration Plugin NOT LOADED!"); | ||
138 | return; | ||
139 | } | ||
140 | |||
141 | if (configurationFilename.Trim() != "") | ||
142 | { | ||
143 | configurationPlugin.SetFileName(configurationFilename); | ||
144 | configurationPlugin.LoadData(); | ||
145 | useFile = true; | ||
146 | } | ||
147 | else | ||
148 | { | ||
149 | MainLog.Instance.Notice("XML Configuration Filename is not valid; will not save to the file."); | ||
150 | useFile = false; | ||
151 | } | ||
152 | |||
153 | foreach (ConfigurationOption configOption in configurationOptions) | ||
154 | { | ||
155 | bool convertSuccess = false; | ||
156 | object return_result = null; | ||
157 | string errorMessage = ""; | ||
158 | bool ignoreNextFromConfig = false; | ||
159 | while (convertSuccess == false) | ||
160 | { | ||
161 | |||
162 | string console_result = ""; | ||
163 | string attribute = null; | ||
164 | if (useFile) | ||
165 | { | ||
166 | if (!ignoreNextFromConfig) | ||
167 | { | ||
168 | attribute = configurationPlugin.GetAttribute(configOption.configurationKey); | ||
169 | } | ||
170 | else | ||
171 | { | ||
172 | ignoreNextFromConfig = false; | ||
173 | } | ||
174 | } | ||
175 | |||
176 | if (attribute == null) | ||
177 | { | ||
178 | if (configOption.configurationUseDefaultNoPrompt) | ||
179 | { | ||
180 | console_result = configOption.configurationDefault; | ||
181 | } | ||
182 | else | ||
183 | { | ||
184 | |||
185 | if (configurationDescription.Trim() != "") | ||
186 | { | ||
187 | console_result = MainLog.Instance.CmdPrompt(configurationDescription + ": " + configOption.configurationQuestion, configOption.configurationDefault); | ||
188 | } | ||
189 | else | ||
190 | { | ||
191 | console_result = MainLog.Instance.CmdPrompt(configOption.configurationQuestion, configOption.configurationDefault); | ||
192 | } | ||
193 | } | ||
194 | } | ||
195 | else | ||
196 | { | ||
197 | console_result = attribute; | ||
198 | } | ||
199 | |||
200 | switch (configOption.configurationType) | ||
201 | { | ||
202 | case ConfigurationOption.ConfigurationTypes.TYPE_STRING: | ||
203 | return_result = console_result; | ||
204 | convertSuccess = true; | ||
205 | break; | ||
206 | case ConfigurationOption.ConfigurationTypes.TYPE_STRING_NOT_EMPTY: | ||
207 | if (console_result.Length > 0) | ||
208 | { | ||
209 | return_result = console_result; | ||
210 | convertSuccess = true; | ||
211 | } | ||
212 | errorMessage = "a string that is not empty"; | ||
213 | break; | ||
214 | case ConfigurationOption.ConfigurationTypes.TYPE_BOOLEAN: | ||
215 | bool boolResult; | ||
216 | if (Boolean.TryParse(console_result, out boolResult)) | ||
217 | { | ||
218 | convertSuccess = true; | ||
219 | return_result = boolResult; | ||
220 | } | ||
221 | errorMessage = "'true' or 'false' (Boolean)"; | ||
222 | break; | ||
223 | case ConfigurationOption.ConfigurationTypes.TYPE_BYTE: | ||
224 | byte byteResult; | ||
225 | if (Byte.TryParse(console_result, out byteResult)) | ||
226 | { | ||
227 | convertSuccess = true; | ||
228 | return_result = byteResult; | ||
229 | } | ||
230 | errorMessage = "a byte (Byte)"; | ||
231 | break; | ||
232 | case ConfigurationOption.ConfigurationTypes.TYPE_CHARACTER: | ||
233 | char charResult; | ||
234 | if (Char.TryParse(console_result, out charResult)) | ||
235 | { | ||
236 | convertSuccess = true; | ||
237 | return_result = charResult; | ||
238 | } | ||
239 | errorMessage = "a character (Char)"; | ||
240 | break; | ||
241 | case ConfigurationOption.ConfigurationTypes.TYPE_INT16: | ||
242 | short shortResult; | ||
243 | if (Int16.TryParse(console_result, out shortResult)) | ||
244 | { | ||
245 | convertSuccess = true; | ||
246 | return_result = shortResult; | ||
247 | } | ||
248 | errorMessage = "a signed 32 bit integer (short)"; | ||
249 | break; | ||
250 | case ConfigurationOption.ConfigurationTypes.TYPE_INT32: | ||
251 | int intResult; | ||
252 | if (Int32.TryParse(console_result, out intResult)) | ||
253 | { | ||
254 | convertSuccess = true; | ||
255 | return_result = intResult; | ||
256 | |||
257 | } | ||
258 | errorMessage = "a signed 32 bit integer (int)"; | ||
259 | break; | ||
260 | case ConfigurationOption.ConfigurationTypes.TYPE_INT64: | ||
261 | long longResult; | ||
262 | if (Int64.TryParse(console_result, out longResult)) | ||
263 | { | ||
264 | convertSuccess = true; | ||
265 | return_result = longResult; | ||
266 | } | ||
267 | errorMessage = "a signed 32 bit integer (long)"; | ||
268 | break; | ||
269 | case ConfigurationOption.ConfigurationTypes.TYPE_IP_ADDRESS: | ||
270 | IPAddress ipAddressResult; | ||
271 | if (IPAddress.TryParse(console_result, out ipAddressResult)) | ||
272 | { | ||
273 | convertSuccess = true; | ||
274 | return_result = ipAddressResult; | ||
275 | } | ||
276 | errorMessage = "an IP Address (IPAddress)"; | ||
277 | break; | ||
278 | case ConfigurationOption.ConfigurationTypes.TYPE_LLUUID: | ||
279 | LLUUID uuidResult; | ||
280 | if (LLUUID.TryParse(console_result, out uuidResult)) | ||
281 | { | ||
282 | convertSuccess = true; | ||
283 | return_result = uuidResult; | ||
284 | } | ||
285 | errorMessage = "a UUID (LLUUID)"; | ||
286 | break; | ||
287 | case ConfigurationOption.ConfigurationTypes.TYPE_LLVECTOR3: | ||
288 | LLVector3 vectorResult; | ||
289 | if (LLVector3.TryParse(console_result, out vectorResult)) | ||
290 | { | ||
291 | convertSuccess = true; | ||
292 | return_result = vectorResult; | ||
293 | } | ||
294 | errorMessage = "a vector (LLVector3)"; | ||
295 | break; | ||
296 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT16: | ||
297 | ushort ushortResult; | ||
298 | if (UInt16.TryParse(console_result, out ushortResult)) | ||
299 | { | ||
300 | convertSuccess = true; | ||
301 | return_result = ushortResult; | ||
302 | } | ||
303 | errorMessage = "an unsigned 16 bit integer (ushort)"; | ||
304 | break; | ||
305 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT32: | ||
306 | uint uintResult; | ||
307 | if (UInt32.TryParse(console_result, out uintResult)) | ||
308 | { | ||
309 | convertSuccess = true; | ||
310 | return_result = uintResult; | ||
311 | |||
312 | } | ||
313 | errorMessage = "an unsigned 32 bit integer (uint)"; | ||
314 | break; | ||
315 | case ConfigurationOption.ConfigurationTypes.TYPE_UINT64: | ||
316 | ulong ulongResult; | ||
317 | if (UInt64.TryParse(console_result, out ulongResult)) | ||
318 | { | ||
319 | convertSuccess = true; | ||
320 | return_result = ulongResult; | ||
321 | } | ||
322 | errorMessage = "an unsigned 64 bit integer (ulong)"; | ||
323 | break; | ||
324 | case ConfigurationOption.ConfigurationTypes.TYPE_FLOAT: | ||
325 | float floatResult; | ||
326 | if (float.TryParse(console_result, NumberStyles.AllowDecimalPoint, Culture.NumberFormatInfo, out floatResult)) | ||
327 | { | ||
328 | convertSuccess = true; | ||
329 | return_result = floatResult; | ||
330 | } | ||
331 | errorMessage = "a single-precision floating point number (float)"; | ||
332 | break; | ||
333 | case ConfigurationOption.ConfigurationTypes.TYPE_DOUBLE: | ||
334 | double doubleResult; | ||
335 | if (Double.TryParse(console_result, NumberStyles.AllowDecimalPoint, Culture.NumberFormatInfo, out doubleResult)) | ||
336 | { | ||
337 | convertSuccess = true; | ||
338 | return_result = doubleResult; | ||
339 | } | ||
340 | errorMessage = "an double-precision floating point number (double)"; | ||
341 | break; | ||
342 | } | ||
343 | |||
344 | if (convertSuccess) | ||
345 | { | ||
346 | if (useFile) | ||
347 | { | ||
348 | configurationPlugin.SetAttribute(configOption.configurationKey, console_result); | ||
349 | } | ||
350 | |||
351 | |||
352 | if (!this.resultFunction(configOption.configurationKey, return_result)) | ||
353 | { | ||
354 | Console.MainLog.Instance.Notice("The handler for the last configuration option denied that input, please try again."); | ||
355 | convertSuccess = false; | ||
356 | ignoreNextFromConfig = true; | ||
357 | } | ||
358 | } | ||
359 | else | ||
360 | { | ||
361 | if (configOption.configurationUseDefaultNoPrompt) | ||
362 | { | ||
363 | MainLog.Instance.Error("CONFIG", string.Format("[{3}]:[{1}] is not valid default for parameter [{0}].\nThe configuration result must be parsable to {2}.\n", configOption.configurationKey, console_result, errorMessage, configurationFilename)); | ||
364 | convertSuccess = true; | ||
365 | } | ||
366 | else | ||
367 | { | ||
368 | MainLog.Instance.Warn("CONFIG", string.Format("[{3}]:[{1}] is not a valid value [{0}].\nThe configuration result must be parsable to {2}.\n", configOption.configurationKey, console_result, errorMessage, configurationFilename)); | ||
369 | ignoreNextFromConfig = true; | ||
370 | } | ||
371 | } | ||
372 | } | ||
373 | } | ||
374 | |||
375 | if(useFile) | ||
376 | { | ||
377 | configurationPlugin.Commit(); | ||
378 | configurationPlugin.Close(); | ||
379 | } | ||
380 | } | ||
381 | |||
382 | private IGenericConfig LoadConfigDll(string dllName) | ||
383 | { | ||
384 | Assembly pluginAssembly = Assembly.LoadFrom(dllName); | ||
385 | IGenericConfig plug = null; | ||
386 | |||
387 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
388 | { | ||
389 | if (pluginType.IsPublic) | ||
390 | { | ||
391 | if (!pluginType.IsAbstract) | ||
392 | { | ||
393 | Type typeInterface = pluginType.GetInterface("IGenericConfig", true); | ||
394 | |||
395 | if (typeInterface != null) | ||
396 | { | ||
397 | plug = (IGenericConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); | ||
398 | } | ||
399 | } | ||
400 | } | ||
401 | } | ||
402 | |||
403 | pluginAssembly = null; | ||
404 | return plug; | ||
405 | } | ||
406 | |||
407 | public void forceSetConfigurationOption(string configuration_key, string configuration_value) | ||
408 | { | ||
409 | this.configurationPlugin.LoadData(); | ||
410 | this.configurationPlugin.SetAttribute(configuration_key, configuration_value); | ||
411 | this.configurationPlugin.Commit(); | ||
412 | this.configurationPlugin.Close(); | ||
413 | } | ||
414 | } | ||
415 | } | ||