ConfigurationLoader.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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}",
  228. 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 ...",
  235. iniPath);
  236. // The ini file path is a http URI
  237. // Try to read it
  238. //
  239. try
  240. {
  241. XmlReader r = XmlReader.Create(iniPath);
  242. XmlConfigSource cs = new XmlConfigSource(r);
  243. m_config.Source.Merge(cs);
  244. success = true;
  245. }
  246. catch (Exception e)
  247. {
  248. m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  249. Environment.Exit(1);
  250. }
  251. }
  252. return success;
  253. }
  254. /// <summary>
  255. /// Setup a default config values in case they aren't present in the ini file
  256. /// </summary>
  257. /// <returns>A Configuration source containing the default configuration</returns>
  258. private static IConfigSource DefaultConfig()
  259. {
  260. IConfigSource defaultConfig = new IniConfigSource();
  261. {
  262. IConfig config = defaultConfig.Configs["Startup"];
  263. if (null == config)
  264. config = defaultConfig.AddConfig("Startup");
  265. config.Set("region_info_source", "filesystem");
  266. config.Set("gridmode", false);
  267. config.Set("physics", "basicphysics");
  268. config.Set("meshing", "ZeroMesher");
  269. config.Set("physical_prim", true);
  270. config.Set("see_into_this_sim_from_neighbor", true);
  271. config.Set("serverside_object_permissions", false);
  272. config.Set("storage_plugin", "OpenSim.Data.SQLite.dll");
  273. config.Set("storage_connection_string", "URI=file:OpenSim.db,version=3");
  274. config.Set("storage_prim_inventories", true);
  275. config.Set("startup_console_commands_file", String.Empty);
  276. config.Set("shutdown_console_commands_file", String.Empty);
  277. config.Set("DefaultScriptEngine", "XEngine");
  278. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  279. // life doesn't really work without this
  280. config.Set("EventQueue", true);
  281. }
  282. {
  283. IConfig config = defaultConfig.Configs["StandAlone"];
  284. if (null == config)
  285. config = defaultConfig.AddConfig("StandAlone");
  286. config.Set("accounts_authenticate", true);
  287. config.Set("welcome_message", "Welcome to OpenSimulator");
  288. config.Set("inventory_plugin", "OpenSim.Data.SQLite.dll");
  289. config.Set("inventory_source", "");
  290. config.Set("userDatabase_plugin", "OpenSim.Data.SQLite.dll");
  291. config.Set("user_source", "");
  292. config.Set("LibrariesXMLFile", string.Format(".{0}inventory{0}Libraries.xml", Path.DirectorySeparatorChar));
  293. }
  294. {
  295. IConfig config = defaultConfig.Configs["Network"];
  296. if (null == config)
  297. config = defaultConfig.AddConfig("Network");
  298. config.Set("default_location_x", 1000);
  299. config.Set("default_location_y", 1000);
  300. config.Set("http_listener_port", ConfigSettings.DefaultRegionHttpPort);
  301. config.Set("remoting_listener_port", ConfigSettings.DefaultRegionRemotingPort);
  302. config.Set("grid_server_url", "http://127.0.0.1:" + ConfigSettings.DefaultGridServerHttpPort.ToString());
  303. config.Set("grid_send_key", "null");
  304. config.Set("grid_recv_key", "null");
  305. config.Set("user_server_url", "http://127.0.0.1:" + ConfigSettings.DefaultUserServerHttpPort.ToString());
  306. config.Set("user_send_key", "null");
  307. config.Set("user_recv_key", "null");
  308. config.Set("asset_server_url", "http://127.0.0.1:" + ConfigSettings.DefaultAssetServerHttpPort.ToString());
  309. config.Set("inventory_server_url", "http://127.0.0.1:" + ConfigSettings.DefaultInventoryServerHttpPort.ToString());
  310. config.Set("secure_inventory_server", "true");
  311. }
  312. return defaultConfig;
  313. }
  314. /// <summary>
  315. /// Read initial region settings from the ConfigSource
  316. /// </summary>
  317. protected virtual void ReadConfigSettings()
  318. {
  319. IConfig startupConfig = m_config.Source.Configs["Startup"];
  320. if (startupConfig != null)
  321. {
  322. m_configSettings.Standalone = !startupConfig.GetBoolean("gridmode", false);
  323. m_configSettings.PhysicsEngine = startupConfig.GetString("physics");
  324. m_configSettings.MeshEngineName = startupConfig.GetString("meshing");
  325. m_configSettings.PhysicalPrim = startupConfig.GetBoolean("physical_prim", true);
  326. m_configSettings.See_into_region_from_neighbor = startupConfig.GetBoolean("see_into_this_sim_from_neighbor", true);
  327. m_configSettings.StorageDll = startupConfig.GetString("storage_plugin");
  328. if (m_configSettings.StorageDll == "OpenSim.DataStore.MonoSqlite.dll")
  329. {
  330. m_configSettings.StorageDll = "OpenSim.Data.SQLite.dll";
  331. m_log.Warn("WARNING: OpenSim.DataStore.MonoSqlite.dll is deprecated. Set storage_plugin to OpenSim.Data.SQLite.dll.");
  332. Thread.Sleep(3000);
  333. }
  334. m_configSettings.StorageConnectionString
  335. = startupConfig.GetString("storage_connection_string");
  336. m_configSettings.EstateConnectionString
  337. = startupConfig.GetString("estate_connection_string", m_configSettings.StorageConnectionString);
  338. m_configSettings.ClientstackDll
  339. = startupConfig.GetString("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  340. }
  341. IConfig standaloneConfig = m_config.Source.Configs["StandAlone"];
  342. if (standaloneConfig != null)
  343. {
  344. m_configSettings.StandaloneAuthenticate = standaloneConfig.GetBoolean("accounts_authenticate", true);
  345. m_configSettings.StandaloneWelcomeMessage = standaloneConfig.GetString("welcome_message");
  346. m_configSettings.StandaloneInventoryPlugin = standaloneConfig.GetString("inventory_plugin");
  347. m_configSettings.StandaloneInventorySource = standaloneConfig.GetString("inventory_source");
  348. m_configSettings.StandaloneUserPlugin = standaloneConfig.GetString("userDatabase_plugin");
  349. m_configSettings.StandaloneUserSource = standaloneConfig.GetString("user_source");
  350. m_configSettings.LibrariesXMLFile = standaloneConfig.GetString("LibrariesXMLFile");
  351. }
  352. m_networkServersInfo.loadFromConfiguration(m_config.Source);
  353. }
  354. }
  355. }