ConfigurationLoader.cs 15 KB

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