MapSearchModule.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Mono.Addins;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Services.Interfaces;
  38. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  39. namespace OpenSim.Region.CoreModules.World.WorldMap
  40. {
  41. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "MapSearchModule")]
  42. public class MapSearchModule : ISharedRegionModule
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. Scene m_scene = null; // only need one for communication with GridService
  47. List<Scene> m_scenes = new List<Scene>();
  48. List<UUID> m_Clients;
  49. #region ISharedRegionModule Members
  50. public void Initialise(IConfigSource source)
  51. {
  52. }
  53. public void AddRegion(Scene scene)
  54. {
  55. if (m_scene == null)
  56. {
  57. m_scene = scene;
  58. }
  59. m_scenes.Add(scene);
  60. scene.EventManager.OnNewClient += OnNewClient;
  61. m_Clients = new List<UUID>();
  62. }
  63. public void RemoveRegion(Scene scene)
  64. {
  65. m_scenes.Remove(scene);
  66. if (m_scene == scene && m_scenes.Count > 0)
  67. m_scene = m_scenes[0];
  68. scene.EventManager.OnNewClient -= OnNewClient;
  69. }
  70. public void PostInitialise()
  71. {
  72. }
  73. public void Close()
  74. {
  75. m_scene = null;
  76. m_scenes.Clear();
  77. }
  78. public string Name
  79. {
  80. get { return "MapSearchModule"; }
  81. }
  82. public Type ReplaceableInterface
  83. {
  84. get { return null; }
  85. }
  86. public void RegionLoaded(Scene scene)
  87. {
  88. }
  89. #endregion
  90. private void OnNewClient(IClientAPI client)
  91. {
  92. client.OnMapNameRequest += OnMapNameRequestHandler;
  93. }
  94. private void OnMapNameRequestHandler(IClientAPI remoteClient, string mapName, uint flags)
  95. {
  96. lock (m_Clients)
  97. {
  98. if (m_Clients.Contains(remoteClient.AgentId))
  99. return;
  100. m_Clients.Add(remoteClient.AgentId);
  101. }
  102. try
  103. {
  104. OnMapNameRequest(remoteClient, mapName, flags);
  105. }
  106. finally
  107. {
  108. lock (m_Clients)
  109. m_Clients.Remove(remoteClient.AgentId);
  110. }
  111. }
  112. private void OnMapNameRequest(IClientAPI remoteClient, string mapName, uint flags)
  113. {
  114. List<MapBlockData> blocks = new List<MapBlockData>();
  115. MapBlockData data;
  116. if (mapName.Length < 3 || (mapName.EndsWith("#") && mapName.Length < 4))
  117. {
  118. // final block, closing the search result
  119. AddFinalBlock(blocks);
  120. // flags are agent flags sent from the viewer.
  121. // they have different values depending on different viewers, apparently
  122. remoteClient.SendMapBlock(blocks, flags);
  123. remoteClient.SendAlertMessage("Use a search string with at least 3 characters");
  124. return;
  125. }
  126. //m_log.DebugFormat("MAP NAME=({0})", mapName);
  127. // Hack to get around the fact that ll V3 now drops the port from the
  128. // map name. See https://jira.secondlife.com/browse/VWR-28570
  129. //
  130. // Caller, use this magic form instead:
  131. // secondlife://http|!!mygrid.com|8002|Region+Name/128/128
  132. // or url encode if possible.
  133. // the hacks we do with this viewer...
  134. //
  135. string mapNameOrig = mapName;
  136. if (mapName.Contains("|"))
  137. mapName = mapName.Replace('|', ':');
  138. if (mapName.Contains("+"))
  139. mapName = mapName.Replace('+', ' ');
  140. if (mapName.Contains("!"))
  141. mapName = mapName.Replace('!', '/');
  142. // try to fetch from GridServer
  143. List<GridRegion> regionInfos = m_scene.GridService.GetRegionsByName(m_scene.RegionInfo.ScopeID, mapName, 20);
  144. m_log.DebugFormat("[MAPSEARCHMODULE]: search {0} returned {1} regions. Flags={2}", mapName, regionInfos.Count, flags);
  145. if (regionInfos.Count > 0)
  146. {
  147. foreach (GridRegion info in regionInfos)
  148. {
  149. data = new MapBlockData();
  150. data.Agents = 0;
  151. data.Access = info.Access;
  152. if (flags == 2) // V2 sends this
  153. data.MapImageId = UUID.Zero;
  154. else
  155. data.MapImageId = info.TerrainImage;
  156. // ugh! V2-3 is very sensitive about the result being
  157. // exactly the same as the requested name
  158. if (regionInfos.Count == 1 && mapNameOrig.Contains("|") || mapNameOrig.Contains("+"))
  159. data.Name = mapNameOrig;
  160. else
  161. data.Name = info.RegionName;
  162. data.RegionFlags = 0; // TODO not used?
  163. data.WaterHeight = 0; // not used
  164. data.X = (ushort)(info.RegionLocX / Constants.RegionSize);
  165. data.Y = (ushort)(info.RegionLocY / Constants.RegionSize);
  166. blocks.Add(data);
  167. }
  168. }
  169. // final block, closing the search result
  170. AddFinalBlock(blocks);
  171. // flags are agent flags sent from the viewer.
  172. // they have different values depending on different viewers, apparently
  173. remoteClient.SendMapBlock(blocks, flags);
  174. // send extra user messages for V3
  175. // because the UI is very confusing
  176. // while we don't fix the hard-coded urls
  177. if (flags == 2)
  178. {
  179. if (regionInfos.Count == 0)
  180. remoteClient.SendAlertMessage("No regions found with that name.");
  181. else if (regionInfos.Count == 1)
  182. remoteClient.SendAlertMessage("Region found!");
  183. }
  184. }
  185. private void AddFinalBlock(List<MapBlockData> blocks)
  186. {
  187. // final block, closing the search result
  188. MapBlockData data = new MapBlockData();
  189. data.Agents = 0;
  190. data.Access = 255;
  191. data.MapImageId = UUID.Zero;
  192. data.Name = "";
  193. data.RegionFlags = 0;
  194. data.WaterHeight = 0; // not used
  195. data.X = 0;
  196. data.Y = 0;
  197. blocks.Add(data);
  198. }
  199. // private Scene GetClientScene(IClientAPI client)
  200. // {
  201. // foreach (Scene s in m_scenes)
  202. // {
  203. // if (client.Scene.RegionInfo.RegionHandle == s.RegionInfo.RegionHandle)
  204. // return s;
  205. // }
  206. // return m_scene;
  207. // }
  208. }
  209. }