SceneGraph.cs 80 KB

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