1
0

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