PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/DataSnapshot/Modules/ObjectSnapshot.cs

https://bitbucket.org/VirtualReality/optional-modules
C# | 290 lines | 209 code | 34 blank | 47 comment | 36 complexity | 5eea7cb42ec3bb833ec3869c1be03caf MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.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 Aurora-Sim 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 Aurora.Framework;
  34. using OpenSim.Region.DataSnapshot.Interfaces;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. namespace OpenSim.Region.DataSnapshot.Providers
  38. {
  39. public class ObjectSnapshot : IDataSnapshotProvider
  40. {
  41. private IScene m_scene = null;
  42. // private DataSnapshotManager m_parent = null;
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private bool m_stale = true;
  45. private static UUID m_DefaultImage = new UUID("89556747-24cb-43ed-920b-47caed15465f");
  46. private static UUID m_BlankImage = new UUID("5748decc-f629-461c-9a36-a35a221fe21f");
  47. public void Initialize(IScene scene, DataSnapshotManager parent)
  48. {
  49. m_scene = scene;
  50. // m_parent = parent;
  51. //To check for staleness, we must catch all incoming client packets.
  52. m_scene.EventManager.OnNewClient += OnNewClient;
  53. scene.EventManager.OnClosingClient += OnClosingClient;
  54. }
  55. public void OnNewClient(IClientAPI client)
  56. {
  57. //Detect object data changes by hooking into the IClientAPI.
  58. //Very dirty, and breaks whenever someone changes the client API.
  59. client.OnAddPrim += delegate (UUID ownerID, UUID groupID, Vector3 RayEnd, Quaternion rot,
  60. PrimitiveBaseShape shape, byte bypassRaycast, Vector3 RayStart, UUID RayTargetID,
  61. byte RayEndIsIntersection) { this.Stale = true; };
  62. client.OnLinkObjects += delegate (IClientAPI remoteClient, uint parent, List<uint> children)
  63. { this.Stale = true; };
  64. client.OnDelinkObjects += delegate(List<uint> primIds, IClientAPI clientApi) { this.Stale = true; };
  65. client.OnGrabUpdate += delegate(UUID objectID, Vector3 offset, Vector3 grapPos,
  66. IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs) { this.Stale = true; };
  67. client.OnObjectAttach += delegate(IClientAPI remoteClient, uint objectLocalID, int AttachmentPt,
  68. bool silent) { this.Stale = true; };
  69. client.OnObjectDuplicate += delegate(uint localID, Vector3 offset, uint dupeFlags, UUID AgentID,
  70. UUID GroupID, Quaternion rot) { this.Stale = true; return true; };
  71. client.OnObjectDuplicateOnRay += delegate(uint localID, uint dupeFlags, UUID AgentID, UUID GroupID,
  72. UUID RayTargetObj, Vector3 RayEnd, Vector3 RayStart, bool BypassRaycast,
  73. bool RayEndIsIntersection, bool CopyCenters, bool CopyRotates) { this.Stale = true; };
  74. client.OnObjectIncludeInSearch += delegate(IClientAPI remoteClient, bool IncludeInSearch, uint localID)
  75. { this.Stale = true; };
  76. client.OnObjectPermissions += delegate(IClientAPI controller, UUID agentID, UUID sessionID,
  77. byte field, uint localId, uint mask, byte set) { this.Stale = true; };
  78. client.OnRezObject += delegate(IClientAPI remoteClient, UUID itemID, Vector3 RayEnd,
  79. Vector3 RayStart, UUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
  80. bool RezSelected,
  81. bool RemoveItem, UUID fromTaskID) { this.Stale = true; };
  82. }
  83. private void OnClosingClient(IClientAPI client)
  84. {
  85. client.OnAddPrim -= delegate(UUID ownerID, UUID groupID, Vector3 RayEnd, Quaternion rot,
  86. PrimitiveBaseShape shape, byte bypassRaycast, Vector3 RayStart, UUID RayTargetID,
  87. byte RayEndIsIntersection) { this.Stale = true; };
  88. client.OnLinkObjects -= delegate(IClientAPI remoteClient, uint parent, List<uint> children)
  89. { this.Stale = true; };
  90. client.OnDelinkObjects -= delegate(List<uint> primIds, IClientAPI clientApi) { this.Stale = true; };
  91. client.OnGrabUpdate -= delegate(UUID objectID, Vector3 offset, Vector3 grapPos,
  92. IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs) { this.Stale = true; };
  93. client.OnObjectAttach -= delegate(IClientAPI remoteClient, uint objectLocalID, int AttachmentPt,
  94. bool silent) { this.Stale = true; };
  95. client.OnObjectDuplicate -= delegate(uint localID, Vector3 offset, uint dupeFlags, UUID AgentID,
  96. UUID GroupID, Quaternion rot) { this.Stale = true; return true; };
  97. client.OnObjectDuplicateOnRay -= delegate(uint localID, uint dupeFlags, UUID AgentID, UUID GroupID,
  98. UUID RayTargetObj, Vector3 RayEnd, Vector3 RayStart, bool BypassRaycast,
  99. bool RayEndIsIntersection, bool CopyCenters, bool CopyRotates) { this.Stale = true; };
  100. client.OnObjectIncludeInSearch -= delegate(IClientAPI remoteClient, bool IncludeInSearch, uint localID)
  101. { this.Stale = true; };
  102. client.OnObjectPermissions -= delegate(IClientAPI controller, UUID agentID, UUID sessionID,
  103. byte field, uint localId, uint mask, byte set) { this.Stale = true; };
  104. client.OnRezObject -= delegate(IClientAPI remoteClient, UUID itemID, Vector3 RayEnd,
  105. Vector3 RayStart, UUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
  106. bool RezSelected,
  107. bool RemoveItem, UUID fromTaskID) { this.Stale = true; };
  108. }
  109. public IScene GetParentScene
  110. {
  111. get { return m_scene; }
  112. }
  113. public XmlNode RequestSnapshotData(XmlDocument nodeFactory)
  114. {
  115. //m_log.Debug("[DATASNAPSHOT]: Generating object data for scene " + m_scene.RegionInfo.RegionName);
  116. XmlNode parent = nodeFactory.CreateNode(XmlNodeType.Element, "objectdata", "");
  117. XmlNode node;
  118. ISceneEntity[] entities = m_scene.Entities.GetEntities();
  119. foreach(ISceneEntity obj in entities)
  120. {
  121. // only objects, not avatars
  122. // m_log.Debug("[DATASNAPSHOT]: Found object " + obj.Name + " in scene");
  123. // libomv will complain about PrimFlags.JointWheel
  124. // being obsolete, so we...
  125. #pragma warning disable 0612
  126. if((obj.RootChild.Flags & PrimFlags.JointWheel) == PrimFlags.JointWheel)
  127. {
  128. ISceneChildEntity m_rootPart = obj.RootChild;
  129. if(m_rootPart != null)
  130. {
  131. ILandObject land = m_scene.RequestModuleInterface<IParcelManagementModule>().GetLandObject(m_rootPart.AbsolutePosition.X, m_rootPart.AbsolutePosition.Y);
  132. XmlNode xmlobject = nodeFactory.CreateNode(XmlNodeType.Element, "object", "");
  133. node = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  134. node.InnerText = obj.UUID.ToString();
  135. xmlobject.AppendChild(node);
  136. node = nodeFactory.CreateNode(XmlNodeType.Element, "title", "");
  137. node.InnerText = m_rootPart.Name;
  138. xmlobject.AppendChild(node);
  139. node = nodeFactory.CreateNode(XmlNodeType.Element, "description", "");
  140. node.InnerText = m_rootPart.Description;
  141. xmlobject.AppendChild(node);
  142. node = nodeFactory.CreateNode(XmlNodeType.Element, "flags", "");
  143. node.InnerText = String.Format("{0:x}", (uint)m_rootPart.Flags);
  144. xmlobject.AppendChild(node);
  145. node = nodeFactory.CreateNode(XmlNodeType.Element, "regionuuid", "");
  146. node.InnerText = m_scene.RegionInfo.RegionSettings.RegionUUID.ToString();
  147. xmlobject.AppendChild(node);
  148. if(land != null && land.LandData != null)
  149. {
  150. node = nodeFactory.CreateNode(XmlNodeType.Element, "parceluuid", "");
  151. node.InnerText = land.LandData.GlobalID.ToString();
  152. xmlobject.AppendChild(node);
  153. }
  154. else
  155. {
  156. // Something is wrong with this object. Let's not list it.
  157. m_log.WarnFormat("[DATASNAPSHOT]: Bad data for object {0} ({1}) in region {2}", obj.Name, obj.UUID, m_scene.RegionInfo.RegionName);
  158. continue;
  159. }
  160. node = nodeFactory.CreateNode(XmlNodeType.Element, "location", "");
  161. Vector3 loc = obj.AbsolutePosition;
  162. node.InnerText = loc.X.ToString() + "/" + loc.Y.ToString() + "/" + loc.Z.ToString();
  163. xmlobject.AppendChild(node);
  164. string bestImage = GuessImage(obj);
  165. if(bestImage != string.Empty)
  166. {
  167. node = nodeFactory.CreateNode(XmlNodeType.Element, "image", "");
  168. node.InnerText = bestImage;
  169. xmlobject.AppendChild(node);
  170. }
  171. parent.AppendChild(xmlobject);
  172. }
  173. }
  174. #pragma warning disable 0612
  175. }
  176. this.Stale = false;
  177. return parent;
  178. }
  179. public String Name
  180. {
  181. get { return "ObjectSnapshot"; }
  182. }
  183. public bool Stale
  184. {
  185. get
  186. {
  187. return m_stale;
  188. }
  189. set
  190. {
  191. m_stale = value;
  192. if (m_stale)
  193. OnStale(this);
  194. }
  195. }
  196. public event ProviderStale OnStale;
  197. /// <summary>
  198. /// Guesses the best image, based on a simple heuristic. It guesses only for boxes.
  199. /// We're optimizing for boxes, because those are the most common objects
  200. /// marked "Show in search" -- boxes with content inside.For other shapes,
  201. /// it's really hard to tell which texture should be grabbed.
  202. /// </summary>
  203. /// <param name="sog"></param>
  204. /// <returns></returns>
  205. private string GuessImage(ISceneEntity sog)
  206. {
  207. string bestguess = string.Empty;
  208. Dictionary<UUID, int> counts = new Dictionary<UUID, int>();
  209. PrimitiveBaseShape shape = sog.RootChild.Shape;
  210. if (shape != null && shape.ProfileShape == ProfileShape.Square)
  211. {
  212. Primitive.TextureEntry textures = shape.Textures;
  213. if (textures != null)
  214. {
  215. if (textures.DefaultTexture != null &&
  216. textures.DefaultTexture.TextureID != UUID.Zero &&
  217. textures.DefaultTexture.TextureID != m_DefaultImage &&
  218. textures.DefaultTexture.TextureID != m_BlankImage &&
  219. textures.DefaultTexture.RGBA.A < 50f)
  220. {
  221. counts[textures.DefaultTexture.TextureID] = 8;
  222. }
  223. if (textures.FaceTextures != null)
  224. {
  225. foreach (Primitive.TextureEntryFace tentry in textures.FaceTextures)
  226. {
  227. if (tentry != null)
  228. {
  229. if (tentry.TextureID != UUID.Zero && tentry.TextureID != m_DefaultImage && tentry.TextureID != m_BlankImage && tentry.RGBA.A < 50)
  230. {
  231. int c = 0;
  232. counts.TryGetValue(tentry.TextureID, out c);
  233. counts[tentry.TextureID] = c + 1;
  234. // decrease the default texture count
  235. if (counts.ContainsKey(textures.DefaultTexture.TextureID))
  236. counts[textures.DefaultTexture.TextureID] = counts[textures.DefaultTexture.TextureID] - 1;
  237. }
  238. }
  239. }
  240. }
  241. // Let's pick the most unique texture
  242. int min = 9999;
  243. foreach (KeyValuePair<UUID, int> kv in counts)
  244. {
  245. if (kv.Value < min && kv.Value >= 1)
  246. {
  247. bestguess = kv.Key.ToString();
  248. min = kv.Value;
  249. }
  250. }
  251. }
  252. }
  253. return bestguess;
  254. }
  255. }
  256. }