MetaEntity.cs 8.9 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.Drawing;
  30. using OpenMetaverse;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Region.Framework.Scenes.Serialization;
  36. using OpenSim.Region.Physics.Manager;
  37. using log4net;
  38. namespace OpenSim.Region.OptionalModules.ContentManagement
  39. {
  40. public class MetaEntity
  41. {
  42. #region Constants
  43. public const float INVISIBLE = .95f;
  44. // Settings for transparency of metaentity
  45. public const float NONE = 0f;
  46. public const float TRANSLUCENT = .5f;
  47. #endregion Constants
  48. #region Static Fields
  49. //private static readonly ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  50. #endregion Static Fields
  51. #region Fields
  52. protected SceneObjectGroup m_Entity = null; // The scene object group that represents this meta entity.
  53. protected uint m_metaLocalid;
  54. #endregion Fields
  55. #region Constructors
  56. public MetaEntity()
  57. {
  58. }
  59. /// <summary>
  60. /// Makes a new meta entity by copying the given scene object group.
  61. /// The physics boolean is just a stub right now.
  62. /// </summary>
  63. public MetaEntity(SceneObjectGroup orig, bool physics)
  64. {
  65. m_Entity = orig.Copy(orig.RootPart.OwnerID, orig.RootPart.GroupID, false);
  66. Initialize(physics);
  67. }
  68. /// <summary>
  69. /// Takes an XML description of a scene object group and converts it to a meta entity.
  70. /// </summary>
  71. public MetaEntity(string objectXML, Scene scene, bool physics)
  72. {
  73. m_Entity = SceneObjectSerializer.FromXml2Format(objectXML);
  74. m_Entity.SetScene(scene);
  75. Initialize(physics);
  76. }
  77. #endregion Constructors
  78. #region Public Properties
  79. public Dictionary<UUID, SceneObjectPart> Children
  80. {
  81. get { return m_Entity.Children; }
  82. set { m_Entity.Children = value; }
  83. }
  84. public uint LocalId
  85. {
  86. get { return m_Entity.LocalId; }
  87. set { m_Entity.LocalId = value; }
  88. }
  89. public SceneObjectGroup ObjectGroup
  90. {
  91. get { return m_Entity; }
  92. }
  93. public int PrimCount
  94. {
  95. get { return m_Entity.PrimCount; }
  96. }
  97. public SceneObjectPart RootPart
  98. {
  99. get { return m_Entity.RootPart; }
  100. }
  101. public Scene Scene
  102. {
  103. get { return m_Entity.Scene; }
  104. }
  105. public UUID UUID
  106. {
  107. get { return m_Entity.UUID; }
  108. set { m_Entity.UUID = value; }
  109. }
  110. #endregion Public Properties
  111. #region Protected Methods
  112. // The metaentity objectgroup must have unique localids as well as unique uuids.
  113. // localids are used by the client to refer to parts.
  114. // uuids are sent to the client and back to the server to identify parts on the server side.
  115. /// <summary>
  116. /// Changes localids and uuids of m_Entity.
  117. /// </summary>
  118. protected void Initialize(bool physics)
  119. {
  120. //make new uuids
  121. Dictionary<UUID, SceneObjectPart> parts = new Dictionary<UUID, SceneObjectPart>();
  122. foreach (SceneObjectPart part in m_Entity.Children.Values)
  123. {
  124. part.ResetIDs(part.LinkNum);
  125. parts.Add(part.UUID, part);
  126. }
  127. //finalize
  128. m_Entity.RootPart.PhysActor = null;
  129. m_Entity.Children = parts;
  130. }
  131. #endregion Protected Methods
  132. #region Public Methods
  133. /// <summary>
  134. /// Hides the metaentity from a single client.
  135. /// </summary>
  136. public virtual void Hide(IClientAPI client)
  137. {
  138. //This deletes the group without removing from any databases.
  139. //This is important because we are not IN any database.
  140. //m_Entity.FakeDeleteGroup();
  141. foreach (SceneObjectPart part in m_Entity.Children.Values)
  142. client.SendKillObject(m_Entity.RegionHandle, part.LocalId);
  143. }
  144. /// <summary>
  145. /// Sends a kill object message to all clients, effectively "hiding" the metaentity even though it's still on the server.
  146. /// </summary>
  147. public virtual void HideFromAll()
  148. {
  149. foreach (SceneObjectPart part in m_Entity.Children.Values)
  150. m_Entity.Scene.ClientManager.ForEachClient(delegate(IClientAPI controller)
  151. { controller.SendKillObject(m_Entity.RegionHandle, part.LocalId); }
  152. );
  153. }
  154. public void SendFullUpdate(IClientAPI client)
  155. {
  156. // Not sure what clientFlags should be but 0 seems to work
  157. SendFullUpdate(client, 0);
  158. }
  159. public void SendFullUpdate(IClientAPI client, uint clientFlags)
  160. {
  161. m_Entity.SendFullUpdateToClient(client);
  162. }
  163. public void SendFullUpdateToAll()
  164. {
  165. m_Entity.Scene.ClientManager.ForEachClient(delegate(IClientAPI controller)
  166. { m_Entity.SendFullUpdateToClient(controller); }
  167. );
  168. }
  169. /// <summary>
  170. /// Makes a single SceneObjectPart see through.
  171. /// </summary>
  172. /// <param name="part">
  173. /// A <see cref="SceneObjectPart"/>
  174. /// The part to make see through
  175. /// </param>
  176. /// <param name="transparencyAmount">
  177. /// A <see cref="System.Single"/>
  178. /// The degree of transparency to imbue the part with, 0f being solid and .95f being invisible.
  179. /// </param>
  180. public static void SetPartTransparency(SceneObjectPart part, float transparencyAmount)
  181. {
  182. Primitive.TextureEntry tex = null;
  183. Color4 texcolor;
  184. try
  185. {
  186. tex = part.Shape.Textures;
  187. texcolor = new Color4();
  188. }
  189. catch(Exception)
  190. {
  191. //m_log.ErrorFormat("[Content Management]: Exception thrown while accessing textures of scene object: " + e);
  192. return;
  193. }
  194. for (uint i = 0; i < tex.FaceTextures.Length; i++)
  195. {
  196. try {
  197. if (tex.FaceTextures[i] != null)
  198. {
  199. texcolor = tex.FaceTextures[i].RGBA;
  200. texcolor.A = transparencyAmount;
  201. tex.FaceTextures[i].RGBA = texcolor;
  202. }
  203. }
  204. catch (Exception)
  205. {
  206. //m_log.ErrorFormat("[Content Management]: Exception thrown while accessing different face textures of object: " + e);
  207. continue;
  208. }
  209. }
  210. try {
  211. texcolor = tex.DefaultTexture.RGBA;
  212. texcolor.A = transparencyAmount;
  213. tex.DefaultTexture.RGBA = texcolor;
  214. part.Shape.TextureEntry = tex.GetBytes();
  215. }
  216. catch (Exception)
  217. {
  218. //m_log.Info("[Content Management]: Exception thrown while accessing default face texture of object: " + e);
  219. }
  220. }
  221. #endregion Public Methods
  222. }
  223. }