AttachmentsModule.cs 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.IO;
  31. using System.Text;
  32. using System.Threading;
  33. using System.Xml;
  34. using log4net;
  35. using Mono.Addins;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using OpenMetaverse.Packets;
  39. using OpenSim.Framework;
  40. using OpenSim.Framework.Console;
  41. using OpenSim.Region.Framework;
  42. using OpenSim.Region.Framework.Interfaces;
  43. using OpenSim.Region.Framework.Scenes;
  44. using OpenSim.Region.Framework.Scenes.Serialization;
  45. using OpenSim.Services.Interfaces;
  46. using PermissionMask = OpenSim.Framework.PermissionMask;
  47. namespace OpenSim.Region.CoreModules.Avatar.Attachments
  48. {
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AttachmentsModule")]
  50. public class AttachmentsModule : IAttachmentsModule, INonSharedRegionModule
  51. {
  52. #region INonSharedRegionModule
  53. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. public int DebugLevel { get; set; }
  55. private Scene m_scene;
  56. private IRegionConsole m_regionConsole;
  57. private IInventoryAccessModule m_invAccessModule;
  58. private bool m_wearReplacesAllOption = true;
  59. /// <summary>
  60. /// Are attachments enabled?
  61. /// </summary>
  62. public bool Enabled { get; private set; }
  63. public string Name { get { return "Attachments Module"; } }
  64. public Type ReplaceableInterface { get { return null; } }
  65. public void Initialise(IConfigSource source)
  66. {
  67. IConfig config = source.Configs["Attachments"];
  68. if (config != null)
  69. {
  70. Enabled = config.GetBoolean("Enabled", true);
  71. m_wearReplacesAllOption = config.GetBoolean("WearReplacesAll", true);
  72. }
  73. else
  74. {
  75. Enabled = true;
  76. }
  77. }
  78. public void AddRegion(Scene scene)
  79. {
  80. m_scene = scene;
  81. if (Enabled)
  82. {
  83. // Only register module with scene if it is enabled. All callers check for a null attachments module.
  84. // Ideally, there should be a null attachments module for when this core attachments module has been
  85. // disabled. Registering only when enabled allows for other attachments module implementations.
  86. m_scene.RegisterModuleInterface<IAttachmentsModule>(this);
  87. m_scene.EventManager.OnNewClient += SubscribeToClientEvents;
  88. m_scene.EventManager.OnStartScript += OnScriptStarted;
  89. m_scene.EventManager.OnStopScript += OnScriptStopped;
  90. }
  91. // TODO: Should probably be subscribing to CloseClient too, but this doesn't yet give us IClientAPI
  92. }
  93. public void RemoveRegion(Scene scene)
  94. {
  95. if (!Enabled)
  96. return;
  97. m_scene.UnregisterModuleInterface<IAttachmentsModule>(this);
  98. m_scene.EventManager.OnNewClient -= SubscribeToClientEvents;
  99. m_scene.EventManager.OnStartScript -= OnScriptStarted;
  100. m_scene.EventManager.OnStopScript -= OnScriptStopped;
  101. }
  102. public void RegionLoaded(Scene scene)
  103. {
  104. if (!Enabled)
  105. return;
  106. m_invAccessModule = m_scene.RequestModuleInterface<IInventoryAccessModule>();
  107. if(m_invAccessModule is null)
  108. {
  109. Enabled = false;
  110. m_scene.UnregisterModuleInterface<IAttachmentsModule>(this);
  111. m_scene.EventManager.OnNewClient -= SubscribeToClientEvents;
  112. m_scene.EventManager.OnStartScript -= OnScriptStarted;
  113. m_scene.EventManager.OnStopScript -= OnScriptStopped;
  114. m_log.WarnFormat("[ATTACHMENTS MODULE]: InventoryService unvailable!. Module disabled");
  115. return;
  116. }
  117. m_regionConsole = scene.RequestModuleInterface<IRegionConsole>();
  118. if (m_regionConsole != null)
  119. {
  120. m_regionConsole.AddCommand("AttachModule", false, "set auto_grant_attach_perms", "set auto_grant_attach_perms true|false", "Allow objects owned by the region owner or estate managers to obtain attach permissions without asking the user", HandleSetAutoGrantAttachPerms);
  121. }
  122. scene.AddCommand(
  123. "Debug",
  124. this,
  125. "debug attachments log",
  126. "debug attachments log [0|1]",
  127. "Turn on attachments debug logging",
  128. " <= 0 - turns off debug logging\n"
  129. + " >= 1 - turns on attachment message debug logging",
  130. HandleDebugAttachmentsLog);
  131. scene.AddCommand(
  132. "Debug",
  133. this,
  134. "debug attachments status",
  135. "debug attachments status",
  136. "Show current attachments debug status",
  137. HandleDebugAttachmentsStatus);
  138. // next should work on console root also
  139. MainConsole.Instance.Commands.AddCommand(
  140. "Users", true, "attachments show",
  141. "attachments show [<first-name> <last-name>]",
  142. "Show attachment information for avatars in this simulator.",
  143. "If no name is supplied then information for all avatars is shown.",
  144. HandleShowAttachmentsCommand);
  145. }
  146. public void Close()
  147. {
  148. if (!Enabled)
  149. return;
  150. RemoveRegion(m_scene);
  151. }
  152. private void HandleDebugAttachmentsLog(string module, string[] args)
  153. {
  154. int debugLevel;
  155. if (!(args.Length == 4 && int.TryParse(args[3], out debugLevel)))
  156. {
  157. MainConsole.Instance.Output("Usage: debug attachments log [0|1]");
  158. }
  159. else
  160. {
  161. DebugLevel = debugLevel;
  162. MainConsole.Instance.Output(
  163. "Set attachments debug level to {0} in {1}", DebugLevel, m_scene.Name);
  164. }
  165. }
  166. private void HandleDebugAttachmentsStatus(string module, string[] args)
  167. {
  168. MainConsole.Instance.Output("Settings for {0}", m_scene.Name);
  169. MainConsole.Instance.Output("Debug logging level: {0}", DebugLevel);
  170. }
  171. protected void HandleShowAttachmentsCommand(string module, string[] cmd)
  172. {
  173. if (cmd.Length != 2 && cmd.Length < 4)
  174. {
  175. MainConsole.Instance.Output("Usage: attachments show [<first-name> <last-name>]");
  176. return;
  177. }
  178. SceneManager sm = SceneManager.Instance;
  179. if(sm == null || sm.Scenes.Count == 0)
  180. return;
  181. bool targetNameSupplied = false;
  182. string optionalTargetFirstName = null;
  183. string optionalTargetLastName = null;
  184. if (cmd.Length >= 4)
  185. {
  186. targetNameSupplied = true;
  187. optionalTargetFirstName = cmd[2];
  188. optionalTargetLastName = cmd[3];
  189. }
  190. StringBuilder sb = new StringBuilder();
  191. sm.ForEachSelectedScene(
  192. scene =>
  193. {
  194. if (targetNameSupplied)
  195. {
  196. ScenePresence sp = scene.GetScenePresence(optionalTargetFirstName, optionalTargetLastName);
  197. if (sp != null && !sp.IsChildAgent)
  198. GetAttachmentsReport(sp, sb);
  199. }
  200. else
  201. {
  202. sb.AppendFormat("--- All attachments for region {0}:\n", scene.Name);
  203. scene.ForEachRootScenePresence(sp => GetAttachmentsReport(sp, sb));
  204. }
  205. });
  206. MainConsole.Instance.Output(sb.ToString());
  207. }
  208. private void GetAttachmentsReport(ScenePresence sp, StringBuilder sb)
  209. {
  210. sb.AppendFormat("Attachments for {0}\n", sp.Name);
  211. ConsoleDisplayList ct = new ConsoleDisplayList();
  212. int totalprims = 0;
  213. List<SceneObjectGroup> attachmentObjects = sp.GetAttachments();
  214. for (int i = 0; i < attachmentObjects.Count; ++i)
  215. {
  216. SceneObjectGroup attachmentObject = attachmentObjects[i];
  217. ct.Indent = 2;
  218. ct.AddRow("Attachment Name", attachmentObject.Name);
  219. ct.AddRow("Local ID", attachmentObject.LocalId);
  220. ct.AddRow("Item ID", attachmentObject.UUID);
  221. if(attachmentObject.FromItemID.IsZero())
  222. ct.AddRow("Temporary", "");
  223. else
  224. ct.AddRow("From Item ID", attachmentObject.FromItemID);
  225. ct.AddRow("Attach Point", ((AttachmentPoint)attachmentObject.AttachmentPoint));
  226. ct.AddRow("Prims", attachmentObject.PrimCount);
  227. ct.AddRow("Position", attachmentObject.RootPart.AttachedPos + "\n");
  228. totalprims += attachmentObject.PrimCount;
  229. }
  230. sb.AppendFormat("--Total Attachment prims for {0} : {1}\n\n", sp.Name, totalprims);
  231. ct.AddToStringBuilder(sb);
  232. }
  233. private void SendConsoleOutput(UUID agentID, string text)
  234. {
  235. if (m_regionConsole == null)
  236. return;
  237. m_regionConsole.SendConsoleOutput(agentID, text);
  238. }
  239. private void HandleSetAutoGrantAttachPerms(string module, string[] parms)
  240. {
  241. UUID agentID = new UUID(parms[parms.Length - 1]);
  242. Array.Resize(ref parms, parms.Length - 1);
  243. if (parms.Length != 3)
  244. {
  245. SendConsoleOutput(agentID, "Command parameter error");
  246. return;
  247. }
  248. string val = parms[2];
  249. if (val != "true" && val != "false")
  250. {
  251. SendConsoleOutput(agentID, "Command parameter error");
  252. return;
  253. }
  254. m_scene.StoreExtraSetting("auto_grant_attach_perms", val);
  255. SendConsoleOutput(agentID, String.Format("auto_grant_attach_perms set to {0}", val));
  256. }
  257. /// <summary>
  258. /// Listen for client triggered running state changes so that we can persist the script's object if necessary.
  259. /// </summary>
  260. /// <param name='localID'></param>
  261. /// <param name='itemID'></param>
  262. private void OnScriptStarted(uint localID, UUID itemID)
  263. {
  264. SceneObjectGroup sog = m_scene.GetGroupByPrim(localID);
  265. if (sog != null && sog.IsAttachment)
  266. sog.HasGroupChanged = true;
  267. }
  268. private void OnScriptStopped(uint localID, UUID itemID)
  269. {
  270. SceneObjectGroup sog = m_scene.GetGroupByPrim(localID);
  271. if (sog != null && sog.IsAttachment)
  272. {
  273. // FIXME: This is a convoluted way for working out whether the script state has changed to stop
  274. // because it has been manually stopped or because the stop was called in UpdateDetachedObject() below
  275. // This needs to be handled in a less tangled way.
  276. ScenePresence sp = m_scene.GetScenePresence(sog.AttachedAvatar);
  277. if (sp.ControllingClient.IsActive)
  278. sog.HasGroupChanged = true;
  279. }
  280. }
  281. #endregion
  282. #region IAttachmentsModule
  283. public void CopyAttachments(IScenePresence sp, AgentData ad)
  284. {
  285. lock (sp.AttachmentsSyncLock)
  286. {
  287. // Attachment objects
  288. List<SceneObjectGroup> attachments = sp.GetAttachments();
  289. if (attachments.Count > 0)
  290. {
  291. ad.AttachmentObjects = new List<ISceneObject>(attachments.Count);
  292. ad.AttachmentObjectStates = new List<string>(attachments.Count);
  293. sp.InTransitScriptStates.Clear();
  294. for (int indx = 0; indx < attachments.Count; ++indx)
  295. {
  296. SceneObjectGroup sog = attachments[indx];
  297. // We need to make a copy and pass that copy
  298. // because of transfers withn the same sim
  299. ISceneObject clone = sog.CloneForNewScene();
  300. // Attachment module assumes that GroupPosition holds the offsets...!
  301. ((SceneObjectGroup)clone).RootPart.GroupPosition = sog.RootPart.AttachedPos;
  302. ((SceneObjectGroup)clone).IsAttachment = false;
  303. ad.AttachmentObjects.Add(clone);
  304. string state = sog.GetStateSnapshot();
  305. ad.AttachmentObjectStates.Add(state);
  306. sp.InTransitScriptStates.Add(state);
  307. // Scripts of the originals will be removed when the Agent is successfully removed.
  308. // sog.RemoveScriptInstances(true);
  309. }
  310. }
  311. }
  312. }
  313. public void CopyAttachments(AgentData ad, IScenePresence isp)
  314. {
  315. ScenePresence sp = isp as ScenePresence;
  316. if (ad.AttachmentObjects != null && ad.AttachmentObjects.Count > 0)
  317. {
  318. lock (isp.AttachmentsSyncLock)
  319. {
  320. if(sp.IsDeleted)
  321. return;
  322. DeleteAttachmentsFromScene(isp, true); // delete
  323. }
  324. List<SceneObjectGroup> attachments = new List<SceneObjectGroup>(ad.AttachmentObjects.Count);
  325. int i = 0;
  326. for (int indx = 0; indx < ad.AttachmentObjects.Count; ++indx)
  327. {
  328. SceneObjectGroup sog = ad.AttachmentObjects[indx] as SceneObjectGroup;
  329. if(sog != null && sog.OwnerID.Equals(sp.UUID))
  330. {
  331. sog.LocalId = 0;
  332. sog.RootPart.ClearUpdateSchedule();
  333. sog.SetState(ad.AttachmentObjectStates[i++], m_scene);
  334. attachments.Add(sog);
  335. }
  336. }
  337. ad.AttachmentObjects = null;
  338. ad.AttachmentObjectStates = null;
  339. if (attachments.Count > 0)
  340. m_scene.IncomingAttechments(sp, attachments);
  341. else
  342. sp.GotAttachmentsData = true;
  343. }
  344. else
  345. sp.GotAttachmentsData = true;
  346. }
  347. public void RezAttachments(IScenePresence sp)
  348. {
  349. if (!Enabled)
  350. return;
  351. if (sp.Appearance == null)
  352. {
  353. m_log.WarnFormat("[ATTACHMENTS MODULE]: Appearance has not been initialized for agent {0}", sp.UUID);
  354. return;
  355. }
  356. if (sp.GetAttachmentsCount() > 0)
  357. {
  358. if (DebugLevel > 0)
  359. m_log.DebugFormat(
  360. "[ATTACHMENTS MODULE]: Not doing simulator-side attachment rez for {0} in {1} as their viewer has already rezzed attachments",
  361. m_scene.Name, sp.Name);
  362. return;
  363. }
  364. if (DebugLevel > 0)
  365. m_log.DebugFormat("[ATTACHMENTS MODULE]: Rezzing any attachments for {0} from simulator-side", sp.Name);
  366. XmlDocument doc = new XmlDocument();
  367. string stateData = String.Empty;
  368. IAttachmentsService attServ = m_scene.RequestModuleInterface<IAttachmentsService>();
  369. if (attServ != null)
  370. {
  371. m_log.DebugFormat("[ATTACHMENT]: Loading attachment data from attachment service");
  372. stateData = attServ.Get(sp.UUID.ToString());
  373. if (stateData != String.Empty)
  374. {
  375. try
  376. {
  377. doc.LoadXml(stateData);
  378. }
  379. catch { }
  380. }
  381. }
  382. Dictionary<UUID, string> itemData = new Dictionary<UUID, string>();
  383. XmlNodeList nodes = doc.GetElementsByTagName("Attachment");
  384. if (nodes.Count > 0)
  385. {
  386. foreach (XmlNode n in nodes)
  387. {
  388. XmlElement elem = (XmlElement)n;
  389. string itemID = elem.GetAttribute("ItemID");
  390. string xml = elem.InnerXml;
  391. itemData[new UUID(itemID)] = xml;
  392. }
  393. }
  394. List<AvatarAttachment> attachments = sp.Appearance.GetAttachments();
  395. // Let's get all items at once, so they get cached
  396. UUID[] items = new UUID[attachments.Count];
  397. for (int i = 0; i < attachments.Count; ++i)
  398. items[i] = attachments[i].ItemID;
  399. m_scene.InventoryService.GetMultipleItems(sp.UUID, items);
  400. for (int indx = 0; indx < attachments.Count; ++indx)
  401. {
  402. AvatarAttachment attach = attachments[indx];
  403. uint attachmentPt = (uint)attach.AttachPoint;
  404. // m_log.DebugFormat(
  405. // "[ATTACHMENTS MODULE]: Doing initial rez of attachment with itemID {0}, assetID {1}, point {2} for {3} in {4}",
  406. // attach.ItemID, attach.AssetID, p, sp.Name, m_scene.RegionInfo.RegionName);
  407. try
  408. {
  409. string xmlData;
  410. XmlDocument d = null;
  411. if (itemData.TryGetValue(attach.ItemID, out xmlData))
  412. {
  413. d = new XmlDocument();
  414. d.LoadXml(xmlData);
  415. m_log.InfoFormat("[ATTACHMENT]: Found saved state for item {0}, loading it", attach.ItemID);
  416. }
  417. // If we're an NPC then skip all the item checks and manipulations since we don't have an
  418. // inventory right now.
  419. RezSingleAttachmentFromInventoryInternal(
  420. sp, sp.PresenceType == PresenceType.Npc ? UUID.Zero : attach.ItemID, attach.AssetID, attachmentPt, true, d);
  421. }
  422. catch (Exception e)
  423. {
  424. UUID agentId = (sp.ControllingClient == null) ? default(UUID) : sp.ControllingClient.AgentId;
  425. m_log.ErrorFormat("[ATTACHMENTS MODULE]: Unable to rez attachment with itemID {0}, assetID {1}, point {2} for {3}: {4}\n{5}",
  426. attach.ItemID, attach.AssetID, attachmentPt, agentId, e.Message, e.StackTrace);
  427. }
  428. }
  429. }
  430. public void DeRezAttachments(IScenePresence sp)
  431. {
  432. if (!Enabled)
  433. return;
  434. List<SceneObjectGroup> attachments = sp.GetAttachments();
  435. if (DebugLevel > 0)
  436. m_log.DebugFormat(
  437. "[ATTACHMENTS MODULE]: Saving for {0} attachments for {1} in {2}",
  438. attachments.Count, sp.Name, m_scene.Name);
  439. if (attachments.Count <= 0)
  440. return;
  441. if (sp.PresenceType != PresenceType.Npc)
  442. {
  443. lock (sp.AttachmentsSyncLock)
  444. {
  445. for (int i = 0; i < attachments.Count; ++i)
  446. {
  447. SceneObjectGroup sog = attachments[i];
  448. UpdateDetachedObject(sp, sog, PrepareScriptInstanceForSave(sog, false));
  449. }
  450. sp.ClearAttachments();
  451. }
  452. }
  453. else
  454. {
  455. lock (sp.AttachmentsSyncLock)
  456. {
  457. for (int i = 0; i < attachments.Count; ++i)
  458. UpdateDetachedObject(sp, attachments[i], String.Empty);
  459. sp.ClearAttachments();
  460. }
  461. }
  462. }
  463. public void DeleteAttachmentsFromScene(IScenePresence sp, bool silent)
  464. {
  465. if (!Enabled)
  466. return;
  467. if (DebugLevel > 0)
  468. m_log.DebugFormat(
  469. "[ATTACHMENTS MODULE]: Deleting attachments from scene {0} for {1}, silent = {2}",
  470. m_scene.RegionInfo.RegionName, sp.Name, silent);
  471. List<SceneObjectGroup> attachments = sp.GetAttachments();
  472. for(int i = 0; i < attachments.Count; ++i)
  473. {
  474. SceneObjectGroup sog = attachments[i];
  475. sog.Scene.DeleteSceneObject(sog, silent);
  476. }
  477. sp.ClearAttachments();
  478. }
  479. public bool AttachObject(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool silent,
  480. bool addToInventory, bool append)
  481. {
  482. if (!Enabled)
  483. return false;
  484. return AttachObjectInternal(sp, group, attachmentPt, silent, addToInventory, false, append);
  485. }
  486. /// <summary>
  487. /// Internal method which actually does all the work for attaching an object.
  488. /// </summary>
  489. /// <returns>The object attached.</returns>
  490. /// <param name='sp'></param>
  491. /// <param name='group'>The object to attach.</param>
  492. /// <param name='attachmentPt'></param>
  493. /// <param name='silent'></param>
  494. /// <param name='addToInventory'>If true then add object to user inventory.</param>
  495. /// <param name='resumeScripts'>If true then scripts are resumed on the attached object.</param>
  496. private bool AttachObjectInternal(IScenePresence sp, SceneObjectGroup group, uint attachmentPt,
  497. bool silent, bool addToInventory, bool resumeScripts, bool append)
  498. {
  499. // m_log.DebugFormat(
  500. // "[ATTACHMENTS MODULE]: Attaching object {0} {1} to {2} point {3} from ground (silent = {4})",
  501. // group.Name, group.LocalId, sp.Name, attachmentPt, silent);
  502. if (group.GetSittingAvatarsCount() != 0)
  503. {
  504. if (DebugLevel > 0)
  505. m_log.WarnFormat(
  506. "[ATTACHMENTS MODULE]: Ignoring request to attach {0} {1} to {2} on {3} since {4} avatars are still sitting on it",
  507. group.Name, group.LocalId, sp.Name, attachmentPt, group.GetSittingAvatarsCount());
  508. return false;
  509. }
  510. Vector3 attachPos = group.AbsolutePosition;
  511. // TODO: this short circuits multiple attachments functionality in LL viewer 2.1+ and should
  512. // be removed when that functionality is implemented in opensim
  513. attachmentPt &= 0x7f;
  514. // If the attachment point isn't the same as the one previously used
  515. // set it's offset position = 0 so that it appears on the attachment point
  516. // and not in a weird location somewhere unknown.
  517. if (attachmentPt != (uint)AttachmentPoint.Default && attachmentPt != group.AttachmentPoint)
  518. {
  519. attachPos = Vector3.Zero;
  520. }
  521. // if the attachment point is the same as previous, make sure we get the saved
  522. // position info.
  523. if (attachmentPt != 0 && attachmentPt == group.RootPart.Shape.LastAttachPoint)
  524. {
  525. attachPos = group.RootPart.AttachedPos;
  526. }
  527. // AttachmentPt 0 means the client chose to 'wear' the attachment.
  528. if (attachmentPt == (uint)AttachmentPoint.Default)
  529. {
  530. // Check object for stored attachment point
  531. attachmentPt = group.AttachmentPoint;
  532. }
  533. // if we didn't find an attach point, look for where it was last attached
  534. if (attachmentPt == 0)
  535. {
  536. attachmentPt = (uint)group.RootPart.Shape.LastAttachPoint;
  537. attachPos = group.RootPart.AttachedPos;
  538. }
  539. // if we still didn't find a suitable attachment point.......
  540. if (attachmentPt == 0)
  541. {
  542. // Stick it on left hand with Zero Offset from the attachment point.
  543. attachmentPt = (uint)AttachmentPoint.LeftHand;
  544. attachPos = Vector3.Zero;
  545. }
  546. if(attachmentPt > (uint)AttachmentPoint.LastValid)
  547. {
  548. m_log.WarnFormat("[ATTACHMENTS MODULE]: Invalid attachment point {0} SP {1}", attachmentPt, sp.Name);
  549. return false;
  550. }
  551. List<SceneObjectGroup> attachments = sp.GetAttachments();
  552. List<SceneObjectGroup> toRemove = new List<SceneObjectGroup>(attachments.Count);
  553. bool doRemCheck = !append;
  554. for(int i = 0; i < attachments.Count; ++i)
  555. {
  556. SceneObjectGroup sog = attachments[i];
  557. // duplications ?
  558. if (group.UUID == sog.UUID)
  559. {
  560. // if (DebugLevel > 0)
  561. // m_log.WarnFormat(
  562. // "[ATTACHMENTS MODULE]: Ignoring request to attach {0} {1} to {2} on {3} since it's already attached",
  563. // group.Name, group.LocalId, sp.Name, attachmentPt);
  564. return false;
  565. }
  566. if(doRemCheck)
  567. {
  568. if(sog.AttachmentPoint == attachmentPt)
  569. {
  570. toRemove.Add(sog);
  571. if(!m_wearReplacesAllOption)
  572. doRemCheck = false;
  573. }
  574. }
  575. }
  576. if(attachments.Count - toRemove.Count >= Constants.MaxAgentAttachments)
  577. {
  578. m_log.WarnFormat("[ATTACHMENTS MODULE]: Max attachments exceded {0}",sp.Name);
  579. return false;
  580. }
  581. group.DetachFromBackup();
  582. group.AttachmentPoint = attachmentPt;
  583. group.RootPart.AttachedPos = attachPos;
  584. // If we're not appending, remove the rest as well
  585. lock (sp.AttachmentsSyncLock)
  586. {
  587. if (toRemove.Count > 0)
  588. {
  589. for (int i = 0; i< toRemove.Count; ++i)
  590. {
  591. SceneObjectGroup g = toRemove[i];
  592. if (!g.FromItemID.IsZero())
  593. DetachSingleAttachmentToInv(sp, g);
  594. }
  595. }
  596. if (addToInventory && sp.PresenceType != PresenceType.Npc)
  597. UpdateUserInventoryWithAttachment(sp, group, attachmentPt, append);
  598. AttachToAgent(sp, group, attachmentPt, attachPos, silent);
  599. if (resumeScripts)
  600. {
  601. // Fire after attach, so we don't get messy perms dialogs
  602. // 4 == AttachedRez
  603. group.CreateScriptInstances(0, true, m_scene.DefaultScriptEngine, 4);
  604. group.ResumeScripts();
  605. }
  606. else
  607. // Do this last so that event listeners have access to all the effects of the attachment
  608. // this can't be done when creating scripts:
  609. // scripts do internal enqueue of attach event
  610. // and not all scripts are loaded at this point
  611. m_scene.EventManager.TriggerOnAttach(group.LocalId, group.FromItemID, sp.UUID);
  612. }
  613. return true;
  614. }
  615. private void UpdateUserInventoryWithAttachment(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool append)
  616. {
  617. // Add the new attachment to inventory if we don't already have it.
  618. UUID newAttachmentItemID = group.FromItemID;
  619. if (newAttachmentItemID.IsZero())
  620. newAttachmentItemID = AddSceneObjectAsNewAttachmentInInv(sp, group).ID;
  621. ShowAttachInUserInventory(sp, attachmentPt, newAttachmentItemID, group, append);
  622. }
  623. public ISceneEntity RezSingleAttachmentFromInventory(IScenePresence sp, UUID itemID, uint AttachmentPt)
  624. {
  625. return RezSingleAttachmentFromInventory(sp, itemID, AttachmentPt, null);
  626. }
  627. public ISceneEntity RezSingleAttachmentFromInventory(IScenePresence sp, UUID itemID, uint AttachmentPt, XmlDocument doc)
  628. {
  629. if (!Enabled)
  630. return null;
  631. if (DebugLevel > 0)
  632. m_log.DebugFormat(
  633. "[ATTACHMENTS MODULE]: RezSingleAttachmentFromInventory to point {0} from item {1} for {2} in {3}",
  634. (AttachmentPoint)AttachmentPt, itemID, sp.Name, m_scene.Name);
  635. // We check the attachments in the avatar appearance here rather than the objects attached to the
  636. // ScenePresence itself so that we can ignore calls by viewer 2/3 to attach objects on startup. We are
  637. // already doing this in ScenePresence.MakeRootAgent(). Simulator-side attaching needs to be done
  638. // because pre-outfit folder viewers (most version 1 viewers) require it.
  639. bool alreadyOn = false;
  640. List<AvatarAttachment> existingAttachments = sp.Appearance.GetAttachments();
  641. foreach (AvatarAttachment existingAttachment in existingAttachments)
  642. {
  643. if (existingAttachment.ItemID == itemID)
  644. {
  645. alreadyOn = true;
  646. break;
  647. }
  648. }
  649. if (alreadyOn)
  650. {
  651. if (DebugLevel > 0)
  652. m_log.DebugFormat(
  653. "[ATTACHMENTS MODULE]: Ignoring request by {0} to wear item {1} at {2} since it is already worn",
  654. sp.Name, itemID, AttachmentPt);
  655. return null;
  656. }
  657. bool append = (AttachmentPt & 0x80) != 0;
  658. AttachmentPt &= 0x7f;
  659. return RezSingleAttachmentFromInventoryInternal(sp, itemID, UUID.Zero, AttachmentPt, append, doc);
  660. }
  661. public void RezMultipleAttachmentsFromInventory(IScenePresence sp, List<KeyValuePair<UUID, uint>> rezlist)
  662. {
  663. if (!Enabled)
  664. return;
  665. if (DebugLevel > 0)
  666. m_log.DebugFormat(
  667. "[ATTACHMENTS MODULE]: Rezzing {0} attachments from inventory for {1} in {2}",
  668. rezlist.Count, sp.Name, m_scene.Name);
  669. foreach (KeyValuePair<UUID, uint> rez in rezlist)
  670. {
  671. RezSingleAttachmentFromInventory(sp, rez.Key, rez.Value);
  672. }
  673. }
  674. public void DetachSingleAttachmentToGround(IScenePresence sp, uint soLocalId)
  675. {
  676. Vector3 pos = new Vector3(2.5f, 0f, 0f);
  677. pos *= ((ScenePresence)sp).Rotation;
  678. pos += sp.AbsolutePosition;
  679. DetachSingleAttachmentToGround(sp, soLocalId, pos, Quaternion.Identity);
  680. }
  681. public void DetachSingleAttachmentToGround(IScenePresence sp, uint soLocalId, Vector3 absolutePos, Quaternion absoluteRot)
  682. {
  683. if (!Enabled)
  684. return;
  685. if (DebugLevel > 0)
  686. m_log.DebugFormat(
  687. "[ATTACHMENTS MODULE]: DetachSingleAttachmentToGround() for {0}, object {1}",
  688. sp.UUID, soLocalId);
  689. SceneObjectGroup so = m_scene.GetGroupByPrim(soLocalId);
  690. if (so == null)
  691. return;
  692. if (so.AttachedAvatar != sp.UUID)
  693. return;
  694. UUID inventoryID = so.FromItemID;
  695. // As per Linden spec, drop is disabled for temp attachs
  696. if (inventoryID.IsZero())
  697. return;
  698. if (DebugLevel > 0)
  699. m_log.DebugFormat(
  700. "[ATTACHMENTS MODULE]: In DetachSingleAttachmentToGround(), object is {0} {1}, associated item is {2}",
  701. so.Name, so.LocalId, inventoryID);
  702. lock (sp.AttachmentsSyncLock)
  703. {
  704. if (!m_scene.Permissions.CanRezObject(
  705. so.PrimCount, sp.UUID, sp.AbsolutePosition))
  706. return;
  707. bool changed = false;
  708. if (!inventoryID.IsZero())
  709. changed = sp.Appearance.DetachAttachment(inventoryID);
  710. if (changed && m_scene.AvatarFactory != null)
  711. m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
  712. so.RootPart.Shape.LastAttachPoint = (byte)so.AttachmentPoint;
  713. sp.RemoveAttachment(so);
  714. so.FromItemID = UUID.Zero;
  715. so.AttachedAvatar = UUID.Zero;
  716. so.ClearPartAttachmentData();
  717. SceneObjectPart rootPart = so.RootPart;
  718. rootPart.SetParentLocalId(0);
  719. so.AbsolutePosition = absolutePos;
  720. if (absoluteRot != Quaternion.Identity)
  721. {
  722. so.UpdateGroupRotationR(absoluteRot);
  723. }
  724. rootPart.RemFlag(PrimFlags.TemporaryOnRez);
  725. so.ApplyPhysics();
  726. rootPart.Rezzed = DateTime.Now;
  727. so.AttachToBackup();
  728. m_scene.EventManager.TriggerParcelPrimCountTainted();
  729. rootPart.ClearUndoState();
  730. List<UUID> uuids = new List<UUID>();
  731. uuids.Add(inventoryID);
  732. m_scene.InventoryService.DeleteItems(sp.UUID, uuids);
  733. sp.ControllingClient.SendRemoveInventoryItem(inventoryID);
  734. }
  735. m_scene.EventManager.TriggerOnAttach(so.LocalId, so.UUID, UUID.Zero);
  736. // Attach (NULL) stops scripts. We don't want that. Resume them.
  737. so.RemoveScriptsPermissions(4 | 2048); // take controls and camera control
  738. so.ResumeScripts();
  739. so.HasGroupChanged = true;
  740. so.RootPart.ScheduleFullUpdate();
  741. so.ScheduleGroupForTerseUpdate();
  742. }
  743. public void DetachSingleAttachmentToInv(IScenePresence sp, SceneObjectGroup so)
  744. {
  745. if (so.AttachedAvatar != sp.UUID)
  746. {
  747. m_log.WarnFormat(
  748. "[ATTACHMENTS MODULE]: Tried to detach object {0} from {1} {2} but attached avatar id was {3} in {4}",
  749. so.Name, sp.Name, sp.UUID, so.AttachedAvatar, m_scene.RegionInfo.RegionName);
  750. return;
  751. }
  752. so.RemoveScriptsPermissions(4 | 2048); // take controls and camera control
  753. // If this didn't come from inventory, it also shouldn't go there
  754. // on detach. It's likely a temp attachment.
  755. if (so.FromItemID.IsZero())
  756. {
  757. PrepareScriptInstanceForSave(so, true);
  758. lock (sp.AttachmentsSyncLock)
  759. {
  760. bool changed = sp.Appearance.DetachAttachment(so.FromItemID);
  761. if (changed && m_scene.AvatarFactory != null)
  762. m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
  763. sp.RemoveAttachment(so);
  764. }
  765. m_scene.DeleteSceneObject(so, false, false);
  766. so.RemoveScriptInstances(true);
  767. so.Dispose();
  768. return;
  769. }
  770. if (DebugLevel > 0)
  771. m_log.DebugFormat(
  772. "[ATTACHMENTS MODULE]: Detaching object {0} {1} (FromItemID {2}) for {3} in {4}",
  773. so.Name, so.LocalId, so.FromItemID, sp.Name, m_scene.Name);
  774. // Scripts MUST be snapshotted before the object is
  775. // removed from the scene because doing otherwise will
  776. // clobber the run flag
  777. // This must be done outside the sp.AttachmentSyncLock so that there is no risk of a deadlock from
  778. // scripts performing attachment operations at the same time. Getting object states stops the scripts.
  779. string scriptedState = PrepareScriptInstanceForSave(so, true);
  780. lock (sp.AttachmentsSyncLock)
  781. {
  782. // Save avatar attachment information
  783. // m_log.Debug("[ATTACHMENTS MODULE]: Detaching from UserID: " + sp.UUID + ", ItemID: " + itemID);
  784. bool changed = sp.Appearance.DetachAttachment(so.FromItemID);
  785. if (changed && m_scene.AvatarFactory != null)
  786. m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
  787. sp.RemoveAttachment(so);
  788. UpdateDetachedObject(sp, so, scriptedState);
  789. }
  790. }
  791. public void UpdateAttachmentPosition(SceneObjectGroup sog, Vector3 pos)
  792. {
  793. if (!Enabled)
  794. return;
  795. sog.UpdateGroupPosition(pos);
  796. sog.HasGroupChanged = true;
  797. }
  798. #endregion
  799. #region AttachmentModule private methods
  800. // This is public but is not part of the IAttachmentsModule interface.
  801. // RegionCombiner module needs to poke at it to deliver client events.
  802. // This breaks the encapsulation of the module and should get fixed somehow.
  803. public void SubscribeToClientEvents(IClientAPI client)
  804. {
  805. client.OnRezSingleAttachmentFromInv += Client_OnRezSingleAttachmentFromInv;
  806. client.OnRezMultipleAttachmentsFromInv += Client_OnRezMultipleAttachmentsFromInv;
  807. client.OnObjectAttach += Client_OnObjectAttach;
  808. client.OnObjectDetach += Client_OnObjectDetach;
  809. client.OnDetachAttachmentIntoInv += Client_OnDetachAttachmentIntoInv;
  810. client.OnObjectDrop += Client_OnObjectDrop;
  811. }
  812. // This is public but is not part of the IAttachmentsModule interface.
  813. // RegionCombiner module needs to poke at it to deliver client events.
  814. // This breaks the encapsulation of the module and should get fixed somehow.
  815. public void UnsubscribeFromClientEvents(IClientAPI client)
  816. {
  817. client.OnRezSingleAttachmentFromInv -= Client_OnRezSingleAttachmentFromInv;
  818. client.OnRezMultipleAttachmentsFromInv -= Client_OnRezMultipleAttachmentsFromInv;
  819. client.OnObjectAttach -= Client_OnObjectAttach;
  820. client.OnObjectDetach -= Client_OnObjectDetach;
  821. client.OnDetachAttachmentIntoInv -= Client_OnDetachAttachmentIntoInv;
  822. client.OnObjectDrop -= Client_OnObjectDrop;
  823. }
  824. /// <summary>
  825. /// Update the attachment asset for the new sog details if they have changed.
  826. /// </summary>
  827. /// <remarks>
  828. /// This is essential for preserving attachment attributes such as permission. Unlike normal scene objects,
  829. /// these details are not stored on the region.
  830. /// </remarks>
  831. /// <param name="sp"></param>
  832. /// <param name="grp"></param>
  833. /// <param name="saveAllScripted"></param>
  834. private void UpdateKnownItem(IScenePresence sp, SceneObjectGroup grp, string scriptedState)
  835. {
  836. if (!grp.HasGroupChanged)
  837. {
  838. if (DebugLevel > 0)
  839. {
  840. m_log.DebugFormat(
  841. "[ATTACHMENTS MODULE]: Don't need to update asset for unchanged attachment {0}, attachpoint {1}",
  842. grp.UUID, grp.AttachmentPoint);
  843. }
  844. return;
  845. }
  846. grp.HasGroupChanged = false;
  847. if (m_invAccessModule is null)
  848. return;
  849. if (grp.FromItemID.IsZero())
  850. {
  851. // We can't save temp attachments
  852. return;
  853. }
  854. if(sp.IsNPC)
  855. return;
  856. m_log.DebugFormat(
  857. "[ATTACHMENTS MODULE]: Updating asset for attachment {0}, attachpoint {1}",
  858. grp.UUID, grp.AttachmentPoint);
  859. InventoryItemBase item = m_scene.InventoryService.GetItem(sp.UUID, grp.FromItemID);
  860. if (item != null)
  861. {
  862. if (item.Owner.NotEqual(sp.UUID))
  863. {
  864. m_log.DebugFormat("[ATTACHMENTS MODULE]: Updating asset for attachment owner mismach: agent {0}, owner {1}",
  865. sp.UUID, item.Owner);
  866. return;
  867. }
  868. string sceneObjectXml = SceneObjectSerializer.ToOriginalXmlFormat(grp, scriptedState);
  869. // attach is rez, need to update permissions
  870. item.Flags &= ~(uint)(InventoryItemFlags.ObjectSlamPerm | InventoryItemFlags.ObjectOverwriteBase |
  871. InventoryItemFlags.ObjectOverwriteOwner | InventoryItemFlags.ObjectOverwriteGroup |
  872. InventoryItemFlags.ObjectOverwriteEveryone | InventoryItemFlags.ObjectOverwriteNextOwner);
  873. uint permsBase = (uint)(PermissionMask.Copy | PermissionMask.Transfer |
  874. PermissionMask.Modify | PermissionMask.Move |
  875. PermissionMask.Export | PermissionMask.FoldedMask);
  876. permsBase &= grp.CurrentAndFoldedNextPermissions();
  877. permsBase |= (uint)PermissionMask.Move;
  878. item.BasePermissions = permsBase;
  879. item.CurrentPermissions = permsBase;
  880. item.NextPermissions = permsBase & grp.RootPart.NextOwnerMask | (uint)PermissionMask.Move;
  881. item.EveryOnePermissions = permsBase & grp.RootPart.EveryoneMask;
  882. item.GroupPermissions = permsBase & grp.RootPart.GroupMask;
  883. item.CurrentPermissions &=
  884. ((uint)PermissionMask.Copy |
  885. (uint)PermissionMask.Transfer |
  886. (uint)PermissionMask.Modify |
  887. (uint)PermissionMask.Move |
  888. (uint)PermissionMask.Export |
  889. (uint)PermissionMask.FoldedMask); // Preserve folded permissions ??
  890. string name = grp.RootPart.Name;
  891. string desc = grp.RootPart.Description;
  892. AssetBase asset = m_scene.CreateAsset(
  893. name, desc,
  894. (sbyte)AssetType.Object,
  895. Utils.StringToBytes(sceneObjectXml),
  896. sp.UUID);
  897. item.Name = name;
  898. item.Description = desc;
  899. item.AssetID = asset.FullID;
  900. item.AssetType = (int)AssetType.Object;
  901. item.InvType = (int)InventoryType.Object;
  902. if (!m_invAccessModule.UpdateInventoryItemAsset(sp.UUID, item, asset))
  903. return;
  904. if (sp.ControllingClient != null)
  905. sp.ControllingClient.SendInventoryItemCreateUpdate(item, 0);
  906. }
  907. }
  908. /// <summary>
  909. /// Attach this scene object to the given avatar.
  910. /// </summary>
  911. /// <remarks>
  912. /// This isn't publicly available since attachments should always perform the corresponding inventory
  913. /// operation (to show the attach in user inventory and update the asset with positional information).
  914. /// </remarks>
  915. /// <param name="sp"></param>
  916. /// <param name="so"></param>
  917. /// <param name="attachmentpoint"></param>
  918. /// <param name="attachOffset"></param>
  919. /// <param name="silent"></param>
  920. private void AttachToAgent(
  921. IScenePresence sp, SceneObjectGroup so, uint attachmentpoint, Vector3 attachOffset, bool silent)
  922. {
  923. if (DebugLevel > 0)
  924. m_log.DebugFormat(
  925. "[ATTACHMENTS MODULE]: Adding attachment {0} to avatar {1} at pt {2} pos {3} {4} in {5}",
  926. so.Name, sp.Name, attachmentpoint, attachOffset, so.RootPart.AttachedPos, m_scene.Name);
  927. // Remove from database and parcel prim count
  928. m_scene.DeleteFromStorage(so.UUID);
  929. m_scene.EventManager.TriggerParcelPrimCountTainted();
  930. foreach (SceneObjectPart part in so.Parts)
  931. {
  932. // if (part.KeyframeMotion != null)
  933. // part.KeyframeMotion.Suspend();
  934. if (part.PhysActor != null)
  935. {
  936. part.RemoveFromPhysics();
  937. }
  938. }
  939. so.RootPart.SetParentLocalId(sp.LocalId);
  940. so.AttachedAvatar = sp.UUID;
  941. so.AttachmentPoint = attachmentpoint;
  942. so.RootPart.AttachedPos = attachOffset;
  943. so.RootPart.GroupPosition = attachOffset; // can not set absolutepos
  944. so.IsAttachment = true;
  945. sp.AddAttachment(so);
  946. if (!silent)
  947. {
  948. if (so.HasPrivateAttachmentPoint)
  949. {
  950. if (DebugLevel > 0)
  951. m_log.DebugFormat(
  952. "[ATTACHMENTS MODULE]: Killing private HUD {0} for avatars other than {1} at attachment point {2}",
  953. so.Name, sp.Name, so.AttachmentPoint);
  954. // As this scene object can now only be seen by the attaching avatar, tell everybody else in the
  955. // scene that it's no longer in their awareness.
  956. m_scene.ForEachClient(
  957. client =>
  958. { if (client.IsActive && client.AgentId != so.AttachedAvatar)
  959. client.SendKillObject(new List<uint>() { so.LocalId });
  960. });
  961. }
  962. // Fudge below is an extremely unhelpful comment. It's probably here so that the scheduled full update
  963. // will succeed, as that will not update if an attachment is selected.
  964. so.IsSelected = false; // fudge....
  965. so.ScheduleGroupForFullAnimUpdate();
  966. }
  967. // In case it is later dropped again, don't let
  968. // it get cleaned up
  969. so.RootPart.RemFlag(PrimFlags.TemporaryOnRez);
  970. }
  971. /// <summary>
  972. /// Add a scene object as a new attachment in the user inventory.
  973. /// </summary>
  974. /// <param name="remoteClient"></param>
  975. /// <param name="grp"></param>
  976. /// <returns>The user inventory item created that holds the attachment.</returns>
  977. private InventoryItemBase AddSceneObjectAsNewAttachmentInInv(IScenePresence sp, SceneObjectGroup grp)
  978. {
  979. if (m_invAccessModule == null)
  980. return null;
  981. if (DebugLevel > 0)
  982. m_log.DebugFormat(
  983. "[ATTACHMENTS MODULE]: Called AddSceneObjectAsAttachment for object {0} {1} for {2}",
  984. grp.Name, grp.LocalId, sp.Name);
  985. InventoryItemBase newItem
  986. = m_invAccessModule.CopyToInventory(
  987. DeRezAction.TakeCopy,
  988. m_scene.InventoryService.GetFolderForType(sp.UUID, FolderType.Object).ID,
  989. new List<SceneObjectGroup> { grp },
  990. sp.ControllingClient, true)[0];
  991. // sets itemID so client can show item as 'attached' in inventory
  992. grp.FromItemID = newItem.ID;
  993. return newItem;
  994. }
  995. /// <summary>
  996. /// Prepares the script instance for save.
  997. /// </summary>
  998. /// <remarks>
  999. /// This involves triggering the detach event and getting the script state (which also stops the script)
  1000. /// This MUST be done outside sp.AttachmentsSyncLock, since otherwise there is a chance of deadlock if a
  1001. /// running script is performing attachment operations.
  1002. /// </remarks>
  1003. /// <returns>
  1004. /// The script state ready for persistence.
  1005. /// </returns>
  1006. /// <param name='grp'>
  1007. /// </param>
  1008. /// <param name='fireDetachEvent'>
  1009. /// If true, then fire the script event before we save its state.
  1010. /// </param>
  1011. private string PrepareScriptInstanceForSave(SceneObjectGroup grp, bool fireDetachEvent)
  1012. {
  1013. if (fireDetachEvent)
  1014. {
  1015. m_scene.EventManager.TriggerOnAttach(grp.LocalId, grp.FromItemID, UUID.Zero);
  1016. // Allow detach event time to do some work before stopping the script
  1017. Thread.Sleep(30);
  1018. }
  1019. using (StringWriter sw = new StringWriter())
  1020. {
  1021. using (XmlTextWriter writer = new XmlTextWriter(sw))
  1022. {
  1023. grp.SaveScriptedState(writer);
  1024. }
  1025. return sw.ToString();
  1026. }
  1027. }
  1028. private void UpdateDetachedObject(IScenePresence sp, SceneObjectGroup so, string scriptedState)
  1029. {
  1030. // Don't save attachments for HG visitors, it
  1031. // messes up their inventory. When a HG visitor logs
  1032. // out on a foreign grid, their attachments will be
  1033. // reloaded in the state they were in when they left
  1034. // the home grid. This is best anyway as the visited
  1035. // grid may use an incompatible script engine.
  1036. bool saveChanged
  1037. = sp.PresenceType != PresenceType.Npc
  1038. && (m_scene.UserManagementModule == null || m_scene.UserManagementModule.IsLocalGridUser(sp.UUID));
  1039. // Remove the object from the scene so no more updates
  1040. // are sent. Doing this before the below changes will ensure
  1041. // updates can't cause "HUD artefacts"
  1042. m_scene.DeleteSceneObject(so, false, false);
  1043. // Prepare sog for storage
  1044. so.AttachedAvatar = UUID.Zero;
  1045. so.RootPart.SetParentLocalId(0);
  1046. so.IsAttachment = false;
  1047. if (saveChanged)
  1048. {
  1049. // We cannot use AbsolutePosition here because that would
  1050. // attempt to cross the prim as it is detached
  1051. so.ForEachPart(x => { x.GroupPosition = so.RootPart.AttachedPos; });
  1052. UpdateKnownItem(sp, so, scriptedState);
  1053. }
  1054. // Now, remove the scripts
  1055. so.RemoveScriptInstances(true);
  1056. so.Dispose();
  1057. }
  1058. protected SceneObjectGroup RezSingleAttachmentFromInventoryInternal(
  1059. IScenePresence sp, UUID itemID, UUID assetID, uint attachmentPt, bool append, XmlDocument doc)
  1060. {
  1061. if (m_invAccessModule == null)
  1062. return null;
  1063. SceneObjectGroup objatt;
  1064. UUID rezGroupID;
  1065. // This will fail if the user aborts login. sp will exist
  1066. // but ControllintClient will be null.
  1067. try
  1068. {
  1069. rezGroupID = sp.ControllingClient.ActiveGroupId;
  1070. }
  1071. catch
  1072. {
  1073. return null;
  1074. }
  1075. bool ItemIDNotZero = itemID.IsNotZero();
  1076. if (ItemIDNotZero)
  1077. objatt = m_invAccessModule.RezObject(sp.ControllingClient,
  1078. itemID, rezGroupID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
  1079. false, false, sp.UUID, true);
  1080. else
  1081. objatt = m_invAccessModule.RezObject(sp.ControllingClient,
  1082. null, rezGroupID, assetID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
  1083. false, false, sp.UUID, true);
  1084. if (objatt == null)
  1085. {
  1086. if(ItemIDNotZero)
  1087. {
  1088. m_log.WarnFormat("[ATTACHMENTS MODULE]: did not attach item {0} to avatar {1} at point {2}",
  1089. itemID, sp.Name, attachmentPt);
  1090. }
  1091. else
  1092. {
  1093. m_log.WarnFormat("[ATTACHMENTS MODULE]: did not attach item with asset {0} to avatar {1} at point {2}",
  1094. assetID, sp.Name, attachmentPt);
  1095. }
  1096. return null;
  1097. }
  1098. if (!ItemIDNotZero)
  1099. {
  1100. // We need to have a FromItemID for multiple attachments on a single attach point to appear. This is
  1101. // true on Singularity 1.8.5 and quite possibly other viewers as well. As NPCs don't have an inventory
  1102. // we will satisfy this requirement by inserting a random UUID.
  1103. objatt.FromItemID = UUID.Random();
  1104. }
  1105. if (DebugLevel > 0)
  1106. m_log.DebugFormat(
  1107. "[ATTACHMENTS MODULE]: Rezzed single object {0} with {1} prims for attachment to {2} on point {3} in {4}",
  1108. objatt.Name, objatt.PrimCount, sp.Name, attachmentPt, m_scene.Name);
  1109. // HasGroupChanged is being set from within RezObject. Ideally it would be set by the caller.
  1110. objatt.HasGroupChanged = false;
  1111. Vector3 lastPos = objatt.RootPart.OffsetPosition;
  1112. Vector3 lastAttPos = objatt.RootPart.AttachedPos;
  1113. uint lastattPoint = objatt.AttachmentPoint;
  1114. bool doneAttach = false;
  1115. // FIXME: Detect whether it's really likely for AttachObject to throw an exception in the normal
  1116. // course of events. If not, then it's probably not worth trying to recover the situation
  1117. // since this is more likely to trigger further exceptions and confuse later debugging. If
  1118. // exceptions can be thrown in expected error conditions (not NREs) then make this consistent
  1119. // since other normal error conditions will simply return false instead.
  1120. // This will throw if the attachment fails
  1121. try
  1122. {
  1123. if (doc != null)
  1124. {
  1125. objatt.LoadScriptState(doc);
  1126. objatt.ResetOwnerChangeFlag();
  1127. }
  1128. doneAttach = AttachObjectInternal(sp, objatt, attachmentPt, false, true, true, append);
  1129. }
  1130. catch (Exception e)
  1131. {
  1132. m_log.ErrorFormat(
  1133. "[ATTACHMENTS MODULE]: Failed to attach {0} {1} for {2}, exception {3}{4}",
  1134. objatt.Name, objatt.UUID, sp.Name, e.Message, e.StackTrace);
  1135. doneAttach = false;
  1136. }
  1137. if(!doneAttach)
  1138. {
  1139. // Make sure the object doesn't stick around and bail
  1140. sp.RemoveAttachment(objatt);
  1141. m_scene.DeleteSceneObject(objatt, false);
  1142. return null;
  1143. }
  1144. if (lastattPoint != objatt.AttachmentPoint ||
  1145. lastPos != objatt.RootPart.OffsetPosition ||
  1146. lastAttPos != objatt.RootPart.AttachedPos)
  1147. objatt.HasGroupChanged = true;
  1148. return objatt;
  1149. }
  1150. /// <summary>
  1151. /// Update the user inventory to reflect an attachment
  1152. /// </summary>
  1153. /// <param name="sp"></param>
  1154. /// <param name="AttachmentPt"></param>
  1155. /// <param name="itemID"></param>
  1156. /// <param name="att"></param>
  1157. private void ShowAttachInUserInventory(IScenePresence sp, uint AttachmentPt, UUID itemID, SceneObjectGroup att, bool append)
  1158. {
  1159. // m_log.DebugFormat(
  1160. // "[USER INVENTORY]: Updating attachment {0} for {1} at {2} using item ID {3}",
  1161. // att.Name, sp.Name, AttachmentPt, itemID);
  1162. if (UUID.Zero == itemID)
  1163. {
  1164. m_log.Error("[ATTACHMENTS MODULE]: Unable to save attachment. Error inventory item ID.");
  1165. return;
  1166. }
  1167. if (0 == AttachmentPt)
  1168. {
  1169. m_log.Error("[ATTACHMENTS MODULE]: Unable to save attachment. Error attachment point.");
  1170. return;
  1171. }
  1172. InventoryItemBase item = m_scene.InventoryService.GetItem(sp.UUID, itemID);
  1173. if (item == null)
  1174. return;
  1175. int attFlag = append ? 0x80 : 0;
  1176. bool changed = sp.Appearance.SetAttachment((int)AttachmentPt | attFlag, itemID, item.AssetID);
  1177. if (changed && m_scene.AvatarFactory != null)
  1178. {
  1179. if (DebugLevel > 0)
  1180. m_log.DebugFormat(
  1181. "[ATTACHMENTS MODULE]: Queueing appearance save for {0}, attachment {1} point {2} in ShowAttachInUserInventory()",
  1182. sp.Name, att.Name, AttachmentPt);
  1183. m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
  1184. }
  1185. }
  1186. #endregion
  1187. #region Client Event Handlers
  1188. private ISceneEntity Client_OnRezSingleAttachmentFromInv(IClientAPI remoteClient, UUID itemID, uint AttachmentPt)
  1189. {
  1190. if (!Enabled)
  1191. return null;
  1192. if (DebugLevel > 0)
  1193. m_log.DebugFormat(
  1194. "[ATTACHMENTS MODULE]: Rezzing attachment to point {0} from item {1} for {2}",
  1195. (AttachmentPoint)AttachmentPt, itemID, remoteClient.Name);
  1196. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1197. if (sp == null)
  1198. {
  1199. m_log.ErrorFormat(
  1200. "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1} in RezSingleAttachmentFromInventory()",
  1201. remoteClient.Name, remoteClient.AgentId);
  1202. return null;
  1203. }
  1204. return RezSingleAttachmentFromInventory(sp, itemID, AttachmentPt);
  1205. }
  1206. private void Client_OnRezMultipleAttachmentsFromInv(IClientAPI remoteClient, List<KeyValuePair<UUID, uint>> rezlist)
  1207. {
  1208. if (!Enabled)
  1209. return;
  1210. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1211. if (sp != null)
  1212. RezMultipleAttachmentsFromInventory(sp, rezlist);
  1213. else
  1214. m_log.ErrorFormat(
  1215. "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1} in RezMultipleAttachmentsFromInventory()",
  1216. remoteClient.Name, remoteClient.AgentId);
  1217. }
  1218. private void Client_OnObjectAttach(IClientAPI remoteClient, uint objectLocalID, uint AttachmentPt, bool silent)
  1219. {
  1220. if (DebugLevel > 0)
  1221. m_log.DebugFormat(
  1222. "[ATTACHMENTS MODULE]: Attaching object local id {0} to {1} point {2} from ground (silent = {3})",
  1223. objectLocalID, remoteClient.Name, AttachmentPt, silent);
  1224. if (!Enabled)
  1225. return;
  1226. try
  1227. {
  1228. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1229. if (sp == null)
  1230. {
  1231. m_log.ErrorFormat(
  1232. "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1}", remoteClient.Name, remoteClient.AgentId);
  1233. return;
  1234. }
  1235. // If we can't take it, we can't attach it!
  1236. SceneObjectPart part = m_scene.GetSceneObjectPart(objectLocalID);
  1237. if (part == null)
  1238. return;
  1239. SceneObjectGroup group = part.ParentGroup;
  1240. if (!m_scene.Permissions.CanTakeObject(group, sp))
  1241. {
  1242. remoteClient.SendAgentAlertMessage(
  1243. "You don't have sufficient permissions to attach this object", false);
  1244. return;
  1245. }
  1246. bool append = (AttachmentPt & 0x80) != 0;
  1247. AttachmentPt &= 0x7f;
  1248. // Calls attach with a Zero position
  1249. if (AttachObject(sp, group , AttachmentPt, false, true, append))
  1250. {
  1251. if (DebugLevel > 0)
  1252. m_log.Debug(
  1253. "[ATTACHMENTS MODULE]: Saving avatar attachment. AgentID: " + remoteClient.AgentId
  1254. + ", AttachmentPoint: " + AttachmentPt);
  1255. // Save avatar attachment information
  1256. m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
  1257. }
  1258. }
  1259. catch (Exception e)
  1260. {
  1261. m_log.ErrorFormat("[ATTACHMENTS MODULE]: exception upon Attach Object {0}{1}", e.Message, e.StackTrace);
  1262. }
  1263. }
  1264. private void Client_OnObjectDetach(uint objectLocalID, IClientAPI remoteClient)
  1265. {
  1266. if (!Enabled)
  1267. return;
  1268. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1269. SceneObjectGroup group = m_scene.GetGroupByPrim(objectLocalID);
  1270. if (sp != null && group != null)
  1271. DetachSingleAttachmentToInv(sp, group);
  1272. }
  1273. private void Client_OnDetachAttachmentIntoInv(UUID itemID, IClientAPI remoteClient)
  1274. {
  1275. if (!Enabled)
  1276. return;
  1277. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1278. if (sp != null)
  1279. {
  1280. List<SceneObjectGroup> attachments = sp.GetAttachments();
  1281. foreach (SceneObjectGroup group in attachments)
  1282. {
  1283. if (group.FromItemID.Equals(itemID) && !group.FromItemID.IsZero())
  1284. {
  1285. DetachSingleAttachmentToInv(sp, group);
  1286. return;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. private void Client_OnObjectDrop(uint soLocalId, IClientAPI remoteClient)
  1292. {
  1293. if (!Enabled)
  1294. return;
  1295. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1296. if (sp != null)
  1297. DetachSingleAttachmentToGround(sp, soLocalId);
  1298. }
  1299. #endregion
  1300. }
  1301. }