LandSnapshot.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 System.Xml;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Region.CoreModules.World.Land;
  36. using OpenSim.Region.DataSnapshot.Interfaces;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  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 = m_scene.LandChannel;
  105. List<ILandObject> parcels = landChannel.AllParcels();
  106. XmlNode parent = nodeFactory.CreateNode(XmlNodeType.Element, "parceldata", "");
  107. if (parcels != null)
  108. {
  109. //foreach (KeyValuePair<int, Land> curParcel in m_landIndexed)
  110. foreach (ILandObject parcel_interface in parcels)
  111. {
  112. // Play it safe
  113. if (!(parcel_interface is LandObject))
  114. continue;
  115. LandObject land = (LandObject)parcel_interface;
  116. LandData parcel = land.landData;
  117. if (m_parent.ExposureLevel.Equals("all") ||
  118. (m_parent.ExposureLevel.Equals("minimum") &&
  119. (parcel.Flags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory))
  120. {
  121. //TODO: make better method of marshalling data from LandData to XmlNode
  122. XmlNode xmlparcel = nodeFactory.CreateNode(XmlNodeType.Element, "parcel", "");
  123. // Attributes of the parcel node
  124. XmlAttribute scripts_attr = nodeFactory.CreateAttribute("scripts");
  125. scripts_attr.Value = GetScriptsPermissions(parcel);
  126. XmlAttribute build_attr = nodeFactory.CreateAttribute("build");
  127. build_attr.Value = GetBuildPermissions(parcel);
  128. XmlAttribute public_attr = nodeFactory.CreateAttribute("public");
  129. public_attr.Value = GetPublicPermissions(parcel);
  130. // Check the category of the Parcel
  131. XmlAttribute category_attr = nodeFactory.CreateAttribute("category");
  132. category_attr.Value = ((int)parcel.Category).ToString();
  133. // Check if the parcel is for sale
  134. XmlAttribute forsale_attr = nodeFactory.CreateAttribute("forsale");
  135. forsale_attr.Value = CheckForSale(parcel);
  136. XmlAttribute sales_attr = nodeFactory.CreateAttribute("salesprice");
  137. sales_attr.Value = parcel.SalePrice.ToString();
  138. XmlAttribute directory_attr = nodeFactory.CreateAttribute("showinsearch");
  139. directory_attr.Value = GetShowInSearch(parcel);
  140. //XmlAttribute entities_attr = nodeFactory.CreateAttribute("entities");
  141. //entities_attr.Value = land.primsOverMe.Count.ToString();
  142. xmlparcel.Attributes.Append(directory_attr);
  143. xmlparcel.Attributes.Append(scripts_attr);
  144. xmlparcel.Attributes.Append(build_attr);
  145. xmlparcel.Attributes.Append(public_attr);
  146. xmlparcel.Attributes.Append(category_attr);
  147. xmlparcel.Attributes.Append(forsale_attr);
  148. xmlparcel.Attributes.Append(sales_attr);
  149. //xmlparcel.Attributes.Append(entities_attr);
  150. //name, description, area, and UUID
  151. XmlNode name = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  152. name.InnerText = parcel.Name;
  153. xmlparcel.AppendChild(name);
  154. XmlNode desc = nodeFactory.CreateNode(XmlNodeType.Element, "description", "");
  155. desc.InnerText = parcel.Description;
  156. xmlparcel.AppendChild(desc);
  157. XmlNode uuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  158. uuid.InnerText = parcel.GlobalID.ToString();
  159. xmlparcel.AppendChild(uuid);
  160. XmlNode area = nodeFactory.CreateNode(XmlNodeType.Element, "area", "");
  161. area.InnerText = parcel.Area.ToString();
  162. xmlparcel.AppendChild(area);
  163. //default location
  164. XmlNode tpLocation = nodeFactory.CreateNode(XmlNodeType.Element, "location", "");
  165. Vector3 loc = parcel.UserLocation;
  166. if (loc.Equals(Vector3.Zero)) // This test is moot at this point: the location is wrong by default
  167. loc = new Vector3((parcel.AABBMax.X + parcel.AABBMin.X) / 2, (parcel.AABBMax.Y + parcel.AABBMin.Y) / 2, (parcel.AABBMax.Z + parcel.AABBMin.Z) / 2);
  168. tpLocation.InnerText = loc.X.ToString() + "/" + loc.Y.ToString() + "/" + loc.Z.ToString();
  169. xmlparcel.AppendChild(tpLocation);
  170. XmlNode infouuid = nodeFactory.CreateNode(XmlNodeType.Element, "infouuid", "");
  171. uint x = (uint)loc.X, y = (uint)loc.Y;
  172. findPointInParcel(land, ref x, ref y); // find a suitable spot
  173. infouuid.InnerText = Util.BuildFakeParcelID(
  174. m_scene.RegionInfo.RegionHandle, x, y).ToString();
  175. xmlparcel.AppendChild(infouuid);
  176. XmlNode dwell = nodeFactory.CreateNode(XmlNodeType.Element, "dwell", "");
  177. dwell.InnerText = parcel.Dwell.ToString();
  178. xmlparcel.AppendChild(dwell);
  179. //TODO: figure how to figure out teleport system landData.landingType
  180. //land texture snapshot uuid
  181. if (parcel.SnapshotID != UUID.Zero)
  182. {
  183. XmlNode textureuuid = nodeFactory.CreateNode(XmlNodeType.Element, "image", "");
  184. textureuuid.InnerText = parcel.SnapshotID.ToString();
  185. xmlparcel.AppendChild(textureuuid);
  186. }
  187. string groupName = String.Empty;
  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, "groupuuid", "");
  193. groupuuid.InnerText = parcel.GroupID.ToString();
  194. groupblock.AppendChild(groupuuid);
  195. IGroupsModule gm = m_scene.RequestModuleInterface<IGroupsModule>();
  196. if (gm != null)
  197. {
  198. GroupRecord g = gm.GetGroupRecord(parcel.GroupID);
  199. if (g != null)
  200. groupName = g.GroupName;
  201. }
  202. XmlNode groupname = nodeFactory.CreateNode(XmlNodeType.Element, "groupname", "");
  203. groupname.InnerText = groupName;
  204. groupblock.AppendChild(groupname);
  205. xmlparcel.AppendChild(groupblock);
  206. }
  207. XmlNode userblock = nodeFactory.CreateNode(XmlNodeType.Element, "owner", "");
  208. UUID userOwnerUUID = parcel.OwnerID;
  209. XmlNode useruuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  210. useruuid.InnerText = userOwnerUUID.ToString();
  211. userblock.AppendChild(useruuid);
  212. if (!parcel.IsGroupOwned)
  213. {
  214. try
  215. {
  216. XmlNode username = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  217. CachedUserInfo profile = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(userOwnerUUID);
  218. username.InnerText = profile.UserProfile.FirstName + " " + profile.UserProfile.SurName;
  219. userblock.AppendChild(username);
  220. }
  221. catch (Exception)
  222. {
  223. //m_log.Info("[DATASNAPSHOT]: Cannot find owner name; ignoring this parcel");
  224. }
  225. }
  226. else
  227. {
  228. XmlNode username = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  229. username.InnerText = groupName;
  230. userblock.AppendChild(username);
  231. }
  232. xmlparcel.AppendChild(userblock);
  233. parent.AppendChild(xmlparcel);
  234. }
  235. }
  236. //snap.AppendChild(parent);
  237. }
  238. this.Stale = false;
  239. return parent;
  240. }
  241. public String Name
  242. {
  243. get { return "LandSnapshot"; }
  244. }
  245. public bool Stale
  246. {
  247. get
  248. {
  249. return m_stale;
  250. }
  251. set
  252. {
  253. m_stale = value;
  254. if (m_stale)
  255. OnStale(this);
  256. }
  257. }
  258. public event ProviderStale OnStale;
  259. #endregion
  260. #region Helper functions
  261. private string GetScriptsPermissions(LandData parcel)
  262. {
  263. if ((parcel.Flags & (uint)Parcel.ParcelFlags.AllowOtherScripts) == (uint)Parcel.ParcelFlags.AllowOtherScripts)
  264. return "true";
  265. else
  266. return "false";
  267. }
  268. private string GetPublicPermissions(LandData parcel)
  269. {
  270. if ((parcel.Flags & (uint)Parcel.ParcelFlags.UseAccessList) == (uint)Parcel.ParcelFlags.UseAccessList)
  271. return "false";
  272. else
  273. return "true";
  274. }
  275. private string GetBuildPermissions(LandData parcel)
  276. {
  277. if ((parcel.Flags & (uint)Parcel.ParcelFlags.CreateObjects) == (uint)Parcel.ParcelFlags.CreateObjects)
  278. return "true";
  279. else
  280. return "false";
  281. }
  282. private string CheckForSale(LandData parcel)
  283. {
  284. if ((parcel.Flags & (uint)Parcel.ParcelFlags.ForSale) == (uint)Parcel.ParcelFlags.ForSale)
  285. return "true";
  286. else
  287. return "false";
  288. }
  289. private string GetShowInSearch(LandData parcel)
  290. {
  291. if ((parcel.Flags & (uint)Parcel.ParcelFlags.ShowDirectory) == (uint)Parcel.ParcelFlags.ShowDirectory)
  292. return "true";
  293. else
  294. return "false";
  295. }
  296. #endregion
  297. #region Change detection hooks
  298. public void OnNewClient(IClientAPI client)
  299. {
  300. //Land hooks
  301. client.OnParcelDivideRequest += delegate(int west, int south, int east, int north,
  302. IClientAPI remote_client) { this.Stale = true; };
  303. client.OnParcelJoinRequest += delegate(int west, int south, int east, int north,
  304. IClientAPI remote_client) { this.Stale = true; };
  305. client.OnParcelPropertiesUpdateRequest += delegate(LandUpdateArgs args, int local_id,
  306. IClientAPI remote_client) { this.Stale = true; };
  307. client.OnParcelBuy += delegate(UUID agentId, UUID groupId, bool final, bool groupOwned,
  308. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  309. { this.Stale = true; };
  310. }
  311. public void ParcelSplitHook(int west, int south, int east, int north, IClientAPI remote_client)
  312. {
  313. this.Stale = true;
  314. }
  315. public void ParcelPropsHook(LandUpdateArgs args, int local_id, IClientAPI remote_client)
  316. {
  317. this.Stale = true;
  318. }
  319. #endregion
  320. // this is needed for non-convex parcels (example: rectangular parcel, and in the exact center
  321. // another, smaller rectangular parcel). Both will have the same initial coordinates.
  322. private void findPointInParcel(ILandObject land, ref uint refX, ref uint refY)
  323. {
  324. m_log.DebugFormat("[DATASNAPSHOT] trying {0}, {1}", refX, refY);
  325. // the point we started with already is in the parcel
  326. if (land.containsPoint((int)refX, (int)refY)) return;
  327. // ... otherwise, we have to search for a point within the parcel
  328. uint startX = (uint)land.landData.AABBMin.X;
  329. uint startY = (uint)land.landData.AABBMin.Y;
  330. uint endX = (uint)land.landData.AABBMax.X;
  331. uint endY = (uint)land.landData.AABBMax.Y;
  332. // default: center of the parcel
  333. refX = (startX + endX) / 2;
  334. refY = (startY + endY) / 2;
  335. // If the center point is within the parcel, take that one
  336. if (land.containsPoint((int)refX, (int)refY)) return;
  337. // otherwise, go the long way.
  338. for (uint y = startY; y <= endY; ++y)
  339. {
  340. for (uint x = startX; x <= endX; ++x)
  341. {
  342. if (land.containsPoint((int)x, (int)y))
  343. {
  344. // found a point
  345. refX = x;
  346. refY = y;
  347. return;
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }