AttachmentsModule.cs 61 KB

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