MetaEntity.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(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. {
  151. m_Entity.Scene.ForEachClient(
  152. delegate(IClientAPI controller)
  153. { controller.SendKillObject(m_Entity.RegionHandle, part.LocalId); }
  154. );
  155. }
  156. }
  157. public void SendFullUpdate(IClientAPI client)
  158. {
  159. // Not sure what clientFlags should be but 0 seems to work
  160. SendFullUpdate(client, 0);
  161. }
  162. public void SendFullUpdate(IClientAPI client, uint clientFlags)
  163. {
  164. m_Entity.SendFullUpdateToClient(client);
  165. }
  166. public void SendFullUpdateToAll()
  167. {
  168. m_Entity.Scene.ForEachClient(
  169. delegate(IClientAPI controller)
  170. { m_Entity.SendFullUpdateToClient(controller); }
  171. );
  172. }
  173. /// <summary>
  174. /// Makes a single SceneObjectPart see through.
  175. /// </summary>
  176. /// <param name="part">
  177. /// A <see cref="SceneObjectPart"/>
  178. /// The part to make see through
  179. /// </param>
  180. /// <param name="transparencyAmount">
  181. /// A <see cref="System.Single"/>
  182. /// The degree of transparency to imbue the part with, 0f being solid and .95f being invisible.
  183. /// </param>
  184. public static void SetPartTransparency(SceneObjectPart part, float transparencyAmount)
  185. {
  186. Primitive.TextureEntry tex = null;
  187. Color4 texcolor;
  188. try
  189. {
  190. tex = part.Shape.Textures;
  191. texcolor = new Color4();
  192. }
  193. catch(Exception)
  194. {
  195. //m_log.ErrorFormat("[Content Management]: Exception thrown while accessing textures of scene object: " + e);
  196. return;
  197. }
  198. for (uint i = 0; i < tex.FaceTextures.Length; i++)
  199. {
  200. try {
  201. if (tex.FaceTextures[i] != null)
  202. {
  203. texcolor = tex.FaceTextures[i].RGBA;
  204. texcolor.A = transparencyAmount;
  205. tex.FaceTextures[i].RGBA = texcolor;
  206. }
  207. }
  208. catch (Exception)
  209. {
  210. //m_log.ErrorFormat("[Content Management]: Exception thrown while accessing different face textures of object: " + e);
  211. continue;
  212. }
  213. }
  214. try {
  215. texcolor = tex.DefaultTexture.RGBA;
  216. texcolor.A = transparencyAmount;
  217. tex.DefaultTexture.RGBA = texcolor;
  218. part.Shape.TextureEntry = tex.GetBytes();
  219. }
  220. catch (Exception)
  221. {
  222. //m_log.Info("[Content Management]: Exception thrown while accessing default face texture of object: " + e);
  223. }
  224. }
  225. #endregion Public Methods
  226. }
  227. }