1
0

SceneObjectPartInventory.cs 35 KB

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