ConfigurationLoader.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 OpenSimulator 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.IO;
  30. using System.Reflection;
  31. using System.Threading;
  32. using System.Xml;
  33. using log4net;
  34. using Nini.Config;
  35. namespace OpenSim.Tools.Configger
  36. {
  37. /// <summary>
  38. /// Loads the Configuration files into nIni
  39. /// </summary>
  40. public class ConfigurationLoader
  41. {
  42. /// <summary>
  43. /// A source of Configuration data
  44. /// </summary>
  45. protected IConfigSource m_config;
  46. /// <summary>
  47. /// Console logger
  48. /// </summary>
  49. private static readonly ILog m_log =
  50. LogManager.GetLogger(
  51. MethodBase.GetCurrentMethod().DeclaringType);
  52. public ConfigurationLoader()
  53. {
  54. }
  55. /// <summary>
  56. /// Loads the region configuration
  57. /// </summary>
  58. /// <param name="argvSource">Parameters passed into the process when started</param>
  59. /// <param name="configSettings"></param>
  60. /// <param name="networkInfo"></param>
  61. /// <returns>A configuration that gets passed to modules</returns>
  62. public IConfigSource LoadConfigSettings()
  63. {
  64. bool iniFileExists = false;
  65. List<string> sources = new List<string>();
  66. string iniFileName = "OpenSim.ini";
  67. string iniFilePath = Path.Combine(".", iniFileName);
  68. if (IsUri(iniFileName))
  69. {
  70. if (!sources.Contains(iniFileName))
  71. sources.Add(iniFileName);
  72. }
  73. else
  74. {
  75. if (File.Exists(iniFilePath))
  76. {
  77. if (!sources.Contains(iniFilePath))
  78. sources.Add(iniFilePath);
  79. }
  80. }
  81. m_config = new IniConfigSource();
  82. m_config.Merge(DefaultConfig());
  83. m_log.Info("[CONFIG] Reading configuration settings");
  84. if (sources.Count == 0)
  85. {
  86. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  87. m_log.FatalFormat("[CONFIG] Did you copy the OpenSim.ini.example file to OpenSim.ini?");
  88. Environment.Exit(1);
  89. }
  90. for (int i = 0 ; i < sources.Count ; i++)
  91. {
  92. if (ReadConfig(sources[i]))
  93. iniFileExists = true;
  94. AddIncludes(sources);
  95. }
  96. if (!iniFileExists)
  97. {
  98. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  99. m_log.FatalFormat("[CONFIG] Configuration exists, but there was an error loading it!");
  100. Environment.Exit(1);
  101. }
  102. return m_config;
  103. }
  104. /// <summary>
  105. /// Adds the included files as ini configuration files
  106. /// </summary>
  107. /// <param name="sources">List of URL strings or filename strings</param>
  108. private void AddIncludes(List<string> sources)
  109. {
  110. //loop over config sources
  111. foreach (IConfig config in m_config.Configs)
  112. {
  113. // Look for Include-* in the key name
  114. string[] keys = config.GetKeys();
  115. foreach (string k in keys)
  116. {
  117. if (k.StartsWith("Include-"))
  118. {
  119. // read the config file to be included.
  120. string file = config.GetString(k);
  121. if (IsUri(file))
  122. {
  123. if (!sources.Contains(file))
  124. sources.Add(file);
  125. }
  126. else
  127. {
  128. string basepath = Path.GetFullPath(".");
  129. string path = Path.Combine(basepath, file);
  130. string[] paths = Util.Glob(path);
  131. foreach (string p in paths)
  132. {
  133. if (!sources.Contains(p))
  134. sources.Add(p);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Check if we can convert the string to a URI
  143. /// </summary>
  144. /// <param name="file">String uri to the remote resource</param>
  145. /// <returns>true if we can convert the string to a Uri object</returns>
  146. bool IsUri(string file)
  147. {
  148. Uri configUri;
  149. return Uri.TryCreate(file, UriKind.Absolute,
  150. out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  151. }
  152. /// <summary>
  153. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  154. /// </summary>
  155. /// <param name="iniPath">Full path to the ini</param>
  156. /// <returns></returns>
  157. private bool ReadConfig(string iniPath)
  158. {
  159. bool success = false;
  160. if (!IsUri(iniPath))
  161. {
  162. m_log.InfoFormat("[CONFIG] Reading configuration file {0}",
  163. Path.GetFullPath(iniPath));
  164. m_config.Merge(new IniConfigSource(iniPath));
  165. success = true;
  166. }
  167. else
  168. {
  169. m_log.InfoFormat("[CONFIG] {0} is a http:// URI, fetching ...",
  170. iniPath);
  171. // The ini file path is a http URI
  172. // Try to read it
  173. //
  174. try
  175. {
  176. XmlReader r = XmlReader.Create(iniPath);
  177. XmlConfigSource cs = new XmlConfigSource(r);
  178. m_config.Merge(cs);
  179. success = true;
  180. }
  181. catch (Exception e)
  182. {
  183. m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  184. Environment.Exit(1);
  185. }
  186. }
  187. return success;
  188. }
  189. /// <summary>
  190. /// Setup a default config values in case they aren't present in the ini file
  191. /// </summary>
  192. /// <returns>A Configuration source containing the default configuration</returns>
  193. private static IConfigSource DefaultConfig()
  194. {
  195. IConfigSource defaultConfig = new IniConfigSource();
  196. {
  197. IConfig config = defaultConfig.Configs["Startup"];
  198. if (null == config)
  199. config = defaultConfig.AddConfig("Startup");
  200. config.Set("region_info_source", "filesystem");
  201. config.Set("gridmode", false);
  202. config.Set("physics", "OpenDynamicsEngine");
  203. config.Set("meshing", "Meshmerizer");
  204. config.Set("physical_prim", true);
  205. config.Set("see_into_this_sim_from_neighbor", true);
  206. config.Set("serverside_object_permissions", false);
  207. config.Set("storage_plugin", "OpenSim.Data.SQLite.dll");
  208. config.Set("storage_connection_string", "URI=file:OpenSim.db,version=3");
  209. config.Set("storage_prim_inventories", true);
  210. config.Set("startup_console_commands_file", String.Empty);
  211. config.Set("shutdown_console_commands_file", String.Empty);
  212. config.Set("DefaultScriptEngine", "XEngine");
  213. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  214. // life doesn't really work without this
  215. config.Set("EventQueue", true);
  216. }
  217. {
  218. IConfig config = defaultConfig.Configs["StandAlone"];
  219. if (null == config)
  220. config = defaultConfig.AddConfig("StandAlone");
  221. config.Set("accounts_authenticate", true);
  222. config.Set("welcome_message", "Welcome to OpenSimulator");
  223. config.Set("inventory_plugin", "OpenSim.Data.SQLite.dll");
  224. config.Set("inventory_source", "");
  225. config.Set("userDatabase_plugin", "OpenSim.Data.SQLite.dll");
  226. config.Set("user_source", "");
  227. config.Set("LibrariesXMLFile", string.Format(".{0}inventory{0}Libraries.xml", Path.DirectorySeparatorChar));
  228. }
  229. {
  230. IConfig config = defaultConfig.Configs["Network"];
  231. if (null == config)
  232. config = defaultConfig.AddConfig("Network");
  233. config.Set("default_location_x", 1000);
  234. config.Set("default_location_y", 1000);
  235. config.Set("grid_send_key", "null");
  236. config.Set("grid_recv_key", "null");
  237. config.Set("user_send_key", "null");
  238. config.Set("user_recv_key", "null");
  239. config.Set("secure_inventory_server", "true");
  240. }
  241. return defaultConfig;
  242. }
  243. }
  244. }