ConfigurationLoader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. // Resolve relative paths with wildcards
  186. string chunkWithoutWildcards = file;
  187. string chunkWithWildcards = string.Empty;
  188. int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' });
  189. if (wildcardIndex != -1)
  190. {
  191. chunkWithoutWildcards = file.Substring(0, wildcardIndex);
  192. chunkWithWildcards = file.Substring(wildcardIndex);
  193. }
  194. string path = Path.Combine(basepath, chunkWithoutWildcards);
  195. path = Path.GetFullPath(path) + chunkWithWildcards;
  196. string[] paths = Util.Glob(path);
  197. foreach (string p in paths)
  198. {
  199. if (!sources.Contains(p))
  200. sources.Add(p);
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// Check if we can convert the string to a URI
  209. /// </summary>
  210. /// <param name="file">String uri to the remote resource</param>
  211. /// <returns>true if we can convert the string to a Uri object</returns>
  212. bool IsUri(string file)
  213. {
  214. Uri configUri;
  215. return Uri.TryCreate(file, UriKind.Absolute,
  216. out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  217. }
  218. /// <summary>
  219. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  220. /// </summary>
  221. /// <param name="iniPath">Full path to the ini</param>
  222. /// <returns></returns>
  223. private bool ReadConfig(string iniPath)
  224. {
  225. bool success = false;
  226. if (!IsUri(iniPath))
  227. {
  228. m_log.InfoFormat("[CONFIG]: Reading configuration file {0}", Path.GetFullPath(iniPath));
  229. m_config.Source.Merge(new IniConfigSource(iniPath));
  230. success = true;
  231. }
  232. else
  233. {
  234. m_log.InfoFormat("[CONFIG]: {0} is a http:// URI, fetching ...", iniPath);
  235. // The ini file path is a http URI
  236. // Try to read it
  237. try
  238. {
  239. XmlReader r = XmlReader.Create(iniPath);
  240. XmlConfigSource cs = new XmlConfigSource(r);
  241. m_config.Source.Merge(cs);
  242. success = true;
  243. }
  244. catch (Exception e)
  245. {
  246. m_log.FatalFormat("[CONFIG]: Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  247. Environment.Exit(1);
  248. }
  249. }
  250. return success;
  251. }
  252. /// <summary>
  253. /// Setup a default config values in case they aren't present in the ini file
  254. /// </summary>
  255. /// <returns>A Configuration source containing the default configuration</returns>
  256. private static IConfigSource DefaultConfig()
  257. {
  258. IConfigSource defaultConfig = new IniConfigSource();
  259. {
  260. IConfig config = defaultConfig.Configs["Startup"];
  261. if (null == config)
  262. config = defaultConfig.AddConfig("Startup");
  263. config.Set("region_info_source", "filesystem");
  264. config.Set("physics", "OpenDynamicsEngine");
  265. config.Set("meshing", "Meshmerizer");
  266. config.Set("physical_prim", true);
  267. config.Set("see_into_this_sim_from_neighbor", true);
  268. config.Set("serverside_object_permissions", false);
  269. config.Set("storage_plugin", "OpenSim.Data.SQLite.dll");
  270. config.Set("storage_connection_string", "URI=file:OpenSim.db,version=3");
  271. config.Set("storage_prim_inventories", true);
  272. config.Set("startup_console_commands_file", String.Empty);
  273. config.Set("shutdown_console_commands_file", String.Empty);
  274. config.Set("DefaultScriptEngine", "XEngine");
  275. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  276. // life doesn't really work without this
  277. config.Set("EventQueue", true);
  278. }
  279. {
  280. IConfig config = defaultConfig.Configs["Network"];
  281. if (null == config)
  282. config = defaultConfig.AddConfig("Network");
  283. config.Set("http_listener_port", ConfigSettings.DefaultRegionHttpPort);
  284. }
  285. return defaultConfig;
  286. }
  287. /// <summary>
  288. /// Read initial region settings from the ConfigSource
  289. /// </summary>
  290. protected virtual void ReadConfigSettings()
  291. {
  292. IConfig startupConfig = m_config.Source.Configs["Startup"];
  293. if (startupConfig != null)
  294. {
  295. m_configSettings.PhysicsEngine = startupConfig.GetString("physics");
  296. m_configSettings.MeshEngineName = startupConfig.GetString("meshing");
  297. m_configSettings.PhysicalPrim = startupConfig.GetBoolean("physical_prim", true);
  298. m_configSettings.See_into_region_from_neighbor = startupConfig.GetBoolean("see_into_this_sim_from_neighbor", true);
  299. m_configSettings.StorageDll = startupConfig.GetString("storage_plugin");
  300. m_configSettings.StorageConnectionString
  301. = startupConfig.GetString("storage_connection_string");
  302. m_configSettings.EstateConnectionString
  303. = startupConfig.GetString("estate_connection_string", m_configSettings.StorageConnectionString);
  304. m_configSettings.ClientstackDll
  305. = startupConfig.GetString("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  306. }
  307. IConfig standaloneConfig = m_config.Source.Configs["StandAlone"];
  308. if (standaloneConfig != null)
  309. {
  310. m_configSettings.StandaloneAuthenticate = standaloneConfig.GetBoolean("accounts_authenticate", true);
  311. m_configSettings.StandaloneWelcomeMessage = standaloneConfig.GetString("welcome_message");
  312. m_configSettings.StandaloneInventoryPlugin = standaloneConfig.GetString("inventory_plugin");
  313. m_configSettings.StandaloneInventorySource = standaloneConfig.GetString("inventory_source");
  314. m_configSettings.StandaloneUserPlugin = standaloneConfig.GetString("userDatabase_plugin");
  315. m_configSettings.StandaloneUserSource = standaloneConfig.GetString("user_source");
  316. m_configSettings.LibrariesXMLFile = standaloneConfig.GetString("LibrariesXMLFile");
  317. }
  318. m_networkServersInfo.loadFromConfiguration(m_config.Source);
  319. }
  320. }
  321. }