SceneObjectPart.Inventory.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Xml.Serialization;
  31. using libsecondlife;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Region.Environment.Interfaces;
  35. using OpenSim.Region.Environment.Scenes.Scripting;
  36. namespace OpenSim.Region.Environment.Scenes
  37. {
  38. public partial class SceneObjectPart : IScriptHost
  39. {
  40. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  41. private string m_inventoryFileName = String.Empty;
  42. /// <summary>
  43. /// The inventory folder for this prim
  44. /// </summary>
  45. private LLUUID m_folderID = LLUUID.Zero;
  46. /// <summary>
  47. /// Exposing this is not particularly good, but it's one of the least evils at the moment to see
  48. /// folder id from prim inventory item data, since it's not (yet) actually stored with the prim.
  49. /// </summary>
  50. public LLUUID FolderID
  51. {
  52. get { return m_folderID; }
  53. set { m_folderID = value; }
  54. }
  55. /// <summary>
  56. /// Serial count for inventory file , used to tell if inventory has changed
  57. /// no need for this to be part of Database backup
  58. /// </summary>
  59. protected uint m_inventorySerial = 0;
  60. public uint InventorySerial
  61. {
  62. get { return m_inventorySerial; }
  63. set { m_inventorySerial = value; }
  64. }
  65. /// <summary>
  66. /// Holds in memory prim inventory
  67. /// </summary>
  68. protected TaskInventoryDictionary m_taskInventory = new TaskInventoryDictionary();
  69. public TaskInventoryDictionary TaskInventory
  70. {
  71. get { return m_taskInventory; }
  72. set { m_taskInventory = value; }
  73. }
  74. /// <summary>
  75. /// Tracks whether inventory has changed since the last persistent backup
  76. /// </summary>
  77. private bool HasInventoryChanged;
  78. /// <summary>
  79. /// Reset LLUUIDs for all the items in the prim's inventory. This involves either generating
  80. /// new ones or setting existing UUIDs to the correct parent UUIDs
  81. /// </summary>
  82. /// <param name="linkNum'>Link number for the part</param>
  83. public void ResetInventoryIDs()
  84. {
  85. lock (TaskInventory)
  86. {
  87. IList<TaskInventoryItem> items = new List<TaskInventoryItem>(TaskInventory.Values);
  88. TaskInventory.Clear();
  89. foreach (TaskInventoryItem item in items)
  90. {
  91. item.ResetIDs(UUID);
  92. TaskInventory.Add(item.ItemID, item);
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Start all the scripts contained in this prim's inventory
  98. /// </summary>
  99. public void StartScripts()
  100. {
  101. lock (m_taskInventory)
  102. {
  103. foreach (TaskInventoryItem item in m_taskInventory.Values)
  104. {
  105. // XXX more hardcoding badness. Should be an enum in TaskInventoryItem
  106. if (10 == item.Type)
  107. {
  108. StartScript(item);
  109. }
  110. }
  111. }
  112. }
  113. public void StopScripts()
  114. {
  115. lock (m_taskInventory)
  116. {
  117. foreach (TaskInventoryItem item in m_taskInventory.Values)
  118. {
  119. if (10 == item.Type)
  120. {
  121. StopScript(item.ItemID);
  122. }
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// Start a script which is in this prim's inventory.
  128. /// </summary>
  129. /// <param name="item"></param>
  130. /// <returns></returns>
  131. public void StartScript(TaskInventoryItem item)
  132. {
  133. // m_log.InfoFormat(
  134. // "[PRIMINVENTORY]: " +
  135. // "Starting script {0}, {1} in prim {2}, {3}",
  136. // item.Name, item.ItemID, Name, UUID);
  137. AddFlag(LLObject.ObjectFlags.Scripted);
  138. if (!((m_parentGroup.Scene.RegionInfo.EstateSettings.regionFlags & Simulator.RegionFlags.SkipScripts) == Simulator.RegionFlags.SkipScripts))
  139. {
  140. AssetBase rezAsset = m_parentGroup.Scene.AssetCache.GetAsset(item.AssetID, false);
  141. if (rezAsset != null)
  142. {
  143. string script = Helpers.FieldToUTF8String(rezAsset.Data);
  144. m_parentGroup.Scene.EventManager.TriggerRezScript(LocalID, item.ItemID, script);
  145. m_parentGroup.AddActiveScriptCount(1);
  146. }
  147. else
  148. {
  149. m_log.ErrorFormat(
  150. "[PRIMINVENTORY]: " +
  151. "Couldn't start script {0}, {1} since asset ID {2} could not be found",
  152. item.Name, item.ItemID, item.AssetID);
  153. }
  154. }
  155. ScheduleFullUpdate();
  156. }
  157. /// <summary>
  158. /// Start a script which is in this prim's inventory.
  159. /// </summary>
  160. /// <param name="itemId">
  161. /// A <see cref="LLUUID"/>
  162. /// </param>
  163. public void StartScript(LLUUID itemId)
  164. {
  165. lock (m_taskInventory)
  166. {
  167. if (m_taskInventory.ContainsKey(itemId))
  168. {
  169. StartScript(m_taskInventory[itemId]);
  170. }
  171. else
  172. {
  173. m_log.ErrorFormat(
  174. "[PRIMINVENTORY]: " +
  175. "Couldn't start script with ID {0} since it couldn't be found for prim {1}, {2}",
  176. itemId, Name, UUID);
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// Stop a script which is in this prim's inventory.
  182. /// </summary>
  183. /// <param name="itemId"></param>
  184. public void StopScript(LLUUID itemId)
  185. {
  186. if (m_taskInventory.ContainsKey(itemId))
  187. {
  188. m_parentGroup.Scene.EventManager.TriggerRemoveScript(LocalID, itemId);
  189. m_parentGroup.AddActiveScriptCount(-1);
  190. }
  191. else
  192. {
  193. m_log.ErrorFormat(
  194. "[PRIMINVENTORY]: " +
  195. "Couldn't stop script with ID {0} since it couldn't be found for prim {1}, {2}",
  196. itemId, Name, UUID);
  197. }
  198. }
  199. /// <summary>
  200. /// Add an item to this prim's inventory.
  201. /// </summary>
  202. /// <param name="item"></param>
  203. public void AddInventoryItem(TaskInventoryItem item)
  204. {
  205. item.ParentID = m_folderID;
  206. item.CreationDate = 1000;
  207. item.ParentPartID = UUID;
  208. lock (m_taskInventory)
  209. {
  210. m_taskInventory.Add(item.ItemID, item);
  211. TriggerScriptChangedEvent(Changed.INVENTORY);
  212. }
  213. m_inventorySerial++;
  214. HasInventoryChanged = true;
  215. }
  216. /// <summary>
  217. /// Restore a whole collection of items to the prim's inventory at once.
  218. /// We assume that the items already have all their fields correctly filled out.
  219. /// The items are not flagged for persistence to the database, since they are being restored
  220. /// from persistence rather than being newly added.
  221. /// </summary>
  222. /// <param name="items"></param>
  223. public void RestoreInventoryItems(ICollection<TaskInventoryItem> items)
  224. {
  225. lock (m_taskInventory)
  226. {
  227. foreach (TaskInventoryItem item in items)
  228. {
  229. m_taskInventory.Add(item.ItemID, item);
  230. TriggerScriptChangedEvent(Changed.INVENTORY);
  231. }
  232. }
  233. m_inventorySerial++;
  234. }
  235. /// <summary>
  236. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  237. /// </summary>
  238. /// <param name="itemID"></param>
  239. /// <returns>null if the item does not exist</returns>
  240. public TaskInventoryItem GetInventoryItem(LLUUID itemID)
  241. {
  242. lock (m_taskInventory)
  243. {
  244. if (m_taskInventory.ContainsKey(itemID))
  245. {
  246. return m_taskInventory[itemID];
  247. }
  248. else
  249. {
  250. m_log.ErrorFormat(
  251. "[PRIMINVENTORY]: " +
  252. "Tried to retrieve item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
  253. itemID, Name, UUID);
  254. }
  255. }
  256. return null;
  257. }
  258. /// <summary>
  259. /// Update an existing inventory item.
  260. /// </summary>
  261. /// <param name="item">The updated item. An item with the same id must already exist
  262. /// in this prim's inventory.</param>
  263. /// <returns>false if the item did not exist, true if the update occurred succesfully</returns>
  264. public bool UpdateInventoryItem(TaskInventoryItem item)
  265. {
  266. lock (m_taskInventory)
  267. {
  268. if (m_taskInventory.ContainsKey(item.ItemID))
  269. {
  270. m_taskInventory[item.ItemID] = item;
  271. m_inventorySerial++;
  272. TriggerScriptChangedEvent(Changed.INVENTORY);
  273. HasInventoryChanged = true;
  274. return true;
  275. }
  276. else
  277. {
  278. m_log.ErrorFormat(
  279. "[PRIMINVENTORY]: " +
  280. "Tried to retrieve item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
  281. item.ItemID, Name, UUID);
  282. }
  283. }
  284. return false;
  285. }
  286. public void AddScriptLPS(int count)
  287. {
  288. m_parentGroup.AddScriptLPS(count);
  289. }
  290. /// <summary>
  291. /// Remove an item from this prim's inventory
  292. /// </summary>
  293. /// <param name="itemID"></param>
  294. /// <returns>Numeric asset type of the item removed. Returns -1 if the item did not exist
  295. /// in this prim's inventory.</returns>
  296. public int RemoveInventoryItem(LLUUID itemID)
  297. {
  298. lock (m_taskInventory)
  299. {
  300. if (m_taskInventory.ContainsKey(itemID))
  301. {
  302. int type = m_taskInventory[itemID].InvType;
  303. m_taskInventory.Remove(itemID);
  304. m_inventorySerial++;
  305. TriggerScriptChangedEvent(Changed.INVENTORY);
  306. HasInventoryChanged = true;
  307. int scriptcount = 0;
  308. lock (m_taskInventory)
  309. {
  310. foreach (TaskInventoryItem item in m_taskInventory.Values)
  311. {
  312. if (item.Type == 10)
  313. {
  314. scriptcount++;
  315. }
  316. }
  317. }
  318. if (scriptcount <= 0)
  319. {
  320. RemFlag(LLObject.ObjectFlags.Scripted);
  321. ScheduleFullUpdate();
  322. }
  323. ScheduleFullUpdate();
  324. return type;
  325. }
  326. else
  327. {
  328. m_log.ErrorFormat(
  329. "[PRIMINVENTORY]: " +
  330. "Tried to remove item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
  331. itemID, Name, UUID);
  332. }
  333. }
  334. return -1;
  335. }
  336. /// <summary>
  337. ///
  338. /// </summary>
  339. /// <param name="client"></param>
  340. /// <param name="localID"></param>
  341. public bool GetInventoryFileName(IClientAPI client, uint localID)
  342. {
  343. if (m_inventorySerial > 0)
  344. {
  345. client.SendTaskInventory(m_uuid, (short) m_inventorySerial,
  346. Helpers.StringToField(m_inventoryFileName));
  347. return true;
  348. }
  349. else
  350. {
  351. client.SendTaskInventory(m_uuid, 0, new byte[0]);
  352. return false;
  353. }
  354. }
  355. public void RequestInventoryFile(IXfer xferManager)
  356. {
  357. byte[] fileData = new byte[0];
  358. InventoryStringBuilder invString = new InventoryStringBuilder(m_folderID, UUID);
  359. lock (m_taskInventory)
  360. {
  361. foreach (TaskInventoryItem item in m_taskInventory.Values)
  362. {
  363. invString.AddItemStart();
  364. invString.AddNameValueLine("item_id", item.ItemID.ToString());
  365. invString.AddNameValueLine("parent_id", item.ParentID.ToString());
  366. invString.AddPermissionsStart();
  367. invString.AddNameValueLine("base_mask", "0x7FFFFFFF");
  368. invString.AddNameValueLine("owner_mask", "0x7FFFFFFF");
  369. invString.AddNameValueLine("group_mask", "0x7FFFFFFF");
  370. invString.AddNameValueLine("everyone_mask", "0x7FFFFFFF");
  371. invString.AddNameValueLine("next_owner_mask", "0x7FFFFFFF");
  372. invString.AddNameValueLine("creator_id", item.CreatorID.ToString());
  373. invString.AddNameValueLine("owner_id", item.OwnerID.ToString());
  374. invString.AddNameValueLine("last_owner_id", item.LastOwnerID.ToString());
  375. invString.AddNameValueLine("group_id", item.GroupID.ToString());
  376. invString.AddSectionEnd();
  377. invString.AddNameValueLine("asset_id", item.AssetID.ToString());
  378. invString.AddNameValueLine("type", TaskInventoryItem.Types[item.Type]);
  379. invString.AddNameValueLine("inv_type", TaskInventoryItem.InvTypes[item.InvType]);
  380. invString.AddNameValueLine("flags", "0x00");
  381. invString.AddNameValueLine("name", item.Name + "|");
  382. invString.AddNameValueLine("desc", item.Description + "|");
  383. invString.AddNameValueLine("creation_date", item.CreationDate.ToString());
  384. invString.AddSectionEnd();
  385. }
  386. }
  387. fileData = Helpers.StringToField(invString.BuildString);
  388. // m_log.InfoFormat(
  389. // "[PRIMINVENTORY]: RequestInventoryFile fileData: {0}", Helpers.FieldToUTF8String(fileData));
  390. if (fileData.Length > 2)
  391. {
  392. xferManager.AddNewFile(m_inventoryFileName, fileData);
  393. }
  394. }
  395. /// <summary>
  396. /// Process inventory backup
  397. /// </summary>
  398. /// <param name="datastore"></param>
  399. public void ProcessInventoryBackup(IRegionDataStore datastore)
  400. {
  401. if (HasInventoryChanged)
  402. {
  403. lock (TaskInventory)
  404. {
  405. datastore.StorePrimInventory(UUID, TaskInventory.Values);
  406. }
  407. HasInventoryChanged = false;
  408. }
  409. }
  410. public class InventoryStringBuilder
  411. {
  412. public string BuildString = String.Empty;
  413. public InventoryStringBuilder(LLUUID folderID, LLUUID parentID)
  414. {
  415. BuildString += "\tinv_object\t0\n\t{\n";
  416. AddNameValueLine("obj_id", folderID.ToString());
  417. AddNameValueLine("parent_id", parentID.ToString());
  418. AddNameValueLine("type", "category");
  419. AddNameValueLine("name", "Contents");
  420. AddSectionEnd();
  421. }
  422. public void AddItemStart()
  423. {
  424. BuildString += "\tinv_item\t0\n";
  425. BuildString += "\t{\n";
  426. }
  427. public void AddPermissionsStart()
  428. {
  429. BuildString += "\tpermissions 0\n";
  430. BuildString += "\t{\n";
  431. }
  432. public void AddSectionEnd()
  433. {
  434. BuildString += "\t}\n";
  435. }
  436. public void AddLine(string addLine)
  437. {
  438. BuildString += addLine;
  439. }
  440. public void AddNameValueLine(string name, string value)
  441. {
  442. BuildString += "\t\t";
  443. BuildString += name + "\t";
  444. BuildString += value + "\n";
  445. }
  446. public void Close()
  447. {
  448. }
  449. }
  450. }
  451. }