LandSnapshot.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 libsecondlife;
  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 libsecondlife.Packets;
  39. namespace OpenSim.Region.DataSnapshot.Providers
  40. {
  41. public class LandSnapshot : IDataSnapshotProvider
  42. {
  43. private Scene m_scene = null;
  44. // private DataSnapshotManager m_parent = null;
  45. //private Dictionary<int, Land> m_landIndexed = new Dictionary<int, Land>();
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private bool m_stale = true;
  48. #region Dead code
  49. /*
  50. * David, I don't think we need this at all. When we do the snapshot, we can
  51. * simply look into the parcels that are marked for ShowDirectory -- see
  52. * conditional in RequestSnapshotData
  53. *
  54. //Revise this, look for more direct way of checking for change in land
  55. #region Client hooks
  56. public void OnNewClient(IClientAPI client)
  57. {
  58. //Land hooks
  59. client.OnParcelDivideRequest += ParcelSplitHook;
  60. client.OnParcelJoinRequest += ParcelSplitHook;
  61. client.OnParcelPropertiesUpdateRequest += ParcelPropsHook;
  62. }
  63. public void ParcelSplitHook(int west, int south, int east, int north, IClientAPI remote_client)
  64. {
  65. PrepareData();
  66. }
  67. public void ParcelPropsHook(ParcelPropertiesUpdatePacket packet, IClientAPI remote_client)
  68. {
  69. PrepareData();
  70. }
  71. #endregion
  72. public void PrepareData()
  73. {
  74. m_log.Info("[EXTERNALDATA]: Generating land data.");
  75. m_landIndexed.Clear();
  76. //Index sim land
  77. foreach (KeyValuePair<int, Land> curLand in m_scene.LandManager.landList)
  78. {
  79. //if ((curLand.Value.landData.landFlags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory)
  80. //{
  81. m_landIndexed.Add(curLand.Key, curLand.Value.Copy());
  82. //}
  83. }
  84. }
  85. public Dictionary<int,Land> IndexedLand {
  86. get { return m_landIndexed; }
  87. }
  88. */
  89. #endregion
  90. #region IDataSnapshotProvider members
  91. public void Initialize(Scene scene, DataSnapshotManager parent)
  92. {
  93. m_scene = scene;
  94. // m_parent = parent;
  95. //Brought back from the dead for staleness checks.
  96. m_scene.EventManager.OnNewClient += OnNewClient;
  97. }
  98. public Scene GetParentScene
  99. {
  100. get { return m_scene; }
  101. }
  102. public XmlNode RequestSnapshotData(XmlDocument nodeFactory)
  103. {
  104. ILandChannel landChannel = (LandChannel)m_scene.LandChannel;
  105. Dictionary<int, ILandObject> landList = null;
  106. try
  107. {
  108. Type landChannelType = typeof(LandChannel);
  109. FieldInfo landListField = landChannelType.GetField("landList", BindingFlags.NonPublic | BindingFlags.Instance);
  110. if (landListField != null)
  111. {
  112. landList = (Dictionary<int, ILandObject>)landListField.GetValue(landChannel);
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. m_log.Error("[DATASNAPSHOT] couldn't access field reflectively\n" + e.ToString());
  118. }
  119. XmlNode parent = nodeFactory.CreateNode(XmlNodeType.Element, "parceldata", "");
  120. if (landList != null)
  121. {
  122. //foreach (KeyValuePair<int, Land> curParcel in m_landIndexed)
  123. foreach (LandObject land in landList.Values)
  124. {
  125. LandData parcel = land.landData;
  126. if ((parcel.landFlags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory)
  127. {
  128. //TODO: make better method of marshalling data from LandData to XmlNode
  129. XmlNode xmlparcel = nodeFactory.CreateNode(XmlNodeType.Element, "parcel", "");
  130. // Attributes of the parcel node
  131. XmlAttribute scripts_attr = nodeFactory.CreateAttribute("scripts");
  132. scripts_attr.Value = GetScriptsPermissions(parcel);
  133. XmlAttribute category_attr = nodeFactory.CreateAttribute("category");
  134. category_attr.Value = parcel.category.ToString();
  135. //XmlAttribute entities_attr = nodeFactory.CreateAttribute("entities");
  136. //entities_attr.Value = land.primsOverMe.Count.ToString();
  137. xmlparcel.Attributes.Append(scripts_attr);
  138. xmlparcel.Attributes.Append(category_attr);
  139. //xmlparcel.Attributes.Append(entities_attr);
  140. //name, description, area, and UUID
  141. XmlNode name = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  142. name.InnerText = parcel.landName;
  143. xmlparcel.AppendChild(name);
  144. XmlNode desc = nodeFactory.CreateNode(XmlNodeType.Element, "description", "");
  145. desc.InnerText = parcel.landDesc;
  146. xmlparcel.AppendChild(desc);
  147. XmlNode uuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  148. uuid.InnerText = parcel.globalID.ToString();
  149. xmlparcel.AppendChild(uuid);
  150. XmlNode area = nodeFactory.CreateNode(XmlNodeType.Element, "area", "");
  151. area.InnerText = parcel.area.ToString();
  152. xmlparcel.AppendChild(area);
  153. //default location
  154. XmlNode tpLocation = nodeFactory.CreateNode(XmlNodeType.Element, "location", "");
  155. LLVector3 loc = parcel.userLocation;
  156. if (loc.Equals(LLVector3.Zero)) // This test is mute at this point: the location is wrong by default
  157. loc = new LLVector3((parcel.AABBMax.X - parcel.AABBMin.X) / 2, (parcel.AABBMax.Y - parcel.AABBMin.Y) / 2, (parcel.AABBMax.Y - parcel.AABBMin.Y) / 2);
  158. tpLocation.InnerText = loc.X.ToString() + "/" + loc.Y.ToString() + "/" + loc.Z.ToString();
  159. xmlparcel.AppendChild(tpLocation);
  160. //TODO: figure how to figure out teleport system landData.landingType
  161. //land texture snapshot uuid
  162. if (parcel.snapshotID != LLUUID.Zero)
  163. {
  164. XmlNode textureuuid = nodeFactory.CreateNode(XmlNodeType.Element, "image", "");
  165. textureuuid.InnerText = parcel.snapshotID.ToString();
  166. xmlparcel.AppendChild(textureuuid);
  167. }
  168. //attached user and group
  169. if (parcel.groupID != LLUUID.Zero)
  170. {
  171. XmlNode groupblock = nodeFactory.CreateNode(XmlNodeType.Element, "group", "");
  172. XmlNode groupuuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  173. groupuuid.InnerText = parcel.groupID.ToString();
  174. groupblock.AppendChild(groupuuid);
  175. //No name yet, there's no way to get a group name since they don't exist yet.
  176. //TODO: When groups are supported, add the group handling code.
  177. xmlparcel.AppendChild(groupblock);
  178. }
  179. if (!parcel.isGroupOwned)
  180. {
  181. XmlNode userblock = nodeFactory.CreateNode(XmlNodeType.Element, "owner", "");
  182. LLUUID userOwnerUUID = parcel.ownerID;
  183. XmlNode useruuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  184. useruuid.InnerText = userOwnerUUID.ToString();
  185. userblock.AppendChild(useruuid);
  186. try
  187. {
  188. XmlNode username = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  189. UserProfileData userProfile = m_scene.CommsManager.UserService.GetUserProfile(userOwnerUUID);
  190. username.InnerText = userProfile.FirstName + " " + userProfile.SurName;
  191. userblock.AppendChild(username);
  192. }
  193. catch (Exception)
  194. {
  195. m_log.Info("[DATASNAPSHOT]: Cannot find owner name; ignoring this parcel");
  196. }
  197. xmlparcel.AppendChild(userblock);
  198. }
  199. //else
  200. //{
  201. // XmlAttribute type = (XmlAttribute)nodeFactory.CreateNode(XmlNodeType.Attribute, "type", "");
  202. // type.InnerText = "owner";
  203. // groupblock.Attributes.Append(type);
  204. //}
  205. parent.AppendChild(xmlparcel);
  206. }
  207. }
  208. //snap.AppendChild(parent);
  209. }
  210. this.Stale = false;
  211. return parent;
  212. }
  213. public String Name
  214. {
  215. get { return "LandSnapshot"; }
  216. }
  217. public bool Stale
  218. {
  219. get
  220. {
  221. return m_stale;
  222. }
  223. set
  224. {
  225. m_stale = value;
  226. if (m_stale)
  227. OnStale(this);
  228. }
  229. }
  230. public event ProviderStale OnStale;
  231. #endregion
  232. #region Helper functions
  233. private string GetScriptsPermissions(LandData parcel)
  234. {
  235. if ((parcel.landFlags & (uint)Parcel.ParcelFlags.AllowOtherScripts) == (uint)Parcel.ParcelFlags.AllowOtherScripts)
  236. return "yes";
  237. else
  238. return "no";
  239. }
  240. #endregion
  241. #region Change detection hooks
  242. public void OnNewClient(IClientAPI client)
  243. {
  244. //Land hooks
  245. client.OnParcelDivideRequest += delegate (int west, int south, int east, int north,
  246. IClientAPI remote_client) { this.Stale = true; };
  247. client.OnParcelJoinRequest += delegate (int west, int south, int east, int north,
  248. IClientAPI remote_client) { this.Stale = true; };
  249. client.OnParcelPropertiesUpdateRequest += delegate(LandUpdateArgs args, int local_id,
  250. IClientAPI remote_client) { this.Stale = true; };
  251. client.OnParcelBuy += delegate (LLUUID agentId, LLUUID groupId, bool final, bool groupOwned,
  252. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  253. { this.Stale = true; };
  254. }
  255. public void ParcelSplitHook(int west, int south, int east, int north, IClientAPI remote_client)
  256. {
  257. this.Stale = true;
  258. }
  259. public void ParcelPropsHook(LandUpdateArgs args, int local_id, IClientAPI remote_client)
  260. {
  261. this.Stale = true;
  262. }
  263. #endregion
  264. }
  265. }