ContentManagementEntity.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. foreach (SceneObjectPart part in m_UnchangedEntity.Parts)
  110. {
  111. // if scene list no longer contains this part, display translucent part and mark with red aura
  112. if (!ContainsKey(sceneEntityList, part.UUID))
  113. {
  114. // if already displaying a red aura over part, make sure its red
  115. if (m_AuraEntities.ContainsKey(part.UUID))
  116. {
  117. m_AuraEntities[part.UUID].SetAura(new Vector3(254, 0, 0), part.Scale);
  118. }
  119. else
  120. {
  121. AuraMetaEntity auraGroup = new AuraMetaEntity(m_Entity.Scene,
  122. part.GetWorldPosition(),
  123. MetaEntity.TRANSLUCENT,
  124. new Vector3(254, 0, 0),
  125. part.Scale
  126. );
  127. m_AuraEntities.Add(part.UUID, auraGroup);
  128. }
  129. SceneObjectPart metaPart = m_Entity.GetLinkNumPart(part.LinkNum);
  130. SetPartTransparency(metaPart, MetaEntity.TRANSLUCENT);
  131. }
  132. // otherwise, scene will not contain the part. note: a group can not remove a part without changing group id
  133. }
  134. // a deleted part has no where to point a beam particle system,
  135. // if a metapart had a particle system (maybe it represented a moved part) remove it
  136. if (m_BeamEntities.ContainsKey(m_UnchangedEntity.RootPart.UUID))
  137. {
  138. m_BeamEntities[m_UnchangedEntity.RootPart.UUID].HideFromAll();
  139. m_BeamEntities.Remove(m_UnchangedEntity.RootPart.UUID);
  140. }
  141. DiffersFromSceneGroup = true;
  142. }
  143. // if scene list does contain group, compare each part in group for differences and display beams and auras appropriately
  144. else
  145. {
  146. MarkWithDifferences((SceneObjectGroup)GetGroupByUUID(sceneEntityList, m_UnchangedEntity.UUID));
  147. }
  148. }
  149. /// <summary>
  150. /// Check if the revisioned scene object group that this CMEntity is based off of contains a child with the given UUID.
  151. /// </summary>
  152. public bool HasChildPrim(UUID uuid)
  153. {
  154. return m_UnchangedEntity.ContainsPart(uuid);
  155. }
  156. /// <summary>
  157. /// Check if the revisioned scene object group that this CMEntity is based off of contains a child with the given LocalId.
  158. /// </summary>
  159. public bool HasChildPrim(uint localID)
  160. {
  161. foreach (SceneObjectPart part in m_UnchangedEntity.Parts)
  162. if (part.LocalId == localID)
  163. return true;
  164. return false;
  165. }
  166. public override void Hide(IClientAPI client)
  167. {
  168. base.Hide(client);
  169. foreach (MetaEntity group in m_AuraEntities.Values)
  170. group.Hide(client);
  171. foreach (MetaEntity group in m_BeamEntities.Values)
  172. group.Hide(client);
  173. }
  174. public override void HideFromAll()
  175. {
  176. base.HideFromAll();
  177. foreach (MetaEntity group in m_AuraEntities.Values)
  178. group.HideFromAll();
  179. foreach (MetaEntity group in m_BeamEntities.Values)
  180. group.HideFromAll();
  181. }
  182. /// <summary>
  183. /// Returns true if there was a change between meta entity and the entity group, false otherwise.
  184. /// If true is returned, it is assumed the metaentity's appearance has changed to reflect the difference (though clients haven't been updated).
  185. /// </summary>
  186. public bool MarkWithDifferences(SceneObjectGroup sceneEntityGroup)
  187. {
  188. SceneObjectPart sceneEntityPart;
  189. SceneObjectPart metaEntityPart;
  190. Diff differences;
  191. bool changed = false;
  192. // Use "UnchangedEntity" to do comparisons because its text, transparency, and other attributes will be just as the user
  193. // had originally saved.
  194. // m_Entity will NOT necessarily be the same entity as the user had saved.
  195. foreach (SceneObjectPart UnchangedPart in m_UnchangedEntity.Parts)
  196. {
  197. //This is the part that we use to show changes.
  198. metaEntityPart = m_Entity.GetLinkNumPart(UnchangedPart.LinkNum);
  199. if (sceneEntityGroup.ContainsPart(UnchangedPart.UUID))
  200. {
  201. sceneEntityPart = sceneEntityGroup.GetChildPart(UnchangedPart.UUID);
  202. differences = Difference.FindDifferences(UnchangedPart, sceneEntityPart);
  203. if (differences != Diff.NONE)
  204. metaEntityPart.Text = "CHANGE: " + differences.ToString();
  205. if (differences != 0)
  206. {
  207. // Root Part that has been modified
  208. if ((differences & Diff.POSITION) > 0)
  209. {
  210. // If the position of any part has changed, make sure the RootPart of the
  211. // meta entity is pointing with a beam particle system
  212. if (m_BeamEntities.ContainsKey(m_UnchangedEntity.RootPart.UUID))
  213. {
  214. m_BeamEntities[m_UnchangedEntity.RootPart.UUID].HideFromAll();
  215. m_BeamEntities.Remove(m_UnchangedEntity.RootPart.UUID);
  216. }
  217. BeamMetaEntity beamGroup = new BeamMetaEntity(m_Entity.Scene,
  218. m_UnchangedEntity.RootPart.GetWorldPosition(),
  219. MetaEntity.TRANSLUCENT,
  220. sceneEntityPart,
  221. new Vector3(0, 0, 254)
  222. );
  223. m_BeamEntities.Add(m_UnchangedEntity.RootPart.UUID, beamGroup);
  224. }
  225. if (m_AuraEntities.ContainsKey(UnchangedPart.UUID))
  226. {
  227. m_AuraEntities[UnchangedPart.UUID].HideFromAll();
  228. m_AuraEntities.Remove(UnchangedPart.UUID);
  229. }
  230. AuraMetaEntity auraGroup = new AuraMetaEntity(m_Entity.Scene,
  231. UnchangedPart.GetWorldPosition(),
  232. MetaEntity.TRANSLUCENT,
  233. new Vector3(0, 0, 254),
  234. UnchangedPart.Scale
  235. );
  236. m_AuraEntities.Add(UnchangedPart.UUID, auraGroup);
  237. SetPartTransparency(metaEntityPart, MetaEntity.TRANSLUCENT);
  238. DiffersFromSceneGroup = true;
  239. }
  240. else // no differences between scene part and meta part
  241. {
  242. if (m_BeamEntities.ContainsKey(m_UnchangedEntity.RootPart.UUID))
  243. {
  244. m_BeamEntities[m_UnchangedEntity.RootPart.UUID].HideFromAll();
  245. m_BeamEntities.Remove(m_UnchangedEntity.RootPart.UUID);
  246. }
  247. if (m_AuraEntities.ContainsKey(UnchangedPart.UUID))
  248. {
  249. m_AuraEntities[UnchangedPart.UUID].HideFromAll();
  250. m_AuraEntities.Remove(UnchangedPart.UUID);
  251. }
  252. SetPartTransparency(metaEntityPart, MetaEntity.NONE);
  253. }
  254. }
  255. else //The entity currently in the scene is missing parts from the metaentity saved, so mark parts red as deleted.
  256. {
  257. if (m_AuraEntities.ContainsKey(UnchangedPart.UUID))
  258. {
  259. m_AuraEntities[UnchangedPart.UUID].HideFromAll();
  260. m_AuraEntities.Remove(UnchangedPart.UUID);
  261. }
  262. AuraMetaEntity auraGroup = new AuraMetaEntity(m_Entity.Scene,
  263. UnchangedPart.GetWorldPosition(),
  264. MetaEntity.TRANSLUCENT,
  265. new Vector3(254, 0, 0),
  266. UnchangedPart.Scale
  267. );
  268. m_AuraEntities.Add(UnchangedPart.UUID, auraGroup);
  269. SetPartTransparency(metaEntityPart, MetaEntity.TRANSLUCENT);
  270. DiffersFromSceneGroup = true;
  271. }
  272. }
  273. return changed;
  274. }
  275. public void SendFullAuraUpdate(IClientAPI client)
  276. {
  277. if (DiffersFromSceneGroup)
  278. {
  279. foreach (AuraMetaEntity group in m_AuraEntities.Values)
  280. group.SendFullUpdate(client);
  281. }
  282. }
  283. public void SendFullAuraUpdateToAll()
  284. {
  285. if (DiffersFromSceneGroup)
  286. {
  287. foreach (AuraMetaEntity group in m_AuraEntities.Values)
  288. group.SendFullUpdateToAll();
  289. }
  290. }
  291. public void SendFullBeamUpdate(IClientAPI client)
  292. {
  293. if (DiffersFromSceneGroup)
  294. {
  295. foreach (BeamMetaEntity group in m_BeamEntities.Values)
  296. group.SendFullUpdate(client);
  297. }
  298. }
  299. public void SendFullBeamUpdateToAll()
  300. {
  301. if (DiffersFromSceneGroup)
  302. {
  303. foreach (BeamMetaEntity group in m_BeamEntities.Values)
  304. group.SendFullUpdateToAll();
  305. }
  306. }
  307. public void SendFullDiffUpdate(IClientAPI client)
  308. {
  309. FindDifferences();
  310. if (DiffersFromSceneGroup)
  311. {
  312. SendFullUpdate(client);
  313. SendFullAuraUpdate(client);
  314. SendFullBeamUpdate(client);
  315. }
  316. }
  317. public void SendFullDiffUpdateToAll()
  318. {
  319. FindDifferences();
  320. if (DiffersFromSceneGroup)
  321. {
  322. SendFullUpdateToAll();
  323. SendFullAuraUpdateToAll();
  324. SendFullBeamUpdateToAll();
  325. }
  326. }
  327. #endregion Public Methods
  328. }
  329. }