ConfigurationMember.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 libsecondlife;
  34. using log4net;
  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.Info("[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.Error("Error loading " + configurationFilename + ": " + e.ToString());
  183. useFile = false;
  184. }
  185. }
  186. else
  187. {
  188. if (configurationFromXMLNode != null)
  189. {
  190. m_log.Info("Loading from XML Node, will not save to the file");
  191. configurationPlugin.LoadDataFromString(configurationFromXMLNode.OuterXml);
  192. }
  193. m_log.Info("XML Configuration Filename is not valid; will not save to the file.");
  194. useFile = false;
  195. }
  196. foreach (ConfigurationOption configOption in configurationOptions)
  197. {
  198. bool convertSuccess = false;
  199. object return_result = null;
  200. string errorMessage = String.Empty;
  201. bool ignoreNextFromConfig = false;
  202. while (convertSuccess == false)
  203. {
  204. string console_result = String.Empty;
  205. string attribute = null;
  206. if (useFile || configurationFromXMLNode != null)
  207. {
  208. if (!ignoreNextFromConfig)
  209. {
  210. attribute = configurationPlugin.GetAttribute(configOption.configurationKey);
  211. }
  212. else
  213. {
  214. ignoreNextFromConfig = false;
  215. }
  216. }
  217. if (attribute == null)
  218. {
  219. if (configOption.configurationUseDefaultNoPrompt || useConsoleToPromptOnError == false)
  220. {
  221. console_result = configOption.configurationDefault;
  222. }
  223. else
  224. {
  225. if ((configOption.shouldIBeAsked != null &&
  226. configOption.shouldIBeAsked(configOption.configurationKey)) ||
  227. configOption.shouldIBeAsked == null)
  228. {
  229. if (configurationDescription.Trim() != String.Empty)
  230. {
  231. console_result =
  232. MainConsole.Instance.CmdPrompt(
  233. configurationDescription + ": " + configOption.configurationQuestion,
  234. configOption.configurationDefault);
  235. }
  236. else
  237. {
  238. console_result =
  239. MainConsole.Instance.CmdPrompt(configOption.configurationQuestion,
  240. configOption.configurationDefault);
  241. }
  242. }
  243. else
  244. {
  245. //Dont Ask! Just use default
  246. console_result = configOption.configurationDefault;
  247. }
  248. }
  249. }
  250. else
  251. {
  252. console_result = attribute;
  253. }
  254. switch (configOption.configurationType)
  255. {
  256. case ConfigurationOption.ConfigurationTypes.TYPE_STRING:
  257. return_result = console_result;
  258. convertSuccess = true;
  259. break;
  260. case ConfigurationOption.ConfigurationTypes.TYPE_STRING_NOT_EMPTY:
  261. if (console_result.Length > 0)
  262. {
  263. return_result = console_result;
  264. convertSuccess = true;
  265. }
  266. errorMessage = "a string that is not empty";
  267. break;
  268. case ConfigurationOption.ConfigurationTypes.TYPE_BOOLEAN:
  269. bool boolResult;
  270. if (Boolean.TryParse(console_result, out boolResult))
  271. {
  272. convertSuccess = true;
  273. return_result = boolResult;
  274. }
  275. errorMessage = "'true' or 'false' (Boolean)";
  276. break;
  277. case ConfigurationOption.ConfigurationTypes.TYPE_BYTE:
  278. byte byteResult;
  279. if (Byte.TryParse(console_result, out byteResult))
  280. {
  281. convertSuccess = true;
  282. return_result = byteResult;
  283. }
  284. errorMessage = "a byte (Byte)";
  285. break;
  286. case ConfigurationOption.ConfigurationTypes.TYPE_CHARACTER:
  287. char charResult;
  288. if (Char.TryParse(console_result, out charResult))
  289. {
  290. convertSuccess = true;
  291. return_result = charResult;
  292. }
  293. errorMessage = "a character (Char)";
  294. break;
  295. case ConfigurationOption.ConfigurationTypes.TYPE_INT16:
  296. short shortResult;
  297. if (Int16.TryParse(console_result, out shortResult))
  298. {
  299. convertSuccess = true;
  300. return_result = shortResult;
  301. }
  302. errorMessage = "a signed 32 bit integer (short)";
  303. break;
  304. case ConfigurationOption.ConfigurationTypes.TYPE_INT32:
  305. int intResult;
  306. if (Int32.TryParse(console_result, out intResult))
  307. {
  308. convertSuccess = true;
  309. return_result = intResult;
  310. }
  311. errorMessage = "a signed 32 bit integer (int)";
  312. break;
  313. case ConfigurationOption.ConfigurationTypes.TYPE_INT64:
  314. long longResult;
  315. if (Int64.TryParse(console_result, out longResult))
  316. {
  317. convertSuccess = true;
  318. return_result = longResult;
  319. }
  320. errorMessage = "a signed 32 bit integer (long)";
  321. break;
  322. case ConfigurationOption.ConfigurationTypes.TYPE_IP_ADDRESS:
  323. IPAddress ipAddressResult;
  324. if (IPAddress.TryParse(console_result, out ipAddressResult))
  325. {
  326. convertSuccess = true;
  327. return_result = ipAddressResult;
  328. }
  329. errorMessage = "an IP Address (IPAddress)";
  330. break;
  331. case ConfigurationOption.ConfigurationTypes.TYPE_LLUUID:
  332. LLUUID uuidResult;
  333. if (LLUUID.TryParse(console_result, out uuidResult))
  334. {
  335. convertSuccess = true;
  336. return_result = uuidResult;
  337. }
  338. errorMessage = "a UUID (LLUUID)";
  339. break;
  340. case ConfigurationOption.ConfigurationTypes.TYPE_LLUUID_NULL_FREE:
  341. LLUUID uuidResult2;
  342. if (LLUUID.TryParse(console_result, out uuidResult2))
  343. {
  344. convertSuccess = true;
  345. if (uuidResult2 == LLUUID.Zero)
  346. uuidResult2 = LLUUID.Random();
  347. return_result = uuidResult2;
  348. }
  349. errorMessage = "a non-null UUID (LLUUID)";
  350. break;
  351. case ConfigurationOption.ConfigurationTypes.TYPE_LLVECTOR3:
  352. LLVector3 vectorResult;
  353. if (LLVector3.TryParse(console_result, out vectorResult))
  354. {
  355. convertSuccess = true;
  356. return_result = vectorResult;
  357. }
  358. errorMessage = "a vector (LLVector3)";
  359. break;
  360. case ConfigurationOption.ConfigurationTypes.TYPE_UINT16:
  361. ushort ushortResult;
  362. if (UInt16.TryParse(console_result, out ushortResult))
  363. {
  364. convertSuccess = true;
  365. return_result = ushortResult;
  366. }
  367. errorMessage = "an unsigned 16 bit integer (ushort)";
  368. break;
  369. case ConfigurationOption.ConfigurationTypes.TYPE_UINT32:
  370. uint uintResult;
  371. if (UInt32.TryParse(console_result, out uintResult))
  372. {
  373. convertSuccess = true;
  374. return_result = uintResult;
  375. }
  376. errorMessage = "an unsigned 32 bit integer (uint)";
  377. break;
  378. case ConfigurationOption.ConfigurationTypes.TYPE_UINT64:
  379. ulong ulongResult;
  380. if (UInt64.TryParse(console_result, out ulongResult))
  381. {
  382. convertSuccess = true;
  383. return_result = ulongResult;
  384. }
  385. errorMessage = "an unsigned 64 bit integer (ulong)";
  386. break;
  387. case ConfigurationOption.ConfigurationTypes.TYPE_FLOAT:
  388. float floatResult;
  389. if (
  390. float.TryParse(console_result, NumberStyles.AllowDecimalPoint, Culture.NumberFormatInfo,
  391. out floatResult))
  392. {
  393. convertSuccess = true;
  394. return_result = floatResult;
  395. }
  396. errorMessage = "a single-precision floating point number (float)";
  397. break;
  398. case ConfigurationOption.ConfigurationTypes.TYPE_DOUBLE:
  399. double doubleResult;
  400. if (
  401. Double.TryParse(console_result, NumberStyles.AllowDecimalPoint, Culture.NumberFormatInfo,
  402. out doubleResult))
  403. {
  404. convertSuccess = true;
  405. return_result = doubleResult;
  406. }
  407. errorMessage = "an double-precision floating point number (double)";
  408. break;
  409. }
  410. if (convertSuccess)
  411. {
  412. if (useFile)
  413. {
  414. configurationPlugin.SetAttribute(configOption.configurationKey, console_result);
  415. }
  416. if (!resultFunction(configOption.configurationKey, return_result))
  417. {
  418. m_log.Info(
  419. "The handler for the last configuration option denied that input, please try again.");
  420. convertSuccess = false;
  421. ignoreNextFromConfig = true;
  422. }
  423. }
  424. else
  425. {
  426. if (configOption.configurationUseDefaultNoPrompt)
  427. {
  428. m_log.Error(string.Format(
  429. "[CONFIG]: [{3}]:[{1}] is not valid default for parameter [{0}].\nThe configuration result must be parsable to {2}.\n",
  430. configOption.configurationKey, console_result, errorMessage,
  431. configurationFilename));
  432. convertSuccess = true;
  433. }
  434. else
  435. {
  436. m_log.Warn(string.Format(
  437. "[CONFIG]: [{3}]:[{1}] is not a valid value [{0}].\nThe configuration result must be parsable to {2}.\n",
  438. configOption.configurationKey, console_result, errorMessage,
  439. configurationFilename));
  440. ignoreNextFromConfig = true;
  441. }
  442. }
  443. }
  444. }
  445. if (useFile)
  446. {
  447. configurationPlugin.Commit();
  448. configurationPlugin.Close();
  449. }
  450. }
  451. private static IGenericConfig LoadConfigDll(string dllName)
  452. {
  453. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  454. IGenericConfig plug = null;
  455. foreach (Type pluginType in pluginAssembly.GetTypes())
  456. {
  457. if (pluginType.IsPublic)
  458. {
  459. if (!pluginType.IsAbstract)
  460. {
  461. Type typeInterface = pluginType.GetInterface("IGenericConfig", true);
  462. if (typeInterface != null)
  463. {
  464. plug =
  465. (IGenericConfig) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  466. }
  467. }
  468. }
  469. }
  470. pluginAssembly = null;
  471. return plug;
  472. }
  473. public void forceSetConfigurationOption(string configuration_key, string configuration_value)
  474. {
  475. configurationPlugin.LoadData();
  476. configurationPlugin.SetAttribute(configuration_key, configuration_value);
  477. configurationPlugin.Commit();
  478. configurationPlugin.Close();
  479. }
  480. }
  481. }