LandSnapshot.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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.Collections.Generic;
  29. using System.Reflection;
  30. using System.Xml;
  31. using OpenMetaverse;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.DataSnapshot.Interfaces;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.Environment.Modules.World.Land;
  37. using OpenSim.Region.Environment.Scenes;
  38. using OpenMetaverse.Packets;
  39. using OpenSim.Framework.Communications.Cache;
  40. namespace OpenSim.Region.DataSnapshot.Providers
  41. {
  42. public class LandSnapshot : IDataSnapshotProvider
  43. {
  44. private Scene m_scene = null;
  45. private DataSnapshotManager m_parent = null;
  46. //private Dictionary<int, Land> m_landIndexed = new Dictionary<int, Land>();
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private bool m_stale = true;
  49. #region Dead code
  50. /*
  51. * David, I don't think we need this at all. When we do the snapshot, we can
  52. * simply look into the parcels that are marked for ShowDirectory -- see
  53. * conditional in RequestSnapshotData
  54. *
  55. //Revise this, look for more direct way of checking for change in land
  56. #region Client hooks
  57. public void OnNewClient(IClientAPI client)
  58. {
  59. //Land hooks
  60. client.OnParcelDivideRequest += ParcelSplitHook;
  61. client.OnParcelJoinRequest += ParcelSplitHook;
  62. client.OnParcelPropertiesUpdateRequest += ParcelPropsHook;
  63. }
  64. public void ParcelSplitHook(int west, int south, int east, int north, IClientAPI remote_client)
  65. {
  66. PrepareData();
  67. }
  68. public void ParcelPropsHook(ParcelPropertiesUpdatePacket packet, IClientAPI remote_client)
  69. {
  70. PrepareData();
  71. }
  72. #endregion
  73. public void PrepareData()
  74. {
  75. m_log.Info("[EXTERNALDATA]: Generating land data.");
  76. m_landIndexed.Clear();
  77. //Index sim land
  78. foreach (KeyValuePair<int, Land> curLand in m_scene.LandManager.landList)
  79. {
  80. //if ((curLand.Value.landData.landFlags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory)
  81. //{
  82. m_landIndexed.Add(curLand.Key, curLand.Value.Copy());
  83. //}
  84. }
  85. }
  86. public Dictionary<int,Land> IndexedLand {
  87. get { return m_landIndexed; }
  88. }
  89. */
  90. #endregion
  91. #region IDataSnapshotProvider members
  92. public void Initialize(Scene scene, DataSnapshotManager parent)
  93. {
  94. m_scene = scene;
  95. m_parent = parent;
  96. //Brought back from the dead for staleness checks.
  97. m_scene.EventManager.OnNewClient += OnNewClient;
  98. }
  99. public Scene GetParentScene
  100. {
  101. get { return m_scene; }
  102. }
  103. public XmlNode RequestSnapshotData(XmlDocument nodeFactory)
  104. {
  105. ILandChannel landChannel = m_scene.LandChannel;
  106. List<ILandObject> parcels = landChannel.AllParcels();
  107. XmlNode parent = nodeFactory.CreateNode(XmlNodeType.Element, "parceldata", "");
  108. if (parcels != null)
  109. {
  110. //foreach (KeyValuePair<int, Land> curParcel in m_landIndexed)
  111. foreach (ILandObject parcel_interface in parcels)
  112. {
  113. // Play it safe
  114. if (!(parcel_interface is LandObject))
  115. continue;
  116. LandObject land = (LandObject)parcel_interface;
  117. LandData parcel = land.landData;
  118. if (m_parent.ExposureLevel.Equals("all") ||
  119. (m_parent.ExposureLevel.Equals("minimum") &&
  120. (parcel.Flags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory))
  121. {
  122. //TODO: make better method of marshalling data from LandData to XmlNode
  123. XmlNode xmlparcel = nodeFactory.CreateNode(XmlNodeType.Element, "parcel", "");
  124. // Attributes of the parcel node
  125. XmlAttribute scripts_attr = nodeFactory.CreateAttribute("scripts");
  126. scripts_attr.Value = GetScriptsPermissions(parcel);
  127. XmlAttribute build_attr = nodeFactory.CreateAttribute("build");
  128. build_attr.Value = GetBuildPermissions(parcel);
  129. XmlAttribute public_attr = nodeFactory.CreateAttribute("public");
  130. public_attr.Value = GetPublicPermissions(parcel);
  131. // Check the category of the Parcel
  132. XmlAttribute category_attr = nodeFactory.CreateAttribute("category");
  133. category_attr.Value = ((int)parcel.Category).ToString();
  134. // Check if the parcel is for sale
  135. XmlAttribute forsale_attr = nodeFactory.CreateAttribute("forsale");
  136. forsale_attr.Value = CheckForSale(parcel);
  137. XmlAttribute sales_attr = nodeFactory.CreateAttribute("salesprice");
  138. sales_attr.Value = parcel.SalePrice.ToString();
  139. XmlAttribute directory_attr = nodeFactory.CreateAttribute("showinsearch");
  140. directory_attr.Value = GetShowInSearch(parcel);
  141. //XmlAttribute entities_attr = nodeFactory.CreateAttribute("entities");
  142. //entities_attr.Value = land.primsOverMe.Count.ToString();
  143. xmlparcel.Attributes.Append(directory_attr);
  144. xmlparcel.Attributes.Append(scripts_attr);
  145. xmlparcel.Attributes.Append(build_attr);
  146. xmlparcel.Attributes.Append(public_attr);
  147. xmlparcel.Attributes.Append(category_attr);
  148. xmlparcel.Attributes.Append(forsale_attr);
  149. xmlparcel.Attributes.Append(sales_attr);
  150. //xmlparcel.Attributes.Append(entities_attr);
  151. //name, description, area, and UUID
  152. XmlNode name = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  153. name.InnerText = parcel.Name;
  154. xmlparcel.AppendChild(name);
  155. XmlNode desc = nodeFactory.CreateNode(XmlNodeType.Element, "description", "");
  156. desc.InnerText = parcel.Description;
  157. xmlparcel.AppendChild(desc);
  158. XmlNode uuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  159. uuid.InnerText = parcel.GlobalID.ToString();
  160. xmlparcel.AppendChild(uuid);
  161. XmlNode area = nodeFactory.CreateNode(XmlNodeType.Element, "area", "");
  162. area.InnerText = parcel.Area.ToString();
  163. xmlparcel.AppendChild(area);
  164. //default location
  165. XmlNode tpLocation = nodeFactory.CreateNode(XmlNodeType.Element, "location", "");
  166. Vector3 loc = parcel.UserLocation;
  167. if (loc.Equals(Vector3.Zero)) // This test is moot at this point: the location is wrong by default
  168. loc = new Vector3((parcel.AABBMax.X + parcel.AABBMin.X) / 2, (parcel.AABBMax.Y + parcel.AABBMin.Y) / 2, (parcel.AABBMax.Z + parcel.AABBMin.Z) / 2);
  169. tpLocation.InnerText = loc.X.ToString() + "/" + loc.Y.ToString() + "/" + loc.Z.ToString();
  170. xmlparcel.AppendChild(tpLocation);
  171. XmlNode infouuid = nodeFactory.CreateNode(XmlNodeType.Element, "infouuid", "");
  172. uint x = (uint)loc.X, y = (uint)loc.Y;
  173. findPointInParcel(land, ref x, ref y); // find a suitable spot
  174. infouuid.InnerText = Util.BuildFakeParcelID(
  175. m_scene.RegionInfo.RegionHandle, x, y).ToString();
  176. xmlparcel.AppendChild(infouuid);
  177. XmlNode dwell = nodeFactory.CreateNode(XmlNodeType.Element, "dwell", "");
  178. dwell.InnerText = "0";
  179. xmlparcel.AppendChild(dwell);
  180. //TODO: figure how to figure out teleport system landData.landingType
  181. //land texture snapshot uuid
  182. if (parcel.SnapshotID != UUID.Zero)
  183. {
  184. XmlNode textureuuid = nodeFactory.CreateNode(XmlNodeType.Element, "image", "");
  185. textureuuid.InnerText = parcel.SnapshotID.ToString();
  186. xmlparcel.AppendChild(textureuuid);
  187. }
  188. //attached user and group
  189. if (parcel.GroupID != UUID.Zero)
  190. {
  191. XmlNode groupblock = nodeFactory.CreateNode(XmlNodeType.Element, "group", "");
  192. XmlNode groupuuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  193. groupuuid.InnerText = parcel.GroupID.ToString();
  194. groupblock.AppendChild(groupuuid);
  195. //No name yet, there's no way to get a group name since they don't exist yet.
  196. //TODO: When groups are supported, add the group handling code.
  197. xmlparcel.AppendChild(groupblock);
  198. }
  199. if (!parcel.IsGroupOwned)
  200. {
  201. XmlNode userblock = nodeFactory.CreateNode(XmlNodeType.Element, "owner", "");
  202. UUID userOwnerUUID = parcel.OwnerID;
  203. XmlNode useruuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  204. useruuid.InnerText = userOwnerUUID.ToString();
  205. userblock.AppendChild(useruuid);
  206. try
  207. {
  208. XmlNode username = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  209. CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(userOwnerUUID);
  210. username.InnerText = profile.UserProfile.FirstName + " " + profile.UserProfile.SurName;
  211. userblock.AppendChild(username);
  212. }
  213. catch (Exception)
  214. {
  215. //m_log.Info("[DATASNAPSHOT]: Cannot find owner name; ignoring this parcel");
  216. }
  217. xmlparcel.AppendChild(userblock);
  218. }
  219. //else
  220. //{
  221. // XmlAttribute type = (XmlAttribute)nodeFactory.CreateNode(XmlNodeType.Attribute, "type", "");
  222. // type.InnerText = "owner";
  223. // groupblock.Attributes.Append(type);
  224. //}
  225. parent.AppendChild(xmlparcel);
  226. }
  227. }
  228. //snap.AppendChild(parent);
  229. }
  230. this.Stale = false;
  231. return parent;
  232. }
  233. public String Name
  234. {
  235. get { return "LandSnapshot"; }
  236. }
  237. public bool Stale
  238. {
  239. get
  240. {
  241. return m_stale;
  242. }
  243. set
  244. {
  245. m_stale = value;
  246. if (m_stale)
  247. OnStale(this);
  248. }
  249. }
  250. public event ProviderStale OnStale;
  251. #endregion
  252. #region Helper functions
  253. private string GetScriptsPermissions(LandData parcel)
  254. {
  255. if ((parcel.Flags & (uint)Parcel.ParcelFlags.AllowOtherScripts) == (uint)Parcel.ParcelFlags.AllowOtherScripts)
  256. return "true";
  257. else
  258. return "false";
  259. }
  260. private string GetPublicPermissions(LandData parcel)
  261. {
  262. if ((parcel.Flags & (uint)Parcel.ParcelFlags.UseAccessList) == (uint)Parcel.ParcelFlags.UseAccessList)
  263. return "false";
  264. else
  265. return "true";
  266. }
  267. private string GetBuildPermissions(LandData parcel)
  268. {
  269. if ((parcel.Flags & (uint)Parcel.ParcelFlags.CreateObjects) == (uint)Parcel.ParcelFlags.CreateObjects)
  270. return "true";
  271. else
  272. return "false";
  273. }
  274. private string CheckForSale(LandData parcel)
  275. {
  276. if ((parcel.Flags & (uint)Parcel.ParcelFlags.ForSale) == (uint)Parcel.ParcelFlags.ForSale)
  277. return "true";
  278. else
  279. return "false";
  280. }
  281. private string GetShowInSearch(LandData parcel)
  282. {
  283. if ((parcel.Flags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory)
  284. return "true";
  285. else
  286. return "false";
  287. }
  288. #endregion
  289. #region Change detection hooks
  290. public void OnNewClient(IClientAPI client)
  291. {
  292. //Land hooks
  293. client.OnParcelDivideRequest += delegate(int west, int south, int east, int north,
  294. IClientAPI remote_client) { this.Stale = true; };
  295. client.OnParcelJoinRequest += delegate(int west, int south, int east, int north,
  296. IClientAPI remote_client) { this.Stale = true; };
  297. client.OnParcelPropertiesUpdateRequest += delegate(LandUpdateArgs args, int local_id,
  298. IClientAPI remote_client) { this.Stale = true; };
  299. client.OnParcelBuy += delegate(UUID agentId, UUID groupId, bool final, bool groupOwned,
  300. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  301. { this.Stale = true; };
  302. }
  303. public void ParcelSplitHook(int west, int south, int east, int north, IClientAPI remote_client)
  304. {
  305. this.Stale = true;
  306. }
  307. public void ParcelPropsHook(LandUpdateArgs args, int local_id, IClientAPI remote_client)
  308. {
  309. this.Stale = true;
  310. }
  311. #endregion
  312. // this is needed for non-convex parcels (example: rectangular parcel, and in the exact center
  313. // another, smaller rectangular parcel). Both will have the same initial coordinates.
  314. private void findPointInParcel(ILandObject land, ref uint refX, ref uint refY)
  315. {
  316. m_log.DebugFormat("[DATASNAPSHOT] trying {0}, {1}", refX, refY);
  317. // the point we started with already is in the parcel
  318. if (land.containsPoint((int)refX, (int)refY)) return;
  319. // ... otherwise, we have to search for a point within the parcel
  320. uint startX = (uint)land.landData.AABBMin.X;
  321. uint startY = (uint)land.landData.AABBMin.Y;
  322. uint endX = (uint)land.landData.AABBMax.X;
  323. uint endY = (uint)land.landData.AABBMax.Y;
  324. // default: center of the parcel
  325. refX = (startX + endX) / 2;
  326. refY = (startY + endY) / 2;
  327. // If the center point is within the parcel, take that one
  328. if (land.containsPoint((int)refX, (int)refY)) return;
  329. // otherwise, go the long way.
  330. for (uint y = startY; y <= endY; ++y)
  331. {
  332. for (uint x = startX; x <= endX; ++x)
  333. {
  334. if (land.containsPoint((int)x, (int)y))
  335. {
  336. // found a point
  337. refX = x;
  338. refY = y;
  339. return;
  340. }
  341. }
  342. }
  343. }
  344. }
  345. }