ObjectSnapshot.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 OpenSim 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 OpenSim.Region.DataSnapshot.Interfaces;
  33. using OpenSim.Region.Environment.Scenes;
  34. using OpenSim.Framework;
  35. using libsecondlife;
  36. namespace OpenSim.Region.DataSnapshot.Providers
  37. {
  38. public class ObjectSnapshot : IDataSnapshotProvider
  39. {
  40. private Scene m_scene = null;
  41. // private DataSnapshotManager m_parent = null;
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private bool m_stale = true;
  44. public void Initialize(Scene scene, DataSnapshotManager parent)
  45. {
  46. m_scene = scene;
  47. // m_parent = parent;
  48. //To check for staleness, we must catch all incoming client packets.
  49. m_scene.EventManager.OnNewClient += OnNewClient;
  50. m_scene.EventManager.OnParcelPrimCountAdd += delegate(SceneObjectGroup obj) { this.Stale = true; };
  51. }
  52. public void OnNewClient(IClientAPI client)
  53. {
  54. //Detect object data changes by hooking into the IClientAPI.
  55. //Very dirty, and breaks whenever someone changes the client API.
  56. client.OnAddPrim += delegate (LLUUID ownerID, LLVector3 RayEnd, LLQuaternion rot,
  57. PrimitiveBaseShape shape, byte bypassRaycast, LLVector3 RayStart, LLUUID RayTargetID,
  58. byte RayEndIsIntersection) { this.Stale = true; };
  59. client.OnLinkObjects += delegate (IClientAPI remoteClient, uint parent, List<uint> children)
  60. { this.Stale = true; };
  61. client.OnDelinkObjects += delegate(List<uint> primIds) { this.Stale = true; };
  62. client.OnGrabUpdate += delegate(LLUUID objectID, LLVector3 offset, LLVector3 grapPos,
  63. IClientAPI remoteClient) { this.Stale = true; };
  64. client.OnObjectAttach += delegate(IClientAPI remoteClient, uint objectLocalID, uint AttachmentPt,
  65. LLQuaternion rot) { this.Stale = true; };
  66. client.OnObjectDuplicate += delegate(uint localID, LLVector3 offset, uint dupeFlags, LLUUID AgentID,
  67. LLUUID GroupID) { this.Stale = true; };
  68. client.OnObjectDuplicateOnRay += delegate(uint localID, uint dupeFlags, LLUUID AgentID, LLUUID GroupID,
  69. LLUUID RayTargetObj, LLVector3 RayEnd, LLVector3 RayStart, bool BypassRaycast,
  70. bool RayEndIsIntersection, bool CopyCenters, bool CopyRotates) { this.Stale = true; };
  71. client.OnObjectIncludeInSearch += delegate(IClientAPI remoteClient, bool IncludeInSearch, uint localID)
  72. { this.Stale = true; };
  73. client.OnObjectPermissions += delegate(IClientAPI controller, LLUUID agentID, LLUUID sessionID,
  74. byte field, uint localId, uint mask, byte set) { this.Stale = true; };
  75. client.OnRezObject += delegate(IClientAPI remoteClient, LLUUID itemID, LLVector3 RayEnd,
  76. LLVector3 RayStart, LLUUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
  77. uint EveryoneMask, uint GroupMask, uint NextOwnerMask, uint ItemFlags, bool RezSelected,
  78. bool RemoveItem, LLUUID fromTaskID) { this.Stale = true; };
  79. }
  80. public Scene GetParentScene
  81. {
  82. get { return m_scene; }
  83. }
  84. public XmlNode RequestSnapshotData(XmlDocument nodeFactory)
  85. {
  86. m_log.Debug("[DATASNAPSHOT]: Generating object data for scene " + m_scene.RegionInfo.RegionName);
  87. XmlNode parent = nodeFactory.CreateNode(XmlNodeType.Element, "objectdata", "");
  88. XmlNode node;
  89. foreach (EntityBase entity in m_scene.Entities.Values)
  90. {
  91. // only objects, not avatars
  92. if (entity is SceneObjectGroup)
  93. {
  94. SceneObjectGroup obj = (SceneObjectGroup)entity;
  95. m_log.Debug("[DATASNAPSHOT]: Found object " + obj.Name + " in scene");
  96. if ((obj.RootPart.Flags & LLObject.ObjectFlags.JointWheel) == LLObject.ObjectFlags.JointWheel) {
  97. XmlNode xmlobject = nodeFactory.CreateNode(XmlNodeType.Element, "object", "");
  98. node = nodeFactory.CreateNode(XmlNodeType.Element, "uuid", "");
  99. node.InnerText = obj.UUID.ToString();
  100. xmlobject.AppendChild(node);
  101. SceneObjectPart m_rootPart = null;
  102. try
  103. {
  104. Type sog = typeof(SceneObjectGroup);
  105. FieldInfo rootField = sog.GetField("m_rootPart", BindingFlags.NonPublic | BindingFlags.Instance);
  106. if (rootField != null)
  107. {
  108. m_rootPart = (SceneObjectPart)rootField.GetValue(obj);
  109. }
  110. }
  111. catch (Exception e)
  112. {
  113. Console.WriteLine("[DATASNAPSHOT] couldn't access field reflectively\n" + e.ToString());
  114. }
  115. if (m_rootPart != null)
  116. {
  117. node = nodeFactory.CreateNode(XmlNodeType.Element, "title", "");
  118. node.InnerText = m_rootPart.Name;
  119. xmlobject.AppendChild(node);
  120. node = nodeFactory.CreateNode(XmlNodeType.Element, "description", "");
  121. node.InnerText = m_rootPart.Description;
  122. xmlobject.AppendChild(node);
  123. node = nodeFactory.CreateNode(XmlNodeType.Element, "flags", "");
  124. node.InnerText = String.Format("{0:x}", m_rootPart.ObjectFlags);
  125. xmlobject.AppendChild(node);
  126. }
  127. parent.AppendChild(xmlobject);
  128. }
  129. }
  130. }
  131. this.Stale = false;
  132. return parent;
  133. }
  134. public String Name
  135. {
  136. get { return "ObjectSnapshot"; }
  137. }
  138. public bool Stale
  139. {
  140. get
  141. {
  142. return m_stale;
  143. }
  144. set
  145. {
  146. m_stale = value;
  147. if (m_stale)
  148. OnStale(this);
  149. }
  150. }
  151. public event ProviderStale OnStale;
  152. }
  153. }