1
0

LandSnapshot.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. ILandChannel landChannel = m_scene.LandChannel;
  105. List<ILandObject> parcels = landChannel.AllParcels();
  106. IDwellModule dwellModule = m_scene.RequestModuleInterface<IDwellModule>();
  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)ParcelFlags.ShowDirectory) == (uint)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. if (dwellModule != null)
  179. dwell.InnerText = dwellModule.GetDwell(parcel.GlobalID).ToString();
  180. else
  181. dwell.InnerText = "0";
  182. xmlparcel.AppendChild(dwell);
  183. //TODO: figure how to figure out teleport system landData.landingType
  184. //land texture snapshot uuid
  185. if (parcel.SnapshotID != UUID.Zero)
  186. {
  187. XmlNode textureuuid = nodeFactory.CreateNode(XmlNodeType.Element, "image", "");
  188. textureuuid.InnerText = parcel.SnapshotID.ToString();
  189. xmlparcel.AppendChild(textureuuid);
  190. }
  191. string groupName = String.Empty;
  192. //attached user and group
  193. if (parcel.GroupID != UUID.Zero)
  194. {
  195. XmlNode groupblock = nodeFactory.CreateNode(XmlNodeType.Element, "group", "");
  196. XmlNode groupuuid = nodeFactory.CreateNode(XmlNodeType.Element, "groupuuid", "");
  197. groupuuid.InnerText = parcel.GroupID.ToString();
  198. groupblock.AppendChild(groupuuid);
  199. IGroupsModule gm = m_scene.RequestModuleInterface<IGroupsModule>();
  200. if (gm != null)
  201. {
  202. GroupRecord g = gm.GetGroupRecord(parcel.GroupID);
  203. if (g != null)
  204. groupName = g.GroupName;
  205. }
  206. XmlNode groupname = nodeFactory.CreateNode(XmlNodeType.Element, "groupname", "");
  207. groupname.InnerText = groupName;
  208. groupblock.AppendChild(groupname);
  209. xmlparcel.AppendChild(groupblock);
  210. }
  211. XmlNode userblock = nodeFactory.CreateNode(XmlNodeType.Element, "owner", "");
  212. UUID userOwnerUUID = parcel.OwnerID;
  213. XmlNode useruuid = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  214. useruuid.InnerText = userOwnerUUID.ToString();
  215. userblock.AppendChild(useruuid);
  216. if (!parcel.IsGroupOwned)
  217. {
  218. try
  219. {
  220. XmlNode username = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  221. UserAccount account = m_scene.UserAccountService.GetUserAccount(m_scene.RegionInfo.ScopeID, userOwnerUUID);
  222. username.InnerText = account.FirstName + " " + account.LastName;
  223. userblock.AppendChild(username);
  224. }
  225. catch (Exception)
  226. {
  227. //m_log.Info("[DATASNAPSHOT]: Cannot find owner name; ignoring this parcel");
  228. }
  229. }
  230. else
  231. {
  232. XmlNode username = nodeFactory.CreateNode(XmlNodeType.Element, "name", "");
  233. username.InnerText = groupName;
  234. userblock.AppendChild(username);
  235. }
  236. xmlparcel.AppendChild(userblock);
  237. parent.AppendChild(xmlparcel);
  238. }
  239. }
  240. //snap.AppendChild(parent);
  241. }
  242. this.Stale = false;
  243. return parent;
  244. }
  245. public String Name
  246. {
  247. get { return "LandSnapshot"; }
  248. }
  249. public bool Stale
  250. {
  251. get
  252. {
  253. return m_stale;
  254. }
  255. set
  256. {
  257. m_stale = value;
  258. if (m_stale)
  259. OnStale(this);
  260. }
  261. }
  262. public event ProviderStale OnStale;
  263. #endregion
  264. #region Helper functions
  265. private string GetScriptsPermissions(LandData parcel)
  266. {
  267. if ((parcel.Flags & (uint)ParcelFlags.AllowOtherScripts) == (uint)ParcelFlags.AllowOtherScripts)
  268. return "true";
  269. else
  270. return "false";
  271. }
  272. private string GetPublicPermissions(LandData parcel)
  273. {
  274. if ((parcel.Flags & (uint)ParcelFlags.UseAccessList) == (uint)ParcelFlags.UseAccessList)
  275. return "false";
  276. else
  277. return "true";
  278. }
  279. private string GetBuildPermissions(LandData parcel)
  280. {
  281. if ((parcel.Flags & (uint)ParcelFlags.CreateObjects) == (uint)ParcelFlags.CreateObjects)
  282. return "true";
  283. else
  284. return "false";
  285. }
  286. private string CheckForSale(LandData parcel)
  287. {
  288. if ((parcel.Flags & (uint)ParcelFlags.ForSale) == (uint)ParcelFlags.ForSale)
  289. return "true";
  290. else
  291. return "false";
  292. }
  293. private string GetShowInSearch(LandData parcel)
  294. {
  295. if ((parcel.Flags & (uint)ParcelFlags.ShowDirectory) == (uint)ParcelFlags.ShowDirectory)
  296. return "true";
  297. else
  298. return "false";
  299. }
  300. #endregion
  301. #region Change detection hooks
  302. public void OnNewClient(IClientAPI client)
  303. {
  304. //Land hooks
  305. client.OnParcelDivideRequest += delegate(int west, int south, int east, int north,
  306. IClientAPI remote_client) { this.Stale = true; };
  307. client.OnParcelJoinRequest += delegate(int west, int south, int east, int north,
  308. IClientAPI remote_client) { this.Stale = true; };
  309. client.OnParcelPropertiesUpdateRequest += delegate(LandUpdateArgs args, int local_id,
  310. IClientAPI remote_client) { this.Stale = true; };
  311. client.OnParcelBuy += delegate(UUID agentId, UUID groupId, bool final, bool groupOwned,
  312. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  313. { this.Stale = true; };
  314. }
  315. public void ParcelSplitHook(int west, int south, int east, int north, IClientAPI remote_client)
  316. {
  317. this.Stale = true;
  318. }
  319. public void ParcelPropsHook(LandUpdateArgs args, int local_id, IClientAPI remote_client)
  320. {
  321. this.Stale = true;
  322. }
  323. #endregion
  324. // this is needed for non-convex parcels (example: rectangular parcel, and in the exact center
  325. // another, smaller rectangular parcel). Both will have the same initial coordinates.
  326. private void findPointInParcel(ILandObject land, ref uint refX, ref uint refY)
  327. {
  328. m_log.DebugFormat("[DATASNAPSHOT] trying {0}, {1}", refX, refY);
  329. // the point we started with already is in the parcel
  330. if (land.ContainsPoint((int)refX, (int)refY)) return;
  331. // ... otherwise, we have to search for a point within the parcel
  332. uint startX = (uint)land.LandData.AABBMin.X;
  333. uint startY = (uint)land.LandData.AABBMin.Y;
  334. uint endX = (uint)land.LandData.AABBMax.X;
  335. uint endY = (uint)land.LandData.AABBMax.Y;
  336. // default: center of the parcel
  337. refX = (startX + endX) / 2;
  338. refY = (startY + endY) / 2;
  339. // If the center point is within the parcel, take that one
  340. if (land.ContainsPoint((int)refX, (int)refY)) return;
  341. // otherwise, go the long way.
  342. for (uint y = startY; y <= endY; ++y)
  343. {
  344. for (uint x = startX; x <= endX; ++x)
  345. {
  346. if (land.ContainsPoint((int)x, (int)y))
  347. {
  348. // found a point
  349. refX = x;
  350. refY = y;
  351. return;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }