SceneGraph.cs 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  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.Threading;
  29. using System.Collections.Generic;
  30. using System.Collections.Concurrent;
  31. using System.Reflection;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Packets;
  34. using log4net;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Scenes.Types;
  37. using OpenSim.Region.PhysicsModules.SharedBase;
  38. using OpenSim.Region.Framework.Interfaces;
  39. namespace OpenSim.Region.Framework.Scenes
  40. {
  41. public delegate void PhysicsCrash();
  42. public delegate void AttachToBackupDelegate(SceneObjectGroup sog);
  43. public delegate void DetachFromBackupDelegate(SceneObjectGroup sog);
  44. public delegate void ChangedBackupDelegate(SceneObjectGroup sog);
  45. /// <summary>
  46. /// This class used to be called InnerScene and may not yet truly be a SceneGraph. The non scene graph components
  47. /// should be migrated out over time.
  48. /// </summary>
  49. public class SceneGraph
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. #region Events
  53. protected internal event PhysicsCrash UnRecoverableError;
  54. private PhysicsCrash handlerPhysicsCrash = null;
  55. public event AttachToBackupDelegate OnAttachToBackup;
  56. public event DetachFromBackupDelegate OnDetachFromBackup;
  57. public event ChangedBackupDelegate OnChangeBackup;
  58. #endregion
  59. #region Fields
  60. protected internal EntityManager Entities = new EntityManager();
  61. private Dictionary<UUID, SceneObjectPart> m_scenePartsByID = new Dictionary<UUID, SceneObjectPart>(1024);
  62. private Dictionary<uint, SceneObjectPart> m_scenePartsByLocalID = new Dictionary<uint, SceneObjectPart>(1024);
  63. private SceneObjectPart[] m_scenePartsArray;
  64. private Dictionary<UUID, ScenePresence> m_scenePresenceMap = new Dictionary<UUID, ScenePresence>();
  65. private Dictionary<uint, ScenePresence> m_scenePresenceLocalIDMap = new Dictionary<uint, ScenePresence>();
  66. private Dictionary<UUID, SceneObjectGroup> m_updateList = new Dictionary<UUID, SceneObjectGroup>();
  67. private List<ScenePresence> m_scenePresenceList;
  68. private Scene m_parentScene;
  69. private PhysicsScene _PhyScene;
  70. private int m_numRootAgents = 0;
  71. private int m_numTotalPrim = 0;
  72. private int m_numPrim = 0;
  73. private int m_numMesh = 0;
  74. private int m_numChildAgents = 0;
  75. private int m_physicalPrim = 0;
  76. private int m_activeScripts = 0;
  77. private int m_scriptLPS = 0;
  78. /// <summary>
  79. /// Lock to prevent object group update, linking, delinking and duplication operations from running concurrently.
  80. /// </summary>
  81. /// <remarks>
  82. /// These operations rely on the parts composition of the object. If allowed to run concurrently then race
  83. /// conditions can occur.
  84. /// </remarks>
  85. private Object m_updateLock = new Object();
  86. private Object m_linkLock = new Object();
  87. private System.Threading.ReaderWriterLockSlim m_scenePresencesLock;
  88. private System.Threading.ReaderWriterLockSlim m_scenePartsLock;
  89. #endregion
  90. protected internal SceneGraph(Scene parent)
  91. {
  92. m_scenePresencesLock = new System.Threading.ReaderWriterLockSlim();
  93. m_scenePartsLock = new System.Threading.ReaderWriterLockSlim();
  94. m_parentScene = parent;
  95. m_scenePresenceList = null;
  96. m_scenePartsArray = null;
  97. }
  98. ~SceneGraph()
  99. {
  100. m_scenePartsLock.Dispose();
  101. m_scenePresencesLock.Dispose();
  102. }
  103. public PhysicsScene PhysicsScene
  104. {
  105. get
  106. {
  107. if (_PhyScene == null)
  108. _PhyScene = m_parentScene.RequestModuleInterface<PhysicsScene>();
  109. return _PhyScene;
  110. }
  111. set
  112. {
  113. // If we're not doing the initial set
  114. // Then we've got to remove the previous
  115. // event handler
  116. if (_PhyScene != null)
  117. _PhyScene.OnPhysicsCrash -= physicsBasedCrash;
  118. _PhyScene = value;
  119. if (_PhyScene != null)
  120. _PhyScene.OnPhysicsCrash += physicsBasedCrash;
  121. }
  122. }
  123. protected internal void Close()
  124. {
  125. bool entered = false;
  126. try
  127. {
  128. try { }
  129. finally
  130. {
  131. m_scenePresencesLock.EnterWriteLock();
  132. entered = true;
  133. }
  134. m_scenePresenceMap = new Dictionary<UUID, ScenePresence>();
  135. m_scenePresenceLocalIDMap = new Dictionary<uint, ScenePresence>();
  136. m_scenePresenceList = null;
  137. }
  138. finally
  139. {
  140. if (entered)
  141. m_scenePresencesLock.ExitWriteLock();
  142. }
  143. entered = false;
  144. try
  145. {
  146. try { }
  147. finally
  148. {
  149. m_scenePartsLock.EnterWriteLock();
  150. entered = true;
  151. }
  152. Entities.Clear();
  153. m_scenePartsArray = null;
  154. m_scenePartsByID = new Dictionary<UUID, SceneObjectPart>();
  155. m_scenePartsByLocalID = new Dictionary<uint, SceneObjectPart>();
  156. if (_PhyScene != null)
  157. _PhyScene.OnPhysicsCrash -= physicsBasedCrash;
  158. _PhyScene = null;
  159. }
  160. finally
  161. {
  162. if (entered)
  163. m_scenePartsLock.ExitWriteLock();
  164. }
  165. }
  166. #region Update Methods
  167. protected internal void UpdatePreparePhysics()
  168. {
  169. }
  170. /// <summary>
  171. /// Update the position of all the scene presences.
  172. /// </summary>
  173. /// <remarks>
  174. /// Called only from the main scene loop.
  175. /// </remarks>
  176. protected internal void UpdatePresences()
  177. {
  178. ForEachScenePresence(delegate(ScenePresence presence)
  179. {
  180. presence.Update();
  181. });
  182. }
  183. protected internal void UpdateScenePresenceMovement()
  184. {
  185. ForEachScenePresence(delegate (ScenePresence presence)
  186. {
  187. presence.UpdateMovement();
  188. });
  189. }
  190. /// <summary>
  191. /// Perform a physics frame update.
  192. /// </summary>
  193. /// <param name="elapsed"></param>
  194. /// <returns></returns>
  195. protected internal float UpdatePhysics(double elapsed)
  196. {
  197. if (PhysicsScene != null)
  198. return PhysicsScene.Simulate((float)elapsed);
  199. return 0;
  200. }
  201. protected internal void ProcessPhysicsPreSimulation()
  202. {
  203. if(PhysicsScene != null)
  204. PhysicsScene.ProcessPreSimulation();
  205. }
  206. public void GetCoarseLocations(out List<Vector3> coarseLocations, out List<UUID> avatarUUIDs, uint maxLocations)
  207. {
  208. coarseLocations = new List<Vector3>();
  209. avatarUUIDs = new List<UUID>();
  210. // coarse locations are sent as BYTE, so limited to the 255m max of normal regions
  211. // try to work around that scale down X and Y acording to region size, so reducing the resolution
  212. //
  213. // viewers need to scale up
  214. float scaleX = (float)m_parentScene.RegionInfo.RegionSizeX / (float)Constants.RegionSize;
  215. if (scaleX == 0)
  216. scaleX = 1.0f;
  217. scaleX = 1.0f / scaleX;
  218. float scaleY = (float)m_parentScene.RegionInfo.RegionSizeY / (float)Constants.RegionSize;
  219. if (scaleY == 0)
  220. scaleY = 1.0f;
  221. scaleY = 1.0f / scaleY;
  222. List<ScenePresence> presences = GetScenePresences();
  223. int len = presences.Count;
  224. if(len > maxLocations)
  225. len = (int)maxLocations;
  226. ScenePresence sp;
  227. for (int i = 0; i < len; ++i)
  228. {
  229. sp = presences[i];
  230. // If this presence is a child agent, we don't want its coarse locations
  231. if (sp.IsChildAgent)
  232. continue;
  233. Vector3 pos = sp.AbsolutePosition;
  234. pos.X *= scaleX;
  235. pos.Y *= scaleY;
  236. coarseLocations.Add(pos);
  237. avatarUUIDs.Add(sp.UUID);
  238. }
  239. }
  240. #endregion
  241. #region Entity Methods
  242. /// <summary>
  243. /// Add an object into the scene that has come from storage
  244. /// </summary>
  245. /// <param name="sceneObject"></param>
  246. /// <param name="attachToBackup">
  247. /// If true, changes to the object will be reflected in its persisted data
  248. /// If false, the persisted data will not be changed even if the object in the scene is changed
  249. /// </param>
  250. /// <param name="alreadyPersisted">
  251. /// If true, we won't persist this object until it changes
  252. /// If false, we'll persist this object immediately
  253. /// </param>
  254. /// <param name="sendClientUpdates">
  255. /// If true, we send updates to the client to tell it about this object
  256. /// If false, we leave it up to the caller to do this
  257. /// </param>
  258. /// <returns>
  259. /// true if the object was added, false if an object with the same uuid was already in the scene
  260. /// </returns>
  261. protected internal bool AddRestoredSceneObject(
  262. SceneObjectGroup sceneObject, bool attachToBackup, bool alreadyPersisted, bool sendClientUpdates)
  263. {
  264. // temporary checks to remove after varsize suport
  265. float regionSizeX = m_parentScene.RegionInfo.RegionSizeX;
  266. float regionSizeY = m_parentScene.RegionInfo.RegionSizeY;
  267. // KF: Check for out-of-region, move inside and make static.
  268. Vector3 npos = sceneObject.RootPart.GroupPosition;
  269. bool clampZ = m_parentScene.ClampNegativeZ;
  270. if (!(((sceneObject.RootPart.Shape.PCode == (byte)PCode.Prim) && (sceneObject.RootPart.Shape.State != 0))) && (npos.X < 0.0 || npos.Y < 0.0 || (npos.Z < 0.0 && clampZ) ||
  271. npos.X > regionSizeX ||
  272. npos.Y > regionSizeY))
  273. {
  274. if (npos.X < 0.0) npos.X = 1.0f;
  275. if (npos.Y < 0.0) npos.Y = 1.0f;
  276. if (npos.Z < 0.0 && clampZ) npos.Z = 0.0f;
  277. if (npos.X > regionSizeX) npos.X = regionSizeX - 1.0f;
  278. if (npos.Y > regionSizeY) npos.Y = regionSizeY - 1.0f;
  279. SceneObjectPart rootpart = sceneObject.RootPart;
  280. rootpart.GroupPosition = npos;
  281. foreach (SceneObjectPart part in sceneObject.Parts)
  282. {
  283. if (part == rootpart)
  284. continue;
  285. part.GroupPosition = npos;
  286. }
  287. rootpart.Velocity = Vector3.Zero;
  288. rootpart.AngularVelocity = Vector3.Zero;
  289. rootpart.Acceleration = Vector3.Zero;
  290. }
  291. bool ret = AddSceneObject(sceneObject, attachToBackup, sendClientUpdates);
  292. if (attachToBackup && (!alreadyPersisted))
  293. {
  294. sceneObject.ForceInventoryPersistence();
  295. sceneObject.HasGroupChanged = true;
  296. }
  297. sceneObject.InvalidateDeepEffectivePerms();
  298. return ret;
  299. }
  300. /// <summary>
  301. /// Add a newly created object to the scene. This will both update the scene, and send information about the
  302. /// new object to all clients interested in the scene.
  303. /// </summary>
  304. /// <param name="sceneObject"></param>
  305. /// <param name="attachToBackup">
  306. /// If true, the object is made persistent into the scene.
  307. /// If false, the object will not persist over server restarts
  308. /// </param>
  309. /// <returns>
  310. /// true if the object was added, false if an object with the same uuid was already in the scene
  311. /// </returns>
  312. protected internal bool AddNewSceneObject(SceneObjectGroup sceneObject, bool attachToBackup, bool sendClientUpdates)
  313. {
  314. bool ret = AddSceneObject(sceneObject, attachToBackup, sendClientUpdates);
  315. // Ensure that we persist this new scene object if it's not an
  316. // attachment
  317. if (attachToBackup)
  318. sceneObject.HasGroupChanged = true;
  319. return ret;
  320. }
  321. /// <summary>
  322. /// Add a newly created object to the scene.
  323. /// </summary>
  324. ///
  325. /// This method does not send updates to the client - callers need to handle this themselves.
  326. /// Caller should also trigger EventManager.TriggerObjectAddedToScene
  327. /// <param name="sceneObject"></param>
  328. /// <param name="attachToBackup"></param>
  329. /// <param name="pos">Position of the object. If null then the position stored in the object is used.</param>
  330. /// <param name="rot">Rotation of the object. If null then the rotation stored in the object is used.</param>
  331. /// <param name="vel">Velocity of the object. This parameter only has an effect if the object is physical</param>
  332. /// <returns></returns>
  333. public bool AddNewSceneObject(
  334. SceneObjectGroup sceneObject, bool attachToBackup, Vector3? pos, Quaternion? rot, Vector3 vel)
  335. {
  336. if (pos != null)
  337. sceneObject.AbsolutePosition = (Vector3)pos;
  338. if (rot != null)
  339. sceneObject.UpdateGroupRotationR((Quaternion)rot);
  340. AddNewSceneObject(sceneObject, attachToBackup, false);
  341. if (sceneObject.RootPart.Shape.PCode == (byte)PCode.Prim)
  342. {
  343. sceneObject.ClearPartAttachmentData();
  344. }
  345. PhysicsActor pa = sceneObject.RootPart.PhysActor;
  346. if (pa != null && pa.IsPhysical && vel != Vector3.Zero)
  347. {
  348. sceneObject.RootPart.ApplyImpulse((vel * sceneObject.GetMass()), false);
  349. }
  350. return true;
  351. }
  352. /// <summary>
  353. /// Add an object to the scene. This will both update the scene, and send information about the
  354. /// new object to all clients interested in the scene.
  355. /// </summary>
  356. /// <remarks>
  357. /// The object's stored position, rotation and velocity are used.
  358. /// </remarks>
  359. /// <param name="sceneObject"></param>
  360. /// <param name="attachToBackup">
  361. /// If true, the object is made persistent into the scene.
  362. /// If false, the object will not persist over server restarts
  363. /// </param>
  364. /// <param name="sendClientUpdates">
  365. /// If true, updates for the new scene object are sent to all viewers in range.
  366. /// If false, it is left to the caller to schedule the update
  367. /// </param>
  368. /// <returns>
  369. /// true if the object was added, false if an object with the same uuid was already in the scene
  370. /// </returns>
  371. protected bool AddSceneObject(SceneObjectGroup sceneObject, bool attachToBackup, bool sendClientUpdates)
  372. {
  373. if (sceneObject == null)
  374. {
  375. m_log.ErrorFormat("[SCENEGRAPH]: Tried to add null scene object");
  376. return false;
  377. }
  378. if (sceneObject.UUID == UUID.Zero)
  379. {
  380. m_log.ErrorFormat(
  381. "[SCENEGRAPH]: Tried to add scene object {0} to {1} with illegal UUID of {2}",
  382. sceneObject.Name, m_parentScene.RegionInfo.RegionName, UUID.Zero);
  383. return false;
  384. }
  385. if (Entities.ContainsKey(sceneObject.UUID))
  386. {
  387. m_log.DebugFormat(
  388. "[SCENEGRAPH]: Scene graph for {0} already contains object {1} in AddSceneObject()",
  389. m_parentScene.RegionInfo.RegionName, sceneObject.UUID);
  390. return false;
  391. }
  392. // m_log.DebugFormat(
  393. // "[SCENEGRAPH]: Adding scene object {0} {1}, with {2} parts on {3}",
  394. // sceneObject.Name, sceneObject.UUID, sceneObject.Parts.Length, m_parentScene.RegionInfo.RegionName);
  395. SceneObjectPart[] parts = sceneObject.Parts;
  396. int partsLength = parts.Length;
  397. SceneObjectPart part;
  398. // Clamp the sizes (scales) of the child prims and add the child prims to the count of all primitives
  399. // (meshes and geometric primitives) in the scene; add child prims to m_numTotalPrim count
  400. if (m_parentScene.m_clampPrimSize)
  401. {
  402. for (int i = 0; i< partsLength; ++i)
  403. {
  404. part = parts[i];
  405. Vector3 scale = part.Shape.Scale;
  406. scale.X = Util.Clamp(scale.X, m_parentScene.m_minNonphys, m_parentScene.m_maxNonphys);
  407. scale.Y = Util.Clamp(scale.Y, m_parentScene.m_minNonphys, m_parentScene.m_maxNonphys);
  408. scale.Z = Util.Clamp(scale.Z, m_parentScene.m_minNonphys, m_parentScene.m_maxNonphys);
  409. part.Shape.Scale = scale;
  410. }
  411. }
  412. sceneObject.AttachToScene(m_parentScene);
  413. bool entered = false;
  414. try
  415. {
  416. try { }
  417. finally
  418. {
  419. m_scenePartsLock.EnterWriteLock();
  420. entered = true;
  421. }
  422. m_numTotalPrim += partsLength;
  423. Entities.Add(sceneObject);
  424. m_scenePartsArray = null;
  425. for (int i = 0; i < partsLength; ++i)
  426. {
  427. part = parts[i];
  428. m_scenePartsByID[part.UUID] = part;
  429. m_scenePartsByLocalID[part.LocalId] = part;
  430. if (part.GetPrimType() == PrimType.SCULPT)
  431. ++m_numMesh;
  432. else
  433. ++m_numPrim;
  434. }
  435. }
  436. finally
  437. {
  438. if(entered)
  439. m_scenePartsLock.ExitWriteLock();
  440. }
  441. if (sendClientUpdates)
  442. sceneObject.ScheduleGroupForFullAnimUpdate();
  443. if (attachToBackup)
  444. sceneObject.AttachToBackup();
  445. return true;
  446. }
  447. /// <summary>
  448. /// Delete an object from the scene
  449. /// </summary>
  450. /// <returns>true if the object was deleted, false if there was no object to delete</returns>
  451. public bool DeleteSceneObject(UUID uuid, bool resultOfObjectLinked)
  452. {
  453. // m_log.DebugFormat(
  454. // "[SCENE GRAPH]: Deleting scene object with uuid {0}, resultOfObjectLinked = {1}",
  455. // uuid, resultOfObjectLinked);
  456. EntityBase entity;
  457. if (!Entities.TryGetValue(uuid, out entity) || (!(entity is SceneObjectGroup)))
  458. return false;
  459. SceneObjectGroup grp = (SceneObjectGroup)entity;
  460. SceneObjectPart[] parts = grp.Parts;
  461. int partsLength = parts.Length;
  462. SceneObjectPart part;
  463. if (!resultOfObjectLinked)
  464. {
  465. // Decrement the total number of primitives (meshes and geometric primitives)
  466. // that are part of the Scene Object being removed
  467. m_numTotalPrim -= grp.PrimCount;
  468. bool isPh = (grp.RootPart.Flags & PrimFlags.Physics) == PrimFlags.Physics;
  469. int nphysparts = 0;
  470. // Go through all parts (primitives and meshes) of this Scene Object
  471. for (int i= 0; i < partsLength; ++i)
  472. {
  473. part = parts[i];
  474. // Keep track of the total number of meshes or geometric primitives left in the scene;
  475. // determine which object this is based on its primitive type: sculpted (sculpt) prim refers to
  476. // a mesh and all other prims (i.e. box, sphere, etc) are geometric primitives
  477. if (part.GetPrimType() == PrimType.SCULPT)
  478. m_numMesh--;
  479. else
  480. m_numPrim--;
  481. if(isPh && part.PhysicsShapeType != (byte)PhysShapeType.none)
  482. nphysparts++;
  483. }
  484. if (nphysparts > 0 )
  485. RemovePhysicalPrim(nphysparts);
  486. }
  487. bool ret = false;
  488. bool entered = false;
  489. try
  490. {
  491. try { }
  492. finally
  493. {
  494. m_scenePartsLock.EnterWriteLock();
  495. entered = true;
  496. }
  497. if (!resultOfObjectLinked)
  498. {
  499. for (int i = 0; i < parts.Length; ++i)
  500. {
  501. part = parts[i];
  502. m_scenePartsByID.Remove(part.UUID);
  503. m_scenePartsByLocalID.Remove(part.LocalId);
  504. }
  505. m_scenePartsArray = null;
  506. }
  507. ret = Entities.Remove(uuid);
  508. }
  509. finally
  510. {
  511. if(entered)
  512. m_scenePartsLock.ExitWriteLock();
  513. }
  514. return ret;
  515. }
  516. /// <summary>
  517. /// Add an object to the list of prims to process on the next update
  518. /// </summary>
  519. /// <param name="obj">
  520. /// A <see cref="SceneObjectGroup"/>
  521. /// </param>
  522. protected internal void AddToUpdateList(SceneObjectGroup obj)
  523. {
  524. lock(m_updateLock)
  525. m_updateList[obj.UUID] = obj;
  526. }
  527. public void FireAttachToBackup(SceneObjectGroup obj)
  528. {
  529. if (OnAttachToBackup != null)
  530. {
  531. OnAttachToBackup(obj);
  532. }
  533. }
  534. public void FireDetachFromBackup(SceneObjectGroup obj)
  535. {
  536. if (OnDetachFromBackup != null)
  537. {
  538. OnDetachFromBackup(obj);
  539. }
  540. }
  541. public void FireChangeBackup(SceneObjectGroup obj)
  542. {
  543. if (OnChangeBackup != null)
  544. {
  545. OnChangeBackup(obj);
  546. }
  547. }
  548. /// <summary>
  549. /// Process all pending updates
  550. /// </summary>
  551. protected internal void UpdateObjectGroups()
  552. {
  553. Dictionary<UUID, SceneObjectGroup> updates;
  554. // Get the current list of updates and clear the list before iterating
  555. lock (m_updateLock)
  556. {
  557. if(m_updateList.Count == 0)
  558. return;
  559. updates = m_updateList;
  560. m_updateList = new Dictionary<UUID, SceneObjectGroup>();
  561. }
  562. // Go through all updates
  563. foreach (SceneObjectGroup sog in updates.Values)
  564. {
  565. if (sog.IsDeleted)
  566. continue;
  567. // Don't abort the whole update if one entity happens to give us an exception.
  568. try
  569. {
  570. sog.Update();
  571. }
  572. catch (Exception e)
  573. {
  574. m_log.ErrorFormat(
  575. "[INNER SCENE]: Failed to update {0}, {1} - {2}", sog.Name, sog.UUID, e);
  576. }
  577. }
  578. updates = null;
  579. }
  580. protected internal void AddPhysicalPrim(int number)
  581. {
  582. m_physicalPrim += number;
  583. }
  584. protected internal void RemovePhysicalPrim(int number)
  585. {
  586. m_physicalPrim -= number;
  587. }
  588. protected internal void AddToScriptLPS(int number)
  589. {
  590. m_scriptLPS += number;
  591. }
  592. protected internal void AddActiveScripts(int number)
  593. {
  594. m_activeScripts += number;
  595. }
  596. protected internal void HandleUndo(IClientAPI remoteClient, UUID primId)
  597. {
  598. if (primId != UUID.Zero)
  599. {
  600. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primId);
  601. if (part != null)
  602. part.Undo();
  603. }
  604. }
  605. protected internal void HandleRedo(IClientAPI remoteClient, UUID primId)
  606. {
  607. if (primId != UUID.Zero)
  608. {
  609. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primId);
  610. if (part != null)
  611. part.Redo();
  612. }
  613. }
  614. protected internal ScenePresence CreateAndAddChildScenePresence(
  615. IClientAPI client, AvatarAppearance appearance, PresenceType type)
  616. {
  617. ScenePresence presence = new ScenePresence(client, m_parentScene, appearance, type);
  618. bool entered = false;
  619. try
  620. {
  621. try{ }
  622. finally
  623. {
  624. m_scenePresencesLock.EnterWriteLock();
  625. entered = true;
  626. }
  627. UUID id = presence.UUID;
  628. Entities[id] = presence;
  629. // ScenePresence always defaults to child agent
  630. ++m_numChildAgents;
  631. uint localid = presence.LocalId;
  632. ScenePresence oldref;
  633. if (m_scenePresenceMap.TryGetValue(id, out oldref))
  634. {
  635. uint oldLocalID = oldref.LocalId;
  636. if (localid != oldLocalID)
  637. m_scenePresenceLocalIDMap.Remove(oldLocalID);
  638. }
  639. m_scenePresenceMap[id] = presence;
  640. m_scenePresenceLocalIDMap[localid] = presence;
  641. m_scenePresenceList = null;
  642. }
  643. finally
  644. {
  645. if(entered)
  646. m_scenePresencesLock.ExitWriteLock();
  647. }
  648. return presence;
  649. }
  650. /// <summary>
  651. /// Remove a presence from the scene
  652. /// </summary>
  653. protected internal void RemoveScenePresence(UUID agentID)
  654. {
  655. if (!Entities.Remove(agentID))
  656. {
  657. m_log.WarnFormat(
  658. "[SCENE GRAPH]: Tried to remove non-existent scene presence with agent ID {0} from scene Entities list",
  659. agentID);
  660. }
  661. bool entered = false;
  662. try
  663. {
  664. try { }
  665. finally
  666. {
  667. m_scenePresencesLock.EnterWriteLock();
  668. entered = true;
  669. }
  670. // Remove the presence reference from the dictionary
  671. ScenePresence oldref;
  672. if(m_scenePresenceMap.TryGetValue(agentID, out oldref))
  673. {
  674. m_scenePresenceMap.Remove(agentID);
  675. // Find the index in the list where the old ref was stored and remove the reference
  676. m_scenePresenceLocalIDMap.Remove(oldref.LocalId);
  677. m_scenePresenceList = null;
  678. }
  679. else
  680. {
  681. m_log.WarnFormat("[SCENE GRAPH]: Tried to remove non-existent scene presence with agent ID {0} from scene ScenePresences list", agentID);
  682. }
  683. }
  684. finally
  685. {
  686. if(entered)
  687. m_scenePresencesLock.ExitWriteLock();
  688. }
  689. }
  690. protected internal void SwapRootChildAgent(bool direction_RC_CR_T_F)
  691. {
  692. if (direction_RC_CR_T_F)
  693. {
  694. m_numRootAgents--;
  695. m_numChildAgents++;
  696. }
  697. else
  698. {
  699. m_numChildAgents--;
  700. m_numRootAgents++;
  701. }
  702. }
  703. public void removeUserCount(bool TypeRCTF)
  704. {
  705. if (TypeRCTF)
  706. {
  707. m_numRootAgents--;
  708. }
  709. else
  710. {
  711. m_numChildAgents--;
  712. }
  713. }
  714. public void RecalculateStats()
  715. {
  716. int rootcount = 0;
  717. int childcount = 0;
  718. List<ScenePresence> presences = GetScenePresences();
  719. for (int i = 0; i < presences.Count; ++i)
  720. {
  721. if (presences[i].IsChildAgent)
  722. ++childcount;
  723. else
  724. ++rootcount;
  725. };
  726. m_numRootAgents = rootcount;
  727. m_numChildAgents = childcount;
  728. }
  729. public int GetChildAgentCount()
  730. {
  731. return m_numChildAgents;
  732. }
  733. public int GetRootAgentCount()
  734. {
  735. return m_numRootAgents;
  736. }
  737. public int GetTotalObjectsCount()
  738. {
  739. return m_numTotalPrim;
  740. }
  741. public int GetTotalPrimObjectsCount()
  742. {
  743. return m_numPrim;
  744. }
  745. public int GetTotalMeshObjectsCount()
  746. {
  747. return m_numMesh;
  748. }
  749. public int GetActiveObjectsCount()
  750. {
  751. return m_physicalPrim;
  752. }
  753. public int GetActiveScriptsCount()
  754. {
  755. return m_activeScripts;
  756. }
  757. public int GetScriptLPS()
  758. {
  759. int returnval = m_scriptLPS;
  760. m_scriptLPS = 0;
  761. return returnval;
  762. }
  763. #endregion
  764. #region Get Methods
  765. /// <summary>
  766. /// Get the controlling client for the given avatar, if there is one.
  767. ///
  768. /// FIXME: The only user of the method right now is Caps.cs, in order to resolve a client API since it can't
  769. /// use the ScenePresence. This could be better solved in a number of ways - we could establish an
  770. /// OpenSim.Framework.IScenePresence, or move the caps code into a region package (which might be the more
  771. /// suitable solution).
  772. /// </summary>
  773. /// <param name="agentId"></param>
  774. /// <returns>null if either the avatar wasn't in the scene, or
  775. /// they do not have a controlling client</returns>
  776. /// <remarks>this used to be protected internal, but that
  777. /// prevents CapabilitiesModule from accessing it</remarks>
  778. public IClientAPI GetControllingClient(UUID agentId)
  779. {
  780. bool entered = false;
  781. try
  782. {
  783. try { }
  784. finally
  785. {
  786. m_scenePresencesLock.EnterReadLock();
  787. entered = true;
  788. }
  789. ScenePresence presence;
  790. if (m_scenePresenceMap.TryGetValue(agentId, out presence))
  791. return presence.ControllingClient;
  792. return null;
  793. }
  794. catch
  795. {
  796. return null;
  797. }
  798. finally
  799. {
  800. if (entered)
  801. m_scenePresencesLock.ExitReadLock();
  802. }
  803. }
  804. /// <summary>
  805. /// Get a reference to the scene presence list. Changes to the list will be done in a copy
  806. /// There is no guarantee that presences will remain in the scene after the list is returned.
  807. /// This list should remain private to SceneGraph. Callers wishing to iterate should instead
  808. /// pass a delegate to ForEachScenePresence.
  809. /// </summary>
  810. /// <returns></returns>
  811. protected internal List<ScenePresence> GetScenePresences()
  812. {
  813. bool entered = false;
  814. try
  815. {
  816. try{ }
  817. finally
  818. {
  819. m_scenePresencesLock.EnterWriteLock();
  820. entered = true;
  821. }
  822. if(m_scenePresenceList == null)
  823. m_scenePresenceList = new List<ScenePresence>(m_scenePresenceMap.Values);
  824. return m_scenePresenceList;
  825. }
  826. catch
  827. {
  828. return new List<ScenePresence>();
  829. }
  830. finally
  831. {
  832. if(entered)
  833. m_scenePresencesLock.ExitWriteLock();
  834. }
  835. }
  836. /// <summary>
  837. /// Request a scene presence by UUID. Fast, indexed lookup.
  838. /// </summary>
  839. /// <param name="agentID"></param>
  840. /// <returns>null if the presence was not found</returns>
  841. protected internal ScenePresence GetScenePresence(UUID agentID)
  842. {
  843. bool entered = false;
  844. try
  845. {
  846. try { }
  847. finally
  848. {
  849. m_scenePresencesLock.EnterReadLock();
  850. entered = true;
  851. }
  852. ScenePresence presence;
  853. m_scenePresenceMap.TryGetValue(agentID, out presence);
  854. return presence;
  855. }
  856. catch
  857. {
  858. return null;
  859. }
  860. finally
  861. {
  862. if (entered)
  863. m_scenePresencesLock.ExitReadLock();
  864. }
  865. }
  866. /// <summary>
  867. /// Request the scene presence by name.
  868. /// </summary>
  869. /// <param name="firstName"></param>
  870. /// <param name="lastName"></param>
  871. /// <returns>null if the presence was not found</returns>
  872. protected internal ScenePresence GetScenePresence(string firstName, string lastName)
  873. {
  874. List<ScenePresence> presences = GetScenePresences();
  875. foreach (ScenePresence presence in presences)
  876. {
  877. if (string.Equals(presence.Firstname, firstName, StringComparison.CurrentCultureIgnoreCase)
  878. && string.Equals(presence.Lastname, lastName, StringComparison.CurrentCultureIgnoreCase))
  879. return presence;
  880. }
  881. return null;
  882. }
  883. /// <summary>
  884. /// Request the scene presence by localID.
  885. /// </summary>
  886. /// <param name="localID"></param>
  887. /// <returns>null if the presence was not found</returns>
  888. protected internal ScenePresence GetScenePresence(uint localID)
  889. {
  890. bool entered = false;
  891. try
  892. {
  893. try { }
  894. finally
  895. {
  896. m_scenePresencesLock.EnterReadLock();
  897. entered = true;
  898. }
  899. ScenePresence sp;
  900. if (m_scenePresenceLocalIDMap.TryGetValue(localID, out sp))
  901. return sp;
  902. }
  903. finally
  904. {
  905. if (entered)
  906. m_scenePresencesLock.ExitReadLock();
  907. }
  908. return null;
  909. }
  910. protected internal bool TryGetScenePresence(UUID agentID, out ScenePresence avatar)
  911. {
  912. bool entered = false;
  913. try
  914. {
  915. try { }
  916. finally
  917. {
  918. m_scenePresencesLock.EnterReadLock();
  919. entered = true;
  920. }
  921. return m_scenePresenceMap.TryGetValue(agentID, out avatar);
  922. }
  923. catch
  924. {
  925. avatar = null;
  926. return false;
  927. }
  928. finally
  929. {
  930. if (entered)
  931. m_scenePresencesLock.ExitReadLock();
  932. }
  933. }
  934. protected internal bool TryGetAvatarByName(string name, out ScenePresence avatar)
  935. {
  936. avatar = null;
  937. List<ScenePresence> presences = GetScenePresences();
  938. foreach (ScenePresence presence in presences)
  939. {
  940. if (String.Compare(name, presence.ControllingClient.Name, true) == 0)
  941. {
  942. avatar = presence;
  943. break;
  944. }
  945. }
  946. return (avatar != null);
  947. }
  948. /// <summary>
  949. /// Get a scene object group that contains the prim with the given local id
  950. /// </summary>
  951. /// <param name="localID"></param>
  952. /// <returns>null if no scene object group containing that prim is found</returns>
  953. public SceneObjectGroup GetGroupByPrim(uint localID)
  954. {
  955. bool entered = false;
  956. try
  957. {
  958. try { }
  959. finally
  960. {
  961. m_scenePartsLock.EnterReadLock();
  962. entered = true;
  963. }
  964. SceneObjectPart sop;
  965. if(m_scenePartsByLocalID.TryGetValue(localID, out sop))
  966. return sop.ParentGroup;
  967. return null;
  968. }
  969. finally
  970. {
  971. if(entered)
  972. m_scenePartsLock.ExitReadLock();
  973. }
  974. }
  975. /// <summary>
  976. /// Get a scene object group that contains the prim with the given uuid
  977. /// </summary>
  978. /// <param name="fullID"></param>
  979. /// <returns>null if no scene object group containing that prim is found</returns>
  980. public SceneObjectGroup GetGroupByPrim(UUID fullID)
  981. {
  982. bool entered = false;
  983. try
  984. {
  985. try { }
  986. finally
  987. {
  988. m_scenePartsLock.EnterReadLock();
  989. entered = true;
  990. }
  991. SceneObjectPart sop;
  992. if (m_scenePartsByID.TryGetValue(fullID, out sop))
  993. return sop.ParentGroup;
  994. return null;
  995. }
  996. finally
  997. {
  998. if (entered)
  999. m_scenePartsLock.ExitReadLock();
  1000. }
  1001. }
  1002. protected internal EntityIntersection GetClosestIntersectingPrim(Ray hray, bool frontFacesOnly, bool faceCenters)
  1003. {
  1004. // Primitive Ray Tracing
  1005. float closestDistance = 280f;
  1006. EntityIntersection result = new EntityIntersection();
  1007. EntityBase[] EntityList = GetEntities();
  1008. foreach (EntityBase ent in EntityList)
  1009. {
  1010. if (ent is SceneObjectGroup)
  1011. {
  1012. SceneObjectGroup reportingG = (SceneObjectGroup)ent;
  1013. EntityIntersection inter = reportingG.TestIntersection(hray, frontFacesOnly, faceCenters);
  1014. if (inter.HitTF && inter.distance < closestDistance)
  1015. {
  1016. closestDistance = inter.distance;
  1017. result = inter;
  1018. }
  1019. }
  1020. }
  1021. return result;
  1022. }
  1023. /// <summary>
  1024. /// Get all the scene object groups.
  1025. /// </summary>
  1026. /// <returns>
  1027. /// The scene object groups. If the scene is empty then an empty list is returned.
  1028. /// </returns>
  1029. protected internal List<SceneObjectGroup> GetSceneObjectGroups()
  1030. {
  1031. EntityBase[] entities = Entities.GetEntities();
  1032. List<SceneObjectGroup> ret = new List<SceneObjectGroup>(256);
  1033. for (int i = 0; i < entities.Length; ++i)
  1034. {
  1035. if(entities[i] is SceneObjectGroup)
  1036. ret.Add((SceneObjectGroup)entities[i]);
  1037. }
  1038. return ret;
  1039. }
  1040. /// <summary>
  1041. /// Get a group in the scene
  1042. /// </summary>
  1043. /// <param name="fullID">UUID of the group</param>
  1044. /// <returns>null if no such group was found</returns>
  1045. protected internal SceneObjectGroup GetSceneObjectGroup(UUID fullID)
  1046. {
  1047. EntityBase entity;
  1048. if (Entities.TryGetValue(fullID, out entity) && (entity is SceneObjectGroup))
  1049. return (SceneObjectGroup)entity;
  1050. return null;
  1051. }
  1052. /// <summary>
  1053. /// Get a group in the scene
  1054. /// <param name="localID">Local id of the root part of the group</param>
  1055. /// <returns>null if no such group was found</returns>
  1056. protected internal SceneObjectGroup GetSceneObjectGroup(uint localID)
  1057. {
  1058. EntityBase entity;
  1059. if (Entities.TryGetValue(localID, out entity) && (entity is SceneObjectGroup))
  1060. return (SceneObjectGroup)entity;
  1061. return null;
  1062. }
  1063. /// <summary>
  1064. /// Get a group by name from the scene (will return the first
  1065. /// found, if there are more than one prim with the same name)
  1066. /// </summary>
  1067. /// <param name="name"></param>
  1068. /// <returns>null if the part was not found</returns>
  1069. protected internal SceneObjectGroup GetSceneObjectGroup(string name)
  1070. {
  1071. EntityBase entity = null;
  1072. EntityBase[] entities = Entities.GetEntities();
  1073. for (int i = 0; i < entities.Length; ++i)
  1074. {
  1075. entity = entities[i];
  1076. if (entity is SceneObjectGroup && entity.Name == name)
  1077. return (SceneObjectGroup)entity;
  1078. }
  1079. return null;
  1080. }
  1081. /// <summary>
  1082. /// Get a part contained in this scene.
  1083. /// </summary>
  1084. /// <param name="localID"></param>
  1085. /// <returns>null if the part was not found</returns>
  1086. protected internal SceneObjectPart GetSceneObjectPart(uint localID)
  1087. {
  1088. bool entered = false;
  1089. try
  1090. {
  1091. try { }
  1092. finally
  1093. {
  1094. m_scenePartsLock.EnterReadLock();
  1095. entered = true;
  1096. }
  1097. SceneObjectPart sop;
  1098. if (m_scenePartsByLocalID.TryGetValue(localID, out sop))
  1099. {
  1100. if (sop.ParentGroup == null || sop.ParentGroup.IsDeleted)
  1101. return null;
  1102. return sop;
  1103. }
  1104. return null;
  1105. }
  1106. finally
  1107. {
  1108. if (entered)
  1109. m_scenePartsLock.ExitReadLock();
  1110. }
  1111. }
  1112. /// <summary>
  1113. /// Get a part contained in this scene.
  1114. /// </summary>
  1115. /// <param name="fullID"></param>
  1116. /// <returns>null if the part was not found</returns>
  1117. protected internal SceneObjectPart GetSceneObjectPart(UUID fullID)
  1118. {
  1119. bool entered = false;
  1120. try
  1121. {
  1122. try { }
  1123. finally
  1124. {
  1125. m_scenePartsLock.EnterReadLock();
  1126. entered = true;
  1127. }
  1128. SceneObjectPart sop;
  1129. if (m_scenePartsByID.TryGetValue(fullID, out sop))
  1130. {
  1131. if (sop.ParentGroup == null || sop.ParentGroup.IsDeleted)
  1132. return null;
  1133. return sop;
  1134. }
  1135. return null;
  1136. }
  1137. finally
  1138. {
  1139. if (entered)
  1140. m_scenePartsLock.ExitReadLock();
  1141. }
  1142. }
  1143. /// <summary>
  1144. /// Get a prim by name from the scene (will return the first
  1145. /// found, if there are more than one prim with the same name)
  1146. /// </summary>
  1147. /// <param name="name"></param>
  1148. /// <returns>null if the part was not found</returns>
  1149. protected internal SceneObjectPart GetSceneObjectPart(string name)
  1150. {
  1151. SceneObjectPart[] parts = GetPartsArray();
  1152. SceneObjectPart sop;
  1153. for (int i = 0; i < parts.Length; ++i)
  1154. {
  1155. sop = parts[i];
  1156. if (sop.ParentGroup == null || sop.ParentGroup.IsDeleted)
  1157. continue;
  1158. if (sop.Name == name)
  1159. return sop;
  1160. }
  1161. return null;
  1162. }
  1163. protected internal SceneObjectPart[] GetPartsArray()
  1164. {
  1165. bool entered = false;
  1166. try
  1167. {
  1168. try { }
  1169. finally
  1170. {
  1171. m_scenePartsLock.EnterWriteLock();
  1172. entered = true;
  1173. }
  1174. if(m_scenePartsArray == null)
  1175. {
  1176. m_scenePartsArray = new SceneObjectPart[m_scenePartsByID.Count];
  1177. m_scenePartsByID.Values.CopyTo(m_scenePartsArray, 0);
  1178. }
  1179. return m_scenePartsArray;
  1180. }
  1181. finally
  1182. {
  1183. if(entered)
  1184. m_scenePartsLock.ExitWriteLock();
  1185. }
  1186. }
  1187. /// <summary>
  1188. /// Returns a list of the entities in the scene. This is a new list so no locking is required to iterate over
  1189. /// it
  1190. /// </summary>
  1191. /// <returns></returns>
  1192. protected internal EntityBase[] GetEntities()
  1193. {
  1194. return Entities.GetEntities();
  1195. }
  1196. #endregion
  1197. #region Other Methods
  1198. protected internal void physicsBasedCrash()
  1199. {
  1200. handlerPhysicsCrash = UnRecoverableError;
  1201. if (handlerPhysicsCrash != null)
  1202. {
  1203. handlerPhysicsCrash();
  1204. }
  1205. }
  1206. /// <summary>
  1207. /// Performs action once on all scene object groups.
  1208. /// </summary>
  1209. /// <param name="action"></param>
  1210. protected internal void ForEachSOG(Action<SceneObjectGroup> action)
  1211. {
  1212. EntityBase[] entities = Entities.GetEntities();
  1213. EntityBase entity;
  1214. for (int i = 0; i < entities.Length; ++i)
  1215. {
  1216. entity = entities[i];
  1217. if (entity is SceneObjectGroup)
  1218. {
  1219. try
  1220. {
  1221. action((SceneObjectGroup)entity);
  1222. }
  1223. catch (Exception e)
  1224. {
  1225. // Catch it and move on. This includes situations where objlist has inconsistent info
  1226. m_log.WarnFormat(
  1227. "[SCENEGRAPH]: Problem processing action in ForEachSOG: {0} {1}", e.Message, e.StackTrace);
  1228. }
  1229. }
  1230. }
  1231. }
  1232. /// <summary>
  1233. /// Performs action on all ROOT (not child) scene presences.
  1234. /// This is just a shortcut function since frequently actions only appy to root SPs
  1235. /// </summary>
  1236. /// <param name="action"></param>
  1237. public void ForEachRootScenePresence(Action<ScenePresence> action)
  1238. {
  1239. List<ScenePresence> presences = GetScenePresences();
  1240. for (int i = 0; i < presences.Count; ++i)
  1241. {
  1242. if(presences[i].IsChildAgent || presences[i].IsDeleted)
  1243. continue;
  1244. try
  1245. {
  1246. action(presences[i]);
  1247. }
  1248. catch (Exception e)
  1249. {
  1250. m_log.Error("[SCENEGRAPH]: Error in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString());
  1251. }
  1252. };
  1253. }
  1254. /// <summary>
  1255. /// Performs action on all scene presences
  1256. /// </summary>
  1257. /// <param name="action"></param>
  1258. public void ForEachScenePresence(Action<ScenePresence> action)
  1259. {
  1260. List<ScenePresence> presences = GetScenePresences();
  1261. for(int i = 0; i < presences.Count; ++i)
  1262. {
  1263. if (presences[i].IsDeleted)
  1264. continue;
  1265. try
  1266. {
  1267. action(presences[i]);
  1268. }
  1269. catch (Exception e)
  1270. {
  1271. m_log.Error("[SCENEGRAPH]: Error in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString());
  1272. }
  1273. }
  1274. }
  1275. #endregion
  1276. #region Client Event handlers
  1277. protected internal void ClientChangeObject(uint localID, object odata, IClientAPI remoteClient)
  1278. {
  1279. SceneObjectPart part = GetSceneObjectPart(localID);
  1280. ObjectChangeData data = (ObjectChangeData)odata;
  1281. if (part != null)
  1282. {
  1283. SceneObjectGroup grp = part.ParentGroup;
  1284. if (grp != null)
  1285. {
  1286. if (m_parentScene.Permissions.CanEditObject(grp, remoteClient))
  1287. {
  1288. // These two are exceptions SL makes in the interpretation
  1289. // of the change flags. Must check them here because otherwise
  1290. // the group flag (see below) would be lost
  1291. if (data.change == ObjectChangeType.groupS)
  1292. data.change = ObjectChangeType.primS;
  1293. if (data.change == ObjectChangeType.groupPS)
  1294. data.change = ObjectChangeType.primPS;
  1295. part.StoreUndoState(data.change); // lets test only saving what we changed
  1296. grp.doChangeObject(part, (ObjectChangeData)data);
  1297. }
  1298. else
  1299. {
  1300. // Is this any kind of group operation?
  1301. if ((data.change & ObjectChangeType.Group) != 0)
  1302. {
  1303. // Is a move and/or rotation requested?
  1304. if ((data.change & (ObjectChangeType.Position | ObjectChangeType.Rotation)) != 0)
  1305. {
  1306. // Are we allowed to move it?
  1307. if (m_parentScene.Permissions.CanMoveObject(grp, remoteClient))
  1308. {
  1309. // Strip all but move and rotation from request
  1310. data.change &= (ObjectChangeType.Group | ObjectChangeType.Position | ObjectChangeType.Rotation);
  1311. part.StoreUndoState(data.change);
  1312. grp.doChangeObject(part, (ObjectChangeData)data);
  1313. }
  1314. }
  1315. }
  1316. }
  1317. }
  1318. }
  1319. }
  1320. /// <summary>
  1321. /// Update the scale of an individual prim.
  1322. /// </summary>
  1323. /// <param name="localID"></param>
  1324. /// <param name="scale"></param>
  1325. /// <param name="remoteClient"></param>
  1326. protected internal void UpdatePrimScale(uint localID, Vector3 scale, IClientAPI remoteClient)
  1327. {
  1328. SceneObjectPart part = GetSceneObjectPart(localID);
  1329. if (part != null)
  1330. {
  1331. if (m_parentScene.Permissions.CanEditObject(part.ParentGroup, remoteClient))
  1332. {
  1333. bool physbuild = false;
  1334. if (part.ParentGroup.RootPart.PhysActor != null)
  1335. {
  1336. part.ParentGroup.RootPart.PhysActor.Building = true;
  1337. physbuild = true;
  1338. }
  1339. part.Resize(scale);
  1340. if (physbuild)
  1341. part.ParentGroup.RootPart.PhysActor.Building = false;
  1342. }
  1343. }
  1344. }
  1345. protected internal void UpdatePrimGroupScale(uint localID, Vector3 scale, IClientAPI remoteClient)
  1346. {
  1347. SceneObjectGroup group = GetGroupByPrim(localID);
  1348. if (group != null)
  1349. {
  1350. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1351. {
  1352. bool physbuild = false;
  1353. if (group.RootPart.PhysActor != null)
  1354. {
  1355. group.RootPart.PhysActor.Building = true;
  1356. physbuild = true;
  1357. }
  1358. group.GroupResize(scale);
  1359. if (physbuild)
  1360. group.RootPart.PhysActor.Building = false;
  1361. }
  1362. }
  1363. }
  1364. /// <summary>
  1365. /// This handles the nifty little tool tip that you get when you drag your mouse over an object
  1366. /// Send to the Object Group to process. We don't know enough to service the request
  1367. /// </summary>
  1368. /// <param name="remoteClient"></param>
  1369. /// <param name="AgentID"></param>
  1370. /// <param name="RequestFlags"></param>
  1371. /// <param name="ObjectID"></param>
  1372. protected internal void RequestObjectPropertiesFamily(
  1373. IClientAPI remoteClient, UUID AgentID, uint RequestFlags, UUID ObjectID)
  1374. {
  1375. SceneObjectGroup group = GetGroupByPrim(ObjectID);
  1376. if (group != null)
  1377. {
  1378. group.ServiceObjectPropertiesFamilyRequest(remoteClient, AgentID, RequestFlags);
  1379. }
  1380. }
  1381. /// <summary>
  1382. ///
  1383. /// </summary>
  1384. /// <param name="localID"></param>
  1385. /// <param name="rot"></param>
  1386. /// <param name="remoteClient"></param>
  1387. protected internal void UpdatePrimSingleRotation(uint localID, Quaternion rot, IClientAPI remoteClient)
  1388. {
  1389. SceneObjectGroup group = GetGroupByPrim(localID);
  1390. if (group != null)
  1391. {
  1392. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient))
  1393. {
  1394. group.UpdateSingleRotation(rot, localID);
  1395. }
  1396. }
  1397. }
  1398. /// <summary>
  1399. ///
  1400. /// </summary>
  1401. /// <param name="localID"></param>
  1402. /// <param name="rot"></param>
  1403. /// <param name="remoteClient"></param>
  1404. protected internal void UpdatePrimSingleRotationPosition(uint localID, Quaternion rot, Vector3 pos, IClientAPI remoteClient)
  1405. {
  1406. SceneObjectGroup group = GetGroupByPrim(localID);
  1407. if (group != null)
  1408. {
  1409. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient))
  1410. {
  1411. group.UpdateSingleRotation(rot, pos, localID);
  1412. }
  1413. }
  1414. }
  1415. /// <summary>
  1416. /// Update the rotation of a whole group.
  1417. /// </summary>
  1418. /// <param name="localID"></param>
  1419. /// <param name="rot"></param>
  1420. /// <param name="remoteClient"></param>
  1421. protected internal void UpdatePrimGroupRotation(uint localID, Quaternion rot, IClientAPI remoteClient)
  1422. {
  1423. SceneObjectGroup group = GetGroupByPrim(localID);
  1424. if (group != null)
  1425. {
  1426. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient))
  1427. {
  1428. group.UpdateGroupRotationR(rot);
  1429. }
  1430. }
  1431. }
  1432. /// <summary>
  1433. ///
  1434. /// </summary>
  1435. /// <param name="localID"></param>
  1436. /// <param name="pos"></param>
  1437. /// <param name="rot"></param>
  1438. /// <param name="remoteClient"></param>
  1439. protected internal void UpdatePrimGroupRotation(uint localID, Vector3 pos, Quaternion rot, IClientAPI remoteClient)
  1440. {
  1441. SceneObjectGroup group = GetGroupByPrim(localID);
  1442. if (group != null)
  1443. {
  1444. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient))
  1445. {
  1446. group.UpdateGroupRotationPR(pos, rot);
  1447. }
  1448. }
  1449. }
  1450. /// <summary>
  1451. /// Update the position of the given part
  1452. /// </summary>
  1453. /// <param name="localID"></param>
  1454. /// <param name="pos"></param>
  1455. /// <param name="remoteClient"></param>
  1456. protected internal void UpdatePrimSinglePosition(uint localID, Vector3 pos, IClientAPI remoteClient)
  1457. {
  1458. SceneObjectGroup group = GetGroupByPrim(localID);
  1459. if (group != null)
  1460. {
  1461. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient) || group.IsAttachment)
  1462. {
  1463. group.UpdateSinglePosition(pos, localID);
  1464. }
  1465. }
  1466. }
  1467. /// <summary>
  1468. /// Update the position of the given group.
  1469. /// </summary>
  1470. /// <param name="localId"></param>
  1471. /// <param name="pos"></param>
  1472. /// <param name="remoteClient"></param>
  1473. public void UpdatePrimGroupPosition(uint localId, Vector3 pos, IClientAPI remoteClient)
  1474. {
  1475. SceneObjectGroup group = GetGroupByPrim(localId);
  1476. if (group != null)
  1477. {
  1478. if (group.IsAttachment || (group.RootPart.Shape.PCode == 9 && group.RootPart.Shape.State != 0))
  1479. {
  1480. // Set the new attachment point data in the object
  1481. byte attachmentPoint = (byte)group.AttachmentPoint;
  1482. group.UpdateGroupPosition(pos);
  1483. group.IsAttachment = false;
  1484. group.AbsolutePosition = group.RootPart.AttachedPos;
  1485. group.AttachmentPoint = attachmentPoint;
  1486. group.HasGroupChanged = true;
  1487. }
  1488. else
  1489. {
  1490. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient)
  1491. && m_parentScene.Permissions.CanObjectEntry(group, false, pos))
  1492. {
  1493. group.UpdateGroupPosition(pos);
  1494. }
  1495. }
  1496. }
  1497. }
  1498. /// <summary>
  1499. /// Update the texture entry of the given prim.
  1500. /// </summary>
  1501. /// <remarks>
  1502. /// A texture entry is an object that contains details of all the textures of the prim's face. In this case,
  1503. /// the texture is given in its byte serialized form.
  1504. /// </remarks>
  1505. /// <param name="localID"></param>
  1506. /// <param name="texture"></param>
  1507. /// <param name="remoteClient"></param>
  1508. protected internal void UpdatePrimTexture(uint localID, byte[] texture, IClientAPI remoteClient)
  1509. {
  1510. SceneObjectPart part = GetSceneObjectPart(localID);
  1511. if(part == null)
  1512. return;
  1513. SceneObjectGroup group = part.ParentGroup;
  1514. if (group != null && !group.IsDeleted)
  1515. {
  1516. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1517. {
  1518. part.UpdateTextureEntry(texture);
  1519. }
  1520. }
  1521. }
  1522. /// <summary>
  1523. /// Update the flags on a scene object. This covers properties such as phantom, physics and temporary.
  1524. /// </summary>
  1525. /// <remarks>
  1526. /// This is currently handling the incoming call from the client stack (e.g. LLClientView).
  1527. /// </remarks>
  1528. /// <param name="localID"></param>
  1529. /// <param name="UsePhysics"></param>
  1530. /// <param name="SetTemporary"></param>
  1531. /// <param name="SetPhantom"></param>
  1532. /// <param name="remoteClient"></param>
  1533. protected internal void UpdatePrimFlags(
  1534. uint localID, bool UsePhysics, bool SetTemporary, bool SetPhantom, ExtraPhysicsData PhysData, IClientAPI remoteClient)
  1535. {
  1536. SceneObjectPart part = GetSceneObjectPart(localID);
  1537. if(part == null)
  1538. return;
  1539. SceneObjectGroup group = part.ParentGroup;
  1540. if(group == null || group.IsDeleted)
  1541. return;
  1542. if (!m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1543. return;
  1544. // VolumeDetect can't be set via UI and will always be off when a change is made there
  1545. // now only change volume dtc if phantom off
  1546. bool wantedPhys = UsePhysics;
  1547. if (PhysData.PhysShapeType == PhysShapeType.invalid) // check for extraPhysics data
  1548. {
  1549. bool vdtc;
  1550. if (SetPhantom) // if phantom keep volumedtc
  1551. vdtc = group.RootPart.VolumeDetectActive;
  1552. else // else turn it off
  1553. vdtc = false;
  1554. group.UpdateFlags(UsePhysics, SetTemporary, SetPhantom, vdtc);
  1555. }
  1556. else
  1557. {
  1558. part.UpdateExtraPhysics(PhysData);
  1559. if (remoteClient != null)
  1560. remoteClient.SendPartPhysicsProprieties(part);
  1561. }
  1562. if (wantedPhys != group.UsesPhysics && remoteClient != null)
  1563. {
  1564. if(m_parentScene.m_linksetPhysCapacity != 0)
  1565. remoteClient.SendAlertMessage("Object physics cancelled because it exceeds limits for physical prims, either size or number of primswith shape type not set to None");
  1566. else
  1567. remoteClient.SendAlertMessage("Object physics cancelled because it exceeds size limits for physical prims");
  1568. group.RootPart.ScheduleFullUpdate();
  1569. }
  1570. }
  1571. /// <summary>
  1572. ///
  1573. /// </summary>
  1574. /// <param name="primLocalID"></param>
  1575. /// <param name="description"></param>
  1576. protected internal void PrimName(IClientAPI remoteClient, uint primLocalID, string name)
  1577. {
  1578. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1579. if (group != null)
  1580. {
  1581. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1582. {
  1583. group.SetPartName(Util.CleanString(name), primLocalID);
  1584. group.HasGroupChanged = true;
  1585. }
  1586. }
  1587. }
  1588. /// <summary>
  1589. /// Handle a prim description set request from a viewer.
  1590. /// </summary>
  1591. /// <param name="primLocalID"></param>
  1592. /// <param name="description"></param>
  1593. protected internal void PrimDescription(IClientAPI remoteClient, uint primLocalID, string description)
  1594. {
  1595. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1596. if (group != null)
  1597. {
  1598. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1599. {
  1600. group.SetPartDescription(Util.CleanString(description), primLocalID);
  1601. group.HasGroupChanged = true;
  1602. }
  1603. }
  1604. }
  1605. /// <summary>
  1606. /// Set a click action for the prim.
  1607. /// </summary>
  1608. /// <param name="remoteClient"></param>
  1609. /// <param name="primLocalID"></param>
  1610. /// <param name="clickAction"></param>
  1611. protected internal void PrimClickAction(IClientAPI remoteClient, uint primLocalID, string clickAction)
  1612. {
  1613. // m_log.DebugFormat(
  1614. // "[SCENEGRAPH]: User {0} set click action for {1} to {2}", remoteClient.Name, primLocalID, clickAction);
  1615. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1616. if (group != null)
  1617. {
  1618. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1619. {
  1620. SceneObjectPart part = group.GetPart(primLocalID);
  1621. if (part != null)
  1622. {
  1623. part.ClickAction = Convert.ToByte(clickAction);
  1624. group.HasGroupChanged = true;
  1625. }
  1626. }
  1627. }
  1628. }
  1629. protected internal void PrimMaterial(IClientAPI remoteClient, uint primLocalID, string material)
  1630. {
  1631. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1632. if (group != null)
  1633. {
  1634. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1635. {
  1636. SceneObjectPart part = group.GetPart(primLocalID);
  1637. if (part != null)
  1638. {
  1639. part.Material = Convert.ToByte(material);
  1640. group.HasGroupChanged = true;
  1641. remoteClient.SendPartPhysicsProprieties(part);
  1642. }
  1643. }
  1644. }
  1645. }
  1646. protected internal void UpdateExtraParam(UUID agentID, uint primLocalID, ushort type, bool inUse, byte[] data)
  1647. {
  1648. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1649. if (group != null)
  1650. {
  1651. if (m_parentScene.Permissions.CanEditObject(group.UUID, agentID))
  1652. {
  1653. group.UpdateExtraParam(primLocalID, type, inUse, data);
  1654. }
  1655. }
  1656. }
  1657. /// <summary>
  1658. ///
  1659. /// </summary>
  1660. /// <param name="primLocalID"></param>
  1661. /// <param name="shapeBlock"></param>
  1662. protected internal void UpdatePrimShape(UUID agentID, uint primLocalID, UpdateShapeArgs shapeBlock)
  1663. {
  1664. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1665. if (group != null)
  1666. {
  1667. if (m_parentScene.Permissions.CanEditObject(group.UUID, agentID))
  1668. {
  1669. ObjectShapePacket.ObjectDataBlock shapeData = new ObjectShapePacket.ObjectDataBlock();
  1670. shapeData.ObjectLocalID = shapeBlock.ObjectLocalID;
  1671. shapeData.PathBegin = shapeBlock.PathBegin;
  1672. shapeData.PathCurve = shapeBlock.PathCurve;
  1673. shapeData.PathEnd = shapeBlock.PathEnd;
  1674. shapeData.PathRadiusOffset = shapeBlock.PathRadiusOffset;
  1675. shapeData.PathRevolutions = shapeBlock.PathRevolutions;
  1676. shapeData.PathScaleX = shapeBlock.PathScaleX;
  1677. shapeData.PathScaleY = shapeBlock.PathScaleY;
  1678. shapeData.PathShearX = shapeBlock.PathShearX;
  1679. shapeData.PathShearY = shapeBlock.PathShearY;
  1680. shapeData.PathSkew = shapeBlock.PathSkew;
  1681. shapeData.PathTaperX = shapeBlock.PathTaperX;
  1682. shapeData.PathTaperY = shapeBlock.PathTaperY;
  1683. shapeData.PathTwist = shapeBlock.PathTwist;
  1684. shapeData.PathTwistBegin = shapeBlock.PathTwistBegin;
  1685. shapeData.ProfileBegin = shapeBlock.ProfileBegin;
  1686. shapeData.ProfileCurve = shapeBlock.ProfileCurve;
  1687. shapeData.ProfileEnd = shapeBlock.ProfileEnd;
  1688. shapeData.ProfileHollow = shapeBlock.ProfileHollow;
  1689. group.UpdateShape(shapeData, primLocalID);
  1690. }
  1691. }
  1692. }
  1693. /// <summary>
  1694. /// Initial method invoked when we receive a link objects request from the client.
  1695. /// </summary>
  1696. /// <param name="client"></param>
  1697. /// <param name="parentPrim"></param>
  1698. /// <param name="childPrims"></param>
  1699. protected internal void LinkObjects(SceneObjectPart root, List<SceneObjectPart> children)
  1700. {
  1701. if (root.KeyframeMotion != null)
  1702. {
  1703. root.KeyframeMotion.Stop();
  1704. root.KeyframeMotion = null;
  1705. }
  1706. SceneObjectGroup parentGroup = root.ParentGroup;
  1707. if (parentGroup == null) return;
  1708. // Cowardly refuse to link to a group owned root
  1709. if (parentGroup.OwnerID == parentGroup.GroupID)
  1710. return;
  1711. Monitor.Enter(m_linkLock);
  1712. try
  1713. {
  1714. List<SceneObjectGroup> childGroups = new List<SceneObjectGroup>();
  1715. // We do this in reverse to get the link order of the prims correct
  1716. for (int i = 0; i < children.Count; i++)
  1717. {
  1718. SceneObjectGroup child = children[i].ParentGroup;
  1719. // Don't try and add a group to itself - this will only cause severe problems later on.
  1720. if (child == parentGroup)
  1721. continue;
  1722. // Make sure no child prim is set for sale
  1723. // So that, on delink, no prims are unwittingly
  1724. // left for sale and sold off
  1725. if (child != null)
  1726. {
  1727. child.RootPart.ObjectSaleType = 0;
  1728. child.RootPart.SalePrice = 10;
  1729. childGroups.Add(child);
  1730. }
  1731. }
  1732. foreach (SceneObjectGroup child in childGroups)
  1733. {
  1734. if (parentGroup.OwnerID == child.OwnerID)
  1735. {
  1736. child.DetachFromBackup();
  1737. parentGroup.LinkToGroup(child);
  1738. // this is here so physics gets updated!
  1739. // Don't remove! Bad juju! Stay away! or fix physics!
  1740. // already done in LinkToGroup
  1741. // child.AbsolutePosition = child.AbsolutePosition;
  1742. }
  1743. }
  1744. // We need to explicitly resend the newly link prim's object properties since no other actions
  1745. // occur on link to invoke this elsewhere (such as object selection)
  1746. if (childGroups.Count > 0)
  1747. {
  1748. parentGroup.RootPart.CreateSelected = true;
  1749. parentGroup.TriggerScriptChangedEvent(Changed.LINK);
  1750. }
  1751. }
  1752. finally
  1753. {
  1754. /*
  1755. lock (SceneObjectGroupsByLocalPartID)
  1756. {
  1757. foreach (SceneObjectPart part in parentGroup.Parts)
  1758. SceneObjectGroupsByLocalPartID[part.LocalId] = parentGroup;
  1759. }
  1760. */
  1761. parentGroup.AdjustChildPrimPermissions(false);
  1762. parentGroup.HasGroupChanged = true;
  1763. parentGroup.ScheduleGroupForFullAnimUpdate();
  1764. Monitor.Exit(m_linkLock);
  1765. }
  1766. }
  1767. /// <summary>
  1768. /// Delink a linkset
  1769. /// </summary>
  1770. /// <param name="prims"></param>
  1771. protected internal void DelinkObjects(List<SceneObjectPart> prims)
  1772. {
  1773. List<SceneObjectPart> childParts = new List<SceneObjectPart>();
  1774. List<SceneObjectPart> rootParts = new List<SceneObjectPart>();
  1775. List<SceneObjectGroup> affectedGroups = new List<SceneObjectGroup>();
  1776. // Look them all up in one go, since that is comparatively expensive
  1777. //
  1778. Monitor.Enter(m_linkLock);
  1779. try
  1780. {
  1781. foreach (SceneObjectPart part in prims)
  1782. {
  1783. if(part == null)
  1784. continue;
  1785. SceneObjectGroup parentSOG = part.ParentGroup;
  1786. if(parentSOG == null || parentSOG.IsDeleted || parentSOG.inTransit || parentSOG.PrimCount == 1)
  1787. continue;
  1788. if (!affectedGroups.Contains(parentSOG))
  1789. {
  1790. affectedGroups.Add(parentSOG);
  1791. if(parentSOG.RootPart.PhysActor != null)
  1792. parentSOG.RootPart.PhysActor.Building = true;
  1793. }
  1794. if (part.KeyframeMotion != null)
  1795. {
  1796. part.KeyframeMotion.Stop();
  1797. part.KeyframeMotion = null;
  1798. }
  1799. if (part.LinkNum < 2) // Root
  1800. {
  1801. rootParts.Add(part);
  1802. }
  1803. else
  1804. {
  1805. part.LastOwnerID = part.ParentGroup.RootPart.LastOwnerID;
  1806. part.RezzerID = part.ParentGroup.RootPart.RezzerID;
  1807. childParts.Add(part);
  1808. }
  1809. }
  1810. if (childParts.Count > 0)
  1811. {
  1812. foreach (SceneObjectPart child in childParts)
  1813. {
  1814. // Unlink all child parts from their groups
  1815. child.ParentGroup.DelinkFromGroup(child, true);
  1816. //child.ParentGroup is now other
  1817. child.ParentGroup.HasGroupChanged = true;
  1818. child.ParentGroup.ScheduleGroupForFullAnimUpdate();
  1819. }
  1820. }
  1821. foreach (SceneObjectPart root in rootParts)
  1822. {
  1823. // In most cases, this will run only one time, and the prim
  1824. // will be a solo prim
  1825. // However, editing linked parts and unlinking may be different
  1826. //
  1827. SceneObjectGroup group = root.ParentGroup;
  1828. List<SceneObjectPart> newSet = new List<SceneObjectPart>(group.Parts);
  1829. newSet.Remove(root);
  1830. int numChildren = newSet.Count;
  1831. if(numChildren == 0)
  1832. break;
  1833. foreach (SceneObjectPart p in newSet)
  1834. group.DelinkFromGroup(p, false);
  1835. SceneObjectPart newRoot = newSet[0];
  1836. // If there is more than one prim remaining, we
  1837. // need to re-link
  1838. //
  1839. if (numChildren > 1)
  1840. {
  1841. // Determine new root
  1842. //
  1843. newSet.RemoveAt(0);
  1844. foreach (SceneObjectPart newChild in newSet)
  1845. newChild.ClearUpdateSchedule();
  1846. LinkObjects(newRoot, newSet);
  1847. }
  1848. else
  1849. {
  1850. newRoot.TriggerScriptChangedEvent(Changed.LINK);
  1851. newRoot.ParentGroup.HasGroupChanged = true;
  1852. newRoot.ParentGroup.InvalidatePartsLinkMaps();
  1853. newRoot.ParentGroup.ScheduleGroupForFullAnimUpdate();
  1854. }
  1855. }
  1856. // trigger events in the roots
  1857. //
  1858. foreach (SceneObjectGroup g in affectedGroups)
  1859. {
  1860. if(g.RootPart.PhysActor != null)
  1861. g.RootPart.PhysActor.Building = false;
  1862. g.AdjustChildPrimPermissions(false);
  1863. // Child prims that have been unlinked and deleted will
  1864. // return unless the root is deleted. This will remove them
  1865. // from the database. They will be rewritten immediately,
  1866. // minus the rows for the unlinked child prims.
  1867. m_parentScene.SimulationDataService.RemoveObject(g.UUID, m_parentScene.RegionInfo.RegionID);
  1868. g.InvalidatePartsLinkMaps();
  1869. g.TriggerScriptChangedEvent(Changed.LINK);
  1870. g.HasGroupChanged = true; // Persist
  1871. g.ScheduleGroupForFullUpdate();
  1872. }
  1873. }
  1874. finally
  1875. {
  1876. Monitor.Exit(m_linkLock);
  1877. }
  1878. }
  1879. protected internal void MakeObjectSearchable(IClientAPI remoteClient, bool IncludeInSearch, uint localID)
  1880. {
  1881. SceneObjectGroup sog = GetGroupByPrim(localID);
  1882. if(sog == null)
  1883. return;
  1884. //Protip: In my day, we didn't call them searchable objects, we called them limited point-to-point joints
  1885. //aka ObjectFlags.JointWheel = IncludeInSearch
  1886. //Permissions model: Object can be REMOVED from search IFF:
  1887. // * User owns object
  1888. //use CanEditObject
  1889. //Object can be ADDED to search IFF:
  1890. // * User owns object
  1891. // * Asset/DRM permission bit "modify" is enabled
  1892. //use CanEditObjectPosition
  1893. // libomv will complain about PrimFlags.JointWheel being
  1894. // deprecated, so we
  1895. #pragma warning disable 0612
  1896. if (IncludeInSearch && m_parentScene.Permissions.CanEditObject(sog, remoteClient))
  1897. {
  1898. sog.RootPart.AddFlag(PrimFlags.JointWheel);
  1899. sog.HasGroupChanged = true;
  1900. }
  1901. else if (!IncludeInSearch && m_parentScene.Permissions.CanMoveObject(sog, remoteClient))
  1902. {
  1903. sog.RootPart.RemFlag(PrimFlags.JointWheel);
  1904. sog.HasGroupChanged = true;
  1905. }
  1906. #pragma warning restore 0612
  1907. }
  1908. /// <summary>
  1909. /// Duplicate the given object.
  1910. /// </summary>
  1911. /// <param name="originalPrim"></param>
  1912. /// <param name="offset"></param>
  1913. /// <param name="flags"></param>
  1914. /// <param name="AgentID"></param>
  1915. /// <param name="GroupID"></param>
  1916. /// <param name="rot"></param>
  1917. /// <returns>null if duplication fails, otherwise the duplicated object</returns>
  1918. /// <summary>
  1919. public SceneObjectGroup DuplicateObject(uint originalPrimID, Vector3 offset, UUID AgentID, UUID GroupID, Quaternion rot, bool createSelected)
  1920. {
  1921. // m_log.DebugFormat(
  1922. // "[SCENE]: Duplication of object {0} at offset {1} requested by agent {2}",
  1923. // originalPrimID, offset, AgentID);
  1924. SceneObjectGroup original = GetGroupByPrim(originalPrimID);
  1925. if (original != null)
  1926. {
  1927. if (m_parentScene.Permissions.CanDuplicateObject(original, AgentID))
  1928. {
  1929. SceneObjectGroup copy = original.Copy(true);
  1930. copy.AbsolutePosition = copy.AbsolutePosition + offset;
  1931. SceneObjectPart[] parts = copy.Parts;
  1932. m_numTotalPrim += parts.Length;
  1933. if (original.OwnerID != AgentID)
  1934. {
  1935. copy.SetOwner(AgentID, GroupID);
  1936. if (m_parentScene.Permissions.PropagatePermissions())
  1937. {
  1938. foreach (SceneObjectPart child in parts)
  1939. {
  1940. child.Inventory.ChangeInventoryOwner(AgentID);
  1941. child.TriggerScriptChangedEvent(Changed.OWNER);
  1942. child.ApplyNextOwnerPermissions();
  1943. }
  1944. copy.InvalidateEffectivePerms();
  1945. }
  1946. }
  1947. bool entered = false;
  1948. try
  1949. {
  1950. try { }
  1951. finally
  1952. {
  1953. m_scenePartsLock.EnterWriteLock();
  1954. entered = true;
  1955. }
  1956. Entities.Add(copy);
  1957. m_scenePartsArray = null;
  1958. foreach (SceneObjectPart part in parts)
  1959. {
  1960. if (part.GetPrimType() == PrimType.SCULPT)
  1961. m_numMesh++;
  1962. else
  1963. m_numPrim++;
  1964. m_scenePartsByID[part.UUID] = part;
  1965. m_scenePartsByLocalID[part.LocalId] = part;
  1966. }
  1967. }
  1968. finally
  1969. {
  1970. if(entered)
  1971. m_scenePartsLock.ExitWriteLock();
  1972. }
  1973. copy.IsSelected = createSelected;
  1974. if (rot != Quaternion.Identity)
  1975. copy.UpdateGroupRotationR(rot);
  1976. // required for physics to update it's position
  1977. copy.ResetChildPrimPhysicsPositions();
  1978. copy.CreateScriptInstances(0, false, m_parentScene.DefaultScriptEngine, 1);
  1979. copy.ResumeScripts();
  1980. copy.HasGroupChanged = true;
  1981. copy.ScheduleGroupForFullAnimUpdate();
  1982. return copy;
  1983. }
  1984. }
  1985. else
  1986. {
  1987. m_log.WarnFormat("[SCENE]: Attempted to duplicate nonexistant prim id {0}", GroupID);
  1988. }
  1989. return null;
  1990. }
  1991. #endregion
  1992. }
  1993. }