SceneObjectPartInventory.cs 38 KB

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