SceneObjectPartInventory.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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.Xml;
  29. using System.IO;
  30. using System.Collections.Generic;
  31. using System.Collections;
  32. using System.Reflection;
  33. using System.Threading;
  34. using OpenMetaverse;
  35. using log4net;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes.Scripting;
  39. using OpenSim.Region.Framework.Scenes.Serialization;
  40. namespace OpenSim.Region.Framework.Scenes
  41. {
  42. public class SceneObjectPartInventory : IEntityInventory
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private string m_inventoryFileName = String.Empty;
  46. private byte[] m_inventoryFileData = new byte[0];
  47. private uint m_inventoryFileNameSerial = 0;
  48. /// <value>
  49. /// The part to which the inventory belongs.
  50. /// </value>
  51. private SceneObjectPart m_part;
  52. /// <summary>
  53. /// Serial count for inventory file , used to tell if inventory has changed
  54. /// no need for this to be part of Database backup
  55. /// </summary>
  56. protected uint m_inventorySerial = 0;
  57. /// <summary>
  58. /// Holds in memory prim inventory
  59. /// </summary>
  60. protected TaskInventoryDictionary m_items = new TaskInventoryDictionary();
  61. /// <summary>
  62. /// Tracks whether inventory has changed since the last persistent backup
  63. /// </summary>
  64. internal bool HasInventoryChanged;
  65. /// <value>
  66. /// Inventory serial number
  67. /// </value>
  68. protected internal uint Serial
  69. {
  70. get { return m_inventorySerial; }
  71. set { m_inventorySerial = value; }
  72. }
  73. /// <value>
  74. /// Raw inventory data
  75. /// </value>
  76. protected internal TaskInventoryDictionary Items
  77. {
  78. get { return m_items; }
  79. set
  80. {
  81. m_items = value;
  82. m_inventorySerial++;
  83. }
  84. }
  85. /// <summary>
  86. /// Constructor
  87. /// </summary>
  88. /// <param name="part">
  89. /// A <see cref="SceneObjectPart"/>
  90. /// </param>
  91. public SceneObjectPartInventory(SceneObjectPart part)
  92. {
  93. m_part = part;
  94. }
  95. /// <summary>
  96. /// Force the task inventory of this prim to persist at the next update sweep
  97. /// </summary>
  98. public void ForceInventoryPersistence()
  99. {
  100. HasInventoryChanged = true;
  101. }
  102. /// <summary>
  103. /// Reset UUIDs for all the items in the prim's inventory.
  104. /// </summary>
  105. /// <remarks>
  106. /// This involves either generating
  107. /// new ones or setting existing UUIDs to the correct parent UUIDs.
  108. ///
  109. /// If this method is called and there are inventory items, then we regard the inventory as having changed.
  110. /// </remarks>
  111. public void ResetInventoryIDs()
  112. {
  113. if (null == m_part)
  114. return;
  115. lock (m_items)
  116. {
  117. if (0 == m_items.Count)
  118. return;
  119. IList<TaskInventoryItem> items = GetInventoryItems();
  120. m_items.Clear();
  121. foreach (TaskInventoryItem item in items)
  122. {
  123. item.ResetIDs(m_part.UUID);
  124. m_items.Add(item.ItemID, item);
  125. }
  126. }
  127. }
  128. public void ResetObjectID()
  129. {
  130. lock (Items)
  131. {
  132. IList<TaskInventoryItem> items = new List<TaskInventoryItem>(Items.Values);
  133. Items.Clear();
  134. foreach (TaskInventoryItem item in items)
  135. {
  136. item.ParentPartID = m_part.UUID;
  137. item.ParentID = m_part.UUID;
  138. Items.Add(item.ItemID, item);
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// Change every item in this inventory to a new owner.
  144. /// </summary>
  145. /// <param name="ownerId"></param>
  146. public void ChangeInventoryOwner(UUID ownerId)
  147. {
  148. lock (Items)
  149. {
  150. if (0 == Items.Count)
  151. {
  152. return;
  153. }
  154. }
  155. HasInventoryChanged = true;
  156. m_part.ParentGroup.HasGroupChanged = true;
  157. List<TaskInventoryItem> items = GetInventoryItems();
  158. foreach (TaskInventoryItem item in items)
  159. {
  160. if (ownerId != item.OwnerID)
  161. item.LastOwnerID = item.OwnerID;
  162. item.OwnerID = ownerId;
  163. item.PermsMask = 0;
  164. item.PermsGranter = UUID.Zero;
  165. item.OwnerChanged = true;
  166. }
  167. }
  168. /// <summary>
  169. /// Change every item in this inventory to a new group.
  170. /// </summary>
  171. /// <param name="groupID"></param>
  172. public void ChangeInventoryGroup(UUID groupID)
  173. {
  174. lock (Items)
  175. {
  176. if (0 == Items.Count)
  177. {
  178. return;
  179. }
  180. }
  181. // Don't let this set the HasGroupChanged flag for attachments
  182. // as this happens during rez and we don't want a new asset
  183. // for each attachment each time
  184. if (!m_part.ParentGroup.IsAttachment)
  185. {
  186. HasInventoryChanged = true;
  187. m_part.ParentGroup.HasGroupChanged = true;
  188. }
  189. List<TaskInventoryItem> items = GetInventoryItems();
  190. foreach (TaskInventoryItem item in items)
  191. {
  192. if (groupID != item.GroupID)
  193. item.GroupID = groupID;
  194. }
  195. }
  196. /// <summary>
  197. /// Start all the scripts contained in this prim's inventory
  198. /// </summary>
  199. public void CreateScriptInstances(int startParam, bool postOnRez, string engine, int stateSource)
  200. {
  201. List<TaskInventoryItem> scripts = GetInventoryScripts();
  202. foreach (TaskInventoryItem item in scripts)
  203. CreateScriptInstance(item, startParam, postOnRez, engine, stateSource);
  204. }
  205. public ArrayList GetScriptErrors(UUID itemID)
  206. {
  207. ArrayList ret = new ArrayList();
  208. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  209. foreach (IScriptModule e in engines)
  210. {
  211. if (e != null)
  212. {
  213. ArrayList errors = e.GetScriptErrors(itemID);
  214. foreach (Object line in errors)
  215. ret.Add(line);
  216. }
  217. }
  218. return ret;
  219. }
  220. /// <summary>
  221. /// Stop all the scripts in this prim.
  222. /// </summary>
  223. /// <param name="sceneObjectBeingDeleted">
  224. /// Should be true if these scripts are being removed because the scene
  225. /// object is being deleted. This will prevent spurious updates to the client.
  226. /// </param>
  227. public void RemoveScriptInstances(bool sceneObjectBeingDeleted)
  228. {
  229. List<TaskInventoryItem> scripts = GetInventoryScripts();
  230. foreach (TaskInventoryItem item in scripts)
  231. RemoveScriptInstance(item.ItemID, sceneObjectBeingDeleted);
  232. }
  233. /// <summary>
  234. /// Start a script which is in this prim's inventory.
  235. /// </summary>
  236. /// <param name="item"></param>
  237. /// <returns></returns>
  238. public void CreateScriptInstance(TaskInventoryItem item, int startParam, bool postOnRez, string engine, int stateSource)
  239. {
  240. // m_log.InfoFormat(
  241. // "[PRIM INVENTORY]: " +
  242. // "Starting script {0}, {1} in prim {2}, {3}",
  243. // item.Name, item.ItemID, Name, UUID);
  244. if (!m_part.ParentGroup.Scene.Permissions.CanRunScript(item.ItemID, m_part.UUID, item.OwnerID))
  245. return;
  246. m_part.AddFlag(PrimFlags.Scripted);
  247. if (!m_part.ParentGroup.Scene.RegionInfo.RegionSettings.DisableScripts)
  248. {
  249. if (stateSource == 2 && // Prim crossing
  250. m_part.ParentGroup.Scene.m_trustBinaries)
  251. {
  252. lock (m_items)
  253. {
  254. m_items[item.ItemID].PermsMask = 0;
  255. m_items[item.ItemID].PermsGranter = UUID.Zero;
  256. }
  257. m_part.ParentGroup.Scene.EventManager.TriggerRezScript(
  258. m_part.LocalId, item.ItemID, String.Empty, startParam, postOnRez, engine, stateSource);
  259. m_part.ParentGroup.AddActiveScriptCount(1);
  260. m_part.ScheduleFullUpdate();
  261. return;
  262. }
  263. AssetBase asset = m_part.ParentGroup.Scene.AssetService.Get(item.AssetID.ToString());
  264. if (null == asset)
  265. {
  266. m_log.ErrorFormat(
  267. "[PRIM INVENTORY]: " +
  268. "Couldn't start script {0}, {1} at {2} in {3} since asset ID {4} could not be found",
  269. item.Name, item.ItemID, m_part.AbsolutePosition,
  270. m_part.ParentGroup.Scene.RegionInfo.RegionName, item.AssetID);
  271. }
  272. else
  273. {
  274. if (m_part.ParentGroup.m_savedScriptState != null)
  275. RestoreSavedScriptState(item.OldItemID, item.ItemID);
  276. lock (m_items)
  277. {
  278. m_items[item.ItemID].PermsMask = 0;
  279. m_items[item.ItemID].PermsGranter = UUID.Zero;
  280. }
  281. string script = Utils.BytesToString(asset.Data);
  282. m_part.ParentGroup.Scene.EventManager.TriggerRezScript(
  283. m_part.LocalId, item.ItemID, script, startParam, postOnRez, engine, stateSource);
  284. m_part.ParentGroup.AddActiveScriptCount(1);
  285. m_part.ScheduleFullUpdate();
  286. }
  287. }
  288. }
  289. private void RestoreSavedScriptState(UUID oldID, UUID newID)
  290. {
  291. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  292. if (engines.Length == 0) // No engine at all
  293. return;
  294. if (m_part.ParentGroup.m_savedScriptState.ContainsKey(oldID))
  295. {
  296. XmlDocument doc = new XmlDocument();
  297. doc.LoadXml(m_part.ParentGroup.m_savedScriptState[oldID]);
  298. ////////// CRUFT WARNING ///////////////////////////////////
  299. //
  300. // Old objects will have <ScriptState><State> ...
  301. // This format is XEngine ONLY
  302. //
  303. // New objects have <State Engine="...." ...><ScriptState>...
  304. // This can be passed to any engine
  305. //
  306. XmlNode n = doc.SelectSingleNode("ScriptState");
  307. if (n != null) // Old format data
  308. {
  309. XmlDocument newDoc = new XmlDocument();
  310. XmlElement rootN = newDoc.CreateElement("", "State", "");
  311. XmlAttribute uuidA = newDoc.CreateAttribute("", "UUID", "");
  312. uuidA.Value = oldID.ToString();
  313. rootN.Attributes.Append(uuidA);
  314. XmlAttribute engineA = newDoc.CreateAttribute("", "Engine", "");
  315. engineA.Value = "XEngine";
  316. rootN.Attributes.Append(engineA);
  317. newDoc.AppendChild(rootN);
  318. XmlNode stateN = newDoc.ImportNode(n, true);
  319. rootN.AppendChild(stateN);
  320. // This created document has only the minimun data
  321. // necessary for XEngine to parse it successfully
  322. m_part.ParentGroup.m_savedScriptState[oldID] = newDoc.OuterXml;
  323. }
  324. foreach (IScriptModule e in engines)
  325. {
  326. if (e != null)
  327. {
  328. if (e.SetXMLState(newID, m_part.ParentGroup.m_savedScriptState[oldID]))
  329. break;
  330. }
  331. }
  332. m_part.ParentGroup.m_savedScriptState.Remove(oldID);
  333. }
  334. }
  335. /// <summary>
  336. /// Start a script which is in this prim's inventory.
  337. /// </summary>
  338. /// <param name="itemId">
  339. /// A <see cref="UUID"/>
  340. /// </param>
  341. public void CreateScriptInstance(UUID itemId, int startParam, bool postOnRez, string engine, int stateSource)
  342. {
  343. TaskInventoryItem item = GetInventoryItem(itemId);
  344. if (item != null)
  345. CreateScriptInstance(item, startParam, postOnRez, engine, stateSource);
  346. else
  347. m_log.ErrorFormat(
  348. "[PRIM INVENTORY]: " +
  349. "Couldn't start script with ID {0} since it couldn't be found for prim {1}, {2} at {3} in {4}",
  350. itemId, m_part.Name, m_part.UUID,
  351. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  352. }
  353. /// <summary>
  354. /// Stop a script which is in this prim's inventory.
  355. /// </summary>
  356. /// <param name="itemId"></param>
  357. /// <param name="sceneObjectBeingDeleted">
  358. /// Should be true if this script is being removed because the scene
  359. /// object is being deleted. This will prevent spurious updates to the client.
  360. /// </param>
  361. public void RemoveScriptInstance(UUID itemId, bool sceneObjectBeingDeleted)
  362. {
  363. bool scriptPresent = false;
  364. lock (m_items)
  365. {
  366. if (m_items.ContainsKey(itemId))
  367. scriptPresent = true;
  368. }
  369. if (scriptPresent)
  370. {
  371. if (!sceneObjectBeingDeleted)
  372. m_part.RemoveScriptEvents(itemId);
  373. m_part.ParentGroup.Scene.EventManager.TriggerRemoveScript(m_part.LocalId, itemId);
  374. m_part.ParentGroup.AddActiveScriptCount(-1);
  375. }
  376. else
  377. {
  378. m_log.ErrorFormat(
  379. "[PRIM INVENTORY]: " +
  380. "Couldn't stop script with ID {0} since it couldn't be found for prim {1}, {2} at {3} in {4}",
  381. itemId, m_part.Name, m_part.UUID,
  382. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  383. }
  384. }
  385. /// <summary>
  386. /// Check if the inventory holds an item with a given name.
  387. /// </summary>
  388. /// <param name="name"></param>
  389. /// <returns></returns>
  390. private bool InventoryContainsName(string name)
  391. {
  392. lock (m_items)
  393. {
  394. foreach (TaskInventoryItem item in m_items.Values)
  395. {
  396. if (item.Name == name)
  397. return true;
  398. }
  399. }
  400. return false;
  401. }
  402. /// <summary>
  403. /// For a given item name, return that name if it is available. Otherwise, return the next available
  404. /// similar name (which is currently the original name with the next available numeric suffix).
  405. /// </summary>
  406. /// <param name="name"></param>
  407. /// <returns></returns>
  408. private string FindAvailableInventoryName(string name)
  409. {
  410. if (!InventoryContainsName(name))
  411. return name;
  412. int suffix=1;
  413. while (suffix < 256)
  414. {
  415. string tryName=String.Format("{0} {1}", name, suffix);
  416. if (!InventoryContainsName(tryName))
  417. return tryName;
  418. suffix++;
  419. }
  420. return String.Empty;
  421. }
  422. /// <summary>
  423. /// Add an item to this prim's inventory. If an item with the same name already exists, then an alternative
  424. /// name is chosen.
  425. /// </summary>
  426. /// <param name="item"></param>
  427. public void AddInventoryItem(TaskInventoryItem item, bool allowedDrop)
  428. {
  429. AddInventoryItem(item.Name, item, allowedDrop);
  430. }
  431. /// <summary>
  432. /// Add an item to this prim's inventory. If an item with the same name already exists, it is replaced.
  433. /// </summary>
  434. /// <param name="item"></param>
  435. public void AddInventoryItemExclusive(TaskInventoryItem item, bool allowedDrop)
  436. {
  437. List<TaskInventoryItem> il = GetInventoryItems();
  438. foreach (TaskInventoryItem i in il)
  439. {
  440. if (i.Name == item.Name)
  441. {
  442. if (i.InvType == (int)InventoryType.LSL)
  443. RemoveScriptInstance(i.ItemID, false);
  444. RemoveInventoryItem(i.ItemID);
  445. break;
  446. }
  447. }
  448. AddInventoryItem(item.Name, item, allowedDrop);
  449. }
  450. /// <summary>
  451. /// Add an item to this prim's inventory.
  452. /// </summary>
  453. /// <param name="name">The name that the new item should have.</param>
  454. /// <param name="item">
  455. /// The item itself. The name within this structure is ignored in favour of the name
  456. /// given in this method's arguments
  457. /// </param>
  458. /// <param name="allowedDrop">
  459. /// Item was only added to inventory because AllowedDrop is set
  460. /// </param>
  461. protected void AddInventoryItem(string name, TaskInventoryItem item, bool allowedDrop)
  462. {
  463. name = FindAvailableInventoryName(name);
  464. if (name == String.Empty)
  465. return;
  466. item.ParentID = m_part.UUID;
  467. item.ParentPartID = m_part.UUID;
  468. item.Name = name;
  469. item.GroupID = m_part.GroupID;
  470. lock (m_items)
  471. m_items.Add(item.ItemID, item);
  472. if (allowedDrop)
  473. m_part.TriggerScriptChangedEvent(Changed.ALLOWED_DROP);
  474. else
  475. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  476. m_inventorySerial++;
  477. //m_inventorySerial += 2;
  478. HasInventoryChanged = true;
  479. m_part.ParentGroup.HasGroupChanged = true;
  480. }
  481. /// <summary>
  482. /// Restore a whole collection of items to the prim's inventory at once.
  483. /// We assume that the items already have all their fields correctly filled out.
  484. /// The items are not flagged for persistence to the database, since they are being restored
  485. /// from persistence rather than being newly added.
  486. /// </summary>
  487. /// <param name="items"></param>
  488. public void RestoreInventoryItems(ICollection<TaskInventoryItem> items)
  489. {
  490. lock (m_items)
  491. {
  492. foreach (TaskInventoryItem item in items)
  493. {
  494. m_items.Add(item.ItemID, item);
  495. // m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  496. }
  497. m_inventorySerial++;
  498. }
  499. }
  500. /// <summary>
  501. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  502. /// </summary>
  503. /// <param name="itemID"></param>
  504. /// <returns>null if the item does not exist</returns>
  505. public TaskInventoryItem GetInventoryItem(UUID itemId)
  506. {
  507. TaskInventoryItem item;
  508. lock (m_items)
  509. m_items.TryGetValue(itemId, out item);
  510. return item;
  511. }
  512. /// <summary>
  513. /// Get inventory items by name.
  514. /// </summary>
  515. /// <param name="name"></param>
  516. /// <returns>
  517. /// A list of inventory items with that name.
  518. /// If no inventory item has that name then an empty list is returned.
  519. /// </returns>
  520. public IList<TaskInventoryItem> GetInventoryItems(string name)
  521. {
  522. IList<TaskInventoryItem> items = new List<TaskInventoryItem>();
  523. lock (m_items)
  524. {
  525. foreach (TaskInventoryItem item in m_items.Values)
  526. {
  527. if (item.Name == name)
  528. items.Add(item);
  529. }
  530. }
  531. return items;
  532. }
  533. public SceneObjectGroup GetRezReadySceneObject(TaskInventoryItem item)
  534. {
  535. AssetBase rezAsset = m_part.ParentGroup.Scene.AssetService.Get(item.AssetID.ToString());
  536. if (null == rezAsset)
  537. {
  538. m_log.WarnFormat(
  539. "[PRIM INVENTORY]: Could not find asset {0} for inventory item {1} in {2}",
  540. item.AssetID, item.Name, m_part.Name);
  541. return null;
  542. }
  543. string xmlData = Utils.BytesToString(rezAsset.Data);
  544. SceneObjectGroup group = SceneObjectSerializer.FromOriginalXmlFormat(xmlData);
  545. group.ResetIDs();
  546. SceneObjectPart rootPart = group.GetChildPart(group.UUID);
  547. // Since renaming the item in the inventory does not affect the name stored
  548. // in the serialization, transfer the correct name from the inventory to the
  549. // object itself before we rez.
  550. rootPart.Name = item.Name;
  551. rootPart.Description = item.Description;
  552. SceneObjectPart[] partList = group.Parts;
  553. group.SetGroup(m_part.GroupID, null);
  554. // TODO: Remove magic number badness
  555. if ((rootPart.OwnerID != item.OwnerID) || (item.CurrentPermissions & 16) != 0 || (item.Flags & (uint)InventoryItemFlags.ObjectSlamPerm) != 0) // Magic number
  556. {
  557. if (m_part.ParentGroup.Scene.Permissions.PropagatePermissions())
  558. {
  559. foreach (SceneObjectPart part in partList)
  560. {
  561. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteEveryone) != 0)
  562. part.EveryoneMask = item.EveryonePermissions;
  563. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteNextOwner) != 0)
  564. part.NextOwnerMask = item.NextPermissions;
  565. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteGroup) != 0)
  566. part.GroupMask = item.GroupPermissions;
  567. }
  568. group.ApplyNextOwnerPermissions();
  569. }
  570. }
  571. foreach (SceneObjectPart part in partList)
  572. {
  573. // TODO: Remove magic number badness
  574. if ((part.OwnerID != item.OwnerID) || (item.CurrentPermissions & 16) != 0 || (item.Flags & (uint)InventoryItemFlags.ObjectSlamPerm) != 0) // Magic number
  575. {
  576. part.LastOwnerID = part.OwnerID;
  577. part.OwnerID = item.OwnerID;
  578. part.Inventory.ChangeInventoryOwner(item.OwnerID);
  579. }
  580. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteEveryone) != 0)
  581. part.EveryoneMask = item.EveryonePermissions;
  582. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteNextOwner) != 0)
  583. part.NextOwnerMask = item.NextPermissions;
  584. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteGroup) != 0)
  585. part.GroupMask = item.GroupPermissions;
  586. }
  587. rootPart.TrimPermissions();
  588. return group;
  589. }
  590. /// <summary>
  591. /// Update an existing inventory item.
  592. /// </summary>
  593. /// <param name="item">The updated item. An item with the same id must already exist
  594. /// in this prim's inventory.</param>
  595. /// <returns>false if the item did not exist, true if the update occurred successfully</returns>
  596. public bool UpdateInventoryItem(TaskInventoryItem item)
  597. {
  598. return UpdateInventoryItem(item, true, true);
  599. }
  600. public bool UpdateInventoryItem(TaskInventoryItem item, bool fireScriptEvents)
  601. {
  602. return UpdateInventoryItem(item, fireScriptEvents, true);
  603. }
  604. public bool UpdateInventoryItem(TaskInventoryItem item, bool fireScriptEvents, bool considerChanged)
  605. {
  606. TaskInventoryItem it = GetInventoryItem(item.ItemID);
  607. if (it != null)
  608. {
  609. // m_log.DebugFormat("[PRIM INVENTORY]: Updating item {0} in {1}", item.Name, m_part.Name);
  610. item.ParentID = m_part.UUID;
  611. item.ParentPartID = m_part.UUID;
  612. // If group permissions have been set on, check that the groupID is up to date in case it has
  613. // changed since permissions were last set.
  614. if (item.GroupPermissions != (uint)PermissionMask.None)
  615. item.GroupID = m_part.GroupID;
  616. if (item.AssetID == UUID.Zero)
  617. item.AssetID = it.AssetID;
  618. lock (m_items)
  619. {
  620. m_items[item.ItemID] = item;
  621. m_inventorySerial++;
  622. }
  623. if (fireScriptEvents)
  624. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  625. if (considerChanged)
  626. {
  627. HasInventoryChanged = true;
  628. m_part.ParentGroup.HasGroupChanged = true;
  629. }
  630. return true;
  631. }
  632. else
  633. {
  634. m_log.ErrorFormat(
  635. "[PRIM INVENTORY]: " +
  636. "Tried to retrieve item ID {0} from prim {1}, {2} at {3} in {4} but the item does not exist in this inventory",
  637. item.ItemID, m_part.Name, m_part.UUID,
  638. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  639. }
  640. return false;
  641. }
  642. /// <summary>
  643. /// Remove an item from this prim's inventory
  644. /// </summary>
  645. /// <param name="itemID"></param>
  646. /// <returns>Numeric asset type of the item removed. Returns -1 if the item did not exist
  647. /// in this prim's inventory.</returns>
  648. public int RemoveInventoryItem(UUID itemID)
  649. {
  650. TaskInventoryItem item = GetInventoryItem(itemID);
  651. if (item != null)
  652. {
  653. int type = m_items[itemID].InvType;
  654. if (type == 10) // Script
  655. {
  656. m_part.RemoveScriptEvents(itemID);
  657. m_part.ParentGroup.Scene.EventManager.TriggerRemoveScript(m_part.LocalId, itemID);
  658. }
  659. m_items.Remove(itemID);
  660. m_inventorySerial++;
  661. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  662. HasInventoryChanged = true;
  663. m_part.ParentGroup.HasGroupChanged = true;
  664. if (!ContainsScripts())
  665. m_part.RemFlag(PrimFlags.Scripted);
  666. m_part.ScheduleFullUpdate();
  667. return type;
  668. }
  669. else
  670. {
  671. m_log.ErrorFormat(
  672. "[PRIM INVENTORY]: " +
  673. "Tried to remove item ID {0} from prim {1}, {2} at {3} in {4} but the item does not exist in this inventory",
  674. itemID, m_part.Name, m_part.UUID,
  675. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  676. }
  677. return -1;
  678. }
  679. private bool CreateInventoryFile()
  680. {
  681. // m_log.DebugFormat(
  682. // "[PRIM INVENTORY]: Creating inventory file for {0} {1} {2}, serial {3}",
  683. // m_part.Name, m_part.UUID, m_part.LocalId, m_inventorySerial);
  684. if (m_inventoryFileName == String.Empty ||
  685. m_inventoryFileNameSerial < m_inventorySerial)
  686. {
  687. // Something changed, we need to create a new file
  688. m_inventoryFileName = "inventory_" + UUID.Random().ToString() + ".tmp";
  689. m_inventoryFileNameSerial = m_inventorySerial;
  690. InventoryStringBuilder invString = new InventoryStringBuilder(m_part.UUID, UUID.Zero);
  691. lock (m_items)
  692. {
  693. foreach (TaskInventoryItem item in m_items.Values)
  694. {
  695. // m_log.DebugFormat(
  696. // "[PRIM INVENTORY]: Adding item {0} {1} for serial {2} on prim {3} {4} {5}",
  697. // item.Name, item.ItemID, m_inventorySerial, m_part.Name, m_part.UUID, m_part.LocalId);
  698. UUID ownerID = item.OwnerID;
  699. uint everyoneMask = 0;
  700. uint baseMask = item.BasePermissions;
  701. uint ownerMask = item.CurrentPermissions;
  702. uint groupMask = item.GroupPermissions;
  703. invString.AddItemStart();
  704. invString.AddNameValueLine("item_id", item.ItemID.ToString());
  705. invString.AddNameValueLine("parent_id", m_part.UUID.ToString());
  706. invString.AddPermissionsStart();
  707. invString.AddNameValueLine("base_mask", Utils.UIntToHexString(baseMask));
  708. invString.AddNameValueLine("owner_mask", Utils.UIntToHexString(ownerMask));
  709. invString.AddNameValueLine("group_mask", Utils.UIntToHexString(groupMask));
  710. invString.AddNameValueLine("everyone_mask", Utils.UIntToHexString(everyoneMask));
  711. invString.AddNameValueLine("next_owner_mask", Utils.UIntToHexString(item.NextPermissions));
  712. invString.AddNameValueLine("creator_id", item.CreatorID.ToString());
  713. invString.AddNameValueLine("owner_id", ownerID.ToString());
  714. invString.AddNameValueLine("last_owner_id", item.LastOwnerID.ToString());
  715. invString.AddNameValueLine("group_id", item.GroupID.ToString());
  716. invString.AddSectionEnd();
  717. invString.AddNameValueLine("asset_id", item.AssetID.ToString());
  718. invString.AddNameValueLine("type", TaskInventoryItem.Types[item.Type]);
  719. invString.AddNameValueLine("inv_type", TaskInventoryItem.InvTypes[item.InvType]);
  720. invString.AddNameValueLine("flags", Utils.UIntToHexString(item.Flags));
  721. invString.AddSaleStart();
  722. invString.AddNameValueLine("sale_type", "not");
  723. invString.AddNameValueLine("sale_price", "0");
  724. invString.AddSectionEnd();
  725. invString.AddNameValueLine("name", item.Name + "|");
  726. invString.AddNameValueLine("desc", item.Description + "|");
  727. invString.AddNameValueLine("creation_date", item.CreationDate.ToString());
  728. invString.AddSectionEnd();
  729. }
  730. }
  731. m_inventoryFileData = Utils.StringToBytes(invString.BuildString);
  732. return true;
  733. }
  734. // No need to recreate, the existing file is fine
  735. return false;
  736. }
  737. /// <summary>
  738. /// Serialize all the metadata for the items in this prim's inventory ready for sending to the client
  739. /// </summary>
  740. /// <param name="xferManager"></param>
  741. public void RequestInventoryFile(IClientAPI client, IXfer xferManager)
  742. {
  743. lock (m_items)
  744. {
  745. // Don't send a inventory xfer name if there are no items. Doing so causes viewer 3 to crash when rezzing
  746. // a new script if any previous deletion has left the prim inventory empty.
  747. if (m_items.Count == 0) // No inventory
  748. {
  749. // m_log.DebugFormat(
  750. // "[PRIM INVENTORY]: Not sending inventory data for part {0} {1} {2} for {3} since no items",
  751. // m_part.Name, m_part.LocalId, m_part.UUID, client.Name);
  752. client.SendTaskInventory(m_part.UUID, 0, new byte[0]);
  753. return;
  754. }
  755. CreateInventoryFile();
  756. // In principle, we should only do the rest if the inventory changed;
  757. // by sending m_inventorySerial to the client, it ought to know
  758. // that nothing changed and that it doesn't need to request the file.
  759. // Unfortunately, it doesn't look like the client optimizes this;
  760. // the client seems to always come back and request the Xfer,
  761. // no matter what value m_inventorySerial has.
  762. // FIXME: Could probably be > 0 here rather than > 2
  763. if (m_inventoryFileData.Length > 2)
  764. {
  765. // Add the file for Xfer
  766. // m_log.DebugFormat(
  767. // "[PRIM INVENTORY]: Adding inventory file {0} (length {1}) for transfer on {2} {3} {4}",
  768. // m_inventoryFileName, m_inventoryFileData.Length, m_part.Name, m_part.UUID, m_part.LocalId);
  769. xferManager.AddNewFile(m_inventoryFileName, m_inventoryFileData);
  770. }
  771. // Tell the client we're ready to Xfer the file
  772. client.SendTaskInventory(m_part.UUID, (short)m_inventorySerial,
  773. Util.StringToBytes256(m_inventoryFileName));
  774. }
  775. }
  776. /// <summary>
  777. /// Process inventory backup
  778. /// </summary>
  779. /// <param name="datastore"></param>
  780. public void ProcessInventoryBackup(ISimulationDataService datastore)
  781. {
  782. if (HasInventoryChanged)
  783. {
  784. HasInventoryChanged = false;
  785. List<TaskInventoryItem> items = GetInventoryItems();
  786. datastore.StorePrimInventory(m_part.UUID, items);
  787. }
  788. }
  789. public class InventoryStringBuilder
  790. {
  791. public string BuildString = String.Empty;
  792. public InventoryStringBuilder(UUID folderID, UUID parentID)
  793. {
  794. BuildString += "\tinv_object\t0\n\t{\n";
  795. AddNameValueLine("obj_id", folderID.ToString());
  796. AddNameValueLine("parent_id", parentID.ToString());
  797. AddNameValueLine("type", "category");
  798. AddNameValueLine("name", "Contents|");
  799. AddSectionEnd();
  800. }
  801. public void AddItemStart()
  802. {
  803. BuildString += "\tinv_item\t0\n";
  804. AddSectionStart();
  805. }
  806. public void AddPermissionsStart()
  807. {
  808. BuildString += "\tpermissions 0\n";
  809. AddSectionStart();
  810. }
  811. public void AddSaleStart()
  812. {
  813. BuildString += "\tsale_info\t0\n";
  814. AddSectionStart();
  815. }
  816. protected void AddSectionStart()
  817. {
  818. BuildString += "\t{\n";
  819. }
  820. public void AddSectionEnd()
  821. {
  822. BuildString += "\t}\n";
  823. }
  824. public void AddLine(string addLine)
  825. {
  826. BuildString += addLine;
  827. }
  828. public void AddNameValueLine(string name, string value)
  829. {
  830. BuildString += "\t\t";
  831. BuildString += name + "\t";
  832. BuildString += value + "\n";
  833. }
  834. public void Close()
  835. {
  836. }
  837. }
  838. public uint MaskEffectivePermissions()
  839. {
  840. uint mask=0x7fffffff;
  841. lock (m_items)
  842. {
  843. foreach (TaskInventoryItem item in m_items.Values)
  844. {
  845. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Copy) == 0)
  846. mask &= ~((uint)PermissionMask.Copy >> 13);
  847. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Transfer) == 0)
  848. mask &= ~((uint)PermissionMask.Transfer >> 13);
  849. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Modify) == 0)
  850. mask &= ~((uint)PermissionMask.Modify >> 13);
  851. if (item.InvType != (int)InventoryType.Object)
  852. {
  853. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Copy) == 0)
  854. mask &= ~((uint)PermissionMask.Copy >> 13);
  855. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Transfer) == 0)
  856. mask &= ~((uint)PermissionMask.Transfer >> 13);
  857. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Modify) == 0)
  858. mask &= ~((uint)PermissionMask.Modify >> 13);
  859. }
  860. else
  861. {
  862. if ((item.CurrentPermissions & ((uint)PermissionMask.Copy >> 13)) == 0)
  863. mask &= ~((uint)PermissionMask.Copy >> 13);
  864. if ((item.CurrentPermissions & ((uint)PermissionMask.Transfer >> 13)) == 0)
  865. mask &= ~((uint)PermissionMask.Transfer >> 13);
  866. if ((item.CurrentPermissions & ((uint)PermissionMask.Modify >> 13)) == 0)
  867. mask &= ~((uint)PermissionMask.Modify >> 13);
  868. }
  869. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  870. mask &= ~(uint)PermissionMask.Copy;
  871. if ((item.CurrentPermissions & (uint)PermissionMask.Transfer) == 0)
  872. mask &= ~(uint)PermissionMask.Transfer;
  873. if ((item.CurrentPermissions & (uint)PermissionMask.Modify) == 0)
  874. mask &= ~(uint)PermissionMask.Modify;
  875. }
  876. }
  877. return mask;
  878. }
  879. public void ApplyNextOwnerPermissions()
  880. {
  881. lock (m_items)
  882. {
  883. foreach (TaskInventoryItem item in m_items.Values)
  884. {
  885. if (item.InvType == (int)InventoryType.Object && (item.CurrentPermissions & 7) != 0)
  886. {
  887. if ((item.CurrentPermissions & ((uint)PermissionMask.Copy >> 13)) == 0)
  888. item.CurrentPermissions &= ~(uint)PermissionMask.Copy;
  889. if ((item.CurrentPermissions & ((uint)PermissionMask.Transfer >> 13)) == 0)
  890. item.CurrentPermissions &= ~(uint)PermissionMask.Transfer;
  891. if ((item.CurrentPermissions & ((uint)PermissionMask.Modify >> 13)) == 0)
  892. item.CurrentPermissions &= ~(uint)PermissionMask.Modify;
  893. }
  894. item.CurrentPermissions &= item.NextPermissions;
  895. item.BasePermissions &= item.NextPermissions;
  896. item.EveryonePermissions &= item.NextPermissions;
  897. item.OwnerChanged = true;
  898. item.PermsMask = 0;
  899. item.PermsGranter = UUID.Zero;
  900. }
  901. }
  902. }
  903. public void ApplyGodPermissions(uint perms)
  904. {
  905. lock (m_items)
  906. {
  907. foreach (TaskInventoryItem item in m_items.Values)
  908. {
  909. item.CurrentPermissions = perms;
  910. item.BasePermissions = perms;
  911. }
  912. }
  913. m_inventorySerial++;
  914. HasInventoryChanged = true;
  915. }
  916. /// <summary>
  917. /// Returns true if this part inventory contains any scripts. False otherwise.
  918. /// </summary>
  919. /// <returns></returns>
  920. public bool ContainsScripts()
  921. {
  922. lock (m_items)
  923. {
  924. foreach (TaskInventoryItem item in m_items.Values)
  925. {
  926. if (item.InvType == (int)InventoryType.LSL)
  927. {
  928. return true;
  929. }
  930. }
  931. }
  932. return false;
  933. }
  934. public List<UUID> GetInventoryList()
  935. {
  936. List<UUID> ret = new List<UUID>();
  937. lock (m_items)
  938. {
  939. foreach (TaskInventoryItem item in m_items.Values)
  940. ret.Add(item.ItemID);
  941. }
  942. return ret;
  943. }
  944. public List<TaskInventoryItem> GetInventoryItems()
  945. {
  946. List<TaskInventoryItem> ret = new List<TaskInventoryItem>();
  947. lock (m_items)
  948. ret = new List<TaskInventoryItem>(m_items.Values);
  949. return ret;
  950. }
  951. public List<TaskInventoryItem> GetInventoryScripts()
  952. {
  953. List<TaskInventoryItem> ret = new List<TaskInventoryItem>();
  954. lock (m_items)
  955. {
  956. foreach (TaskInventoryItem item in m_items.Values)
  957. if (item.InvType == (int)InventoryType.LSL)
  958. ret.Add(item);
  959. }
  960. return ret;
  961. }
  962. public Dictionary<UUID, string> GetScriptStates()
  963. {
  964. Dictionary<UUID, string> ret = new Dictionary<UUID, string>();
  965. if (m_part.ParentGroup.Scene == null) // Group not in a scene
  966. return ret;
  967. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  968. if (engines.Length == 0) // No engine at all
  969. return ret;
  970. List<TaskInventoryItem> scripts = GetInventoryScripts();
  971. foreach (TaskInventoryItem item in scripts)
  972. {
  973. foreach (IScriptModule e in engines)
  974. {
  975. if (e != null)
  976. {
  977. string n = e.GetXMLState(item.ItemID);
  978. if (n != String.Empty)
  979. {
  980. if (!ret.ContainsKey(item.ItemID))
  981. ret[item.ItemID] = n;
  982. break;
  983. }
  984. }
  985. }
  986. }
  987. return ret;
  988. }
  989. public void ResumeScripts()
  990. {
  991. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  992. if (engines.Length == 0)
  993. return;
  994. List<TaskInventoryItem> scripts = GetInventoryScripts();
  995. foreach (TaskInventoryItem item in scripts)
  996. {
  997. foreach (IScriptModule engine in engines)
  998. {
  999. if (engine != null)
  1000. {
  1001. if (item.OwnerChanged)
  1002. engine.PostScriptEvent(item.ItemID, "changed", new Object[] { (int)Changed.OWNER });
  1003. item.OwnerChanged = false;
  1004. engine.ResumeScript(item.ItemID);
  1005. }
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }