ConfigurationLoader.cs 17 KB

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