ConfigurationLoader.cs 14 KB

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