ConfigurationLoader.cs 15 KB

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