ConfigurationMember.cs 24 KB

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