ConfigurationLoader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.IO;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Xml;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. namespace OpenSim
  36. {
  37. public class ConfigurationLoader
  38. {
  39. protected ConfigSettings m_configSettings;
  40. protected OpenSimConfigSource m_config;
  41. protected NetworkServersInfo m_networkServersInfo;
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. public ConfigurationLoader()
  44. {
  45. }
  46. public OpenSimConfigSource LoadConfigSettings(IConfigSource configSource, out ConfigSettings configSettings, out NetworkServersInfo networkInfo)
  47. {
  48. m_configSettings = configSettings = new ConfigSettings();
  49. m_networkServersInfo = networkInfo = new NetworkServersInfo();
  50. bool iniFileExists = false;
  51. IConfig startupConfig = configSource.Configs["Startup"];
  52. string iniFileName = startupConfig.GetString("inifile", "OpenSim.ini");
  53. Application.iniFilePath = Path.Combine(Util.configDir(), iniFileName);
  54. string masterFileName = startupConfig.GetString("inimaster", "");
  55. string masterfilePath = Path.Combine(Util.configDir(), masterFileName);
  56. string iniDirName = startupConfig.GetString("inidirectory", "config");
  57. //string iniDirPath = Path.Combine(Util.configDir(), iniDirName);
  58. m_config = new OpenSimConfigSource();
  59. m_config.Source = new IniConfigSource();
  60. m_config.Source.Merge(DefaultConfig());
  61. m_log.Info("[CONFIG] Reading configuration settings");
  62. Uri configUri;
  63. String xmlPath = Path.Combine(Util.configDir(), "OpenSim.xml");
  64. //check for master .INI file (name passed in command line, no default), or XML over http
  65. if (masterFileName.Length > 0) // If a master file name is given ...
  66. {
  67. m_log.InfoFormat("[CONFIG] Reading config master file {0}", masterfilePath);
  68. bool isMasterUri = Uri.TryCreate(masterFileName, UriKind.Absolute, out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  69. if (!ReadConfig(masterFileName, masterfilePath, m_config, isMasterUri))
  70. {
  71. m_log.FatalFormat("[CONFIG] Could not open master config file {0}", masterfilePath);
  72. }
  73. }
  74. if (Directory.Exists(iniDirName))
  75. {
  76. m_log.InfoFormat("Searching folder: {0} , for config ini files", iniDirName);
  77. string[] fileEntries = Directory.GetFiles(iniDirName);
  78. foreach (string filePath in fileEntries)
  79. {
  80. if (Path.GetExtension(filePath).ToLower() == ".ini")
  81. {
  82. // m_log.InfoFormat("reading ini file < {0} > from config dir", filePath);
  83. ReadConfig(Path.GetFileName(filePath), filePath, m_config, false);
  84. }
  85. }
  86. }
  87. // Check for .INI file (either default or name passed on command
  88. // line) or XML config source over http
  89. bool isIniUri = Uri.TryCreate(iniFileName, UriKind.Absolute, out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  90. iniFileExists = ReadConfig(iniFileName, Application.iniFilePath, m_config, isIniUri);
  91. if (!iniFileExists)
  92. {
  93. // check for a xml config file
  94. if (File.Exists(xmlPath))
  95. {
  96. Application.iniFilePath = xmlPath;
  97. m_log.InfoFormat("Reading XML configuration from {0}", Path.GetFullPath(xmlPath));
  98. iniFileExists = true;
  99. m_config.Source = new XmlConfigSource();
  100. m_config.Source.Merge(new XmlConfigSource(Application.iniFilePath));
  101. }
  102. }
  103. m_config.Source.Merge(configSource);
  104. if (!iniFileExists)
  105. {
  106. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  107. if (!isIniUri)
  108. m_log.FatalFormat("[CONFIG] Tried to load {0}, ", Path.GetFullPath(Application.iniFilePath));
  109. else
  110. m_log.FatalFormat("[CONFIG] Tried to load from URI {0}, ", iniFileName);
  111. m_log.FatalFormat("[CONFIG] and XML source {0}", Path.GetFullPath(xmlPath));
  112. m_log.FatalFormat("[CONFIG] Did you copy the OpenSim.ini.example file to OpenSim.ini?");
  113. Environment.Exit(1);
  114. }
  115. ReadConfigSettings();
  116. return m_config;
  117. }
  118. /// <summary>
  119. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  120. /// </summary>
  121. /// <param name="iniName">The name of the ini to load</param>
  122. /// <param name="iniPath">Full path to the ini</param>
  123. /// <param name="m_config">The current configuration source</param>
  124. /// <param name="isUri">Boolean representing whether the ini source is a URI path over http or a file on the system</param>
  125. /// <returns></returns>
  126. private bool ReadConfig(string iniName, string iniPath, OpenSimConfigSource m_config, bool isUri)
  127. {
  128. bool success = false;
  129. if (!isUri && File.Exists(iniPath))
  130. {
  131. m_log.InfoFormat("[CONFIG] Reading configuration file {0}", Path.GetFullPath(iniPath));
  132. // From reading Nini's code, it seems that later merged keys replace earlier ones.
  133. m_config.Source.Merge(new IniConfigSource(iniPath));
  134. success = true;
  135. }
  136. else
  137. {
  138. if (isUri)
  139. {
  140. m_log.InfoFormat("[CONFIG] {0} is a http:// URI, fetching ...", iniName);
  141. // The ini file path is a http URI
  142. // Try to read it
  143. try
  144. {
  145. XmlReader r = XmlReader.Create(iniName);
  146. XmlConfigSource cs = new XmlConfigSource(r);
  147. m_config.Source.Merge(cs);
  148. success = true;
  149. m_log.InfoFormat("[CONFIG] Loaded config from {0}", iniName);
  150. }
  151. catch (Exception e)
  152. {
  153. m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniName);
  154. Environment.Exit(1);
  155. }
  156. }
  157. }
  158. return success;
  159. }
  160. /// <summary>
  161. /// Setup a default config values in case they aren't present in the ini file
  162. /// </summary>
  163. /// <returns></returns>
  164. public static IConfigSource DefaultConfig()
  165. {
  166. IConfigSource defaultConfig = new IniConfigSource();
  167. {
  168. IConfig config = defaultConfig.Configs["Startup"];
  169. if (null == config)
  170. config = defaultConfig.AddConfig("Startup");
  171. config.Set("region_info_source", "filesystem");
  172. config.Set("gridmode", false);
  173. config.Set("physics", "basicphysics");
  174. config.Set("meshing", "ZeroMesher");
  175. config.Set("physical_prim", true);
  176. config.Set("see_into_this_sim_from_neighbor", true);
  177. config.Set("serverside_object_permissions", false);
  178. config.Set("storage_plugin", "OpenSim.Data.SQLite.dll");
  179. config.Set("storage_connection_string", "URI=file:OpenSim.db,version=3");
  180. config.Set("storage_prim_inventories", true);
  181. config.Set("startup_console_commands_file", String.Empty);
  182. config.Set("shutdown_console_commands_file", String.Empty);
  183. config.Set("DefaultScriptEngine", "XEngine");
  184. config.Set("asset_database", "default");
  185. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  186. // life doesn't really work without this
  187. config.Set("EventQueue", true);
  188. }
  189. {
  190. IConfig config = defaultConfig.Configs["StandAlone"];
  191. if (null == config)
  192. config = defaultConfig.AddConfig("StandAlone");
  193. config.Set("accounts_authenticate", false);
  194. config.Set("welcome_message", "Welcome to OpenSimulator");
  195. config.Set("inventory_plugin", "OpenSim.Data.SQLite.dll");
  196. config.Set("inventory_source", "");
  197. config.Set("userDatabase_plugin", "OpenSim.Data.SQLite.dll");
  198. config.Set("user_source", "");
  199. config.Set("asset_plugin", "OpenSim.Data.SQLite.dll");
  200. config.Set("asset_source", "");
  201. config.Set("LibrariesXMLFile", string.Format(".{0}inventory{0}Libraries.xml", Path.DirectorySeparatorChar));
  202. config.Set("AssetSetsXMLFile", string.Format(".{0}assets{0}AssetSets.xml", Path.DirectorySeparatorChar));
  203. config.Set("dump_assets_to_file", false);
  204. }
  205. {
  206. IConfig config = defaultConfig.Configs["Network"];
  207. if (null == config)
  208. config = defaultConfig.AddConfig("Network");
  209. config.Set("default_location_x", 1000);
  210. config.Set("default_location_y", 1000);
  211. config.Set("http_listener_port", NetworkServersInfo.DefaultHttpListenerPort);
  212. config.Set("remoting_listener_port", NetworkServersInfo.RemotingListenerPort);
  213. config.Set("grid_server_url", "http://127.0.0.1:" + GridConfig.DefaultHttpPort.ToString());
  214. config.Set("grid_send_key", "null");
  215. config.Set("grid_recv_key", "null");
  216. config.Set("user_server_url", "http://127.0.0.1:" + UserConfig.DefaultHttpPort.ToString());
  217. config.Set("user_send_key", "null");
  218. config.Set("user_recv_key", "null");
  219. config.Set("asset_server_url", "http://127.0.0.1:" + AssetConfig.DefaultHttpPort.ToString());
  220. config.Set("inventory_server_url", "http://127.0.0.1:" + InventoryConfig.DefaultHttpPort.ToString());
  221. config.Set("secure_inventory_server", "true");
  222. }
  223. return defaultConfig;
  224. }
  225. protected virtual void ReadConfigSettings()
  226. {
  227. IConfig startupConfig = m_config.Source.Configs["Startup"];
  228. if (startupConfig != null)
  229. {
  230. m_configSettings.Standalone = !startupConfig.GetBoolean("gridmode", false);
  231. m_configSettings.PhysicsEngine = startupConfig.GetString("physics");
  232. m_configSettings.MeshEngineName = startupConfig.GetString("meshing");
  233. m_configSettings.PhysicalPrim = startupConfig.GetBoolean("physical_prim", true);
  234. m_configSettings.See_into_region_from_neighbor = startupConfig.GetBoolean("see_into_this_sim_from_neighbor", true);
  235. m_configSettings.StorageDll = startupConfig.GetString("storage_plugin");
  236. if (m_configSettings.StorageDll == "OpenSim.DataStore.MonoSqlite.dll")
  237. {
  238. m_configSettings.StorageDll = "OpenSim.Data.SQLite.dll";
  239. m_log.Warn("WARNING: OpenSim.DataStore.MonoSqlite.dll is deprecated. Set storage_plugin to OpenSim.Data.SQLite.dll.");
  240. Thread.Sleep(3000);
  241. }
  242. m_configSettings.StorageConnectionString = startupConfig.GetString("storage_connection_string");
  243. m_configSettings.EstateConnectionString = startupConfig.GetString("estate_connection_string", m_configSettings.StorageConnectionString);
  244. m_configSettings.AssetStorage = startupConfig.GetString("asset_database");
  245. m_configSettings.AssetCache = startupConfig.GetString("AssetCache");
  246. m_configSettings.ClientstackDll = startupConfig.GetString("clientstack_plugin");
  247. }
  248. IConfig standaloneConfig = m_config.Source.Configs["StandAlone"];
  249. if (standaloneConfig != null)
  250. {
  251. m_configSettings.StandaloneAuthenticate = standaloneConfig.GetBoolean("accounts_authenticate", true);
  252. m_configSettings.StandaloneWelcomeMessage = standaloneConfig.GetString("welcome_message");
  253. m_configSettings.StandaloneInventoryPlugin = standaloneConfig.GetString("inventory_plugin");
  254. m_configSettings.StandaloneInventorySource = standaloneConfig.GetString("inventory_source");
  255. m_configSettings.StandaloneUserPlugin = standaloneConfig.GetString("userDatabase_plugin");
  256. m_configSettings.StandaloneUserSource = standaloneConfig.GetString("user_source");
  257. m_configSettings.StandaloneAssetPlugin = standaloneConfig.GetString("asset_plugin");
  258. m_configSettings.StandaloneAssetSource = standaloneConfig.GetString("asset_source");
  259. m_configSettings.LibrariesXMLFile = standaloneConfig.GetString("LibrariesXMLFile");
  260. m_configSettings.AssetSetsXMLFile = standaloneConfig.GetString("AssetSetsXMLFile");
  261. m_configSettings.DumpAssetsToFile = standaloneConfig.GetBoolean("dump_assets_to_file", false);
  262. }
  263. m_networkServersInfo.loadFromConfiguration(m_config.Source);
  264. }
  265. }
  266. }