MapSearchModule.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. IWorldMapModule m_WorldMap;
  50. IWorldMapModule WorldMap
  51. {
  52. get
  53. {
  54. if (m_WorldMap == null)
  55. m_WorldMap = m_scene.RequestModuleInterface<IWorldMapModule>();
  56. return m_WorldMap;
  57. }
  58. }
  59. #region ISharedRegionModule Members
  60. public void Initialise(IConfigSource source)
  61. {
  62. }
  63. public void AddRegion(Scene scene)
  64. {
  65. if (m_scene == null)
  66. {
  67. m_scene = scene;
  68. }
  69. m_scenes.Add(scene);
  70. scene.EventManager.OnNewClient += OnNewClient;
  71. m_Clients = new List<UUID>();
  72. }
  73. public void RemoveRegion(Scene scene)
  74. {
  75. m_scenes.Remove(scene);
  76. if (m_scene == scene && m_scenes.Count > 0)
  77. m_scene = m_scenes[0];
  78. scene.EventManager.OnNewClient -= OnNewClient;
  79. }
  80. public void PostInitialise()
  81. {
  82. }
  83. public void Close()
  84. {
  85. m_scene = null;
  86. m_scenes.Clear();
  87. }
  88. public string Name
  89. {
  90. get { return "MapSearchModule"; }
  91. }
  92. public Type ReplaceableInterface
  93. {
  94. get { return null; }
  95. }
  96. public void RegionLoaded(Scene scene)
  97. {
  98. }
  99. #endregion
  100. private void OnNewClient(IClientAPI client)
  101. {
  102. client.OnMapNameRequest += OnMapNameRequestHandler;
  103. }
  104. private void OnMapNameRequestHandler(IClientAPI remoteClient, string mapName, uint flags)
  105. {
  106. lock (m_Clients)
  107. {
  108. if (m_Clients.Contains(remoteClient.AgentId))
  109. return;
  110. m_Clients.Add(remoteClient.AgentId);
  111. }
  112. OnMapNameRequest(remoteClient, mapName, flags);
  113. }
  114. private void OnMapNameRequest(IClientAPI remoteClient, string mapName, uint flags)
  115. {
  116. Util.FireAndForget(x =>
  117. {
  118. try
  119. {
  120. List<MapBlockData> blocks = new List<MapBlockData>();
  121. if (mapName.Length < 3 || (mapName.EndsWith("#") && mapName.Length < 4))
  122. {
  123. // final block, closing the search result
  124. AddFinalBlock(blocks,mapName);
  125. // flags are agent flags sent from the viewer.
  126. // they have different values depending on different viewers, apparently
  127. remoteClient.SendMapBlock(blocks, flags);
  128. remoteClient.SendAlertMessage("Use a search string with at least 3 characters");
  129. return;
  130. }
  131. //m_log.DebugFormat("MAP NAME=({0})", mapName);
  132. // Hack to get around the fact that ll V3 now drops the port from the
  133. // map name. See https://jira.secondlife.com/browse/VWR-28570
  134. //
  135. // Caller, use this magic form instead:
  136. // secondlife://http|!!mygrid.com|8002|Region+Name/128/128
  137. // or url encode if possible.
  138. // the hacks we do with this viewer...
  139. //
  140. bool needOriginalName = false;
  141. string mapNameOrig = mapName;
  142. if (mapName.Contains("|"))
  143. {
  144. mapName = mapName.Replace('|', ':');
  145. needOriginalName = true;
  146. }
  147. if (mapName.Contains("+"))
  148. {
  149. mapName = mapName.Replace('+', ' ');
  150. needOriginalName = true;
  151. }
  152. if (mapName.Contains("!"))
  153. {
  154. mapName = mapName.Replace('!', '/');
  155. needOriginalName = true;
  156. }
  157. if (mapName.Contains("."))
  158. needOriginalName = true;
  159. // try to fetch from GridServer
  160. List<GridRegion> regionInfos = m_scene.GridService.GetRegionsByName(m_scene.RegionInfo.ScopeID, mapName, 20);
  161. // if (regionInfos.Count == 0)
  162. // remoteClient.SendAlertMessage("Hyperlink could not be established.");
  163. //m_log.DebugFormat("[MAPSEARCHMODULE]: search {0} returned {1} regions", mapName, regionInfos.Count);
  164. MapBlockData data;
  165. if (regionInfos != null && regionInfos.Count > 0)
  166. {
  167. foreach (GridRegion info in regionInfos)
  168. {
  169. data = new MapBlockData();
  170. data.Agents = 0;
  171. data.Access = info.Access;
  172. MapBlockData block = new MapBlockData();
  173. WorldMap.MapBlockFromGridRegion(block, info, flags);
  174. if (flags == 2 && regionInfos.Count == 1 && needOriginalName)
  175. block.Name = mapNameOrig;
  176. blocks.Add(block);
  177. }
  178. }
  179. // final block, closing the search result
  180. AddFinalBlock(blocks,mapNameOrig);
  181. // flags are agent flags sent from the viewer.
  182. // they have different values depending on different viewers, apparently
  183. remoteClient.SendMapBlock(blocks, flags);
  184. // send extra user messages for V3
  185. // because the UI is very confusing
  186. // while we don't fix the hard-coded urls
  187. if (flags == 2)
  188. {
  189. if (regionInfos == null || regionInfos.Count == 0)
  190. remoteClient.SendAgentAlertMessage("No regions found with that name.", true);
  191. // else if (regionInfos.Count == 1)
  192. // remoteClient.SendAgentAlertMessage("Region found!", false);
  193. }
  194. }
  195. finally
  196. {
  197. lock (m_Clients)
  198. m_Clients.Remove(remoteClient.AgentId);
  199. }
  200. });
  201. }
  202. private void AddFinalBlock(List<MapBlockData> blocks,string name)
  203. {
  204. // final block, closing the search result
  205. MapBlockData data = new MapBlockData();
  206. data.Agents = 0;
  207. data.Access = (byte)SimAccess.NonExistent;
  208. data.MapImageId = UUID.Zero;
  209. data.Name = name;
  210. data.RegionFlags = 0;
  211. data.WaterHeight = 0; // not used
  212. data.X = 0;
  213. data.Y = 0;
  214. blocks.Add(data);
  215. }
  216. // private Scene GetClientScene(IClientAPI client)
  217. // {
  218. // foreach (Scene s in m_scenes)
  219. // {
  220. // if (client.Scene.RegionInfo.RegionHandle == s.RegionInfo.RegionHandle)
  221. // return s;
  222. // }
  223. // return m_scene;
  224. // }
  225. }
  226. }