LandSnapshot.cs 18 KB

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