ContentManagementEntity.cs 16 KB

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