MapSearchModule.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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;
  28. using System.Reflection;
  29. using System.Collections.Generic;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Environment.Interfaces;
  32. using OpenSim.Region.Environment.Scenes;
  33. using OpenMetaverse;
  34. using log4net;
  35. using Nini.Config;
  36. namespace OpenSim.Region.Environment.Modules.World.WorldMap
  37. {
  38. public class MapSearchModule : IRegionModule
  39. {
  40. private static readonly ILog m_log =
  41. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. Scene m_scene = null; // only need one for communication with GridService
  43. #region IRegionModule Members
  44. public void Initialise(Scene scene, IConfigSource source)
  45. {
  46. if (m_scene == null) m_scene = scene;
  47. scene.EventManager.OnNewClient += OnNewClient;
  48. }
  49. public void PostInitialise()
  50. {
  51. }
  52. public void Close()
  53. {
  54. m_scene = null;
  55. }
  56. public string Name
  57. {
  58. get { return "MapSearchModule"; }
  59. }
  60. public bool IsSharedModule
  61. {
  62. get { return true; }
  63. }
  64. #endregion
  65. private void OnNewClient(IClientAPI client)
  66. {
  67. client.OnMapNameRequest += OnMapNameRequest;
  68. }
  69. private void OnMapNameRequest(IClientAPI remoteClient, string mapName)
  70. {
  71. if (mapName.Length < 3)
  72. {
  73. remoteClient.SendAlertMessage("Use a search string with at least 3 characters");
  74. return;
  75. }
  76. // try to fetch from GridServer
  77. List<RegionInfo> regionInfos = m_scene.SceneGridService.RequestNamedRegions(mapName, 20);
  78. if (regionInfos == null)
  79. {
  80. m_log.Warn("[MAPSEARCHMODULE]: RequestNamedRegions returned null. Old gridserver?");
  81. // service wasn't available; maybe still an old GridServer. Try the old API, though it will return only one region
  82. regionInfos = new List<RegionInfo>();
  83. RegionInfo info = m_scene.SceneGridService.RequestClosestRegion(mapName);
  84. if (info != null) regionInfos.Add(info);
  85. }
  86. List<MapBlockData> blocks = new List<MapBlockData>();
  87. MapBlockData data;
  88. if (regionInfos.Count > 0)
  89. {
  90. foreach (RegionInfo info in regionInfos)
  91. {
  92. data = new MapBlockData();
  93. data.Agents = 0;
  94. data.Access = 21; // TODO what's this?
  95. data.MapImageId = info.RegionSettings.TerrainImageID;
  96. data.Name = info.RegionName;
  97. data.RegionFlags = 0; // TODO not used?
  98. data.WaterHeight = 0; // not used
  99. data.X = (ushort)info.RegionLocX;
  100. data.Y = (ushort)info.RegionLocY;
  101. blocks.Add(data);
  102. }
  103. }
  104. // final block, closing the search result
  105. data = new MapBlockData();
  106. data.Agents = 0;
  107. data.Access = 255;
  108. data.MapImageId = UUID.Zero;
  109. data.Name = mapName;
  110. data.RegionFlags = 0;
  111. data.WaterHeight = 0; // not used
  112. data.X = 0;
  113. data.Y = 0;
  114. blocks.Add(data);
  115. remoteClient.SendMapBlock(blocks, 0);
  116. }
  117. }
  118. }