AttachmentsModule.cs 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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.RootPart.GroupPosition = attachOffset; // can not set absolutepos
  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.IsActive && 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. bool ItemIDNotZero = itemID != UUID.Zero;
  1042. if (ItemIDNotZero)
  1043. objatt = m_invAccessModule.RezObject(sp.ControllingClient,
  1044. itemID, rezGroupID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
  1045. false, false, sp.UUID, true);
  1046. else
  1047. objatt = m_invAccessModule.RezObject(sp.ControllingClient,
  1048. null, rezGroupID, assetID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
  1049. false, false, sp.UUID, true);
  1050. if (objatt == null)
  1051. {
  1052. if(ItemIDNotZero)
  1053. {
  1054. m_log.WarnFormat("[ATTACHMENTS MODULE]: did not attach item {0} to avatar {1} at point {2}",
  1055. itemID, sp.Name, attachmentPt);
  1056. }
  1057. else
  1058. {
  1059. m_log.WarnFormat("[ATTACHMENTS MODULE]: did not attach item with asset {0} to avatar {1} at point {2}",
  1060. assetID, sp.Name, attachmentPt);
  1061. }
  1062. return null;
  1063. }
  1064. if (!ItemIDNotZero)
  1065. {
  1066. // We need to have a FromItemID for multiple attachments on a single attach point to appear. This is
  1067. // true on Singularity 1.8.5 and quite possibly other viewers as well. As NPCs don't have an inventory
  1068. // we will satisfy this requirement by inserting a random UUID.
  1069. objatt.FromItemID = UUID.Random();
  1070. }
  1071. if (DebugLevel > 0)
  1072. m_log.DebugFormat(
  1073. "[ATTACHMENTS MODULE]: Rezzed single object {0} with {1} prims for attachment to {2} on point {3} in {4}",
  1074. objatt.Name, objatt.PrimCount, sp.Name, attachmentPt, m_scene.Name);
  1075. // HasGroupChanged is being set from within RezObject. Ideally it would be set by the caller.
  1076. objatt.HasGroupChanged = false;
  1077. Vector3 lastPos = objatt.RootPart.OffsetPosition;
  1078. Vector3 lastAttPos = objatt.RootPart.AttachedPos;
  1079. uint lastattPoint = objatt.AttachmentPoint;
  1080. bool doneAttach = false;
  1081. // FIXME: Detect whether it's really likely for AttachObject to throw an exception in the normal
  1082. // course of events. If not, then it's probably not worth trying to recover the situation
  1083. // since this is more likely to trigger further exceptions and confuse later debugging. If
  1084. // exceptions can be thrown in expected error conditions (not NREs) then make this consistent
  1085. // since other normal error conditions will simply return false instead.
  1086. // This will throw if the attachment fails
  1087. try
  1088. {
  1089. if (doc != null)
  1090. {
  1091. objatt.LoadScriptState(doc);
  1092. objatt.ResetOwnerChangeFlag();
  1093. }
  1094. doneAttach = AttachObjectInternal(sp, objatt, attachmentPt, false, true, true, append);
  1095. }
  1096. catch (Exception e)
  1097. {
  1098. m_log.ErrorFormat(
  1099. "[ATTACHMENTS MODULE]: Failed to attach {0} {1} for {2}, exception {3}{4}",
  1100. objatt.Name, objatt.UUID, sp.Name, e.Message, e.StackTrace);
  1101. doneAttach = false;
  1102. }
  1103. if(!doneAttach)
  1104. {
  1105. // Make sure the object doesn't stick around and bail
  1106. sp.RemoveAttachment(objatt);
  1107. m_scene.DeleteSceneObject(objatt, false);
  1108. return null;
  1109. }
  1110. if (lastattPoint != objatt.AttachmentPoint ||
  1111. lastPos != objatt.RootPart.OffsetPosition ||
  1112. lastAttPos != objatt.RootPart.AttachedPos)
  1113. objatt.HasGroupChanged = true;
  1114. return objatt;
  1115. }
  1116. /// <summary>
  1117. /// Update the user inventory to reflect an attachment
  1118. /// </summary>
  1119. /// <param name="sp"></param>
  1120. /// <param name="AttachmentPt"></param>
  1121. /// <param name="itemID"></param>
  1122. /// <param name="att"></param>
  1123. private void ShowAttachInUserInventory(IScenePresence sp, uint AttachmentPt, UUID itemID, SceneObjectGroup att, bool append)
  1124. {
  1125. // m_log.DebugFormat(
  1126. // "[USER INVENTORY]: Updating attachment {0} for {1} at {2} using item ID {3}",
  1127. // att.Name, sp.Name, AttachmentPt, itemID);
  1128. if (UUID.Zero == itemID)
  1129. {
  1130. m_log.Error("[ATTACHMENTS MODULE]: Unable to save attachment. Error inventory item ID.");
  1131. return;
  1132. }
  1133. if (0 == AttachmentPt)
  1134. {
  1135. m_log.Error("[ATTACHMENTS MODULE]: Unable to save attachment. Error attachment point.");
  1136. return;
  1137. }
  1138. InventoryItemBase item = m_scene.InventoryService.GetItem(sp.UUID, itemID);
  1139. if (item == null)
  1140. return;
  1141. int attFlag = append ? 0x80 : 0;
  1142. bool changed = sp.Appearance.SetAttachment((int)AttachmentPt | attFlag, itemID, item.AssetID);
  1143. if (changed && m_scene.AvatarFactory != null)
  1144. {
  1145. if (DebugLevel > 0)
  1146. m_log.DebugFormat(
  1147. "[ATTACHMENTS MODULE]: Queueing appearance save for {0}, attachment {1} point {2} in ShowAttachInUserInventory()",
  1148. sp.Name, att.Name, AttachmentPt);
  1149. m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
  1150. }
  1151. }
  1152. #endregion
  1153. #region Client Event Handlers
  1154. private ISceneEntity Client_OnRezSingleAttachmentFromInv(IClientAPI remoteClient, UUID itemID, uint AttachmentPt)
  1155. {
  1156. if (!Enabled)
  1157. return null;
  1158. if (DebugLevel > 0)
  1159. m_log.DebugFormat(
  1160. "[ATTACHMENTS MODULE]: Rezzing attachment to point {0} from item {1} for {2}",
  1161. (AttachmentPoint)AttachmentPt, itemID, remoteClient.Name);
  1162. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1163. if (sp == null)
  1164. {
  1165. m_log.ErrorFormat(
  1166. "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1} in RezSingleAttachmentFromInventory()",
  1167. remoteClient.Name, remoteClient.AgentId);
  1168. return null;
  1169. }
  1170. return RezSingleAttachmentFromInventory(sp, itemID, AttachmentPt);
  1171. }
  1172. private void Client_OnRezMultipleAttachmentsFromInv(IClientAPI remoteClient, List<KeyValuePair<UUID, uint>> rezlist)
  1173. {
  1174. if (!Enabled)
  1175. return;
  1176. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1177. if (sp != null)
  1178. RezMultipleAttachmentsFromInventory(sp, rezlist);
  1179. else
  1180. m_log.ErrorFormat(
  1181. "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1} in RezMultipleAttachmentsFromInventory()",
  1182. remoteClient.Name, remoteClient.AgentId);
  1183. }
  1184. private void Client_OnObjectAttach(IClientAPI remoteClient, uint objectLocalID, uint AttachmentPt, bool silent)
  1185. {
  1186. if (DebugLevel > 0)
  1187. m_log.DebugFormat(
  1188. "[ATTACHMENTS MODULE]: Attaching object local id {0} to {1} point {2} from ground (silent = {3})",
  1189. objectLocalID, remoteClient.Name, AttachmentPt, silent);
  1190. if (!Enabled)
  1191. return;
  1192. try
  1193. {
  1194. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1195. if (sp == null)
  1196. {
  1197. m_log.ErrorFormat(
  1198. "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1}", remoteClient.Name, remoteClient.AgentId);
  1199. return;
  1200. }
  1201. // If we can't take it, we can't attach it!
  1202. SceneObjectPart part = m_scene.GetSceneObjectPart(objectLocalID);
  1203. if (part == null)
  1204. return;
  1205. SceneObjectGroup group = part.ParentGroup;
  1206. if (!m_scene.Permissions.CanTakeObject(group, sp))
  1207. {
  1208. remoteClient.SendAgentAlertMessage(
  1209. "You don't have sufficient permissions to attach this object", false);
  1210. return;
  1211. }
  1212. bool append = (AttachmentPt & 0x80) != 0;
  1213. AttachmentPt &= 0x7f;
  1214. // Calls attach with a Zero position
  1215. if (AttachObject(sp, group , AttachmentPt, false, true, append))
  1216. {
  1217. if (DebugLevel > 0)
  1218. m_log.Debug(
  1219. "[ATTACHMENTS MODULE]: Saving avatar attachment. AgentID: " + remoteClient.AgentId
  1220. + ", AttachmentPoint: " + AttachmentPt);
  1221. // Save avatar attachment information
  1222. m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
  1223. }
  1224. }
  1225. catch (Exception e)
  1226. {
  1227. m_log.ErrorFormat("[ATTACHMENTS MODULE]: exception upon Attach Object {0}{1}", e.Message, e.StackTrace);
  1228. }
  1229. }
  1230. private void Client_OnObjectDetach(uint objectLocalID, IClientAPI remoteClient)
  1231. {
  1232. if (!Enabled)
  1233. return;
  1234. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1235. SceneObjectGroup group = m_scene.GetGroupByPrim(objectLocalID);
  1236. if (sp != null && group != null)
  1237. DetachSingleAttachmentToInv(sp, group);
  1238. }
  1239. private void Client_OnDetachAttachmentIntoInv(UUID itemID, IClientAPI remoteClient)
  1240. {
  1241. if (!Enabled)
  1242. return;
  1243. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1244. if (sp != null)
  1245. {
  1246. List<SceneObjectGroup> attachments = sp.GetAttachments();
  1247. foreach (SceneObjectGroup group in attachments)
  1248. {
  1249. if (group.FromItemID == itemID && group.FromItemID != UUID.Zero)
  1250. {
  1251. DetachSingleAttachmentToInv(sp, group);
  1252. return;
  1253. }
  1254. }
  1255. }
  1256. }
  1257. private void Client_OnObjectDrop(uint soLocalId, IClientAPI remoteClient)
  1258. {
  1259. if (!Enabled)
  1260. return;
  1261. ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
  1262. if (sp != null)
  1263. DetachSingleAttachmentToGround(sp, soLocalId);
  1264. }
  1265. #endregion
  1266. }
  1267. }