SceneObjectPartInventory.cs 40 KB

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