LoadRegionsPlugin.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 OpenSim 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.Collections.Generic;
  28. using System.Reflection;
  29. using System.Threading;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.RegionLoader.Filesystem;
  34. using OpenSim.Framework.RegionLoader.Web;
  35. namespace OpenSim.ApplicationPlugins.LoadRegions
  36. {
  37. public class LoadRegionsPlugin : IApplicationPlugin
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. #region IApplicationPlugin Members
  41. // TODO: required by IPlugin, but likely not at all right
  42. string m_name = "LoadRegionsPlugin";
  43. string m_version = "0.0";
  44. public string Version { get { return m_version; } }
  45. public string Name { get { return m_name; } }
  46. public void Initialise()
  47. {
  48. m_log.Info("[LOADREGIONS]: " + Name + " cannot be default-initialized!");
  49. throw new PluginNotInitialisedException(Name);
  50. }
  51. public void Initialise(OpenSimBase openSim)
  52. {
  53. m_log.Info("[LOADREGIONS]: Load Regions addin being initialised");
  54. IRegionLoader regionLoader;
  55. if (openSim.ConfigSource.Source.Configs["Startup"].GetString("region_info_source", "filesystem") == "filesystem")
  56. {
  57. m_log.Info("[LOADREGIONS]: Loading Region Info from filesystem");
  58. regionLoader = new RegionLoaderFileSystem();
  59. }
  60. else
  61. {
  62. m_log.Info("[LOADREGIONSPLUGIN]: Loading Region Info from web");
  63. regionLoader = new RegionLoaderWebServer();
  64. }
  65. regionLoader.SetIniConfigSource(openSim.ConfigSource.Source);
  66. RegionInfo[] regionsToLoad = regionLoader.LoadRegions();
  67. openSim.ModuleLoader.LoadDefaultSharedModules();
  68. if (!CheckRegionsForSanity(regionsToLoad))
  69. {
  70. m_log.Error("[LOADREGIONS]: Halting startup due to conflicts in region configurations");
  71. System.Environment.Exit(1);
  72. }
  73. for (int i = 0; i < regionsToLoad.Length; i++)
  74. {
  75. m_log.Debug("[LOADREGIONS]: Creating Region: " + regionsToLoad[i].RegionName + " (ThreadID: " + Thread.CurrentThread.ManagedThreadId.ToString() +
  76. ")");
  77. openSim.CreateRegion(regionsToLoad[i], true);
  78. }
  79. openSim.ModuleLoader.PostInitialise();
  80. openSim.ModuleLoader.ClearCache();
  81. }
  82. public void Dispose()
  83. {
  84. }
  85. #endregion
  86. /// <summary>
  87. /// Check that region configuration information makes sense.
  88. /// </summary>
  89. /// <param name="regions"></param>
  90. /// <returns>True if we're sane, false if we're insane</returns>
  91. private bool CheckRegionsForSanity(RegionInfo[] regions)
  92. {
  93. if (regions.Length <= 0)
  94. return true;
  95. List<RegionInfo> checkedRegions = new List<RegionInfo>();
  96. checkedRegions.Add(regions[0]);
  97. for (int i = 1; i < regions.Length; i++)
  98. {
  99. RegionInfo region = regions[i];
  100. foreach (RegionInfo checkedRegion in checkedRegions)
  101. {
  102. if (region.RegionID == checkedRegion.RegionID)
  103. {
  104. m_log.ErrorFormat(
  105. "[LOADREGIONS]: Regions {0} and {1} have the same UUID {2}",
  106. region.RegionName, checkedRegion.RegionName, region.RegionID);
  107. return false;
  108. }
  109. else if (region.RegionLocX == checkedRegion.RegionLocX && region.RegionLocY == checkedRegion.RegionLocY)
  110. {
  111. m_log.ErrorFormat(
  112. "[LOADREGIONS]: Regions {0} and {1} have the same location {2} {3}",
  113. region.RegionName, checkedRegion.RegionName, region.RegionLocX, region.RegionLocY);
  114. return false;
  115. }
  116. else if (region.InternalEndPoint.Port == checkedRegion.InternalEndPoint.Port)
  117. {
  118. m_log.ErrorFormat(
  119. "[LOADREGIONS]: Regions {0} and {1} have the same internal IP port {2}",
  120. region.RegionName, checkedRegion.RegionName, region.InternalEndPoint.Port);
  121. return false;
  122. }
  123. }
  124. }
  125. return true;
  126. }
  127. public void LoadRegionFromConfig(OpenSimBase openSim, ulong regionhandle)
  128. {
  129. m_log.Info("[LOADREGIONS]: Load Regions addin being initialised");
  130. IRegionLoader regionLoader;
  131. if (openSim.ConfigSource.Source.Configs["Startup"].GetString("region_info_source", "filesystem") == "filesystem")
  132. {
  133. m_log.Info("[LOADREGIONS]: Loading Region Info from filesystem");
  134. regionLoader = new RegionLoaderFileSystem();
  135. }
  136. else
  137. {
  138. m_log.Info("[LOADREGIONS]: Loading Region Info from web");
  139. regionLoader = new RegionLoaderWebServer();
  140. }
  141. regionLoader.SetIniConfigSource(openSim.ConfigSource.Source);
  142. RegionInfo[] regionsToLoad = regionLoader.LoadRegions();
  143. for (int i = 0; i < regionsToLoad.Length; i++)
  144. {
  145. if (regionhandle == regionsToLoad[i].RegionHandle)
  146. {
  147. m_log.Debug("[LOADREGIONS]: Creating Region: " + regionsToLoad[i].RegionName + " (ThreadID: " +
  148. Thread.CurrentThread.ManagedThreadId.ToString() + ")");
  149. openSim.CreateRegion(regionsToLoad[i], true);
  150. }
  151. }
  152. }
  153. }
  154. }