LandSnapshot.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Xml;
  32. using libsecondlife;
  33. using log4net;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.DataSnapshot.Interfaces;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Environment.Modules.World.Land;
  38. using OpenSim.Region.Environment.Scenes;
  39. namespace OpenSim.Region.DataSnapshot
  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 ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. #region Dead code
  48. /*
  49. * David, I don't think we need this at all. When we do the snapshot, we can
  50. * simply look into the parcels that are marked for ShowDirectory -- see
  51. * conditional in RequestSnapshotData
  52. *
  53. //Revise this, look for more direct way of checking for change in land
  54. #region Client hooks
  55. public void OnNewClient(IClientAPI client)
  56. {
  57. //Land hooks
  58. client.OnParcelDivideRequest += ParcelSplitHook;
  59. client.OnParcelJoinRequest += ParcelSplitHook;
  60. client.OnParcelPropertiesUpdateRequest += ParcelPropsHook;
  61. }
  62. public void ParcelSplitHook(int west, int south, int east, int north, IClientAPI remote_client)
  63. {
  64. PrepareData();
  65. }
  66. public void ParcelPropsHook(ParcelPropertiesUpdatePacket packet, IClientAPI remote_client)
  67. {
  68. PrepareData();
  69. }
  70. #endregion
  71. public void PrepareData()
  72. {
  73. m_log.Info("[EXTERNALDATA]: Generating land data.");
  74. m_landIndexed.Clear();
  75. //Index sim land
  76. foreach (KeyValuePair<int, Land> curLand in m_scene.LandManager.landList)
  77. {
  78. //if ((curLand.Value.landData.landFlags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory)
  79. //{
  80. m_landIndexed.Add(curLand.Key, curLand.Value.Copy());
  81. //}
  82. }
  83. }
  84. public Dictionary<int,Land> IndexedLand {
  85. get { return m_landIndexed; }
  86. }
  87. */
  88. #endregion
  89. #region IDataSnapshotProvider members
  90. public void Initialize(Scene scene, DataSnapshotManager parent)
  91. {
  92. m_scene = scene;
  93. m_parent = parent;
  94. //m_scene.EventManager.OnNewClient += OnNewClient;
  95. }
  96. public Scene GetParentScene
  97. {
  98. get { return m_scene; }
  99. }
  100. public XmlNode RequestSnapshotData(XmlDocument nodeFactory)
  101. {
  102. ILandChannel landChannel = (LandChannel)m_scene.LandChannel;
  103. Dictionary<int, ILandObject> landList = null;
  104. try
  105. {
  106. Type landChannelType = typeof(LandChannel);
  107. FieldInfo landListField = landChannelType.GetField("landList", BindingFlags.NonPublic | BindingFlags.Instance);
  108. if (landListField != null)
  109. {
  110. landList = (Dictionary<int, ILandObject>)landListField.GetValue(landChannel);
  111. }
  112. }
  113. catch (Exception e)
  114. {
  115. Console.WriteLine("[DATASNAPSHOT] couldn't access field reflectively\n" + e.ToString());
  116. }
  117. XmlNode parent = nodeFactory.CreateNode(XmlNodeType.Element, "parceldata", "");
  118. if (landList != null)
  119. {
  120. //foreach (KeyValuePair<int, Land> curParcel in m_landIndexed)
  121. foreach (LandObject land in landList.Values)
  122. {
  123. LandData parcel = land.landData;
  124. if ((parcel.landFlags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory)
  125. {
  126. //TODO: make better method of marshalling data from LandData to XmlNode
  127. XmlNode xmlparcel = nodeFactory.CreateNode(XmlNodeType.Element, "parcel", "");
  128. // Attributes of the parcel node
  129. XmlAttribute scripts_attr = nodeFactory.CreateAttribute("scripts");
  130. scripts_attr.Value = GetScriptsPermissions(parcel);
  131. XmlAttribute category_attr = nodeFactory.CreateAttribute("category");
  132. category_attr.Value = parcel.category.ToString();
  133. //XmlAttribute entities_attr = nodeFactory.CreateAttribute("entities");
  134. //entities_attr.Value = land.primsOverMe.Count.ToString();
  135. xmlparcel.Attributes.Append(scripts_attr);
  136. xmlparcel.Attributes.Append(category_attr);
  137. //xmlparcel.Attributes.Append(entities_attr);
  138. //name, description, area, and UUID
  139. XmlNode name = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  140. name.InnerText = parcel.landName;
  141. xmlparcel.AppendChild(name);
  142. XmlNode desc = nodeFactory.CreateNode(XmlNodeType.Element, "description", "");
  143. desc.InnerText = parcel.landDesc;
  144. xmlparcel.AppendChild(desc);
  145. XmlNode uuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  146. uuid.InnerText = parcel.globalID.ToString();
  147. xmlparcel.AppendChild(uuid);
  148. XmlNode area = nodeFactory.CreateNode(XmlNodeType.Element, "area", "");
  149. area.InnerText = parcel.area.ToString();
  150. xmlparcel.AppendChild(area);
  151. //default location
  152. XmlNode tpLocation = nodeFactory.CreateNode(XmlNodeType.Element, "location", "");
  153. LLVector3 loc = parcel.userLocation;
  154. if (loc.Equals(LLVector3.Zero)) // This test is mute at this point: the location is wrong by default
  155. loc = new LLVector3((parcel.AABBMax.X - parcel.AABBMin.X) / 2, (parcel.AABBMax.Y - parcel.AABBMin.Y) / 2, (parcel.AABBMax.Y - parcel.AABBMin.Y) / 2);
  156. tpLocation.InnerText = loc.X.ToString() + "/" + loc.Y.ToString() + "/" + loc.Z.ToString();
  157. xmlparcel.AppendChild(tpLocation);
  158. //TODO: figure how to figure out teleport system landData.landingType
  159. //land texture snapshot uuid
  160. if (parcel.snapshotID != LLUUID.Zero)
  161. {
  162. XmlNode textureuuid = nodeFactory.CreateNode(XmlNodeType.Element, "image", "");
  163. textureuuid.InnerText = parcel.snapshotID.ToString();
  164. xmlparcel.AppendChild(textureuuid);
  165. }
  166. //attached user and group
  167. if (parcel.groupID != LLUUID.Zero)
  168. {
  169. XmlNode groupblock = nodeFactory.CreateNode(XmlNodeType.Element, "group", "");
  170. XmlNode groupuuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  171. groupuuid.InnerText = parcel.groupID.ToString();
  172. groupblock.AppendChild(groupuuid);
  173. //No name yet, there's no way to get a group name since they don't exist yet.
  174. //TODO: When groups are supported, add the group handling code.
  175. xmlparcel.AppendChild(groupblock);
  176. }
  177. if (!parcel.isGroupOwned)
  178. {
  179. XmlNode userblock = nodeFactory.CreateNode(XmlNodeType.Element, "owner", "");
  180. LLUUID userOwnerUUID = parcel.ownerID;
  181. XmlNode useruuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  182. useruuid.InnerText = userOwnerUUID.ToString();
  183. userblock.AppendChild(useruuid);
  184. try
  185. {
  186. XmlNode username = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  187. UserProfileData userProfile = m_scene.CommsManager.UserService.GetUserProfile(userOwnerUUID);
  188. username.InnerText = userProfile.FirstName + " " + userProfile.SurName;
  189. userblock.AppendChild(username);
  190. }
  191. catch (Exception)
  192. {
  193. m_log.Info("[DATASNAPSHOT]: Cannot find owner name; ignoring this parcel");
  194. }
  195. xmlparcel.AppendChild(userblock);
  196. }
  197. //else
  198. //{
  199. // XmlAttribute type = (XmlAttribute)nodeFactory.CreateNode(XmlNodeType.Attribute, "type", "");
  200. // type.InnerText = "owner";
  201. // groupblock.Attributes.Append(type);
  202. //}
  203. parent.AppendChild(xmlparcel);
  204. }
  205. }
  206. //snap.AppendChild(parent);
  207. }
  208. return parent;
  209. }
  210. #endregion
  211. #region Helper functions
  212. private string GetScriptsPermissions(LandData parcel)
  213. {
  214. if ((parcel.landFlags & (uint)Parcel.ParcelFlags.AllowOtherScripts) == (uint)Parcel.ParcelFlags.AllowOtherScripts)
  215. return "yes";
  216. else
  217. return "no";
  218. }
  219. #endregion
  220. }
  221. }