ContentManagementEntity.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 ContentManagementEntity : MetaEntity
  41. {
  42. #region Static Fields
  43. // static float TimeToDiff = 0;
  44. // static float TimeToCreateEntities = 0;
  45. #endregion Static Fields
  46. #region Fields
  47. protected Dictionary<UUID, AuraMetaEntity> m_AuraEntities = new Dictionary<UUID, AuraMetaEntity>();
  48. protected Dictionary<UUID, BeamMetaEntity> m_BeamEntities = new Dictionary<UUID, BeamMetaEntity>();
  49. // The LinkNum of parts in m_Entity and m_UnchangedEntity are the same though UUID and LocalId are different.
  50. // This can come in handy.
  51. protected SceneObjectGroup m_UnchangedEntity = null;
  52. /// <value>
  53. /// Should be set to true when there is a difference between m_UnchangedEntity and the corresponding scene object group in the scene entity list.
  54. /// </value>
  55. bool DiffersFromSceneGroup = false;
  56. #endregion Fields
  57. #region Constructors
  58. public ContentManagementEntity(SceneObjectGroup Unchanged, bool physics)
  59. : base(Unchanged, false)
  60. {
  61. m_UnchangedEntity = Unchanged.Copy(false);
  62. }
  63. public ContentManagementEntity(string objectXML, Scene scene, bool physics)
  64. : base(objectXML, scene, false)
  65. {
  66. m_UnchangedEntity = SceneObjectSerializer.FromXml2Format(objectXML);
  67. }
  68. #endregion Constructors
  69. #region Public Properties
  70. public SceneObjectGroup UnchangedEntity
  71. {
  72. get { return m_UnchangedEntity; }
  73. }
  74. #endregion Public Properties
  75. #region Private Methods
  76. /// <summary>
  77. /// Check if an entitybase list (like that returned by scene.GetEntities()) contains a group with the rootpart uuid that matches the current uuid.
  78. /// </summary>
  79. private bool ContainsKey(List<EntityBase> list, UUID uuid)
  80. {
  81. foreach (EntityBase part in list)
  82. if (part.UUID == uuid)
  83. return true;
  84. return false;
  85. }
  86. private SceneObjectGroup GetGroupByUUID(System.Collections.Generic.List<EntityBase> list, UUID uuid)
  87. {
  88. foreach (EntityBase ent in list)
  89. {
  90. if (ent is SceneObjectGroup)
  91. if (ent.UUID == uuid)
  92. return (SceneObjectGroup)ent;
  93. }
  94. return null;
  95. }
  96. #endregion Private Methods
  97. #region Public Methods
  98. /// <summary>
  99. /// Search for a corresponding group UUID in the scene. If not found, then the revisioned group this CMEntity represents has been deleted. Mark the metaentity appropriately.
  100. /// If a matching UUID is found in a scene object group, compare the two for differences. If differences exist, Mark the metaentity appropriately.
  101. /// </summary>
  102. public void FindDifferences()
  103. {
  104. List<EntityBase> sceneEntityList = new List<EntityBase>(m_Entity.Scene.GetEntities());
  105. DiffersFromSceneGroup = false;
  106. // if group is not contained in scene's list
  107. if (!ContainsKey(sceneEntityList, m_UnchangedEntity.UUID))
  108. {
  109. lock (m_UnchangedEntity.Children)
  110. {
  111. foreach (SceneObjectPart part in m_UnchangedEntity.Children.Values)
  112. {
  113. // if scene list no longer contains this part, display translucent part and mark with red aura
  114. if (!ContainsKey(sceneEntityList, part.UUID))
  115. {
  116. // if already displaying a red aura over part, make sure its red
  117. if (m_AuraEntities.ContainsKey(part.UUID))
  118. {
  119. m_AuraEntities[part.UUID].SetAura(new Vector3(254,0,0), part.Scale);
  120. }
  121. else
  122. {
  123. AuraMetaEntity auraGroup = new AuraMetaEntity(m_Entity.Scene,
  124. part.GetWorldPosition(),
  125. MetaEntity.TRANSLUCENT,
  126. new Vector3(254,0,0),
  127. part.Scale
  128. );
  129. m_AuraEntities.Add(part.UUID, auraGroup);
  130. }
  131. SceneObjectPart metaPart = m_Entity.GetLinkNumPart(part.LinkNum);
  132. SetPartTransparency(metaPart, MetaEntity.TRANSLUCENT);
  133. }
  134. // otherwise, scene will not contain the part. note: a group can not remove a part without changing group id
  135. }
  136. }
  137. // a deleted part has no where to point a beam particle system,
  138. // if a metapart had a particle system (maybe it represented a moved part) remove it
  139. if (m_BeamEntities.ContainsKey(m_UnchangedEntity.RootPart.UUID))
  140. {
  141. m_BeamEntities[m_UnchangedEntity.RootPart.UUID].HideFromAll();
  142. m_BeamEntities.Remove(m_UnchangedEntity.RootPart.UUID);
  143. }
  144. DiffersFromSceneGroup = true;
  145. }
  146. // if scene list does contain group, compare each part in group for differences and display beams and auras appropriately
  147. else
  148. {
  149. MarkWithDifferences((SceneObjectGroup)GetGroupByUUID(sceneEntityList, m_UnchangedEntity.UUID));
  150. }
  151. }
  152. /// <summary>
  153. /// Check if the revisioned scene object group that this CMEntity is based off of contains a child with the given UUID.
  154. /// </summary>
  155. public bool HasChildPrim(UUID uuid)
  156. {
  157. lock (m_UnchangedEntity.Children)
  158. if (m_UnchangedEntity.Children.ContainsKey(uuid))
  159. return true;
  160. return false;
  161. }
  162. /// <summary>
  163. /// Check if the revisioned scene object group that this CMEntity is based off of contains a child with the given LocalId.
  164. /// </summary>
  165. public bool HasChildPrim(uint localID)
  166. {
  167. lock (m_UnchangedEntity.Children)
  168. {
  169. foreach (SceneObjectPart part in m_UnchangedEntity.Children.Values)
  170. if (part.LocalId == localID)
  171. return true;
  172. }
  173. return false;
  174. }
  175. public override void Hide(IClientAPI client)
  176. {
  177. base.Hide(client);
  178. foreach (MetaEntity group in m_AuraEntities.Values)
  179. group.Hide(client);
  180. foreach (MetaEntity group in m_BeamEntities.Values)
  181. group.Hide(client);
  182. }
  183. public override void HideFromAll()
  184. {
  185. base.HideFromAll();
  186. foreach (MetaEntity group in m_AuraEntities.Values)
  187. group.HideFromAll();
  188. foreach (MetaEntity group in m_BeamEntities.Values)
  189. group.HideFromAll();
  190. }
  191. /// <summary>
  192. /// Returns true if there was a change between meta entity and the entity group, false otherwise.
  193. /// If true is returned, it is assumed the metaentity's appearance has changed to reflect the difference (though clients haven't been updated).
  194. /// </summary>
  195. public bool MarkWithDifferences(SceneObjectGroup sceneEntityGroup)
  196. {
  197. SceneObjectPart sceneEntityPart;
  198. SceneObjectPart metaEntityPart;
  199. Diff differences;
  200. bool changed = false;
  201. // Use "UnchangedEntity" to do comparisons because its text, transparency, and other attributes will be just as the user
  202. // had originally saved.
  203. // m_Entity will NOT necessarily be the same entity as the user had saved.
  204. lock (m_UnchangedEntity.Children)
  205. {
  206. foreach (SceneObjectPart UnchangedPart in m_UnchangedEntity.Children.Values)
  207. {
  208. //This is the part that we use to show changes.
  209. metaEntityPart = m_Entity.GetLinkNumPart(UnchangedPart.LinkNum);
  210. if (sceneEntityGroup.Children.ContainsKey(UnchangedPart.UUID))
  211. {
  212. sceneEntityPart = sceneEntityGroup.Children[UnchangedPart.UUID];
  213. differences = Difference.FindDifferences(UnchangedPart, sceneEntityPart);
  214. if (differences != Diff.NONE)
  215. metaEntityPart.Text = "CHANGE: " + differences.ToString();
  216. if (differences != 0)
  217. {
  218. // Root Part that has been modified
  219. if ((differences&Diff.POSITION) > 0)
  220. {
  221. // If the position of any part has changed, make sure the RootPart of the
  222. // meta entity is pointing with a beam particle system
  223. if (m_BeamEntities.ContainsKey(m_UnchangedEntity.RootPart.UUID))
  224. {
  225. m_BeamEntities[m_UnchangedEntity.RootPart.UUID].HideFromAll();
  226. m_BeamEntities.Remove(m_UnchangedEntity.RootPart.UUID);
  227. }
  228. BeamMetaEntity beamGroup = new BeamMetaEntity(m_Entity.Scene,
  229. m_UnchangedEntity.RootPart.GetWorldPosition(),
  230. MetaEntity.TRANSLUCENT,
  231. sceneEntityPart,
  232. new Vector3(0,0,254)
  233. );
  234. m_BeamEntities.Add(m_UnchangedEntity.RootPart.UUID, beamGroup);
  235. }
  236. if (m_AuraEntities.ContainsKey(UnchangedPart.UUID))
  237. {
  238. m_AuraEntities[UnchangedPart.UUID].HideFromAll();
  239. m_AuraEntities.Remove(UnchangedPart.UUID);
  240. }
  241. AuraMetaEntity auraGroup = new AuraMetaEntity(m_Entity.Scene,
  242. UnchangedPart.GetWorldPosition(),
  243. MetaEntity.TRANSLUCENT,
  244. new Vector3(0,0,254),
  245. UnchangedPart.Scale
  246. );
  247. m_AuraEntities.Add(UnchangedPart.UUID, auraGroup);
  248. SetPartTransparency(metaEntityPart, MetaEntity.TRANSLUCENT);
  249. DiffersFromSceneGroup = true;
  250. }
  251. else // no differences between scene part and meta part
  252. {
  253. if (m_BeamEntities.ContainsKey(m_UnchangedEntity.RootPart.UUID))
  254. {
  255. m_BeamEntities[m_UnchangedEntity.RootPart.UUID].HideFromAll();
  256. m_BeamEntities.Remove(m_UnchangedEntity.RootPart.UUID);
  257. }
  258. if (m_AuraEntities.ContainsKey(UnchangedPart.UUID))
  259. {
  260. m_AuraEntities[UnchangedPart.UUID].HideFromAll();
  261. m_AuraEntities.Remove(UnchangedPart.UUID);
  262. }
  263. SetPartTransparency(metaEntityPart, MetaEntity.NONE);
  264. }
  265. }
  266. else //The entity currently in the scene is missing parts from the metaentity saved, so mark parts red as deleted.
  267. {
  268. if (m_AuraEntities.ContainsKey(UnchangedPart.UUID))
  269. {
  270. m_AuraEntities[UnchangedPart.UUID].HideFromAll();
  271. m_AuraEntities.Remove(UnchangedPart.UUID);
  272. }
  273. AuraMetaEntity auraGroup = new AuraMetaEntity(m_Entity.Scene,
  274. UnchangedPart.GetWorldPosition(),
  275. MetaEntity.TRANSLUCENT,
  276. new Vector3(254,0,0),
  277. UnchangedPart.Scale
  278. );
  279. m_AuraEntities.Add(UnchangedPart.UUID, auraGroup);
  280. SetPartTransparency(metaEntityPart, MetaEntity.TRANSLUCENT);
  281. DiffersFromSceneGroup = true;
  282. }
  283. }
  284. }
  285. return changed;
  286. }
  287. public void SendFullAuraUpdate(IClientAPI client)
  288. {
  289. if (DiffersFromSceneGroup)
  290. {
  291. foreach (AuraMetaEntity group in m_AuraEntities.Values)
  292. group.SendFullUpdate(client);
  293. }
  294. }
  295. public void SendFullAuraUpdateToAll()
  296. {
  297. if (DiffersFromSceneGroup)
  298. {
  299. foreach (AuraMetaEntity group in m_AuraEntities.Values)
  300. group.SendFullUpdateToAll();
  301. }
  302. }
  303. public void SendFullBeamUpdate(IClientAPI client)
  304. {
  305. if (DiffersFromSceneGroup)
  306. {
  307. foreach (BeamMetaEntity group in m_BeamEntities.Values)
  308. group.SendFullUpdate(client);
  309. }
  310. }
  311. public void SendFullBeamUpdateToAll()
  312. {
  313. if (DiffersFromSceneGroup)
  314. {
  315. foreach (BeamMetaEntity group in m_BeamEntities.Values)
  316. group.SendFullUpdateToAll();
  317. }
  318. }
  319. public void SendFullDiffUpdate(IClientAPI client)
  320. {
  321. FindDifferences();
  322. if (DiffersFromSceneGroup)
  323. {
  324. SendFullUpdate(client);
  325. SendFullAuraUpdate(client);
  326. SendFullBeamUpdate(client);
  327. }
  328. }
  329. public void SendFullDiffUpdateToAll()
  330. {
  331. FindDifferences();
  332. if (DiffersFromSceneGroup)
  333. {
  334. SendFullUpdateToAll();
  335. SendFullAuraUpdateToAll();
  336. SendFullBeamUpdateToAll();
  337. }
  338. }
  339. #endregion Public Methods
  340. }
  341. }