LoadRegionsPlugin.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.Reflection;
  30. using System.Threading;
  31. using log4net;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.RegionLoader.Filesystem;
  34. using OpenSim.Framework.RegionLoader.Web;
  35. using OpenSim.Region.CoreModules.Agent.AssetTransaction;
  36. using OpenSim.Region.CoreModules.Avatar.InstantMessage;
  37. using OpenSim.Region.CoreModules.Scripting.DynamicTexture;
  38. using OpenSim.Region.CoreModules.Scripting.LoadImageURL;
  39. using OpenSim.Region.CoreModules.Scripting.XMLRPC;
  40. namespace OpenSim.ApplicationPlugins.LoadRegions
  41. {
  42. public class LoadRegionsPlugin : IApplicationPlugin, IRegionCreator
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. public event NewRegionCreated OnNewRegionCreated;
  46. private NewRegionCreated m_newRegionCreatedHandler;
  47. #region IApplicationPlugin Members
  48. // TODO: required by IPlugin, but likely not at all right
  49. private string m_name = "LoadRegionsPlugin";
  50. private string m_version = "0.0";
  51. public string Version
  52. {
  53. get { return m_version; }
  54. }
  55. public string Name
  56. {
  57. get { return m_name; }
  58. }
  59. protected OpenSimBase m_openSim;
  60. public void Initialise()
  61. {
  62. m_log.Error("[LOADREGIONS]: " + Name + " cannot be default-initialized!");
  63. throw new PluginNotInitialisedException(Name);
  64. }
  65. public void Initialise(OpenSimBase openSim)
  66. {
  67. m_openSim = openSim;
  68. m_openSim.ApplicationRegistry.RegisterInterface<IRegionCreator>(this);
  69. }
  70. public void PostInitialise()
  71. {
  72. //m_log.Info("[LOADREGIONS]: Load Regions addin being initialised");
  73. IRegionLoader regionLoader;
  74. if (m_openSim.ConfigSource.Source.Configs["Startup"].GetString("region_info_source", "filesystem") == "filesystem")
  75. {
  76. m_log.Info("[LOADREGIONS]: Loading region configurations from filesystem");
  77. regionLoader = new RegionLoaderFileSystem();
  78. }
  79. else
  80. {
  81. m_log.Info("[LOADREGIONSPLUGIN]: Loading region configurations from web");
  82. regionLoader = new RegionLoaderWebServer();
  83. }
  84. m_log.Info("[LOADREGIONSPLUGIN]: Loading region configurations...");
  85. regionLoader.SetIniConfigSource(m_openSim.ConfigSource.Source);
  86. RegionInfo[] regionsToLoad = regionLoader.LoadRegions();
  87. m_log.Info("[LOADREGIONSPLUGIN]: Loading specific shared modules...");
  88. m_log.Info("[LOADREGIONSPLUGIN]: DynamicTextureModule...");
  89. m_openSim.ModuleLoader.LoadDefaultSharedModule(new DynamicTextureModule());
  90. m_log.Info("[LOADREGIONSPLUGIN]: InstantMessageModule...");
  91. m_openSim.ModuleLoader.LoadDefaultSharedModule(new InstantMessageModule());
  92. m_log.Info("[LOADREGIONSPLUGIN]: LoadImageURLModule...");
  93. m_openSim.ModuleLoader.LoadDefaultSharedModule(new LoadImageURLModule());
  94. m_log.Info("[LOADREGIONSPLUGIN]: XMLRPCModule...");
  95. m_openSim.ModuleLoader.LoadDefaultSharedModule(new XMLRPCModule());
  96. m_log.Info("[LOADREGIONSPLUGIN]: AssetTransactionModule...");
  97. m_openSim.ModuleLoader.LoadDefaultSharedModule(new AssetTransactionModule());
  98. m_log.Info("[LOADREGIONSPLUGIN]: Done.");
  99. if (!CheckRegionsForSanity(regionsToLoad))
  100. {
  101. m_log.Error("[LOADREGIONS]: Halting startup due to conflicts in region configurations");
  102. Environment.Exit(1);
  103. }
  104. for (int i = 0; i < regionsToLoad.Length; i++)
  105. {
  106. IScene scene;
  107. m_log.Debug("[LOADREGIONS]: Creating Region: " + regionsToLoad[i].RegionName + " (ThreadID: " +
  108. Thread.CurrentThread.ManagedThreadId.ToString() +
  109. ")");
  110. m_openSim.CreateRegion(regionsToLoad[i], true, out scene);
  111. if (scene != null)
  112. {
  113. m_newRegionCreatedHandler = OnNewRegionCreated;
  114. if (m_newRegionCreatedHandler != null)
  115. {
  116. m_newRegionCreatedHandler(scene);
  117. }
  118. }
  119. }
  120. m_openSim.ModuleLoader.PostInitialise();
  121. m_openSim.ModuleLoader.ClearCache();
  122. }
  123. public void Dispose()
  124. {
  125. }
  126. #endregion
  127. /// <summary>
  128. /// Check that region configuration information makes sense.
  129. /// </summary>
  130. /// <param name="regions"></param>
  131. /// <returns>True if we're sane, false if we're insane</returns>
  132. private bool CheckRegionsForSanity(RegionInfo[] regions)
  133. {
  134. if (regions.Length <= 1)
  135. return true;
  136. for (int i = 0; i < regions.Length - 1; i++)
  137. {
  138. for (int j = i + 1; j < regions.Length; j++)
  139. {
  140. if (regions[i].RegionID == regions[j].RegionID)
  141. {
  142. m_log.ErrorFormat(
  143. "[LOADREGIONS]: Regions {0} and {1} have the same UUID {2}",
  144. regions[i].RegionName, regions[j].RegionName, regions[i].RegionID);
  145. return false;
  146. }
  147. else if (
  148. regions[i].RegionLocX == regions[j].RegionLocX && regions[i].RegionLocY == regions[j].RegionLocY)
  149. {
  150. m_log.ErrorFormat(
  151. "[LOADREGIONS]: Regions {0} and {1} have the same grid location ({2}, {3})",
  152. regions[i].RegionName, regions[j].RegionName, regions[i].RegionLocX, regions[i].RegionLocY);
  153. return false;
  154. }
  155. else if (regions[i].InternalEndPoint.Port == regions[j].InternalEndPoint.Port)
  156. {
  157. m_log.ErrorFormat(
  158. "[LOADREGIONS]: Regions {0} and {1} have the same internal IP port {2}",
  159. regions[i].RegionName, regions[j].RegionName, regions[i].InternalEndPoint.Port);
  160. return false;
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. public void LoadRegionFromConfig(OpenSimBase openSim, ulong regionhandle)
  167. {
  168. m_log.Info("[LOADREGIONS]: Load Regions addin being initialised");
  169. IRegionLoader regionLoader;
  170. if (openSim.ConfigSource.Source.Configs["Startup"].GetString("region_info_source", "filesystem") == "filesystem")
  171. {
  172. m_log.Info("[LOADREGIONS]: Loading Region Info from filesystem");
  173. regionLoader = new RegionLoaderFileSystem();
  174. }
  175. else
  176. {
  177. m_log.Info("[LOADREGIONS]: Loading Region Info from web");
  178. regionLoader = new RegionLoaderWebServer();
  179. }
  180. regionLoader.SetIniConfigSource(openSim.ConfigSource.Source);
  181. RegionInfo[] regionsToLoad = regionLoader.LoadRegions();
  182. for (int i = 0; i < regionsToLoad.Length; i++)
  183. {
  184. if (regionhandle == regionsToLoad[i].RegionHandle)
  185. {
  186. IScene scene;
  187. m_log.Debug("[LOADREGIONS]: Creating Region: " + regionsToLoad[i].RegionName + " (ThreadID: " +
  188. Thread.CurrentThread.ManagedThreadId.ToString() + ")");
  189. openSim.CreateRegion(regionsToLoad[i], true, out scene);
  190. }
  191. }
  192. }
  193. }
  194. }