ConfigurationMember.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Globalization;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Framework.Console;
  36. namespace OpenSim.Framework
  37. {
  38. public class ConfigurationMember
  39. {
  40. #region Delegates
  41. public delegate bool ConfigurationOptionResult(string configuration_key, object configuration_result);
  42. public delegate void ConfigurationOptionsLoad();
  43. #endregion
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private int cE = 0;
  46. private string configurationDescription = String.Empty;
  47. private string configurationFilename = String.Empty;
  48. private XmlNode configurationFromXMLNode = null;
  49. private List<ConfigurationOption> configurationOptions = new List<ConfigurationOption>();
  50. private IGenericConfig configurationPlugin = null;
  51. /// <summary>
  52. /// This is the default configuration DLL loaded
  53. /// </summary>
  54. private string configurationPluginFilename = "OpenSim.Framework.Configuration.XML.dll";
  55. private ConfigurationOptionsLoad loadFunction;
  56. private ConfigurationOptionResult resultFunction;
  57. private bool useConsoleToPromptOnError = true;
  58. public ConfigurationMember(string configuration_filename, string configuration_description,
  59. ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function, bool use_console_to_prompt_on_error)
  60. {
  61. configurationFilename = configuration_filename;
  62. configurationDescription = configuration_description;
  63. loadFunction = load_function;
  64. resultFunction = result_function;
  65. useConsoleToPromptOnError = use_console_to_prompt_on_error;
  66. }
  67. public ConfigurationMember(XmlNode configuration_xml, string configuration_description,
  68. ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function, bool use_console_to_prompt_on_error)
  69. {
  70. configurationFilename = String.Empty;
  71. configurationFromXMLNode = configuration_xml;
  72. configurationDescription = configuration_description;
  73. loadFunction = load_function;
  74. resultFunction = result_function;
  75. useConsoleToPromptOnError = use_console_to_prompt_on_error;
  76. }
  77. public void setConfigurationFilename(string filename)
  78. {
  79. configurationFilename = filename;
  80. }
  81. public void setConfigurationDescription(string desc)
  82. {
  83. configurationDescription = desc;
  84. }
  85. public void setConfigurationResultFunction(ConfigurationOptionResult result)
  86. {
  87. resultFunction = result;
  88. }
  89. public void forceConfigurationPluginLibrary(string dll_filename)
  90. {
  91. configurationPluginFilename = dll_filename;
  92. }
  93. private void checkAndAddConfigOption(ConfigurationOption option)
  94. {
  95. if ((option.configurationKey != String.Empty && option.configurationQuestion != String.Empty) ||
  96. (option.configurationKey != String.Empty && option.configurationUseDefaultNoPrompt))
  97. {
  98. if (!configurationOptions.Contains(option))
  99. {
  100. configurationOptions.Add(option);
  101. }
  102. }
  103. else
  104. {
  105. m_log.Info(
  106. "Required fields for adding a configuration option is invalid. Will not add this option (" +
  107. option.configurationKey + ")");
  108. }
  109. }
  110. public void addConfigurationOption(string configuration_key,
  111. ConfigurationOption.ConfigurationTypes configuration_type,
  112. string configuration_question, string configuration_default,
  113. bool use_default_no_prompt)
  114. {
  115. ConfigurationOption configOption = new ConfigurationOption();
  116. configOption.configurationKey = configuration_key;
  117. configOption.configurationQuestion = configuration_question;
  118. configOption.configurationDefault = configuration_default;
  119. configOption.configurationType = configuration_type;
  120. configOption.configurationUseDefaultNoPrompt = use_default_no_prompt;
  121. configOption.shouldIBeAsked = null; //Assumes true, I can ask whenever
  122. checkAndAddConfigOption(configOption);
  123. }
  124. public void addConfigurationOption(string configuration_key,
  125. ConfigurationOption.ConfigurationTypes configuration_type,
  126. string configuration_question, string configuration_default,
  127. bool use_default_no_prompt,
  128. ConfigurationOption.ConfigurationOptionShouldBeAsked shouldIBeAskedDelegate)
  129. {
  130. ConfigurationOption configOption = new ConfigurationOption();
  131. configOption.configurationKey = configuration_key;
  132. configOption.configurationQuestion = configuration_question;
  133. configOption.configurationDefault = configuration_default;
  134. configOption.configurationType = configuration_type;
  135. configOption.configurationUseDefaultNoPrompt = use_default_no_prompt;
  136. configOption.shouldIBeAsked = shouldIBeAskedDelegate;
  137. checkAndAddConfigOption(configOption);
  138. }
  139. // TEMP - REMOVE
  140. public void performConfigurationRetrieve()
  141. {
  142. if (cE > 1)
  143. m_log.Error("READING CONFIGURATION COUT: " + cE.ToString());
  144. configurationPlugin = LoadConfigDll(configurationPluginFilename);
  145. configurationOptions.Clear();
  146. if (loadFunction == null)
  147. {
  148. m_log.Error("Load Function for '" + configurationDescription +
  149. "' is null. Refusing to run configuration.");
  150. return;
  151. }
  152. if (resultFunction == null)
  153. {
  154. m_log.Error("Result Function for '" + configurationDescription +
  155. "' is null. Refusing to run configuration.");
  156. return;
  157. }
  158. //m_log.Debug("[CONFIG]: Calling Configuration Load Function...");
  159. loadFunction();
  160. if (configurationOptions.Count <= 0)
  161. {
  162. m_log.Error("[CONFIG]: No configuration options were specified for '" + configurationOptions +
  163. "'. Refusing to continue configuration.");
  164. return;
  165. }
  166. bool useFile = true;
  167. if (configurationPlugin == null)
  168. {
  169. m_log.Error("[CONFIG]: Configuration Plugin NOT LOADED!");
  170. return;
  171. }
  172. if (configurationFilename.Trim() != String.Empty)
  173. {
  174. configurationPlugin.SetFileName(configurationFilename);
  175. try
  176. {
  177. configurationPlugin.LoadData();
  178. useFile = true;
  179. }
  180. catch (XmlException e)
  181. {
  182. m_log.WarnFormat("[CONFIG] Not using {0}: {1}",
  183. configurationFilename,
  184. e.Message.ToString());
  185. //m_log.Error("Error loading " + configurationFilename + ": " + e.ToString());
  186. useFile = false;
  187. }
  188. }
  189. else
  190. {
  191. if (configurationFromXMLNode != null)
  192. {
  193. m_log.Info("Loading from XML Node, will not save to the file");
  194. configurationPlugin.LoadDataFromString(configurationFromXMLNode.OuterXml);
  195. }
  196. m_log.Info("XML Configuration Filename is not valid; will not save to the file.");
  197. useFile = false;
  198. }
  199. foreach (ConfigurationOption configOption in configurationOptions)
  200. {
  201. bool convertSuccess = false;
  202. object return_result = null;
  203. string errorMessage = String.Empty;
  204. bool ignoreNextFromConfig = false;
  205. while (convertSuccess == false)
  206. {
  207. string console_result = String.Empty;
  208. string attribute = null;
  209. if (useFile || configurationFromXMLNode != null)
  210. {
  211. if (!ignoreNextFromConfig)
  212. {
  213. attribute = configurationPlugin.GetAttribute(configOption.configurationKey);
  214. }
  215. else
  216. {
  217. ignoreNextFromConfig = false;
  218. }
  219. }
  220. if (attribute == null)
  221. {
  222. if (configOption.configurationUseDefaultNoPrompt || useConsoleToPromptOnError == false)
  223. {
  224. console_result = configOption.configurationDefault;
  225. }
  226. else
  227. {
  228. if ((configOption.shouldIBeAsked != null &&
  229. configOption.shouldIBeAsked(configOption.configurationKey)) ||
  230. configOption.shouldIBeAsked == null)
  231. {
  232. if (configurationDescription.Trim() != String.Empty)
  233. {
  234. console_result =
  235. MainConsole.Instance.CmdPrompt(
  236. configurationDescription + ": " + configOption.configurationQuestion,
  237. configOption.configurationDefault);
  238. }
  239. else
  240. {
  241. console_result =
  242. MainConsole.Instance.CmdPrompt(configOption.configurationQuestion,
  243. configOption.configurationDefault);
  244. }
  245. }
  246. else
  247. {
  248. //Dont Ask! Just use default
  249. console_result = configOption.configurationDefault;
  250. }
  251. }
  252. }
  253. else
  254. {
  255. console_result = attribute;
  256. }
  257. // if the first character is a "$", assume it's the name
  258. // of an environment variable and substitute with the value of that variable
  259. if (console_result.StartsWith("$"))
  260. console_result = System.Environment.GetEnvironmentVariable(console_result.Substring(1));
  261. switch (configOption.configurationType)
  262. {
  263. case ConfigurationOption.ConfigurationTypes.TYPE_STRING:
  264. return_result = console_result;
  265. convertSuccess = true;
  266. break;
  267. case ConfigurationOption.ConfigurationTypes.TYPE_STRING_NOT_EMPTY:
  268. if (console_result.Length > 0)
  269. {
  270. return_result = console_result;
  271. convertSuccess = true;
  272. }
  273. errorMessage = "a string that is not empty";
  274. break;
  275. case ConfigurationOption.ConfigurationTypes.TYPE_BOOLEAN:
  276. bool boolResult;
  277. if (Boolean.TryParse(console_result, out boolResult))
  278. {
  279. convertSuccess = true;
  280. return_result = boolResult;
  281. }
  282. errorMessage = "'true' or 'false' (Boolean)";
  283. break;
  284. case ConfigurationOption.ConfigurationTypes.TYPE_BYTE:
  285. byte byteResult;
  286. if (Byte.TryParse(console_result, out byteResult))
  287. {
  288. convertSuccess = true;
  289. return_result = byteResult;
  290. }
  291. errorMessage = "a byte (Byte)";
  292. break;
  293. case ConfigurationOption.ConfigurationTypes.TYPE_CHARACTER:
  294. char charResult;
  295. if (Char.TryParse(console_result, out charResult))
  296. {
  297. convertSuccess = true;
  298. return_result = charResult;
  299. }
  300. errorMessage = "a character (Char)";
  301. break;
  302. case ConfigurationOption.ConfigurationTypes.TYPE_INT16:
  303. short shortResult;
  304. if (Int16.TryParse(console_result, out shortResult))
  305. {
  306. convertSuccess = true;
  307. return_result = shortResult;
  308. }
  309. errorMessage = "a signed 32 bit integer (short)";
  310. break;
  311. case ConfigurationOption.ConfigurationTypes.TYPE_INT32:
  312. int intResult;
  313. if (Int32.TryParse(console_result, out intResult))
  314. {
  315. convertSuccess = true;
  316. return_result = intResult;
  317. }
  318. errorMessage = "a signed 32 bit integer (int)";
  319. break;
  320. case ConfigurationOption.ConfigurationTypes.TYPE_INT64:
  321. long longResult;
  322. if (Int64.TryParse(console_result, out longResult))
  323. {
  324. convertSuccess = true;
  325. return_result = longResult;
  326. }
  327. errorMessage = "a signed 32 bit integer (long)";
  328. break;
  329. case ConfigurationOption.ConfigurationTypes.TYPE_IP_ADDRESS:
  330. IPAddress ipAddressResult;
  331. if (IPAddress.TryParse(console_result, out ipAddressResult))
  332. {
  333. convertSuccess = true;
  334. return_result = ipAddressResult;
  335. }
  336. errorMessage = "an IP Address (IPAddress)";
  337. break;
  338. case ConfigurationOption.ConfigurationTypes.TYPE_UUID:
  339. UUID uuidResult;
  340. if (UUID.TryParse(console_result, out uuidResult))
  341. {
  342. convertSuccess = true;
  343. return_result = uuidResult;
  344. }
  345. errorMessage = "a UUID (UUID)";
  346. break;
  347. case ConfigurationOption.ConfigurationTypes.TYPE_UUID_NULL_FREE:
  348. UUID uuidResult2;
  349. if (UUID.TryParse(console_result, out uuidResult2))
  350. {
  351. convertSuccess = true;
  352. if (uuidResult2 == UUID.Zero)
  353. uuidResult2 = UUID.Random();
  354. return_result = uuidResult2;
  355. }
  356. errorMessage = "a non-null UUID (UUID)";
  357. break;
  358. case ConfigurationOption.ConfigurationTypes.TYPE_Vector3:
  359. Vector3 vectorResult;
  360. if (Vector3.TryParse(console_result, out vectorResult))
  361. {
  362. convertSuccess = true;
  363. return_result = vectorResult;
  364. }
  365. errorMessage = "a vector (Vector3)";
  366. break;
  367. case ConfigurationOption.ConfigurationTypes.TYPE_UINT16:
  368. ushort ushortResult;
  369. if (UInt16.TryParse(console_result, out ushortResult))
  370. {
  371. convertSuccess = true;
  372. return_result = ushortResult;
  373. }
  374. errorMessage = "an unsigned 16 bit integer (ushort)";
  375. break;
  376. case ConfigurationOption.ConfigurationTypes.TYPE_UINT32:
  377. uint uintResult;
  378. if (UInt32.TryParse(console_result, out uintResult))
  379. {
  380. convertSuccess = true;
  381. return_result = uintResult;
  382. }
  383. errorMessage = "an unsigned 32 bit integer (uint)";
  384. break;
  385. case ConfigurationOption.ConfigurationTypes.TYPE_UINT64:
  386. ulong ulongResult;
  387. if (UInt64.TryParse(console_result, out ulongResult))
  388. {
  389. convertSuccess = true;
  390. return_result = ulongResult;
  391. }
  392. errorMessage = "an unsigned 64 bit integer (ulong)";
  393. break;
  394. case ConfigurationOption.ConfigurationTypes.TYPE_FLOAT:
  395. float floatResult;
  396. if (
  397. float.TryParse(console_result, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, Culture.NumberFormatInfo,
  398. out floatResult))
  399. {
  400. convertSuccess = true;
  401. return_result = floatResult;
  402. }
  403. errorMessage = "a single-precision floating point number (float)";
  404. break;
  405. case ConfigurationOption.ConfigurationTypes.TYPE_DOUBLE:
  406. double doubleResult;
  407. if (
  408. Double.TryParse(console_result, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, Culture.NumberFormatInfo,
  409. out doubleResult))
  410. {
  411. convertSuccess = true;
  412. return_result = doubleResult;
  413. }
  414. errorMessage = "an double-precision floating point number (double)";
  415. break;
  416. }
  417. if (convertSuccess)
  418. {
  419. if (useFile)
  420. {
  421. configurationPlugin.SetAttribute(configOption.configurationKey, console_result);
  422. }
  423. if (!resultFunction(configOption.configurationKey, return_result))
  424. {
  425. m_log.Info(
  426. "The handler for the last configuration option denied that input, please try again.");
  427. convertSuccess = false;
  428. ignoreNextFromConfig = true;
  429. }
  430. }
  431. else
  432. {
  433. if (configOption.configurationUseDefaultNoPrompt)
  434. {
  435. m_log.Error(string.Format(
  436. "[CONFIG]: [{3}]:[{1}] is not valid default for parameter [{0}].\nThe configuration result must be parsable to {2}.\n",
  437. configOption.configurationKey, console_result, errorMessage,
  438. configurationFilename));
  439. convertSuccess = true;
  440. }
  441. else
  442. {
  443. m_log.Warn(string.Format(
  444. "[CONFIG]: [{3}]:[{1}] is not a valid value [{0}].\nThe configuration result must be parsable to {2}.\n",
  445. configOption.configurationKey, console_result, errorMessage,
  446. configurationFilename));
  447. ignoreNextFromConfig = true;
  448. }
  449. }
  450. }
  451. }
  452. if (useFile)
  453. {
  454. configurationPlugin.Commit();
  455. configurationPlugin.Close();
  456. }
  457. }
  458. private static IGenericConfig LoadConfigDll(string dllName)
  459. {
  460. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  461. IGenericConfig plug = null;
  462. foreach (Type pluginType in pluginAssembly.GetTypes())
  463. {
  464. if (pluginType.IsPublic)
  465. {
  466. if (!pluginType.IsAbstract)
  467. {
  468. Type typeInterface = pluginType.GetInterface("IGenericConfig", true);
  469. if (typeInterface != null)
  470. {
  471. plug =
  472. (IGenericConfig) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  473. }
  474. }
  475. }
  476. }
  477. pluginAssembly = null;
  478. return plug;
  479. }
  480. public void forceSetConfigurationOption(string configuration_key, string configuration_value)
  481. {
  482. configurationPlugin.LoadData();
  483. configurationPlugin.SetAttribute(configuration_key, configuration_value);
  484. configurationPlugin.Commit();
  485. configurationPlugin.Close();
  486. }
  487. }
  488. }