SceneObjectPartInventory.cs 33 KB

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