SceneObjectPartInventory.cs 36 KB

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