SceneObjectPartInventory.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. This involves either generating
  104. /// new ones or setting existing UUIDs to the correct parent UUIDs.
  105. ///
  106. /// If this method is called and there are inventory items, then we regard the inventory as having changed.
  107. /// </summary>
  108. /// <param name="linkNum">Link number for the part</param>
  109. public void ResetInventoryIDs()
  110. {
  111. if (null == m_part || null == m_part.ParentGroup)
  112. return;
  113. lock (m_items)
  114. {
  115. if (0 == m_items.Count)
  116. return;
  117. IList<TaskInventoryItem> items = GetInventoryItems();
  118. m_items.Clear();
  119. foreach (TaskInventoryItem item in items)
  120. {
  121. item.ResetIDs(m_part.UUID);
  122. m_items.Add(item.ItemID, item);
  123. }
  124. }
  125. }
  126. public void ResetObjectID()
  127. {
  128. lock (Items)
  129. {
  130. IList<TaskInventoryItem> items = new List<TaskInventoryItem>(Items.Values);
  131. Items.Clear();
  132. foreach (TaskInventoryItem item in items)
  133. {
  134. item.ParentPartID = m_part.UUID;
  135. item.ParentID = m_part.UUID;
  136. Items.Add(item.ItemID, item);
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Change every item in this inventory to a new owner.
  142. /// </summary>
  143. /// <param name="ownerId"></param>
  144. public void ChangeInventoryOwner(UUID ownerId)
  145. {
  146. lock (Items)
  147. {
  148. if (0 == Items.Count)
  149. {
  150. return;
  151. }
  152. }
  153. HasInventoryChanged = true;
  154. m_part.ParentGroup.HasGroupChanged = true;
  155. List<TaskInventoryItem> items = GetInventoryItems();
  156. foreach (TaskInventoryItem item in items)
  157. {
  158. if (ownerId != item.OwnerID)
  159. item.LastOwnerID = item.OwnerID;
  160. item.OwnerID = ownerId;
  161. item.PermsMask = 0;
  162. item.PermsGranter = UUID.Zero;
  163. item.OwnerChanged = true;
  164. }
  165. }
  166. /// <summary>
  167. /// Change every item in this inventory to a new group.
  168. /// </summary>
  169. /// <param name="groupID"></param>
  170. public void ChangeInventoryGroup(UUID groupID)
  171. {
  172. lock (Items)
  173. {
  174. if (0 == Items.Count)
  175. {
  176. return;
  177. }
  178. }
  179. // Don't let this set the HasGroupChanged flag for attachments
  180. // as this happens during rez and we don't want a new asset
  181. // for each attachment each time
  182. if (!m_part.ParentGroup.IsAttachment)
  183. {
  184. HasInventoryChanged = true;
  185. m_part.ParentGroup.HasGroupChanged = true;
  186. }
  187. List<TaskInventoryItem> items = GetInventoryItems();
  188. foreach (TaskInventoryItem item in items)
  189. {
  190. if (groupID != item.GroupID)
  191. item.GroupID = groupID;
  192. }
  193. }
  194. /// <summary>
  195. /// Start all the scripts contained in this prim's inventory
  196. /// </summary>
  197. public void CreateScriptInstances(int startParam, bool postOnRez, string engine, int stateSource)
  198. {
  199. List<TaskInventoryItem> scripts = GetInventoryScripts();
  200. foreach (TaskInventoryItem item in scripts)
  201. CreateScriptInstance(item, startParam, postOnRez, engine, stateSource);
  202. }
  203. public ArrayList GetScriptErrors(UUID itemID)
  204. {
  205. ArrayList ret = new ArrayList();
  206. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  207. if (engines == null) // No engine at all
  208. return ret;
  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 == null) // 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. if (m_inventoryFileName == String.Empty ||
  682. m_inventoryFileNameSerial < m_inventorySerial)
  683. {
  684. // Something changed, we need to create a new file
  685. m_inventoryFileName = "inventory_" + UUID.Random().ToString() + ".tmp";
  686. m_inventoryFileNameSerial = m_inventorySerial;
  687. InventoryStringBuilder invString = new InventoryStringBuilder(m_part.UUID, UUID.Zero);
  688. lock (m_items)
  689. {
  690. foreach (TaskInventoryItem item in m_items.Values)
  691. {
  692. UUID ownerID = item.OwnerID;
  693. uint everyoneMask = 0;
  694. uint baseMask = item.BasePermissions;
  695. uint ownerMask = item.CurrentPermissions;
  696. uint groupMask = item.GroupPermissions;
  697. invString.AddItemStart();
  698. invString.AddNameValueLine("item_id", item.ItemID.ToString());
  699. invString.AddNameValueLine("parent_id", m_part.UUID.ToString());
  700. invString.AddPermissionsStart();
  701. invString.AddNameValueLine("base_mask", Utils.UIntToHexString(baseMask));
  702. invString.AddNameValueLine("owner_mask", Utils.UIntToHexString(ownerMask));
  703. invString.AddNameValueLine("group_mask", Utils.UIntToHexString(groupMask));
  704. invString.AddNameValueLine("everyone_mask", Utils.UIntToHexString(everyoneMask));
  705. invString.AddNameValueLine("next_owner_mask", Utils.UIntToHexString(item.NextPermissions));
  706. invString.AddNameValueLine("creator_id", item.CreatorID.ToString());
  707. invString.AddNameValueLine("owner_id", ownerID.ToString());
  708. invString.AddNameValueLine("last_owner_id", item.LastOwnerID.ToString());
  709. invString.AddNameValueLine("group_id", item.GroupID.ToString());
  710. invString.AddSectionEnd();
  711. invString.AddNameValueLine("asset_id", item.AssetID.ToString());
  712. invString.AddNameValueLine("type", TaskInventoryItem.Types[item.Type]);
  713. invString.AddNameValueLine("inv_type", TaskInventoryItem.InvTypes[item.InvType]);
  714. invString.AddNameValueLine("flags", Utils.UIntToHexString(item.Flags));
  715. invString.AddSaleStart();
  716. invString.AddNameValueLine("sale_type", "not");
  717. invString.AddNameValueLine("sale_price", "0");
  718. invString.AddSectionEnd();
  719. invString.AddNameValueLine("name", item.Name + "|");
  720. invString.AddNameValueLine("desc", item.Description + "|");
  721. invString.AddNameValueLine("creation_date", item.CreationDate.ToString());
  722. invString.AddSectionEnd();
  723. }
  724. }
  725. m_inventoryFileData = Utils.StringToBytes(invString.BuildString);
  726. return true;
  727. }
  728. // No need to recreate, the existing file is fine
  729. return false;
  730. }
  731. /// <summary>
  732. /// Serialize all the metadata for the items in this prim's inventory ready for sending to the client
  733. /// </summary>
  734. /// <param name="xferManager"></param>
  735. public void RequestInventoryFile(IClientAPI client, IXfer xferManager)
  736. {
  737. CreateInventoryFile();
  738. if (m_inventorySerial == 0) // No inventory
  739. {
  740. client.SendTaskInventory(m_part.UUID, 0, new byte[0]);
  741. return;
  742. }
  743. // In principle, we should only do the rest if the inventory changed;
  744. // by sending m_inventorySerial to the client, it ought to know
  745. // that nothing changed and that it doesn't need to request the file.
  746. // Unfortunately, it doesn't look like the client optimizes this;
  747. // the client seems to always come back and request the Xfer,
  748. // no matter what value m_inventorySerial has.
  749. if (m_inventoryFileData.Length > 2)
  750. // Add the file for Xfer
  751. xferManager.AddNewFile(m_inventoryFileName, m_inventoryFileData);
  752. // Tell the client we're ready to Xfer the file
  753. client.SendTaskInventory(m_part.UUID, (short)m_inventorySerial,
  754. Util.StringToBytes256(m_inventoryFileName));
  755. }
  756. /// <summary>
  757. /// Process inventory backup
  758. /// </summary>
  759. /// <param name="datastore"></param>
  760. public void ProcessInventoryBackup(ISimulationDataService datastore)
  761. {
  762. if (HasInventoryChanged)
  763. {
  764. HasInventoryChanged = false;
  765. List<TaskInventoryItem> items = GetInventoryItems();
  766. datastore.StorePrimInventory(m_part.UUID, items);
  767. }
  768. }
  769. public class InventoryStringBuilder
  770. {
  771. public string BuildString = String.Empty;
  772. public InventoryStringBuilder(UUID folderID, UUID parentID)
  773. {
  774. BuildString += "\tinv_object\t0\n\t{\n";
  775. AddNameValueLine("obj_id", folderID.ToString());
  776. AddNameValueLine("parent_id", parentID.ToString());
  777. AddNameValueLine("type", "category");
  778. AddNameValueLine("name", "Contents|");
  779. AddSectionEnd();
  780. }
  781. public void AddItemStart()
  782. {
  783. BuildString += "\tinv_item\t0\n";
  784. AddSectionStart();
  785. }
  786. public void AddPermissionsStart()
  787. {
  788. BuildString += "\tpermissions 0\n";
  789. AddSectionStart();
  790. }
  791. public void AddSaleStart()
  792. {
  793. BuildString += "\tsale_info\t0\n";
  794. AddSectionStart();
  795. }
  796. protected void AddSectionStart()
  797. {
  798. BuildString += "\t{\n";
  799. }
  800. public void AddSectionEnd()
  801. {
  802. BuildString += "\t}\n";
  803. }
  804. public void AddLine(string addLine)
  805. {
  806. BuildString += addLine;
  807. }
  808. public void AddNameValueLine(string name, string value)
  809. {
  810. BuildString += "\t\t";
  811. BuildString += name + "\t";
  812. BuildString += value + "\n";
  813. }
  814. public void Close()
  815. {
  816. }
  817. }
  818. public uint MaskEffectivePermissions()
  819. {
  820. uint mask=0x7fffffff;
  821. lock (m_items)
  822. {
  823. foreach (TaskInventoryItem item in m_items.Values)
  824. {
  825. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Copy) == 0)
  826. mask &= ~((uint)PermissionMask.Copy >> 13);
  827. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Transfer) == 0)
  828. mask &= ~((uint)PermissionMask.Transfer >> 13);
  829. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Modify) == 0)
  830. mask &= ~((uint)PermissionMask.Modify >> 13);
  831. if (item.InvType != (int)InventoryType.Object)
  832. {
  833. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Copy) == 0)
  834. mask &= ~((uint)PermissionMask.Copy >> 13);
  835. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Transfer) == 0)
  836. mask &= ~((uint)PermissionMask.Transfer >> 13);
  837. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Modify) == 0)
  838. mask &= ~((uint)PermissionMask.Modify >> 13);
  839. }
  840. else
  841. {
  842. if ((item.CurrentPermissions & ((uint)PermissionMask.Copy >> 13)) == 0)
  843. mask &= ~((uint)PermissionMask.Copy >> 13);
  844. if ((item.CurrentPermissions & ((uint)PermissionMask.Transfer >> 13)) == 0)
  845. mask &= ~((uint)PermissionMask.Transfer >> 13);
  846. if ((item.CurrentPermissions & ((uint)PermissionMask.Modify >> 13)) == 0)
  847. mask &= ~((uint)PermissionMask.Modify >> 13);
  848. }
  849. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  850. mask &= ~(uint)PermissionMask.Copy;
  851. if ((item.CurrentPermissions & (uint)PermissionMask.Transfer) == 0)
  852. mask &= ~(uint)PermissionMask.Transfer;
  853. if ((item.CurrentPermissions & (uint)PermissionMask.Modify) == 0)
  854. mask &= ~(uint)PermissionMask.Modify;
  855. }
  856. }
  857. return mask;
  858. }
  859. public void ApplyNextOwnerPermissions()
  860. {
  861. lock (m_items)
  862. {
  863. foreach (TaskInventoryItem item in m_items.Values)
  864. {
  865. if (item.InvType == (int)InventoryType.Object && (item.CurrentPermissions & 7) != 0)
  866. {
  867. if ((item.CurrentPermissions & ((uint)PermissionMask.Copy >> 13)) == 0)
  868. item.CurrentPermissions &= ~(uint)PermissionMask.Copy;
  869. if ((item.CurrentPermissions & ((uint)PermissionMask.Transfer >> 13)) == 0)
  870. item.CurrentPermissions &= ~(uint)PermissionMask.Transfer;
  871. if ((item.CurrentPermissions & ((uint)PermissionMask.Modify >> 13)) == 0)
  872. item.CurrentPermissions &= ~(uint)PermissionMask.Modify;
  873. }
  874. item.CurrentPermissions &= item.NextPermissions;
  875. item.BasePermissions &= item.NextPermissions;
  876. item.EveryonePermissions &= item.NextPermissions;
  877. item.OwnerChanged = true;
  878. item.PermsMask = 0;
  879. item.PermsGranter = UUID.Zero;
  880. }
  881. }
  882. }
  883. public void ApplyGodPermissions(uint perms)
  884. {
  885. lock (m_items)
  886. {
  887. foreach (TaskInventoryItem item in m_items.Values)
  888. {
  889. item.CurrentPermissions = perms;
  890. item.BasePermissions = perms;
  891. }
  892. }
  893. m_inventorySerial++;
  894. HasInventoryChanged = true;
  895. }
  896. public bool ContainsScripts()
  897. {
  898. lock (m_items)
  899. {
  900. foreach (TaskInventoryItem item in m_items.Values)
  901. {
  902. if (item.InvType == (int)InventoryType.LSL)
  903. {
  904. return true;
  905. }
  906. }
  907. }
  908. return false;
  909. }
  910. public List<UUID> GetInventoryList()
  911. {
  912. List<UUID> ret = new List<UUID>();
  913. lock (m_items)
  914. {
  915. foreach (TaskInventoryItem item in m_items.Values)
  916. ret.Add(item.ItemID);
  917. }
  918. return ret;
  919. }
  920. public List<TaskInventoryItem> GetInventoryItems()
  921. {
  922. List<TaskInventoryItem> ret = new List<TaskInventoryItem>();
  923. lock (m_items)
  924. ret = new List<TaskInventoryItem>(m_items.Values);
  925. return ret;
  926. }
  927. public List<TaskInventoryItem> GetInventoryScripts()
  928. {
  929. List<TaskInventoryItem> ret = new List<TaskInventoryItem>();
  930. lock (m_items)
  931. {
  932. foreach (TaskInventoryItem item in m_items.Values)
  933. if (item.InvType == (int)InventoryType.LSL)
  934. ret.Add(item);
  935. }
  936. return ret;
  937. }
  938. public Dictionary<UUID, string> GetScriptStates()
  939. {
  940. Dictionary<UUID, string> ret = new Dictionary<UUID, string>();
  941. if (m_part.ParentGroup.Scene == null) // Group not in a scene
  942. return ret;
  943. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  944. if (engines == null) // No engine at all
  945. return ret;
  946. List<TaskInventoryItem> scripts = GetInventoryScripts();
  947. foreach (TaskInventoryItem item in scripts)
  948. {
  949. foreach (IScriptModule e in engines)
  950. {
  951. if (e != null)
  952. {
  953. string n = e.GetXMLState(item.ItemID);
  954. if (n != String.Empty)
  955. {
  956. if (!ret.ContainsKey(item.ItemID))
  957. ret[item.ItemID] = n;
  958. break;
  959. }
  960. }
  961. }
  962. }
  963. return ret;
  964. }
  965. public void ResumeScripts()
  966. {
  967. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  968. if (engines == null)
  969. return;
  970. List<TaskInventoryItem> scripts = GetInventoryScripts();
  971. foreach (TaskInventoryItem item in scripts)
  972. {
  973. foreach (IScriptModule engine in engines)
  974. {
  975. if (engine != null)
  976. {
  977. if (item.OwnerChanged)
  978. engine.PostScriptEvent(item.ItemID, "changed", new Object[] { (int)Changed.OWNER });
  979. item.OwnerChanged = false;
  980. engine.ResumeScript(item.ItemID);
  981. }
  982. }
  983. }
  984. }
  985. }
  986. }