SceneGraph.cs 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  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. ForEachScenePresence(delegate(ScenePresence presence)
  719. {
  720. if (presence.IsChildAgent)
  721. ++childcount;
  722. else
  723. ++rootcount;
  724. });
  725. m_numRootAgents = rootcount;
  726. m_numChildAgents = childcount;
  727. }
  728. public int GetChildAgentCount()
  729. {
  730. return m_numChildAgents;
  731. }
  732. public int GetRootAgentCount()
  733. {
  734. return m_numRootAgents;
  735. }
  736. public int GetTotalObjectsCount()
  737. {
  738. return m_numTotalPrim;
  739. }
  740. public int GetTotalPrimObjectsCount()
  741. {
  742. return m_numPrim;
  743. }
  744. public int GetTotalMeshObjectsCount()
  745. {
  746. return m_numMesh;
  747. }
  748. public int GetActiveObjectsCount()
  749. {
  750. return m_physicalPrim;
  751. }
  752. public int GetActiveScriptsCount()
  753. {
  754. return m_activeScripts;
  755. }
  756. public int GetScriptLPS()
  757. {
  758. int returnval = m_scriptLPS;
  759. m_scriptLPS = 0;
  760. return returnval;
  761. }
  762. #endregion
  763. #region Get Methods
  764. /// <summary>
  765. /// Get the controlling client for the given avatar, if there is one.
  766. ///
  767. /// FIXME: The only user of the method right now is Caps.cs, in order to resolve a client API since it can't
  768. /// use the ScenePresence. This could be better solved in a number of ways - we could establish an
  769. /// OpenSim.Framework.IScenePresence, or move the caps code into a region package (which might be the more
  770. /// suitable solution).
  771. /// </summary>
  772. /// <param name="agentId"></param>
  773. /// <returns>null if either the avatar wasn't in the scene, or
  774. /// they do not have a controlling client</returns>
  775. /// <remarks>this used to be protected internal, but that
  776. /// prevents CapabilitiesModule from accessing it</remarks>
  777. public IClientAPI GetControllingClient(UUID agentId)
  778. {
  779. bool entered = false;
  780. try
  781. {
  782. try { }
  783. finally
  784. {
  785. m_scenePresencesLock.EnterReadLock();
  786. entered = true;
  787. }
  788. ScenePresence presence;
  789. if (m_scenePresenceMap.TryGetValue(agentId, out presence))
  790. return presence.ControllingClient;
  791. return null;
  792. }
  793. catch
  794. {
  795. return null;
  796. }
  797. finally
  798. {
  799. if (entered)
  800. m_scenePresencesLock.ExitReadLock();
  801. }
  802. }
  803. /// <summary>
  804. /// Get a reference to the scene presence list. Changes to the list will be done in a copy
  805. /// There is no guarantee that presences will remain in the scene after the list is returned.
  806. /// This list should remain private to SceneGraph. Callers wishing to iterate should instead
  807. /// pass a delegate to ForEachScenePresence.
  808. /// </summary>
  809. /// <returns></returns>
  810. protected internal List<ScenePresence> GetScenePresences()
  811. {
  812. bool entered = false;
  813. try
  814. {
  815. try{ }
  816. finally
  817. {
  818. m_scenePresencesLock.EnterWriteLock();
  819. entered = true;
  820. }
  821. if(m_scenePresenceList == null)
  822. m_scenePresenceList = new List<ScenePresence>(m_scenePresenceMap.Values);
  823. return m_scenePresenceList;
  824. }
  825. catch
  826. {
  827. return new List<ScenePresence>();
  828. }
  829. finally
  830. {
  831. if(entered)
  832. m_scenePresencesLock.ExitWriteLock();
  833. }
  834. }
  835. /// <summary>
  836. /// Request a scene presence by UUID. Fast, indexed lookup.
  837. /// </summary>
  838. /// <param name="agentID"></param>
  839. /// <returns>null if the presence was not found</returns>
  840. protected internal ScenePresence GetScenePresence(UUID agentID)
  841. {
  842. bool entered = false;
  843. try
  844. {
  845. try { }
  846. finally
  847. {
  848. m_scenePresencesLock.EnterReadLock();
  849. entered = true;
  850. }
  851. ScenePresence presence;
  852. m_scenePresenceMap.TryGetValue(agentID, out presence);
  853. return presence;
  854. }
  855. catch
  856. {
  857. return null;
  858. }
  859. finally
  860. {
  861. if (entered)
  862. m_scenePresencesLock.ExitReadLock();
  863. }
  864. }
  865. /// <summary>
  866. /// Request the scene presence by name.
  867. /// </summary>
  868. /// <param name="firstName"></param>
  869. /// <param name="lastName"></param>
  870. /// <returns>null if the presence was not found</returns>
  871. protected internal ScenePresence GetScenePresence(string firstName, string lastName)
  872. {
  873. List<ScenePresence> presences = GetScenePresences();
  874. foreach (ScenePresence presence in presences)
  875. {
  876. if (string.Equals(presence.Firstname, firstName, StringComparison.CurrentCultureIgnoreCase)
  877. && string.Equals(presence.Lastname, lastName, StringComparison.CurrentCultureIgnoreCase))
  878. return presence;
  879. }
  880. return null;
  881. }
  882. /// <summary>
  883. /// Request the scene presence by localID.
  884. /// </summary>
  885. /// <param name="localID"></param>
  886. /// <returns>null if the presence was not found</returns>
  887. protected internal ScenePresence GetScenePresence(uint localID)
  888. {
  889. bool entered = false;
  890. try
  891. {
  892. try { }
  893. finally
  894. {
  895. m_scenePresencesLock.EnterReadLock();
  896. entered = true;
  897. }
  898. ScenePresence sp;
  899. if (m_scenePresenceLocalIDMap.TryGetValue(localID, out sp))
  900. return sp;
  901. }
  902. finally
  903. {
  904. if (entered)
  905. m_scenePresencesLock.ExitReadLock();
  906. }
  907. return null;
  908. }
  909. protected internal bool TryGetScenePresence(UUID agentID, out ScenePresence avatar)
  910. {
  911. bool entered = false;
  912. try
  913. {
  914. try { }
  915. finally
  916. {
  917. m_scenePresencesLock.EnterReadLock();
  918. entered = true;
  919. }
  920. return m_scenePresenceMap.TryGetValue(agentID, out avatar);
  921. }
  922. catch
  923. {
  924. avatar = null;
  925. return false;
  926. }
  927. finally
  928. {
  929. if (entered)
  930. m_scenePresencesLock.ExitReadLock();
  931. }
  932. }
  933. protected internal bool TryGetAvatarByName(string name, out ScenePresence avatar)
  934. {
  935. avatar = null;
  936. List<ScenePresence> presences = GetScenePresences();
  937. foreach (ScenePresence presence in presences)
  938. {
  939. if (String.Compare(name, presence.ControllingClient.Name, true) == 0)
  940. {
  941. avatar = presence;
  942. break;
  943. }
  944. }
  945. return (avatar != null);
  946. }
  947. /// <summary>
  948. /// Get a scene object group that contains the prim with the given local id
  949. /// </summary>
  950. /// <param name="localID"></param>
  951. /// <returns>null if no scene object group containing that prim is found</returns>
  952. public SceneObjectGroup GetGroupByPrim(uint localID)
  953. {
  954. bool entered = false;
  955. try
  956. {
  957. try { }
  958. finally
  959. {
  960. m_scenePartsLock.EnterReadLock();
  961. entered = true;
  962. }
  963. SceneObjectPart sop;
  964. if(m_scenePartsByLocalID.TryGetValue(localID, out sop))
  965. return sop.ParentGroup;
  966. return null;
  967. }
  968. finally
  969. {
  970. if(entered)
  971. m_scenePartsLock.ExitReadLock();
  972. }
  973. }
  974. /// <summary>
  975. /// Get a scene object group that contains the prim with the given uuid
  976. /// </summary>
  977. /// <param name="fullID"></param>
  978. /// <returns>null if no scene object group containing that prim is found</returns>
  979. public SceneObjectGroup GetGroupByPrim(UUID fullID)
  980. {
  981. bool entered = false;
  982. try
  983. {
  984. try { }
  985. finally
  986. {
  987. m_scenePartsLock.EnterReadLock();
  988. entered = true;
  989. }
  990. SceneObjectPart sop;
  991. if (m_scenePartsByID.TryGetValue(fullID, out sop))
  992. return sop.ParentGroup;
  993. return null;
  994. }
  995. finally
  996. {
  997. if (entered)
  998. m_scenePartsLock.ExitReadLock();
  999. }
  1000. }
  1001. protected internal EntityIntersection GetClosestIntersectingPrim(Ray hray, bool frontFacesOnly, bool faceCenters)
  1002. {
  1003. // Primitive Ray Tracing
  1004. float closestDistance = 280f;
  1005. EntityIntersection result = new EntityIntersection();
  1006. EntityBase[] EntityList = GetEntities();
  1007. foreach (EntityBase ent in EntityList)
  1008. {
  1009. if (ent is SceneObjectGroup)
  1010. {
  1011. SceneObjectGroup reportingG = (SceneObjectGroup)ent;
  1012. EntityIntersection inter = reportingG.TestIntersection(hray, frontFacesOnly, faceCenters);
  1013. if (inter.HitTF && inter.distance < closestDistance)
  1014. {
  1015. closestDistance = inter.distance;
  1016. result = inter;
  1017. }
  1018. }
  1019. }
  1020. return result;
  1021. }
  1022. /// <summary>
  1023. /// Get all the scene object groups.
  1024. /// </summary>
  1025. /// <returns>
  1026. /// The scene object groups. If the scene is empty then an empty list is returned.
  1027. /// </returns>
  1028. protected internal List<SceneObjectGroup> GetSceneObjectGroups()
  1029. {
  1030. EntityBase[] entities = Entities.GetEntities();
  1031. List<SceneObjectGroup> ret = new List<SceneObjectGroup>(256);
  1032. for (int i = 0; i < entities.Length; ++i)
  1033. {
  1034. if(entities[i] is SceneObjectGroup)
  1035. ret.Add((SceneObjectGroup)entities[i]);
  1036. }
  1037. return ret;
  1038. }
  1039. /// <summary>
  1040. /// Get a group in the scene
  1041. /// </summary>
  1042. /// <param name="fullID">UUID of the group</param>
  1043. /// <returns>null if no such group was found</returns>
  1044. protected internal SceneObjectGroup GetSceneObjectGroup(UUID fullID)
  1045. {
  1046. EntityBase entity;
  1047. if (Entities.TryGetValue(fullID, out entity) && (entity is SceneObjectGroup))
  1048. return (SceneObjectGroup)entity;
  1049. return null;
  1050. }
  1051. /// <summary>
  1052. /// Get a group in the scene
  1053. /// <param name="localID">Local id of the root part of the group</param>
  1054. /// <returns>null if no such group was found</returns>
  1055. protected internal SceneObjectGroup GetSceneObjectGroup(uint localID)
  1056. {
  1057. EntityBase entity;
  1058. if (Entities.TryGetValue(localID, out entity) && (entity is SceneObjectGroup))
  1059. return (SceneObjectGroup)entity;
  1060. return null;
  1061. }
  1062. /// <summary>
  1063. /// Get a group by name from the scene (will return the first
  1064. /// found, if there are more than one prim with the same name)
  1065. /// </summary>
  1066. /// <param name="name"></param>
  1067. /// <returns>null if the part was not found</returns>
  1068. protected internal SceneObjectGroup GetSceneObjectGroup(string name)
  1069. {
  1070. EntityBase entity = null;
  1071. EntityBase[] entities = Entities.GetEntities();
  1072. for (int i = 0; i < entities.Length; ++i)
  1073. {
  1074. entity = entities[i];
  1075. if (entity is SceneObjectGroup && entity.Name == name)
  1076. return (SceneObjectGroup)entity;
  1077. }
  1078. return null;
  1079. }
  1080. /// <summary>
  1081. /// Get a part contained in this scene.
  1082. /// </summary>
  1083. /// <param name="localID"></param>
  1084. /// <returns>null if the part was not found</returns>
  1085. protected internal SceneObjectPart GetSceneObjectPart(uint localID)
  1086. {
  1087. bool entered = false;
  1088. try
  1089. {
  1090. try { }
  1091. finally
  1092. {
  1093. m_scenePartsLock.EnterReadLock();
  1094. entered = true;
  1095. }
  1096. SceneObjectPart sop;
  1097. if (m_scenePartsByLocalID.TryGetValue(localID, out sop))
  1098. {
  1099. if (sop.ParentGroup == null || sop.ParentGroup.IsDeleted)
  1100. return null;
  1101. return sop;
  1102. }
  1103. return null;
  1104. }
  1105. finally
  1106. {
  1107. if (entered)
  1108. m_scenePartsLock.ExitReadLock();
  1109. }
  1110. }
  1111. /// <summary>
  1112. /// Get a part contained in this scene.
  1113. /// </summary>
  1114. /// <param name="fullID"></param>
  1115. /// <returns>null if the part was not found</returns>
  1116. protected internal SceneObjectPart GetSceneObjectPart(UUID fullID)
  1117. {
  1118. bool entered = false;
  1119. try
  1120. {
  1121. try { }
  1122. finally
  1123. {
  1124. m_scenePartsLock.EnterReadLock();
  1125. entered = true;
  1126. }
  1127. SceneObjectPart sop;
  1128. if (m_scenePartsByID.TryGetValue(fullID, out sop))
  1129. {
  1130. if (sop.ParentGroup == null || sop.ParentGroup.IsDeleted)
  1131. return null;
  1132. return sop;
  1133. }
  1134. return null;
  1135. }
  1136. finally
  1137. {
  1138. if (entered)
  1139. m_scenePartsLock.ExitReadLock();
  1140. }
  1141. }
  1142. /// <summary>
  1143. /// Get a prim by name from the scene (will return the first
  1144. /// found, if there are more than one prim with the same name)
  1145. /// </summary>
  1146. /// <param name="name"></param>
  1147. /// <returns>null if the part was not found</returns>
  1148. protected internal SceneObjectPart GetSceneObjectPart(string name)
  1149. {
  1150. SceneObjectPart[] parts = GetPartsArray();
  1151. SceneObjectPart sop;
  1152. for (int i = 0; i < parts.Length; ++i)
  1153. {
  1154. sop = parts[i];
  1155. if (sop.ParentGroup == null || sop.ParentGroup.IsDeleted)
  1156. continue;
  1157. if (sop.Name == name)
  1158. return sop;
  1159. }
  1160. return null;
  1161. }
  1162. protected internal SceneObjectPart[] GetPartsArray()
  1163. {
  1164. bool entered = false;
  1165. try
  1166. {
  1167. try { }
  1168. finally
  1169. {
  1170. m_scenePartsLock.EnterWriteLock();
  1171. entered = true;
  1172. }
  1173. if(m_scenePartsArray == null)
  1174. {
  1175. m_scenePartsArray = new SceneObjectPart[m_scenePartsByID.Count];
  1176. m_scenePartsByID.Values.CopyTo(m_scenePartsArray, 0);
  1177. }
  1178. return m_scenePartsArray;
  1179. }
  1180. finally
  1181. {
  1182. if(entered)
  1183. m_scenePartsLock.ExitWriteLock();
  1184. }
  1185. }
  1186. /// <summary>
  1187. /// Returns a list of the entities in the scene. This is a new list so no locking is required to iterate over
  1188. /// it
  1189. /// </summary>
  1190. /// <returns></returns>
  1191. protected internal EntityBase[] GetEntities()
  1192. {
  1193. return Entities.GetEntities();
  1194. }
  1195. #endregion
  1196. #region Other Methods
  1197. protected internal void physicsBasedCrash()
  1198. {
  1199. handlerPhysicsCrash = UnRecoverableError;
  1200. if (handlerPhysicsCrash != null)
  1201. {
  1202. handlerPhysicsCrash();
  1203. }
  1204. }
  1205. /// <summary>
  1206. /// Performs action once on all scene object groups.
  1207. /// </summary>
  1208. /// <param name="action"></param>
  1209. protected internal void ForEachSOG(Action<SceneObjectGroup> action)
  1210. {
  1211. EntityBase[] entities = Entities.GetEntities();
  1212. EntityBase entity;
  1213. for (int i = 0; i < entities.Length; ++i)
  1214. {
  1215. entity = entities[i];
  1216. if (entity is SceneObjectGroup)
  1217. {
  1218. try
  1219. {
  1220. action((SceneObjectGroup)entity);
  1221. }
  1222. catch (Exception e)
  1223. {
  1224. // Catch it and move on. This includes situations where objlist has inconsistent info
  1225. m_log.WarnFormat(
  1226. "[SCENEGRAPH]: Problem processing action in ForEachSOG: {0} {1}", e.Message, e.StackTrace);
  1227. }
  1228. }
  1229. }
  1230. }
  1231. /// <summary>
  1232. /// Performs action on all ROOT (not child) scene presences.
  1233. /// This is just a shortcut function since frequently actions only appy to root SPs
  1234. /// </summary>
  1235. /// <param name="action"></param>
  1236. public void ForEachAvatar(Action<ScenePresence> action)
  1237. {
  1238. ForEachScenePresence(delegate(ScenePresence sp)
  1239. {
  1240. if (!sp.IsChildAgent)
  1241. action(sp);
  1242. });
  1243. }
  1244. /// <summary>
  1245. /// Performs action on all scene presences. This can ultimately run the actions in parallel but
  1246. /// any delegates passed in will need to implement their own locking on data they reference and
  1247. /// modify outside of the scope of the delegate.
  1248. /// </summary>
  1249. /// <param name="action"></param>
  1250. public void ForEachScenePresence(Action<ScenePresence> action)
  1251. {
  1252. List<ScenePresence> presences = GetScenePresences();
  1253. foreach (ScenePresence sp in presences)
  1254. {
  1255. try
  1256. {
  1257. action(sp);
  1258. }
  1259. catch (Exception e)
  1260. {
  1261. m_log.Error("[SCENEGRAPH]: Error in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString());
  1262. }
  1263. }
  1264. }
  1265. #endregion
  1266. #region Client Event handlers
  1267. protected internal void ClientChangeObject(uint localID, object odata, IClientAPI remoteClient)
  1268. {
  1269. SceneObjectPart part = GetSceneObjectPart(localID);
  1270. ObjectChangeData data = (ObjectChangeData)odata;
  1271. if (part != null)
  1272. {
  1273. SceneObjectGroup grp = part.ParentGroup;
  1274. if (grp != null)
  1275. {
  1276. if (m_parentScene.Permissions.CanEditObject(grp, remoteClient))
  1277. {
  1278. // These two are exceptions SL makes in the interpretation
  1279. // of the change flags. Must check them here because otherwise
  1280. // the group flag (see below) would be lost
  1281. if (data.change == ObjectChangeType.groupS)
  1282. data.change = ObjectChangeType.primS;
  1283. if (data.change == ObjectChangeType.groupPS)
  1284. data.change = ObjectChangeType.primPS;
  1285. part.StoreUndoState(data.change); // lets test only saving what we changed
  1286. grp.doChangeObject(part, (ObjectChangeData)data);
  1287. }
  1288. else
  1289. {
  1290. // Is this any kind of group operation?
  1291. if ((data.change & ObjectChangeType.Group) != 0)
  1292. {
  1293. // Is a move and/or rotation requested?
  1294. if ((data.change & (ObjectChangeType.Position | ObjectChangeType.Rotation)) != 0)
  1295. {
  1296. // Are we allowed to move it?
  1297. if (m_parentScene.Permissions.CanMoveObject(grp, remoteClient))
  1298. {
  1299. // Strip all but move and rotation from request
  1300. data.change &= (ObjectChangeType.Group | ObjectChangeType.Position | ObjectChangeType.Rotation);
  1301. part.StoreUndoState(data.change);
  1302. grp.doChangeObject(part, (ObjectChangeData)data);
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. }
  1309. }
  1310. /// <summary>
  1311. /// Update the scale of an individual prim.
  1312. /// </summary>
  1313. /// <param name="localID"></param>
  1314. /// <param name="scale"></param>
  1315. /// <param name="remoteClient"></param>
  1316. protected internal void UpdatePrimScale(uint localID, Vector3 scale, IClientAPI remoteClient)
  1317. {
  1318. SceneObjectPart part = GetSceneObjectPart(localID);
  1319. if (part != null)
  1320. {
  1321. if (m_parentScene.Permissions.CanEditObject(part.ParentGroup, remoteClient))
  1322. {
  1323. bool physbuild = false;
  1324. if (part.ParentGroup.RootPart.PhysActor != null)
  1325. {
  1326. part.ParentGroup.RootPart.PhysActor.Building = true;
  1327. physbuild = true;
  1328. }
  1329. part.Resize(scale);
  1330. if (physbuild)
  1331. part.ParentGroup.RootPart.PhysActor.Building = false;
  1332. }
  1333. }
  1334. }
  1335. protected internal void UpdatePrimGroupScale(uint localID, Vector3 scale, IClientAPI remoteClient)
  1336. {
  1337. SceneObjectGroup group = GetGroupByPrim(localID);
  1338. if (group != null)
  1339. {
  1340. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1341. {
  1342. bool physbuild = false;
  1343. if (group.RootPart.PhysActor != null)
  1344. {
  1345. group.RootPart.PhysActor.Building = true;
  1346. physbuild = true;
  1347. }
  1348. group.GroupResize(scale);
  1349. if (physbuild)
  1350. group.RootPart.PhysActor.Building = false;
  1351. }
  1352. }
  1353. }
  1354. /// <summary>
  1355. /// This handles the nifty little tool tip that you get when you drag your mouse over an object
  1356. /// Send to the Object Group to process. We don't know enough to service the request
  1357. /// </summary>
  1358. /// <param name="remoteClient"></param>
  1359. /// <param name="AgentID"></param>
  1360. /// <param name="RequestFlags"></param>
  1361. /// <param name="ObjectID"></param>
  1362. protected internal void RequestObjectPropertiesFamily(
  1363. IClientAPI remoteClient, UUID AgentID, uint RequestFlags, UUID ObjectID)
  1364. {
  1365. SceneObjectGroup group = GetGroupByPrim(ObjectID);
  1366. if (group != null)
  1367. {
  1368. group.ServiceObjectPropertiesFamilyRequest(remoteClient, AgentID, RequestFlags);
  1369. }
  1370. }
  1371. /// <summary>
  1372. ///
  1373. /// </summary>
  1374. /// <param name="localID"></param>
  1375. /// <param name="rot"></param>
  1376. /// <param name="remoteClient"></param>
  1377. protected internal void UpdatePrimSingleRotation(uint localID, Quaternion rot, IClientAPI remoteClient)
  1378. {
  1379. SceneObjectGroup group = GetGroupByPrim(localID);
  1380. if (group != null)
  1381. {
  1382. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient))
  1383. {
  1384. group.UpdateSingleRotation(rot, localID);
  1385. }
  1386. }
  1387. }
  1388. /// <summary>
  1389. ///
  1390. /// </summary>
  1391. /// <param name="localID"></param>
  1392. /// <param name="rot"></param>
  1393. /// <param name="remoteClient"></param>
  1394. protected internal void UpdatePrimSingleRotationPosition(uint localID, Quaternion rot, Vector3 pos, IClientAPI remoteClient)
  1395. {
  1396. SceneObjectGroup group = GetGroupByPrim(localID);
  1397. if (group != null)
  1398. {
  1399. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient))
  1400. {
  1401. group.UpdateSingleRotation(rot, pos, localID);
  1402. }
  1403. }
  1404. }
  1405. /// <summary>
  1406. /// Update the rotation of a whole group.
  1407. /// </summary>
  1408. /// <param name="localID"></param>
  1409. /// <param name="rot"></param>
  1410. /// <param name="remoteClient"></param>
  1411. protected internal void UpdatePrimGroupRotation(uint localID, Quaternion rot, IClientAPI remoteClient)
  1412. {
  1413. SceneObjectGroup group = GetGroupByPrim(localID);
  1414. if (group != null)
  1415. {
  1416. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient))
  1417. {
  1418. group.UpdateGroupRotationR(rot);
  1419. }
  1420. }
  1421. }
  1422. /// <summary>
  1423. ///
  1424. /// </summary>
  1425. /// <param name="localID"></param>
  1426. /// <param name="pos"></param>
  1427. /// <param name="rot"></param>
  1428. /// <param name="remoteClient"></param>
  1429. protected internal void UpdatePrimGroupRotation(uint localID, Vector3 pos, Quaternion rot, IClientAPI remoteClient)
  1430. {
  1431. SceneObjectGroup group = GetGroupByPrim(localID);
  1432. if (group != null)
  1433. {
  1434. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient))
  1435. {
  1436. group.UpdateGroupRotationPR(pos, rot);
  1437. }
  1438. }
  1439. }
  1440. /// <summary>
  1441. /// Update the position of the given part
  1442. /// </summary>
  1443. /// <param name="localID"></param>
  1444. /// <param name="pos"></param>
  1445. /// <param name="remoteClient"></param>
  1446. protected internal void UpdatePrimSinglePosition(uint localID, Vector3 pos, IClientAPI remoteClient)
  1447. {
  1448. SceneObjectGroup group = GetGroupByPrim(localID);
  1449. if (group != null)
  1450. {
  1451. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient) || group.IsAttachment)
  1452. {
  1453. group.UpdateSinglePosition(pos, localID);
  1454. }
  1455. }
  1456. }
  1457. /// <summary>
  1458. /// Update the position of the given group.
  1459. /// </summary>
  1460. /// <param name="localId"></param>
  1461. /// <param name="pos"></param>
  1462. /// <param name="remoteClient"></param>
  1463. public void UpdatePrimGroupPosition(uint localId, Vector3 pos, IClientAPI remoteClient)
  1464. {
  1465. SceneObjectGroup group = GetGroupByPrim(localId);
  1466. if (group != null)
  1467. {
  1468. if (group.IsAttachment || (group.RootPart.Shape.PCode == 9 && group.RootPart.Shape.State != 0))
  1469. {
  1470. // Set the new attachment point data in the object
  1471. byte attachmentPoint = (byte)group.AttachmentPoint;
  1472. group.UpdateGroupPosition(pos);
  1473. group.IsAttachment = false;
  1474. group.AbsolutePosition = group.RootPart.AttachedPos;
  1475. group.AttachmentPoint = attachmentPoint;
  1476. group.HasGroupChanged = true;
  1477. }
  1478. else
  1479. {
  1480. if (m_parentScene.Permissions.CanMoveObject(group, remoteClient)
  1481. && m_parentScene.Permissions.CanObjectEntry(group, false, pos))
  1482. {
  1483. group.UpdateGroupPosition(pos);
  1484. }
  1485. }
  1486. }
  1487. }
  1488. /// <summary>
  1489. /// Update the texture entry of the given prim.
  1490. /// </summary>
  1491. /// <remarks>
  1492. /// A texture entry is an object that contains details of all the textures of the prim's face. In this case,
  1493. /// the texture is given in its byte serialized form.
  1494. /// </remarks>
  1495. /// <param name="localID"></param>
  1496. /// <param name="texture"></param>
  1497. /// <param name="remoteClient"></param>
  1498. protected internal void UpdatePrimTexture(uint localID, byte[] texture, IClientAPI remoteClient)
  1499. {
  1500. SceneObjectPart part = GetSceneObjectPart(localID);
  1501. if(part == null)
  1502. return;
  1503. SceneObjectGroup group = part.ParentGroup;
  1504. if (group != null && !group.IsDeleted)
  1505. {
  1506. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1507. {
  1508. part.UpdateTextureEntry(texture);
  1509. }
  1510. }
  1511. }
  1512. /// <summary>
  1513. /// Update the flags on a scene object. This covers properties such as phantom, physics and temporary.
  1514. /// </summary>
  1515. /// <remarks>
  1516. /// This is currently handling the incoming call from the client stack (e.g. LLClientView).
  1517. /// </remarks>
  1518. /// <param name="localID"></param>
  1519. /// <param name="UsePhysics"></param>
  1520. /// <param name="SetTemporary"></param>
  1521. /// <param name="SetPhantom"></param>
  1522. /// <param name="remoteClient"></param>
  1523. protected internal void UpdatePrimFlags(
  1524. uint localID, bool UsePhysics, bool SetTemporary, bool SetPhantom, ExtraPhysicsData PhysData, IClientAPI remoteClient)
  1525. {
  1526. SceneObjectPart part = GetSceneObjectPart(localID);
  1527. if(part == null)
  1528. return;
  1529. SceneObjectGroup group = part.ParentGroup;
  1530. if(group == null || group.IsDeleted)
  1531. return;
  1532. if (!m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1533. return;
  1534. // VolumeDetect can't be set via UI and will always be off when a change is made there
  1535. // now only change volume dtc if phantom off
  1536. bool wantedPhys = UsePhysics;
  1537. if (PhysData.PhysShapeType == PhysShapeType.invalid) // check for extraPhysics data
  1538. {
  1539. bool vdtc;
  1540. if (SetPhantom) // if phantom keep volumedtc
  1541. vdtc = group.RootPart.VolumeDetectActive;
  1542. else // else turn it off
  1543. vdtc = false;
  1544. group.UpdateFlags(UsePhysics, SetTemporary, SetPhantom, vdtc);
  1545. }
  1546. else
  1547. {
  1548. part.UpdateExtraPhysics(PhysData);
  1549. if (remoteClient != null)
  1550. remoteClient.SendPartPhysicsProprieties(part);
  1551. }
  1552. if (wantedPhys != group.UsesPhysics && remoteClient != null)
  1553. {
  1554. if(m_parentScene.m_linksetPhysCapacity != 0)
  1555. remoteClient.SendAlertMessage("Object physics cancelled because it exceeds limits for physical prims, either size or number of primswith shape type not set to None");
  1556. else
  1557. remoteClient.SendAlertMessage("Object physics cancelled because it exceeds size limits for physical prims");
  1558. group.RootPart.ScheduleFullUpdate();
  1559. }
  1560. }
  1561. /// <summary>
  1562. ///
  1563. /// </summary>
  1564. /// <param name="primLocalID"></param>
  1565. /// <param name="description"></param>
  1566. protected internal void PrimName(IClientAPI remoteClient, uint primLocalID, string name)
  1567. {
  1568. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1569. if (group != null)
  1570. {
  1571. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1572. {
  1573. group.SetPartName(Util.CleanString(name), primLocalID);
  1574. group.HasGroupChanged = true;
  1575. }
  1576. }
  1577. }
  1578. /// <summary>
  1579. /// Handle a prim description set request from a viewer.
  1580. /// </summary>
  1581. /// <param name="primLocalID"></param>
  1582. /// <param name="description"></param>
  1583. protected internal void PrimDescription(IClientAPI remoteClient, uint primLocalID, string description)
  1584. {
  1585. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1586. if (group != null)
  1587. {
  1588. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1589. {
  1590. group.SetPartDescription(Util.CleanString(description), primLocalID);
  1591. group.HasGroupChanged = true;
  1592. }
  1593. }
  1594. }
  1595. /// <summary>
  1596. /// Set a click action for the prim.
  1597. /// </summary>
  1598. /// <param name="remoteClient"></param>
  1599. /// <param name="primLocalID"></param>
  1600. /// <param name="clickAction"></param>
  1601. protected internal void PrimClickAction(IClientAPI remoteClient, uint primLocalID, string clickAction)
  1602. {
  1603. // m_log.DebugFormat(
  1604. // "[SCENEGRAPH]: User {0} set click action for {1} to {2}", remoteClient.Name, primLocalID, clickAction);
  1605. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1606. if (group != null)
  1607. {
  1608. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1609. {
  1610. SceneObjectPart part = group.GetPart(primLocalID);
  1611. if (part != null)
  1612. {
  1613. part.ClickAction = Convert.ToByte(clickAction);
  1614. group.HasGroupChanged = true;
  1615. }
  1616. }
  1617. }
  1618. }
  1619. protected internal void PrimMaterial(IClientAPI remoteClient, uint primLocalID, string material)
  1620. {
  1621. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1622. if (group != null)
  1623. {
  1624. if (m_parentScene.Permissions.CanEditObject(group, remoteClient))
  1625. {
  1626. SceneObjectPart part = group.GetPart(primLocalID);
  1627. if (part != null)
  1628. {
  1629. part.Material = Convert.ToByte(material);
  1630. group.HasGroupChanged = true;
  1631. remoteClient.SendPartPhysicsProprieties(part);
  1632. }
  1633. }
  1634. }
  1635. }
  1636. protected internal void UpdateExtraParam(UUID agentID, uint primLocalID, ushort type, bool inUse, byte[] data)
  1637. {
  1638. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1639. if (group != null)
  1640. {
  1641. if (m_parentScene.Permissions.CanEditObject(group.UUID, agentID))
  1642. {
  1643. group.UpdateExtraParam(primLocalID, type, inUse, data);
  1644. }
  1645. }
  1646. }
  1647. /// <summary>
  1648. ///
  1649. /// </summary>
  1650. /// <param name="primLocalID"></param>
  1651. /// <param name="shapeBlock"></param>
  1652. protected internal void UpdatePrimShape(UUID agentID, uint primLocalID, UpdateShapeArgs shapeBlock)
  1653. {
  1654. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1655. if (group != null)
  1656. {
  1657. if (m_parentScene.Permissions.CanEditObject(group.UUID, agentID))
  1658. {
  1659. ObjectShapePacket.ObjectDataBlock shapeData = new ObjectShapePacket.ObjectDataBlock();
  1660. shapeData.ObjectLocalID = shapeBlock.ObjectLocalID;
  1661. shapeData.PathBegin = shapeBlock.PathBegin;
  1662. shapeData.PathCurve = shapeBlock.PathCurve;
  1663. shapeData.PathEnd = shapeBlock.PathEnd;
  1664. shapeData.PathRadiusOffset = shapeBlock.PathRadiusOffset;
  1665. shapeData.PathRevolutions = shapeBlock.PathRevolutions;
  1666. shapeData.PathScaleX = shapeBlock.PathScaleX;
  1667. shapeData.PathScaleY = shapeBlock.PathScaleY;
  1668. shapeData.PathShearX = shapeBlock.PathShearX;
  1669. shapeData.PathShearY = shapeBlock.PathShearY;
  1670. shapeData.PathSkew = shapeBlock.PathSkew;
  1671. shapeData.PathTaperX = shapeBlock.PathTaperX;
  1672. shapeData.PathTaperY = shapeBlock.PathTaperY;
  1673. shapeData.PathTwist = shapeBlock.PathTwist;
  1674. shapeData.PathTwistBegin = shapeBlock.PathTwistBegin;
  1675. shapeData.ProfileBegin = shapeBlock.ProfileBegin;
  1676. shapeData.ProfileCurve = shapeBlock.ProfileCurve;
  1677. shapeData.ProfileEnd = shapeBlock.ProfileEnd;
  1678. shapeData.ProfileHollow = shapeBlock.ProfileHollow;
  1679. group.UpdateShape(shapeData, primLocalID);
  1680. }
  1681. }
  1682. }
  1683. /// <summary>
  1684. /// Initial method invoked when we receive a link objects request from the client.
  1685. /// </summary>
  1686. /// <param name="client"></param>
  1687. /// <param name="parentPrim"></param>
  1688. /// <param name="childPrims"></param>
  1689. protected internal void LinkObjects(SceneObjectPart root, List<SceneObjectPart> children)
  1690. {
  1691. if (root.KeyframeMotion != null)
  1692. {
  1693. root.KeyframeMotion.Stop();
  1694. root.KeyframeMotion = null;
  1695. }
  1696. SceneObjectGroup parentGroup = root.ParentGroup;
  1697. if (parentGroup == null) return;
  1698. // Cowardly refuse to link to a group owned root
  1699. if (parentGroup.OwnerID == parentGroup.GroupID)
  1700. return;
  1701. Monitor.Enter(m_linkLock);
  1702. try
  1703. {
  1704. List<SceneObjectGroup> childGroups = new List<SceneObjectGroup>();
  1705. // We do this in reverse to get the link order of the prims correct
  1706. for (int i = 0; i < children.Count; i++)
  1707. {
  1708. SceneObjectGroup child = children[i].ParentGroup;
  1709. // Don't try and add a group to itself - this will only cause severe problems later on.
  1710. if (child == parentGroup)
  1711. continue;
  1712. // Make sure no child prim is set for sale
  1713. // So that, on delink, no prims are unwittingly
  1714. // left for sale and sold off
  1715. if (child != null)
  1716. {
  1717. child.RootPart.ObjectSaleType = 0;
  1718. child.RootPart.SalePrice = 10;
  1719. childGroups.Add(child);
  1720. }
  1721. }
  1722. foreach (SceneObjectGroup child in childGroups)
  1723. {
  1724. if (parentGroup.OwnerID == child.OwnerID)
  1725. {
  1726. child.DetachFromBackup();
  1727. parentGroup.LinkToGroup(child);
  1728. // this is here so physics gets updated!
  1729. // Don't remove! Bad juju! Stay away! or fix physics!
  1730. // already done in LinkToGroup
  1731. // child.AbsolutePosition = child.AbsolutePosition;
  1732. }
  1733. }
  1734. // We need to explicitly resend the newly link prim's object properties since no other actions
  1735. // occur on link to invoke this elsewhere (such as object selection)
  1736. if (childGroups.Count > 0)
  1737. {
  1738. parentGroup.RootPart.CreateSelected = true;
  1739. parentGroup.TriggerScriptChangedEvent(Changed.LINK);
  1740. }
  1741. }
  1742. finally
  1743. {
  1744. /*
  1745. lock (SceneObjectGroupsByLocalPartID)
  1746. {
  1747. foreach (SceneObjectPart part in parentGroup.Parts)
  1748. SceneObjectGroupsByLocalPartID[part.LocalId] = parentGroup;
  1749. }
  1750. */
  1751. parentGroup.AdjustChildPrimPermissions(false);
  1752. parentGroup.HasGroupChanged = true;
  1753. parentGroup.ScheduleGroupForFullAnimUpdate();
  1754. Monitor.Exit(m_linkLock);
  1755. }
  1756. }
  1757. /// <summary>
  1758. /// Delink a linkset
  1759. /// </summary>
  1760. /// <param name="prims"></param>
  1761. protected internal void DelinkObjects(List<SceneObjectPart> prims)
  1762. {
  1763. List<SceneObjectPart> childParts = new List<SceneObjectPart>();
  1764. List<SceneObjectPart> rootParts = new List<SceneObjectPart>();
  1765. List<SceneObjectGroup> affectedGroups = new List<SceneObjectGroup>();
  1766. // Look them all up in one go, since that is comparatively expensive
  1767. //
  1768. Monitor.Enter(m_linkLock);
  1769. try
  1770. {
  1771. foreach (SceneObjectPart part in prims)
  1772. {
  1773. if(part == null)
  1774. continue;
  1775. SceneObjectGroup parentSOG = part.ParentGroup;
  1776. if(parentSOG == null || parentSOG.IsDeleted || parentSOG.inTransit || parentSOG.PrimCount == 1)
  1777. continue;
  1778. if (!affectedGroups.Contains(parentSOG))
  1779. {
  1780. affectedGroups.Add(parentSOG);
  1781. if(parentSOG.RootPart.PhysActor != null)
  1782. parentSOG.RootPart.PhysActor.Building = true;
  1783. }
  1784. if (part.KeyframeMotion != null)
  1785. {
  1786. part.KeyframeMotion.Stop();
  1787. part.KeyframeMotion = null;
  1788. }
  1789. if (part.LinkNum < 2) // Root
  1790. {
  1791. rootParts.Add(part);
  1792. }
  1793. else
  1794. {
  1795. part.LastOwnerID = part.ParentGroup.RootPart.LastOwnerID;
  1796. part.RezzerID = part.ParentGroup.RootPart.RezzerID;
  1797. childParts.Add(part);
  1798. }
  1799. }
  1800. if (childParts.Count > 0)
  1801. {
  1802. foreach (SceneObjectPart child in childParts)
  1803. {
  1804. // Unlink all child parts from their groups
  1805. child.ParentGroup.DelinkFromGroup(child, true);
  1806. //child.ParentGroup is now other
  1807. child.ParentGroup.HasGroupChanged = true;
  1808. child.ParentGroup.ScheduleGroupForFullAnimUpdate();
  1809. }
  1810. }
  1811. foreach (SceneObjectPart root in rootParts)
  1812. {
  1813. // In most cases, this will run only one time, and the prim
  1814. // will be a solo prim
  1815. // However, editing linked parts and unlinking may be different
  1816. //
  1817. SceneObjectGroup group = root.ParentGroup;
  1818. List<SceneObjectPart> newSet = new List<SceneObjectPart>(group.Parts);
  1819. newSet.Remove(root);
  1820. int numChildren = newSet.Count;
  1821. if(numChildren == 0)
  1822. break;
  1823. foreach (SceneObjectPart p in newSet)
  1824. group.DelinkFromGroup(p, false);
  1825. SceneObjectPart newRoot = newSet[0];
  1826. // If there is more than one prim remaining, we
  1827. // need to re-link
  1828. //
  1829. if (numChildren > 1)
  1830. {
  1831. // Determine new root
  1832. //
  1833. newSet.RemoveAt(0);
  1834. foreach (SceneObjectPart newChild in newSet)
  1835. newChild.ClearUpdateSchedule();
  1836. LinkObjects(newRoot, newSet);
  1837. }
  1838. else
  1839. {
  1840. newRoot.TriggerScriptChangedEvent(Changed.LINK);
  1841. newRoot.ParentGroup.HasGroupChanged = true;
  1842. newRoot.ParentGroup.InvalidatePartsLinkMaps();
  1843. newRoot.ParentGroup.ScheduleGroupForFullAnimUpdate();
  1844. }
  1845. }
  1846. // trigger events in the roots
  1847. //
  1848. foreach (SceneObjectGroup g in affectedGroups)
  1849. {
  1850. if(g.RootPart.PhysActor != null)
  1851. g.RootPart.PhysActor.Building = false;
  1852. g.AdjustChildPrimPermissions(false);
  1853. // Child prims that have been unlinked and deleted will
  1854. // return unless the root is deleted. This will remove them
  1855. // from the database. They will be rewritten immediately,
  1856. // minus the rows for the unlinked child prims.
  1857. m_parentScene.SimulationDataService.RemoveObject(g.UUID, m_parentScene.RegionInfo.RegionID);
  1858. g.InvalidatePartsLinkMaps();
  1859. g.TriggerScriptChangedEvent(Changed.LINK);
  1860. g.HasGroupChanged = true; // Persist
  1861. g.ScheduleGroupForFullUpdate();
  1862. }
  1863. }
  1864. finally
  1865. {
  1866. Monitor.Exit(m_linkLock);
  1867. }
  1868. }
  1869. protected internal void MakeObjectSearchable(IClientAPI remoteClient, bool IncludeInSearch, uint localID)
  1870. {
  1871. SceneObjectGroup sog = GetGroupByPrim(localID);
  1872. if(sog == null)
  1873. return;
  1874. //Protip: In my day, we didn't call them searchable objects, we called them limited point-to-point joints
  1875. //aka ObjectFlags.JointWheel = IncludeInSearch
  1876. //Permissions model: Object can be REMOVED from search IFF:
  1877. // * User owns object
  1878. //use CanEditObject
  1879. //Object can be ADDED to search IFF:
  1880. // * User owns object
  1881. // * Asset/DRM permission bit "modify" is enabled
  1882. //use CanEditObjectPosition
  1883. // libomv will complain about PrimFlags.JointWheel being
  1884. // deprecated, so we
  1885. #pragma warning disable 0612
  1886. if (IncludeInSearch && m_parentScene.Permissions.CanEditObject(sog, remoteClient))
  1887. {
  1888. sog.RootPart.AddFlag(PrimFlags.JointWheel);
  1889. sog.HasGroupChanged = true;
  1890. }
  1891. else if (!IncludeInSearch && m_parentScene.Permissions.CanMoveObject(sog, remoteClient))
  1892. {
  1893. sog.RootPart.RemFlag(PrimFlags.JointWheel);
  1894. sog.HasGroupChanged = true;
  1895. }
  1896. #pragma warning restore 0612
  1897. }
  1898. /// <summary>
  1899. /// Duplicate the given object.
  1900. /// </summary>
  1901. /// <param name="originalPrim"></param>
  1902. /// <param name="offset"></param>
  1903. /// <param name="flags"></param>
  1904. /// <param name="AgentID"></param>
  1905. /// <param name="GroupID"></param>
  1906. /// <param name="rot"></param>
  1907. /// <returns>null if duplication fails, otherwise the duplicated object</returns>
  1908. /// <summary>
  1909. public SceneObjectGroup DuplicateObject(uint originalPrimID, Vector3 offset, UUID AgentID, UUID GroupID, Quaternion rot, bool createSelected)
  1910. {
  1911. // m_log.DebugFormat(
  1912. // "[SCENE]: Duplication of object {0} at offset {1} requested by agent {2}",
  1913. // originalPrimID, offset, AgentID);
  1914. SceneObjectGroup original = GetGroupByPrim(originalPrimID);
  1915. if (original != null)
  1916. {
  1917. if (m_parentScene.Permissions.CanDuplicateObject(original, AgentID))
  1918. {
  1919. SceneObjectGroup copy = original.Copy(true);
  1920. copy.AbsolutePosition = copy.AbsolutePosition + offset;
  1921. SceneObjectPart[] parts = copy.Parts;
  1922. m_numTotalPrim += parts.Length;
  1923. if (original.OwnerID != AgentID)
  1924. {
  1925. copy.SetOwner(AgentID, GroupID);
  1926. if (m_parentScene.Permissions.PropagatePermissions())
  1927. {
  1928. foreach (SceneObjectPart child in parts)
  1929. {
  1930. child.Inventory.ChangeInventoryOwner(AgentID);
  1931. child.TriggerScriptChangedEvent(Changed.OWNER);
  1932. child.ApplyNextOwnerPermissions();
  1933. }
  1934. copy.InvalidateEffectivePerms();
  1935. }
  1936. }
  1937. bool entered = false;
  1938. try
  1939. {
  1940. try { }
  1941. finally
  1942. {
  1943. m_scenePartsLock.EnterWriteLock();
  1944. entered = true;
  1945. }
  1946. Entities.Add(copy);
  1947. m_scenePartsArray = null;
  1948. foreach (SceneObjectPart part in parts)
  1949. {
  1950. if (part.GetPrimType() == PrimType.SCULPT)
  1951. m_numMesh++;
  1952. else
  1953. m_numPrim++;
  1954. m_scenePartsByID[part.UUID] = part;
  1955. m_scenePartsByLocalID[part.LocalId] = part;
  1956. }
  1957. }
  1958. finally
  1959. {
  1960. if(entered)
  1961. m_scenePartsLock.ExitWriteLock();
  1962. }
  1963. copy.IsSelected = createSelected;
  1964. if (rot != Quaternion.Identity)
  1965. copy.UpdateGroupRotationR(rot);
  1966. // required for physics to update it's position
  1967. copy.ResetChildPrimPhysicsPositions();
  1968. copy.CreateScriptInstances(0, false, m_parentScene.DefaultScriptEngine, 1);
  1969. copy.ResumeScripts();
  1970. copy.HasGroupChanged = true;
  1971. copy.ScheduleGroupForFullAnimUpdate();
  1972. return copy;
  1973. }
  1974. }
  1975. else
  1976. {
  1977. m_log.WarnFormat("[SCENE]: Attempted to duplicate nonexistant prim id {0}", GroupID);
  1978. }
  1979. return null;
  1980. }
  1981. #endregion
  1982. }
  1983. }