SceneObjectPartInventory.cs 36 KB

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