ConfigurationLoader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. using OpenSim.Framework;
  36. namespace OpenSim
  37. {
  38. /// <summary>
  39. /// Loads the Configuration files into nIni
  40. /// </summary>
  41. public class ConfigurationLoader
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. /// <summary>
  45. /// Various Config settings the region needs to start
  46. /// Physics Engine, Mesh Engine, GridMode, PhysicsPrim allowed, Neighbor,
  47. /// StorageDLL, Storage Connection String, Estate connection String, Client Stack
  48. /// Standalone settings.
  49. /// </summary>
  50. protected ConfigSettings m_configSettings;
  51. /// <summary>
  52. /// A source of Configuration data
  53. /// </summary>
  54. protected OpenSimConfigSource m_config;
  55. /// <summary>
  56. /// Grid Service Information. This refers to classes and addresses of the grid service
  57. /// </summary>
  58. protected NetworkServersInfo m_networkServersInfo;
  59. /// <summary>
  60. /// Loads the region configuration
  61. /// </summary>
  62. /// <param name="argvSource">Parameters passed into the process when started</param>
  63. /// <param name="configSettings"></param>
  64. /// <param name="networkInfo"></param>
  65. /// <returns>A configuration that gets passed to modules</returns>
  66. public OpenSimConfigSource LoadConfigSettings(
  67. IConfigSource argvSource, out ConfigSettings configSettings,
  68. out NetworkServersInfo networkInfo)
  69. {
  70. m_configSettings = configSettings = new ConfigSettings();
  71. m_networkServersInfo = networkInfo = new NetworkServersInfo();
  72. bool iniFileExists = false;
  73. IConfig startupConfig = argvSource.Configs["Startup"];
  74. List<string> sources = new List<string>();
  75. string masterFileName =
  76. startupConfig.GetString("inimaster", String.Empty);
  77. if (IsUri(masterFileName))
  78. {
  79. if (!sources.Contains(masterFileName))
  80. sources.Add(masterFileName);
  81. }
  82. else
  83. {
  84. string masterFilePath = Path.GetFullPath(
  85. Path.Combine(Util.configDir(), masterFileName));
  86. if (masterFileName != String.Empty &&
  87. File.Exists(masterFilePath) &&
  88. (!sources.Contains(masterFilePath)))
  89. sources.Add(masterFilePath);
  90. }
  91. string iniFileName =
  92. startupConfig.GetString("inifile", "OpenSim.ini");
  93. if (IsUri(iniFileName))
  94. {
  95. if (!sources.Contains(iniFileName))
  96. sources.Add(iniFileName);
  97. Application.iniFilePath = iniFileName;
  98. }
  99. else
  100. {
  101. Application.iniFilePath = Path.GetFullPath(
  102. Path.Combine(Util.configDir(), iniFileName));
  103. if (!File.Exists(Application.iniFilePath))
  104. {
  105. iniFileName = "OpenSim.xml";
  106. Application.iniFilePath = Path.GetFullPath(
  107. Path.Combine(Util.configDir(), iniFileName));
  108. }
  109. if (File.Exists(Application.iniFilePath))
  110. {
  111. if (!sources.Contains(Application.iniFilePath))
  112. sources.Add(Application.iniFilePath);
  113. }
  114. }
  115. string iniDirName =
  116. startupConfig.GetString("inidirectory", "config");
  117. string iniDirPath =
  118. Path.Combine(Util.configDir(), iniDirName);
  119. if (Directory.Exists(iniDirPath))
  120. {
  121. m_log.InfoFormat("Searching folder {0} for config ini files",
  122. iniDirPath);
  123. string[] fileEntries = Directory.GetFiles(iniDirName);
  124. foreach (string filePath in fileEntries)
  125. {
  126. if (Path.GetExtension(filePath).ToLower() == ".ini")
  127. {
  128. if (!sources.Contains(Path.GetFullPath(filePath)))
  129. sources.Add(Path.GetFullPath(filePath));
  130. }
  131. }
  132. }
  133. m_config = new OpenSimConfigSource();
  134. m_config.Source = new IniConfigSource();
  135. m_config.Source.Merge(DefaultConfig());
  136. m_log.Info("[CONFIG]: Reading configuration settings");
  137. if (sources.Count == 0)
  138. {
  139. m_log.FatalFormat("[CONFIG]: Could not load any configuration");
  140. m_log.FatalFormat("[CONFIG]: Did you copy the OpenSim.ini.example file to OpenSim.ini?");
  141. Environment.Exit(1);
  142. }
  143. for (int i = 0 ; i < sources.Count ; i++)
  144. {
  145. if (ReadConfig(sources[i]))
  146. iniFileExists = true;
  147. AddIncludes(sources);
  148. }
  149. if (!iniFileExists)
  150. {
  151. m_log.FatalFormat("[CONFIG]: Could not load any configuration");
  152. m_log.FatalFormat("[CONFIG]: Configuration exists, but there was an error loading it!");
  153. Environment.Exit(1);
  154. }
  155. // Make sure command line options take precedence
  156. m_config.Source.Merge(argvSource);
  157. ReadConfigSettings();
  158. return m_config;
  159. }
  160. /// <summary>
  161. /// Adds the included files as ini configuration files
  162. /// </summary>
  163. /// <param name="sources">List of URL strings or filename strings</param>
  164. private void AddIncludes(List<string> sources)
  165. {
  166. //loop over config sources
  167. foreach (IConfig config in m_config.Source.Configs)
  168. {
  169. // Look for Include-* in the key name
  170. string[] keys = config.GetKeys();
  171. foreach (string k in keys)
  172. {
  173. if (k.StartsWith("Include-"))
  174. {
  175. // read the config file to be included.
  176. string file = config.GetString(k);
  177. if (IsUri(file))
  178. {
  179. if (!sources.Contains(file))
  180. sources.Add(file);
  181. }
  182. else
  183. {
  184. string basepath = Path.GetFullPath(Util.configDir());
  185. string path = Path.Combine(basepath, file);
  186. string[] paths = Util.Glob(path);
  187. foreach (string p in paths)
  188. {
  189. if (!sources.Contains(p))
  190. sources.Add(p);
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// Check if we can convert the string to a URI
  199. /// </summary>
  200. /// <param name="file">String uri to the remote resource</param>
  201. /// <returns>true if we can convert the string to a Uri object</returns>
  202. bool IsUri(string file)
  203. {
  204. Uri configUri;
  205. return Uri.TryCreate(file, UriKind.Absolute,
  206. out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  207. }
  208. /// <summary>
  209. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  210. /// </summary>
  211. /// <param name="iniPath">Full path to the ini</param>
  212. /// <returns></returns>
  213. private bool ReadConfig(string iniPath)
  214. {
  215. bool success = false;
  216. if (!IsUri(iniPath))
  217. {
  218. m_log.InfoFormat("[CONFIG]: Reading configuration file {0}", Path.GetFullPath(iniPath));
  219. m_config.Source.Merge(new IniConfigSource(iniPath));
  220. success = true;
  221. }
  222. else
  223. {
  224. m_log.InfoFormat("[CONFIG]: {0} is a http:// URI, fetching ...", iniPath);
  225. // The ini file path is a http URI
  226. // Try to read it
  227. try
  228. {
  229. XmlReader r = XmlReader.Create(iniPath);
  230. XmlConfigSource cs = new XmlConfigSource(r);
  231. m_config.Source.Merge(cs);
  232. success = true;
  233. }
  234. catch (Exception e)
  235. {
  236. m_log.FatalFormat("[CONFIG]: Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  237. Environment.Exit(1);
  238. }
  239. }
  240. return success;
  241. }
  242. /// <summary>
  243. /// Setup a default config values in case they aren't present in the ini file
  244. /// </summary>
  245. /// <returns>A Configuration source containing the default configuration</returns>
  246. private static IConfigSource DefaultConfig()
  247. {
  248. IConfigSource defaultConfig = new IniConfigSource();
  249. {
  250. IConfig config = defaultConfig.Configs["Startup"];
  251. if (null == config)
  252. config = defaultConfig.AddConfig("Startup");
  253. config.Set("region_info_source", "filesystem");
  254. config.Set("physics", "OpenDynamicsEngine");
  255. config.Set("meshing", "Meshmerizer");
  256. config.Set("physical_prim", true);
  257. config.Set("see_into_this_sim_from_neighbor", true);
  258. config.Set("serverside_object_permissions", false);
  259. config.Set("storage_plugin", "OpenSim.Data.SQLite.dll");
  260. config.Set("storage_connection_string", "URI=file:OpenSim.db,version=3");
  261. config.Set("storage_prim_inventories", true);
  262. config.Set("startup_console_commands_file", String.Empty);
  263. config.Set("shutdown_console_commands_file", String.Empty);
  264. config.Set("DefaultScriptEngine", "XEngine");
  265. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  266. // life doesn't really work without this
  267. config.Set("EventQueue", true);
  268. }
  269. {
  270. IConfig config = defaultConfig.Configs["StandAlone"];
  271. if (null == config)
  272. config = defaultConfig.AddConfig("StandAlone");
  273. config.Set("accounts_authenticate", true);
  274. config.Set("welcome_message", "Welcome to OpenSimulator");
  275. config.Set("inventory_plugin", "OpenSim.Data.SQLite.dll");
  276. config.Set("inventory_source", "");
  277. config.Set("userDatabase_plugin", "OpenSim.Data.SQLite.dll");
  278. config.Set("user_source", "");
  279. config.Set("LibrariesXMLFile", string.Format(".{0}inventory{0}Libraries.xml", Path.DirectorySeparatorChar));
  280. }
  281. {
  282. IConfig config = defaultConfig.Configs["Network"];
  283. if (null == config)
  284. config = defaultConfig.AddConfig("Network");
  285. config.Set("http_listener_port", ConfigSettings.DefaultRegionHttpPort);
  286. }
  287. return defaultConfig;
  288. }
  289. /// <summary>
  290. /// Read initial region settings from the ConfigSource
  291. /// </summary>
  292. protected virtual void ReadConfigSettings()
  293. {
  294. IConfig startupConfig = m_config.Source.Configs["Startup"];
  295. if (startupConfig != null)
  296. {
  297. m_configSettings.PhysicsEngine = startupConfig.GetString("physics");
  298. m_configSettings.MeshEngineName = startupConfig.GetString("meshing");
  299. m_configSettings.PhysicalPrim = startupConfig.GetBoolean("physical_prim", true);
  300. m_configSettings.See_into_region_from_neighbor = startupConfig.GetBoolean("see_into_this_sim_from_neighbor", true);
  301. m_configSettings.StorageDll = startupConfig.GetString("storage_plugin");
  302. m_configSettings.StorageConnectionString
  303. = startupConfig.GetString("storage_connection_string");
  304. m_configSettings.EstateConnectionString
  305. = startupConfig.GetString("estate_connection_string", m_configSettings.StorageConnectionString);
  306. m_configSettings.ClientstackDll
  307. = startupConfig.GetString("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  308. }
  309. IConfig standaloneConfig = m_config.Source.Configs["StandAlone"];
  310. if (standaloneConfig != null)
  311. {
  312. m_configSettings.StandaloneAuthenticate = standaloneConfig.GetBoolean("accounts_authenticate", true);
  313. m_configSettings.StandaloneWelcomeMessage = standaloneConfig.GetString("welcome_message");
  314. m_configSettings.StandaloneInventoryPlugin = standaloneConfig.GetString("inventory_plugin");
  315. m_configSettings.StandaloneInventorySource = standaloneConfig.GetString("inventory_source");
  316. m_configSettings.StandaloneUserPlugin = standaloneConfig.GetString("userDatabase_plugin");
  317. m_configSettings.StandaloneUserSource = standaloneConfig.GetString("user_source");
  318. m_configSettings.LibrariesXMLFile = standaloneConfig.GetString("LibrariesXMLFile");
  319. }
  320. m_networkServersInfo.loadFromConfiguration(m_config.Source);
  321. }
  322. }
  323. }