HGWorldMapModule.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenMetaverse.StructuredData;
  34. using Mono.Addins;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.CoreModules.World.WorldMap;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  41. namespace OpenSim.Region.CoreModules.Hypergrid
  42. {
  43. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGWorldMapModule")]
  44. public class HGWorldMapModule : WorldMapModule
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. // Remember the map area that each client has been exposed to in this region
  48. private Dictionary<UUID, List<MapBlockData>> m_SeenMapBlocks = new Dictionary<UUID, List<MapBlockData>>();
  49. private string m_MapImageServerURL = string.Empty;
  50. private IUserManagement m_UserManagement;
  51. #region INonSharedRegionModule Members
  52. public override void Initialise(IConfigSource source)
  53. {
  54. string[] configSections = new string[] { "Map", "Startup" };
  55. if (Util.GetConfigVarFromSections<string>(
  56. source, "WorldMapModule", configSections, "WorldMap") == "HGWorldMap")
  57. {
  58. m_Enabled = true;
  59. m_MapImageServerURL = Util.GetConfigVarFromSections<string>(source, "MapTileURL", new string[] {"LoginService", "HGWorldMap", "SimulatorFeatures"});
  60. if (!string.IsNullOrEmpty(m_MapImageServerURL))
  61. {
  62. m_MapImageServerURL = m_MapImageServerURL.Trim();
  63. if (!m_MapImageServerURL.EndsWith("/"))
  64. m_MapImageServerURL = m_MapImageServerURL + "/";
  65. }
  66. expireBlackListTime = (double)Util.GetConfigVarFromSections<int>(source, "BlacklistTimeout", configSections, 10 * 60);
  67. m_exportPrintScale =
  68. Util.GetConfigVarFromSections<bool>(source, "ExportMapAddScale", configSections, m_exportPrintScale);
  69. m_exportPrintRegionName =
  70. Util.GetConfigVarFromSections<bool>(source, "ExportMapAddRegionName", configSections, m_exportPrintRegionName);
  71. m_showNPCs =
  72. Util.GetConfigVarFromSections<bool>(source, "ShowNPCs", configSections, m_showNPCs);
  73. }
  74. }
  75. public override void AddRegion(Scene scene)
  76. {
  77. if (!m_Enabled)
  78. return;
  79. base.AddRegion(scene);
  80. scene.EventManager.OnClientClosed += EventManager_OnClientClosed;
  81. }
  82. public override void RegionLoaded(Scene scene)
  83. {
  84. if (!m_Enabled)
  85. return;
  86. base.RegionLoaded(scene);
  87. ISimulatorFeaturesModule featuresModule = m_scene.RequestModuleInterface<ISimulatorFeaturesModule>();
  88. if (featuresModule != null)
  89. featuresModule.OnSimulatorFeaturesRequest += OnSimulatorFeaturesRequest;
  90. m_UserManagement = m_scene.RequestModuleInterface<IUserManagement>();
  91. }
  92. public override void RemoveRegion(Scene scene)
  93. {
  94. if (!m_Enabled)
  95. return;
  96. base.RemoveRegion(scene);
  97. scene.EventManager.OnClientClosed -= EventManager_OnClientClosed;
  98. }
  99. public override string Name
  100. {
  101. get { return "HGWorldMap"; }
  102. }
  103. #endregion
  104. void EventManager_OnClientClosed(UUID clientID, Scene scene)
  105. {
  106. ScenePresence sp = scene.GetScenePresence(clientID);
  107. if (sp != null)
  108. {
  109. if (m_SeenMapBlocks.ContainsKey(clientID))
  110. {
  111. List<MapBlockData> mapBlocks = m_SeenMapBlocks[clientID];
  112. foreach (MapBlockData b in mapBlocks)
  113. {
  114. b.Name = string.Empty;
  115. // Set 'simulator is offline'. We need this because the viewer ignores SimAccess.Unknown (255)
  116. b.Access = (byte)SimAccess.Down;
  117. }
  118. m_log.DebugFormat("[HG MAP]: Resetting {0} blocks", mapBlocks.Count);
  119. sp.ControllingClient.SendMapBlock(mapBlocks, 0);
  120. m_SeenMapBlocks.Remove(clientID);
  121. }
  122. }
  123. }
  124. protected override List<MapBlockData> GetAndSendBlocksInternal(IClientAPI remoteClient, int minX, int minY, int maxX, int maxY, uint flag)
  125. {
  126. List<MapBlockData> mapBlocks = base.GetAndSendBlocksInternal(remoteClient, minX, minY, maxX, maxY, flag);
  127. lock (m_SeenMapBlocks)
  128. {
  129. if (!m_SeenMapBlocks.ContainsKey(remoteClient.AgentId))
  130. {
  131. m_SeenMapBlocks.Add(remoteClient.AgentId, mapBlocks);
  132. }
  133. else
  134. {
  135. List<MapBlockData> seen = m_SeenMapBlocks[remoteClient.AgentId];
  136. List<MapBlockData> newBlocks = new List<MapBlockData>();
  137. foreach (MapBlockData b in mapBlocks)
  138. if (seen.Find(delegate(MapBlockData bdata) { return bdata.X == b.X && bdata.Y == b.Y; }) == null)
  139. newBlocks.Add(b);
  140. seen.AddRange(newBlocks);
  141. }
  142. }
  143. return mapBlocks;
  144. }
  145. private void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features)
  146. {
  147. if (m_UserManagement != null && !string.IsNullOrEmpty(m_MapImageServerURL) && !m_UserManagement.IsLocalGridUser(agentID))
  148. {
  149. OSD extras = new OSDMap();
  150. if (features.ContainsKey("OpenSimExtras"))
  151. extras = features["OpenSimExtras"];
  152. else
  153. features["OpenSimExtras"] = extras;
  154. ((OSDMap)extras)["map-server-url"] = m_MapImageServerURL;
  155. }
  156. }
  157. }
  158. class MapArea
  159. {
  160. public int minX;
  161. public int minY;
  162. public int maxX;
  163. public int maxY;
  164. public MapArea(int mix, int miy, int max, int may)
  165. {
  166. minX = mix;
  167. minY = miy;
  168. maxX = max;
  169. maxY = may;
  170. }
  171. public void Print()
  172. {
  173. Console.WriteLine(String.Format(" --> Area is minX={0} minY={1} minY={2} maxY={3}", minX, minY, maxY, maxY));
  174. }
  175. }
  176. }