ObjectSnapshot.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.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 Scene 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(Scene 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. m_scene.EventManager.OnParcelPrimCountAdd += delegate(SceneObjectGroup obj) { this.Stale = true; };
  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, uint addFlags) { 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, uint AttachmentPt,
  68. bool silent) { this.Stale = true; };
  69. client.OnObjectDuplicate += delegate(uint localID, Vector3 offset, uint dupeFlags, UUID AgentID,
  70. UUID GroupID) { this.Stale = 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, UUID groupID,
  79. Vector3 RayEnd,
  80. Vector3 RayStart, UUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
  81. bool RezSelected,
  82. bool RemoveItem, UUID fromTaskID) { this.Stale = true; };
  83. }
  84. public Scene GetParentScene
  85. {
  86. get { return m_scene; }
  87. }
  88. public XmlNode RequestSnapshotData(XmlDocument nodeFactory)
  89. {
  90. m_log.Debug("[DATASNAPSHOT]: Generating object data for scene " + m_scene.RegionInfo.RegionName);
  91. XmlNode parent = nodeFactory.CreateNode(XmlNodeType.Element, "objectdata", "");
  92. XmlNode node;
  93. EntityBase[] entities = m_scene.Entities.GetEntities();
  94. foreach (EntityBase entity in entities)
  95. {
  96. // only objects, not avatars
  97. if (entity is SceneObjectGroup)
  98. {
  99. SceneObjectGroup obj = (SceneObjectGroup)entity;
  100. // m_log.Debug("[DATASNAPSHOT]: Found object " + obj.Name + " in scene");
  101. // libomv will complain about PrimFlags.JointWheel
  102. // being obsolete, so we...
  103. #pragma warning disable 0612
  104. if ((obj.RootPart.Flags & PrimFlags.JointWheel) == PrimFlags.JointWheel)
  105. {
  106. SceneObjectPart m_rootPart = obj.RootPart;
  107. ILandObject land = m_scene.LandChannel.GetLandObject(m_rootPart.AbsolutePosition.X, m_rootPart.AbsolutePosition.Y);
  108. XmlNode xmlobject = nodeFactory.CreateNode(XmlNodeType.Element, "object", "");
  109. node = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  110. node.InnerText = obj.UUID.ToString();
  111. xmlobject.AppendChild(node);
  112. node = nodeFactory.CreateNode(XmlNodeType.Element, "title", "");
  113. node.InnerText = m_rootPart.Name;
  114. xmlobject.AppendChild(node);
  115. node = nodeFactory.CreateNode(XmlNodeType.Element, "description", "");
  116. node.InnerText = m_rootPart.Description;
  117. xmlobject.AppendChild(node);
  118. node = nodeFactory.CreateNode(XmlNodeType.Element, "flags", "");
  119. node.InnerText = String.Format("{0:x}", (uint)m_rootPart.Flags);
  120. xmlobject.AppendChild(node);
  121. node = nodeFactory.CreateNode(XmlNodeType.Element, "regionuuid", "");
  122. node.InnerText = m_scene.RegionInfo.RegionSettings.RegionUUID.ToString();
  123. xmlobject.AppendChild(node);
  124. if (land != null && land.LandData != null)
  125. {
  126. node = nodeFactory.CreateNode(XmlNodeType.Element, "parceluuid", "");
  127. node.InnerText = land.LandData.GlobalID.ToString();
  128. xmlobject.AppendChild(node);
  129. }
  130. else
  131. {
  132. // Something is wrong with this object. Let's not list it.
  133. m_log.WarnFormat("[DATASNAPSHOT]: Bad data for object {0} ({1}) in region {2}", obj.Name, obj.UUID, m_scene.RegionInfo.RegionName);
  134. continue;
  135. }
  136. node = nodeFactory.CreateNode(XmlNodeType.Element, "location", "");
  137. Vector3 loc = obj.AbsolutePosition;
  138. node.InnerText = loc.X.ToString() + "/" + loc.Y.ToString() + "/" + loc.Z.ToString();
  139. xmlobject.AppendChild(node);
  140. string bestImage = GuessImage(obj);
  141. if (bestImage != string.Empty)
  142. {
  143. node = nodeFactory.CreateNode(XmlNodeType.Element, "image", "");
  144. node.InnerText = bestImage;
  145. xmlobject.AppendChild(node);
  146. }
  147. parent.AppendChild(xmlobject);
  148. }
  149. #pragma warning disable 0612
  150. }
  151. }
  152. this.Stale = false;
  153. return parent;
  154. }
  155. public String Name
  156. {
  157. get { return "ObjectSnapshot"; }
  158. }
  159. public bool Stale
  160. {
  161. get
  162. {
  163. return m_stale;
  164. }
  165. set
  166. {
  167. m_stale = value;
  168. if (m_stale)
  169. OnStale(this);
  170. }
  171. }
  172. public event ProviderStale OnStale;
  173. /// <summary>
  174. /// Guesses the best image, based on a simple heuristic. It guesses only for boxes.
  175. /// We're optimizing for boxes, because those are the most common objects
  176. /// marked "Show in search" -- boxes with content inside.For other shapes,
  177. /// it's really hard to tell which texture should be grabbed.
  178. /// </summary>
  179. /// <param name="sog"></param>
  180. /// <returns></returns>
  181. private string GuessImage(SceneObjectGroup sog)
  182. {
  183. string bestguess = string.Empty;
  184. Dictionary<UUID, int> counts = new Dictionary<UUID, int>();
  185. PrimitiveBaseShape shape = sog.RootPart.Shape;
  186. if (shape != null && shape.ProfileShape == ProfileShape.Square)
  187. {
  188. Primitive.TextureEntry textures = shape.Textures;
  189. if (textures != null)
  190. {
  191. if (textures.DefaultTexture != null &&
  192. textures.DefaultTexture.TextureID != UUID.Zero &&
  193. textures.DefaultTexture.TextureID != m_DefaultImage &&
  194. textures.DefaultTexture.TextureID != m_BlankImage &&
  195. textures.DefaultTexture.RGBA.A < 50f)
  196. {
  197. counts[textures.DefaultTexture.TextureID] = 8;
  198. }
  199. if (textures.FaceTextures != null)
  200. {
  201. foreach (Primitive.TextureEntryFace tentry in textures.FaceTextures)
  202. {
  203. if (tentry != null)
  204. {
  205. if (tentry.TextureID != UUID.Zero && tentry.TextureID != m_DefaultImage && tentry.TextureID != m_BlankImage && tentry.RGBA.A < 50)
  206. {
  207. int c = 0;
  208. counts.TryGetValue(tentry.TextureID, out c);
  209. counts[tentry.TextureID] = c + 1;
  210. // decrease the default texture count
  211. if (counts.ContainsKey(textures.DefaultTexture.TextureID))
  212. counts[textures.DefaultTexture.TextureID] = counts[textures.DefaultTexture.TextureID] - 1;
  213. }
  214. }
  215. }
  216. }
  217. // Let's pick the most unique texture
  218. int min = 9999;
  219. foreach (KeyValuePair<UUID, int> kv in counts)
  220. {
  221. if (kv.Value < min && kv.Value >= 1)
  222. {
  223. bestguess = kv.Key.ToString();
  224. min = kv.Value;
  225. }
  226. }
  227. }
  228. }
  229. return bestguess;
  230. }
  231. }
  232. }