29 using System.Collections.Generic;
30 using System.Globalization;
32 using System.Reflection;
38 namespace OpenSim.Framework
44 public delegate
bool ConfigurationOptionResult(
string configuration_key,
object configuration_result);
46 public delegate
void ConfigurationOptionsLoad();
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
53 private string configurationDescription = String.Empty;
54 private string configurationFilename = String.Empty;
55 private XmlNode configurationFromXMLNode = null;
56 private List<ConfigurationOption> configurationOptions =
new List<ConfigurationOption>();
62 private string configurationPluginFilename =
"OpenSim.Framework.Configuration.XML.dll";
64 private ConfigurationOptionsLoad loadFunction;
65 private ConfigurationOptionResult resultFunction;
67 private bool useConsoleToPromptOnError =
true;
70 ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function,
bool use_console_to_prompt_on_error)
72 configurationFilename = configuration_filename;
73 configurationDescription = configuration_description;
74 loadFunction = load_function;
75 resultFunction = result_function;
76 useConsoleToPromptOnError = use_console_to_prompt_on_error;
80 ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function,
bool use_console_to_prompt_on_error)
82 configurationFilename = String.Empty;
83 configurationFromXMLNode = configuration_xml;
84 configurationDescription = configuration_description;
85 loadFunction = load_function;
86 resultFunction = result_function;
87 useConsoleToPromptOnError = use_console_to_prompt_on_error;
92 configurationFilename = filename;
97 configurationDescription = desc;
102 resultFunction = result;
107 configurationPluginFilename = dll_filename;
115 if (!configurationOptions.Contains(option))
117 configurationOptions.Add(option);
123 "Required fields for adding a configuration option is invalid. Will not add this option (" +
124 option.configurationKey +
")");
130 string configuration_question,
string configuration_default,
131 bool use_default_no_prompt)
134 configOption.configurationKey = configuration_key;
135 configOption.configurationQuestion = configuration_question;
136 configOption.configurationDefault = configuration_default;
137 configOption.configurationType = configuration_type;
138 configOption.configurationUseDefaultNoPrompt = use_default_no_prompt;
139 configOption.shouldIBeAsked = null;
140 checkAndAddConfigOption(configOption);
145 string configuration_question,
string configuration_default,
146 bool use_default_no_prompt,
150 configOption.configurationKey = configuration_key;
151 configOption.configurationQuestion = configuration_question;
152 configOption.configurationDefault = configuration_default;
153 configOption.configurationType = configuration_type;
154 configOption.configurationUseDefaultNoPrompt = use_default_no_prompt;
155 configOption.shouldIBeAsked = shouldIBeAskedDelegate;
156 checkAndAddConfigOption(configOption);
163 m_log.Error(
"READING CONFIGURATION COUT: " + cE.ToString());
166 configurationPlugin = LoadConfigDll(configurationPluginFilename);
167 configurationOptions.Clear();
168 if (loadFunction == null)
170 m_log.Error(
"Load Function for '" + configurationDescription +
171 "' is null. Refusing to run configuration.");
175 if (resultFunction == null)
177 m_log.Error(
"Result Function for '" + configurationDescription +
178 "' is null. Refusing to run configuration.");
185 if (configurationOptions.Count <= 0)
187 m_log.Error(
"[CONFIG]: No configuration options were specified for '" + configurationOptions +
188 "'. Refusing to continue configuration.");
193 if (configurationPlugin == null)
195 m_log.Error(
"[CONFIG]: Configuration Plugin NOT LOADED!");
199 if (configurationFilename.Trim() != String.Empty)
201 configurationPlugin.SetFileName(configurationFilename);
204 configurationPlugin.LoadData();
207 catch (XmlException e)
209 m_log.WarnFormat(
"[CONFIG] Not using {0}: {1}",
210 configurationFilename,
211 e.Message.ToString());
218 if (configurationFromXMLNode != null)
220 m_log.Info(
"Loading from XML Node, will not save to the file");
221 configurationPlugin.LoadDataFromString(configurationFromXMLNode.OuterXml);
224 m_log.Info(
"XML Configuration Filename is not valid; will not save to the file.");
230 bool convertSuccess =
false;
231 object return_result = null;
232 string errorMessage = String.Empty;
233 bool ignoreNextFromConfig =
false;
234 while (convertSuccess ==
false)
236 string console_result = String.Empty;
237 string attribute = null;
238 if (useFile || configurationFromXMLNode != null)
240 if (!ignoreNextFromConfig)
242 attribute = configurationPlugin.GetAttribute(configOption.configurationKey);
246 ignoreNextFromConfig =
false;
250 if (attribute == null)
254 console_result = configOption.configurationDefault;
262 if (configurationDescription.Trim() != String.Empty)
265 MainConsole.Instance.CmdPrompt(
266 configurationDescription +
": " + configOption.configurationQuestion,
267 configOption.configurationDefault);
272 MainConsole.Instance.CmdPrompt(configOption.configurationQuestion,
273 configOption.configurationDefault);
279 console_result = configOption.configurationDefault;
285 console_result = attribute;
290 if (console_result.StartsWith(
"$"))
291 console_result = Environment.GetEnvironmentVariable(console_result.Substring(1));
295 case ConfigurationOption.ConfigurationTypes.TYPE_STRING:
296 return_result = console_result;
297 convertSuccess =
true;
299 case ConfigurationOption.ConfigurationTypes.TYPE_STRING_NOT_EMPTY:
300 if (console_result.Length > 0)
302 return_result = console_result;
303 convertSuccess =
true;
305 errorMessage =
"a string that is not empty";
307 case ConfigurationOption.ConfigurationTypes.TYPE_BOOLEAN:
309 if (Boolean.TryParse(console_result, out boolResult))
311 convertSuccess =
true;
312 return_result = boolResult;
314 errorMessage =
"'true' or 'false' (Boolean)";
316 case ConfigurationOption.ConfigurationTypes.TYPE_BYTE:
318 if (Byte.TryParse(console_result, out byteResult))
320 convertSuccess =
true;
321 return_result = byteResult;
323 errorMessage =
"a byte (Byte)";
325 case ConfigurationOption.ConfigurationTypes.TYPE_CHARACTER:
327 if (Char.TryParse(console_result, out charResult))
329 convertSuccess =
true;
330 return_result = charResult;
332 errorMessage =
"a character (Char)";
334 case ConfigurationOption.ConfigurationTypes.TYPE_INT16:
336 if (Int16.TryParse(console_result, out shortResult))
338 convertSuccess =
true;
339 return_result = shortResult;
341 errorMessage =
"a signed 32 bit integer (short)";
343 case ConfigurationOption.ConfigurationTypes.TYPE_INT32:
345 if (Int32.TryParse(console_result, out intResult))
347 convertSuccess =
true;
348 return_result = intResult;
350 errorMessage =
"a signed 32 bit integer (int)";
352 case ConfigurationOption.ConfigurationTypes.TYPE_INT64:
354 if (Int64.TryParse(console_result, out longResult))
356 convertSuccess =
true;
357 return_result = longResult;
359 errorMessage =
"a signed 32 bit integer (long)";
361 case ConfigurationOption.ConfigurationTypes.TYPE_IP_ADDRESS:
362 IPAddress ipAddressResult;
363 if (IPAddress.TryParse(console_result, out ipAddressResult))
365 convertSuccess =
true;
366 return_result = ipAddressResult;
368 errorMessage =
"an IP Address (IPAddress)";
370 case ConfigurationOption.ConfigurationTypes.TYPE_UUID:
372 if (UUID.TryParse(console_result, out uuidResult))
374 convertSuccess =
true;
375 return_result = uuidResult;
377 errorMessage =
"a UUID (UUID)";
379 case ConfigurationOption.ConfigurationTypes.TYPE_UUID_NULL_FREE:
381 if (UUID.TryParse(console_result, out uuidResult2))
383 convertSuccess =
true;
385 if (uuidResult2 == UUID.Zero)
386 uuidResult2 = UUID.Random();
388 return_result = uuidResult2;
390 errorMessage =
"a non-null UUID (UUID)";
392 case ConfigurationOption.ConfigurationTypes.TYPE_Vector3:
393 Vector3 vectorResult;
394 if (Vector3.TryParse(console_result, out vectorResult))
396 convertSuccess =
true;
397 return_result = vectorResult;
399 errorMessage =
"a vector (Vector3)";
401 case ConfigurationOption.ConfigurationTypes.TYPE_UINT16:
403 if (UInt16.TryParse(console_result, out ushortResult))
405 convertSuccess =
true;
406 return_result = ushortResult;
408 errorMessage =
"an unsigned 16 bit integer (ushort)";
410 case ConfigurationOption.ConfigurationTypes.TYPE_UINT32:
412 if (UInt32.TryParse(console_result, out uintResult))
414 convertSuccess =
true;
415 return_result = uintResult;
417 errorMessage =
"an unsigned 32 bit integer (uint)";
419 case ConfigurationOption.ConfigurationTypes.TYPE_UINT64:
421 if (UInt64.TryParse(console_result, out ulongResult))
423 convertSuccess =
true;
424 return_result = ulongResult;
426 errorMessage =
"an unsigned 64 bit integer (ulong)";
428 case ConfigurationOption.ConfigurationTypes.TYPE_FLOAT:
431 float.TryParse(console_result, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
Culture.
NumberFormatInfo,
434 convertSuccess =
true;
435 return_result = floatResult;
437 errorMessage =
"a single-precision floating point number (float)";
439 case ConfigurationOption.ConfigurationTypes.TYPE_DOUBLE:
442 Double.TryParse(console_result, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
Culture.
NumberFormatInfo,
445 convertSuccess =
true;
446 return_result = doubleResult;
448 errorMessage =
"an double-precision floating point number (double)";
456 configurationPlugin.SetAttribute(configOption.configurationKey, console_result);
462 "The handler for the last configuration option denied that input, please try again.");
463 convertSuccess =
false;
464 ignoreNextFromConfig =
true;
471 m_log.Error(string.Format(
472 "[CONFIG]: [{3}]:[{1}] is not valid default for parameter [{0}].\nThe configuration result must be parsable to {2}.\n",
473 configOption.configurationKey, console_result, errorMessage,
474 configurationFilename));
475 convertSuccess =
true;
479 m_log.Warn(string.Format(
480 "[CONFIG]: [{3}]:[{1}] is not a valid value [{0}].\nThe configuration result must be parsable to {2}.\n",
481 configOption.configurationKey, console_result, errorMessage,
482 configurationFilename));
483 ignoreNextFromConfig =
true;
491 configurationPlugin.Commit();
492 configurationPlugin.Close();
498 Assembly pluginAssembly = Assembly.LoadFrom(dllName);
501 foreach (Type pluginType
in pluginAssembly.GetTypes())
503 if (pluginType.IsPublic)
505 if (!pluginType.IsAbstract)
507 Type typeInterface = pluginType.GetInterface(
"IGenericConfig",
true);
509 if (typeInterface != null)
512 (
IGenericConfig) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
518 pluginAssembly = null;
524 configurationPlugin.LoadData();
525 configurationPlugin.SetAttribute(configuration_key, configuration_value);
526 configurationPlugin.Commit();
527 configurationPlugin.Close();
static NumberFormatInfo NumberFormatInfo
bool configurationUseDefaultNoPrompt
delegate bool ConfigurationOptionShouldBeAsked(string configuration_key)
ConfigurationMember(XmlNode configuration_xml, string configuration_description, ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function, bool use_console_to_prompt_on_error)
void performConfigurationRetrieve()
ConfigurationMember(string configuration_filename, string configuration_description, ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function, bool use_console_to_prompt_on_error)
void forceSetConfigurationOption(string configuration_key, string configuration_value)
void addConfigurationOption(string configuration_key, ConfigurationOption.ConfigurationTypes configuration_type, string configuration_question, string configuration_default, bool use_default_no_prompt)
ConfigurationTypes configurationType
void setConfigurationResultFunction(ConfigurationOptionResult result)
ConfigurationOptionShouldBeAsked shouldIBeAsked
void setConfigurationDescription(string desc)
void setConfigurationFilename(string filename)
void forceConfigurationPluginLibrary(string dll_filename)
string configurationQuestion
void addConfigurationOption(string configuration_key, ConfigurationOption.ConfigurationTypes configuration_type, string configuration_question, string configuration_default, bool use_default_no_prompt, ConfigurationOption.ConfigurationOptionShouldBeAsked shouldIBeAskedDelegate)