SceneObjectPartInventory.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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.Interfaces;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.Environment.Scenes.Scripting;
  37. namespace OpenSim.Region.Environment.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. AssetCache cache = m_part.ParentGroup.Scene.AssetCache;
  232. cache.GetAsset(item.AssetID, delegate(UUID assetID, AssetBase asset)
  233. {
  234. if (null == asset)
  235. {
  236. m_log.ErrorFormat(
  237. "[PRIM INVENTORY]: " +
  238. "Couldn't start script {0}, {1} since asset ID {2} could not be found",
  239. item.Name, item.ItemID, item.AssetID);
  240. }
  241. else
  242. {
  243. m_items[item.ItemID].PermsMask = 0;
  244. m_items[item.ItemID].PermsGranter = UUID.Zero;
  245. string script = Utils.BytesToString(asset.Data);
  246. m_part.ParentGroup.Scene.EventManager.TriggerRezScript(
  247. m_part.LocalId, item.ItemID, script, startParam, postOnRez, engine, stateSource);
  248. m_part.ParentGroup.AddActiveScriptCount(1);
  249. m_part.ScheduleFullUpdate();
  250. }
  251. }, false);
  252. }
  253. }
  254. /// <summary>
  255. /// Start a script which is in this prim's inventory.
  256. /// </summary>
  257. /// <param name="itemId">
  258. /// A <see cref="UUID"/>
  259. /// </param>
  260. public void CreateScriptInstance(UUID itemId, int startParam, bool postOnRez, string engine, int stateSource)
  261. {
  262. lock (m_items)
  263. {
  264. if (m_items.ContainsKey(itemId))
  265. {
  266. CreateScriptInstance(m_items[itemId], startParam, postOnRez, engine, stateSource);
  267. }
  268. else
  269. {
  270. m_log.ErrorFormat(
  271. "[PRIM INVENTORY]: " +
  272. "Couldn't start script with ID {0} since it couldn't be found for prim {1}, {2}",
  273. itemId, m_part.Name, m_part.UUID);
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// Stop a script which is in this prim's inventory.
  279. /// </summary>
  280. /// <param name="itemId"></param>
  281. public void RemoveScriptInstance(UUID itemId)
  282. {
  283. if (m_items.ContainsKey(itemId))
  284. {
  285. m_part.ParentGroup.Scene.EventManager.TriggerRemoveScript(m_part.LocalId, itemId);
  286. m_part.ParentGroup.AddActiveScriptCount(-1);
  287. }
  288. else
  289. {
  290. m_log.ErrorFormat(
  291. "[PRIM INVENTORY]: " +
  292. "Couldn't stop script with ID {0} since it couldn't be found for prim {1}, {2}",
  293. itemId, m_part.Name, m_part.UUID);
  294. }
  295. }
  296. /// <summary>
  297. /// Check if the inventory holds an item with a given name.
  298. /// This method assumes that the task inventory is already locked.
  299. /// </summary>
  300. /// <param name="name"></param>
  301. /// <returns></returns>
  302. private bool InventoryContainsName(string name)
  303. {
  304. foreach (TaskInventoryItem item in Items.Values)
  305. {
  306. if (item.Name == name)
  307. return true;
  308. }
  309. return false;
  310. }
  311. /// <summary>
  312. /// For a given item name, return that name if it is available. Otherwise, return the next available
  313. /// similar name (which is currently the original name with the next available numeric suffix).
  314. /// </summary>
  315. /// <param name="name"></param>
  316. /// <returns></returns>
  317. private string FindAvailableInventoryName(string name)
  318. {
  319. if (!InventoryContainsName(name))
  320. return name;
  321. int suffix=1;
  322. while (suffix < 256)
  323. {
  324. string tryName=String.Format("{0} {1}", name, suffix);
  325. if (!InventoryContainsName(tryName))
  326. return tryName;
  327. suffix++;
  328. }
  329. return String.Empty;
  330. }
  331. /// <summary>
  332. /// Add an item to this prim's inventory. If an item with the same name already exists, then an alternative
  333. /// name is chosen.
  334. /// </summary>
  335. /// <param name="item"></param>
  336. public void AddInventoryItem(TaskInventoryItem item, bool allowedDrop)
  337. {
  338. AddInventoryItem(item.Name, item, allowedDrop);
  339. }
  340. /// <summary>
  341. /// Add an item to this prim's inventory. If an item with the same name already exists, it is replaced.
  342. /// </summary>
  343. /// <param name="item"></param>
  344. public void AddInventoryItemExclusive(TaskInventoryItem item, bool allowedDrop)
  345. {
  346. List<TaskInventoryItem> il = new List<TaskInventoryItem>(m_items.Values);
  347. foreach (TaskInventoryItem i in il)
  348. {
  349. if (i.Name == item.Name)
  350. {
  351. if (i.InvType == (int)InventoryType.LSL)
  352. RemoveScriptInstance(i.ItemID);
  353. RemoveInventoryItem(i.ItemID);
  354. break;
  355. }
  356. }
  357. AddInventoryItem(item.Name, item, allowedDrop);
  358. }
  359. /// <summary>
  360. /// Add an item to this prim's inventory.
  361. /// </summary>
  362. /// <param name="name">The name that the new item should have.</param>
  363. /// <param name="item">
  364. /// The item itself. The name within this structure is ignored in favour of the name
  365. /// given in this method's arguments
  366. /// </param>
  367. /// <param name="allowedDrop">
  368. /// Item was only added to inventory because AllowedDrop is set
  369. /// </param>
  370. protected void AddInventoryItem(string name, TaskInventoryItem item, bool allowedDrop)
  371. {
  372. name = FindAvailableInventoryName(name);
  373. if (name == String.Empty)
  374. return;
  375. item.ParentID = m_part.UUID;
  376. item.ParentPartID = m_part.UUID;
  377. item.Name = name;
  378. lock (m_items)
  379. {
  380. m_items.Add(item.ItemID, item);
  381. if (allowedDrop)
  382. m_part.TriggerScriptChangedEvent(Changed.ALLOWED_DROP);
  383. else
  384. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  385. }
  386. m_inventorySerial++;
  387. //m_inventorySerial += 2;
  388. HasInventoryChanged = true;
  389. m_part.ParentGroup.HasGroupChanged = true;
  390. }
  391. /// <summary>
  392. /// Restore a whole collection of items to the prim's inventory at once.
  393. /// We assume that the items already have all their fields correctly filled out.
  394. /// The items are not flagged for persistence to the database, since they are being restored
  395. /// from persistence rather than being newly added.
  396. /// </summary>
  397. /// <param name="items"></param>
  398. public void RestoreInventoryItems(ICollection<TaskInventoryItem> items)
  399. {
  400. lock (m_items)
  401. {
  402. foreach (TaskInventoryItem item in items)
  403. {
  404. m_items.Add(item.ItemID, item);
  405. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  406. }
  407. }
  408. m_inventorySerial++;
  409. }
  410. /// <summary>
  411. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  412. /// </summary>
  413. /// <param name="itemID"></param>
  414. /// <returns>null if the item does not exist</returns>
  415. public TaskInventoryItem GetInventoryItem(UUID itemId)
  416. {
  417. TaskInventoryItem item;
  418. m_items.TryGetValue(itemId, out item);
  419. return item;
  420. }
  421. /// <summary>
  422. /// Update an existing inventory item.
  423. /// </summary>
  424. /// <param name="item">The updated item. An item with the same id must already exist
  425. /// in this prim's inventory.</param>
  426. /// <returns>false if the item did not exist, true if the update occurred successfully</returns>
  427. public bool UpdateInventoryItem(TaskInventoryItem item)
  428. {
  429. lock (m_items)
  430. {
  431. if (m_items.ContainsKey(item.ItemID))
  432. {
  433. item.ParentID = m_part.UUID;
  434. item.ParentPartID = m_part.UUID;
  435. item.Flags = m_items[item.ItemID].Flags;
  436. if (item.AssetID == UUID.Zero)
  437. {
  438. item.AssetID = m_items[item.ItemID].AssetID;
  439. }
  440. else if ((InventoryType)item.Type == InventoryType.Notecard)
  441. {
  442. ScenePresence presence = m_part.ParentGroup.Scene.GetScenePresence(item.OwnerID);
  443. if (presence != null)
  444. {
  445. presence.ControllingClient.SendAgentAlertMessage(
  446. "Notecard saved", false);
  447. }
  448. }
  449. m_items[item.ItemID] = item;
  450. m_inventorySerial++;
  451. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  452. HasInventoryChanged = true;
  453. m_part.ParentGroup.HasGroupChanged = true;
  454. return true;
  455. }
  456. else
  457. {
  458. m_log.ErrorFormat(
  459. "[PRIM INVENTORY]: " +
  460. "Tried to retrieve item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
  461. item.ItemID, m_part.Name, m_part.UUID);
  462. }
  463. }
  464. return false;
  465. }
  466. /// <summary>
  467. /// Remove an item from this prim's inventory
  468. /// </summary>
  469. /// <param name="itemID"></param>
  470. /// <returns>Numeric asset type of the item removed. Returns -1 if the item did not exist
  471. /// in this prim's inventory.</returns>
  472. public int RemoveInventoryItem(UUID itemID)
  473. {
  474. lock (m_items)
  475. {
  476. if (m_items.ContainsKey(itemID))
  477. {
  478. int type = m_items[itemID].InvType;
  479. if (type == 10) // Script
  480. {
  481. m_part.ParentGroup.Scene.EventManager.TriggerRemoveScript(m_part.LocalId, itemID);
  482. }
  483. m_items.Remove(itemID);
  484. m_inventorySerial++;
  485. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  486. HasInventoryChanged = true;
  487. m_part.ParentGroup.HasGroupChanged = true;
  488. int scriptcount = 0;
  489. lock (m_items)
  490. {
  491. foreach (TaskInventoryItem item in m_items.Values)
  492. {
  493. if (item.Type == 10)
  494. {
  495. scriptcount++;
  496. }
  497. }
  498. }
  499. if (scriptcount <= 0)
  500. {
  501. m_part.RemFlag(PrimFlags.Scripted);
  502. }
  503. m_part.ScheduleFullUpdate();
  504. return type;
  505. }
  506. else
  507. {
  508. m_log.ErrorFormat(
  509. "[PRIM INVENTORY]: " +
  510. "Tried to remove item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
  511. itemID, m_part.Name, m_part.UUID);
  512. }
  513. }
  514. return -1;
  515. }
  516. public string GetInventoryFileName()
  517. {
  518. if (m_inventoryFileName == String.Empty)
  519. m_inventoryFileName = "inventory_" + UUID.Random().ToString() + ".tmp";
  520. if (m_inventoryFileNameSerial < m_inventorySerial)
  521. {
  522. m_inventoryFileName = "inventory_" + UUID.Random().ToString() + ".tmp";
  523. }
  524. return m_inventoryFileName;
  525. }
  526. /// <summary>
  527. /// Return the name with which a client can request a xfer of this prim's inventory metadata
  528. /// </summary>
  529. /// <param name="client"></param>
  530. /// <param name="localID"></param>
  531. public bool GetInventoryFileName(IClientAPI client, uint localID)
  532. {
  533. // m_log.DebugFormat(
  534. // "[PRIM INVENTORY]: Received request from client {0} for inventory file name of {1}, {2}",
  535. // client.AgentId, Name, UUID);
  536. if (m_inventorySerial > 0)
  537. {
  538. client.SendTaskInventory(m_part.UUID, (short)m_inventorySerial,
  539. Utils.StringToBytes(GetInventoryFileName()));
  540. return true;
  541. }
  542. else
  543. {
  544. client.SendTaskInventory(m_part.UUID, 0, new byte[0]);
  545. return false;
  546. }
  547. }
  548. /// <summary>
  549. /// Serialize all the metadata for the items in this prim's inventory ready for sending to the client
  550. /// </summary>
  551. /// <param name="xferManager"></param>
  552. public void RequestInventoryFile(IClientAPI client, IXfer xferManager)
  553. {
  554. byte[] fileData = new byte[0];
  555. // Confusingly, the folder item has to be the object id, while the 'parent id' has to be zero. This matches
  556. // what appears to happen in the Second Life protocol. If this isn't the case. then various functionality
  557. // isn't available (such as drag from prim inventory to agent inventory)
  558. InventoryStringBuilder invString = new InventoryStringBuilder(m_part.UUID, UUID.Zero);
  559. lock (m_items)
  560. {
  561. foreach (TaskInventoryItem item in m_items.Values)
  562. {
  563. UUID ownerID = item.OwnerID;
  564. uint everyoneMask = 0;
  565. uint baseMask = item.BasePermissions;
  566. uint ownerMask = item.CurrentPermissions;
  567. invString.AddItemStart();
  568. invString.AddNameValueLine("item_id", item.ItemID.ToString());
  569. invString.AddNameValueLine("parent_id", m_part.UUID.ToString());
  570. invString.AddPermissionsStart();
  571. invString.AddNameValueLine("base_mask", Utils.UIntToHexString(baseMask));
  572. invString.AddNameValueLine("owner_mask", Utils.UIntToHexString(ownerMask));
  573. invString.AddNameValueLine("group_mask", Utils.UIntToHexString(0));
  574. invString.AddNameValueLine("everyone_mask", Utils.UIntToHexString(everyoneMask));
  575. invString.AddNameValueLine("next_owner_mask", Utils.UIntToHexString(item.NextPermissions));
  576. invString.AddNameValueLine("creator_id", item.CreatorID.ToString());
  577. invString.AddNameValueLine("owner_id", ownerID.ToString());
  578. invString.AddNameValueLine("last_owner_id", item.LastOwnerID.ToString());
  579. invString.AddNameValueLine("group_id", item.GroupID.ToString());
  580. invString.AddSectionEnd();
  581. invString.AddNameValueLine("asset_id", item.AssetID.ToString());
  582. invString.AddNameValueLine("type", TaskInventoryItem.Types[item.Type]);
  583. invString.AddNameValueLine("inv_type", TaskInventoryItem.InvTypes[item.InvType]);
  584. invString.AddNameValueLine("flags", Utils.UIntToHexString(item.Flags));
  585. invString.AddSaleStart();
  586. invString.AddNameValueLine("sale_type", "not");
  587. invString.AddNameValueLine("sale_price", "0");
  588. invString.AddSectionEnd();
  589. invString.AddNameValueLine("name", item.Name + "|");
  590. invString.AddNameValueLine("desc", item.Description + "|");
  591. invString.AddNameValueLine("creation_date", item.CreationDate.ToString());
  592. invString.AddSectionEnd();
  593. }
  594. }
  595. fileData = Utils.StringToBytes(invString.BuildString);
  596. //Console.WriteLine(Utils.BytesToString(fileData));
  597. //m_log.Debug("[PRIM INVENTORY]: RequestInventoryFile fileData: " + Utils.BytesToString(fileData));
  598. if (fileData.Length > 2)
  599. {
  600. xferManager.AddNewFile(m_inventoryFileName, fileData);
  601. }
  602. }
  603. /// <summary>
  604. /// Process inventory backup
  605. /// </summary>
  606. /// <param name="datastore"></param>
  607. public void ProcessInventoryBackup(IRegionDataStore datastore)
  608. {
  609. if (HasInventoryChanged)
  610. {
  611. lock (Items)
  612. {
  613. datastore.StorePrimInventory(m_part.UUID, Items.Values);
  614. }
  615. HasInventoryChanged = false;
  616. }
  617. }
  618. public class InventoryStringBuilder
  619. {
  620. public string BuildString = String.Empty;
  621. public InventoryStringBuilder(UUID folderID, UUID parentID)
  622. {
  623. BuildString += "\tinv_object\t0\n\t{\n";
  624. AddNameValueLine("obj_id", folderID.ToString());
  625. AddNameValueLine("parent_id", parentID.ToString());
  626. AddNameValueLine("type", "category");
  627. AddNameValueLine("name", "Contents|");
  628. AddSectionEnd();
  629. }
  630. public void AddItemStart()
  631. {
  632. BuildString += "\tinv_item\t0\n";
  633. AddSectionStart();
  634. }
  635. public void AddPermissionsStart()
  636. {
  637. BuildString += "\tpermissions 0\n";
  638. AddSectionStart();
  639. }
  640. public void AddSaleStart()
  641. {
  642. BuildString += "\tsale_info\t0\n";
  643. AddSectionStart();
  644. }
  645. protected void AddSectionStart()
  646. {
  647. BuildString += "\t{\n";
  648. }
  649. public void AddSectionEnd()
  650. {
  651. BuildString += "\t}\n";
  652. }
  653. public void AddLine(string addLine)
  654. {
  655. BuildString += addLine;
  656. }
  657. public void AddNameValueLine(string name, string value)
  658. {
  659. BuildString += "\t\t";
  660. BuildString += name + "\t";
  661. BuildString += value + "\n";
  662. }
  663. public void Close()
  664. {
  665. }
  666. }
  667. public uint MaskEffectivePermissions()
  668. {
  669. uint mask=0x7fffffff;
  670. foreach (TaskInventoryItem item in m_items.Values)
  671. {
  672. if (item.InvType != 6)
  673. {
  674. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Copy) == 0)
  675. mask &= ~((uint)PermissionMask.Copy >> 13);
  676. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Transfer) == 0)
  677. mask &= ~((uint)PermissionMask.Transfer >> 13);
  678. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Modify) == 0)
  679. mask &= ~((uint)PermissionMask.Modify >> 13);
  680. }
  681. else
  682. {
  683. if ((item.CurrentPermissions & ((uint)PermissionMask.Copy >> 13)) == 0)
  684. mask &= ~((uint)PermissionMask.Copy >> 13);
  685. if ((item.CurrentPermissions & ((uint)PermissionMask.Transfer >> 13)) == 0)
  686. mask &= ~((uint)PermissionMask.Transfer >> 13);
  687. if ((item.CurrentPermissions & ((uint)PermissionMask.Modify >> 13)) == 0)
  688. mask &= ~((uint)PermissionMask.Modify >> 13);
  689. }
  690. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  691. mask &= ~(uint)PermissionMask.Copy;
  692. if ((item.CurrentPermissions & (uint)PermissionMask.Transfer) == 0)
  693. mask &= ~(uint)PermissionMask.Transfer;
  694. if ((item.CurrentPermissions & (uint)PermissionMask.Modify) == 0)
  695. mask &= ~(uint)PermissionMask.Modify;
  696. }
  697. return mask;
  698. }
  699. public void ApplyNextOwnerPermissions()
  700. {
  701. foreach (TaskInventoryItem item in m_items.Values)
  702. {
  703. if (item.InvType == 6 && (item.CurrentPermissions & 7) != 0)
  704. {
  705. if ((item.CurrentPermissions & ((uint)PermissionMask.Copy >> 13)) == 0)
  706. item.CurrentPermissions &= ~(uint)PermissionMask.Copy;
  707. if ((item.CurrentPermissions & ((uint)PermissionMask.Transfer >> 13)) == 0)
  708. item.CurrentPermissions &= ~(uint)PermissionMask.Transfer;
  709. if ((item.CurrentPermissions & ((uint)PermissionMask.Modify >> 13)) == 0)
  710. item.CurrentPermissions &= ~(uint)PermissionMask.Modify;
  711. item.CurrentPermissions |= 8;
  712. }
  713. item.CurrentPermissions &= item.NextPermissions;
  714. item.BasePermissions &= item.NextPermissions;
  715. item.EveryonePermissions &= item.NextPermissions;
  716. }
  717. m_part.TriggerScriptChangedEvent(Changed.OWNER);
  718. }
  719. public void ApplyGodPermissions(uint perms)
  720. {
  721. foreach (TaskInventoryItem item in m_items.Values)
  722. {
  723. item.CurrentPermissions = perms;
  724. item.BasePermissions = perms;
  725. }
  726. }
  727. public bool ContainsScripts()
  728. {
  729. foreach (TaskInventoryItem item in m_items.Values)
  730. {
  731. if (item.InvType == 10)
  732. {
  733. return true;
  734. }
  735. }
  736. return false;
  737. }
  738. public List<UUID> GetInventoryList()
  739. {
  740. List<UUID> ret = new List<UUID>();
  741. foreach (TaskInventoryItem item in m_items.Values)
  742. ret.Add(item.ItemID);
  743. return ret;
  744. }
  745. public string[] GetScriptAssemblies()
  746. {
  747. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  748. List<string> ret = new List<string>();
  749. foreach (TaskInventoryItem item in m_items.Values)
  750. {
  751. if (item.InvType == 10)
  752. {
  753. foreach (IScriptModule e in engines)
  754. {
  755. string n = e.GetAssemblyName(item.ItemID);
  756. if (n != "")
  757. {
  758. if (!ret.Contains(n))
  759. ret.Add(n);
  760. break;
  761. }
  762. }
  763. }
  764. }
  765. return ret.ToArray();
  766. }
  767. public Dictionary<UUID, string> GetScriptStates()
  768. {
  769. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  770. Dictionary<UUID, string> ret = new Dictionary<UUID, string>();
  771. foreach (TaskInventoryItem item in m_items.Values)
  772. {
  773. if (item.InvType == 10)
  774. {
  775. foreach (IScriptModule e in engines)
  776. {
  777. string n = e.GetXMLState(item.ItemID);
  778. if (n != "")
  779. {
  780. if (!ret.ContainsKey(item.ItemID))
  781. ret[item.ItemID] = n;
  782. break;
  783. }
  784. }
  785. }
  786. }
  787. return ret;
  788. }
  789. }
  790. }