AgentInventory.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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.Collections.Generic;
  29. using libsecondlife;
  30. using libsecondlife.Packets;
  31. using OpenSim.Framework.Types;
  32. using OpenSim.Framework.Utilities;
  33. namespace OpenSim.Framework.Inventory
  34. {
  35. public class AgentInventory
  36. {
  37. //Holds the local copy of Inventory info for a agent
  38. public Dictionary<LLUUID, InventoryFolder> InventoryFolders;
  39. public Dictionary<LLUUID, InventoryItem> InventoryItems;
  40. public InventoryFolder InventoryRoot;
  41. public int LastCached; //maybe used by opensim app, time this was last stored/compared to user server
  42. public LLUUID AgentID;
  43. public AvatarWearable[] Wearables;
  44. public AgentInventory()
  45. {
  46. InventoryFolders = new Dictionary<LLUUID, InventoryFolder>();
  47. InventoryItems = new Dictionary<LLUUID, InventoryItem>();
  48. this.Initialise();
  49. }
  50. public virtual void Initialise()
  51. {
  52. Wearables = new AvatarWearable[13]; //should be 12 of these
  53. for (int i = 0; i < 13; i++)
  54. {
  55. Wearables[i] = new AvatarWearable();
  56. }
  57. }
  58. public bool CreateNewFolder(LLUUID folderID, ushort type)
  59. {
  60. InventoryFolder Folder = new InventoryFolder();
  61. Folder.FolderID = folderID;
  62. Folder.OwnerID = this.AgentID;
  63. Folder.DefaultType = type;
  64. this.InventoryFolders.Add(Folder.FolderID, Folder);
  65. return (true);
  66. }
  67. public void CreateRootFolder(LLUUID newAgentID, bool createTextures)
  68. {
  69. this.AgentID = newAgentID;
  70. InventoryRoot = new InventoryFolder();
  71. InventoryRoot.FolderID = LLUUID.Random();
  72. InventoryRoot.ParentID = new LLUUID();
  73. InventoryRoot.Version = 1;
  74. InventoryRoot.DefaultType = 8;
  75. InventoryRoot.OwnerID = this.AgentID;
  76. InventoryRoot.FolderName = "My Inventory";
  77. InventoryFolders.Add(InventoryRoot.FolderID, InventoryRoot);
  78. InventoryRoot.OwnerID = this.AgentID;
  79. if (createTextures)
  80. {
  81. this.CreateNewFolder(LLUUID.Random(), 0, "Textures", InventoryRoot.FolderID);
  82. }
  83. }
  84. public bool CreateNewFolder(LLUUID folderID, ushort type, string folderName)
  85. {
  86. InventoryFolder Folder = new InventoryFolder();
  87. Folder.FolderID = folderID;
  88. Folder.OwnerID = this.AgentID;
  89. Folder.DefaultType = type;
  90. Folder.FolderName = folderName;
  91. this.InventoryFolders.Add(Folder.FolderID, Folder);
  92. return (true);
  93. }
  94. public bool CreateNewFolder(LLUUID folderID, ushort type, string folderName, LLUUID parent)
  95. {
  96. if (!this.InventoryFolders.ContainsKey(folderID))
  97. {
  98. System.Console.WriteLine("creating new folder called " + folderName + " in agents inventory");
  99. InventoryFolder Folder = new InventoryFolder();
  100. Folder.FolderID = folderID;
  101. Folder.OwnerID = this.AgentID;
  102. Folder.DefaultType = type;
  103. Folder.FolderName = folderName;
  104. Folder.ParentID = parent;
  105. this.InventoryFolders.Add(Folder.FolderID, Folder);
  106. }
  107. return (true);
  108. }
  109. public bool HasFolder(LLUUID folderID)
  110. {
  111. if (this.InventoryFolders.ContainsKey(folderID))
  112. {
  113. return true;
  114. }
  115. return false;
  116. }
  117. public LLUUID GetFolderID(string folderName)
  118. {
  119. foreach (InventoryFolder inv in this.InventoryFolders.Values)
  120. {
  121. if (inv.FolderName == folderName)
  122. {
  123. return inv.FolderID;
  124. }
  125. }
  126. return LLUUID.Zero;
  127. }
  128. public bool UpdateItemAsset(LLUUID itemID, AssetBase asset)
  129. {
  130. if(this.InventoryItems.ContainsKey(itemID))
  131. {
  132. InventoryItem Item = this.InventoryItems[itemID];
  133. Item.AssetID = asset.FullID;
  134. System.Console.WriteLine("updated inventory item " + itemID.ToStringHyphenated() + " so it now is set to asset " + asset.FullID.ToStringHyphenated());
  135. //TODO need to update the rest of the info
  136. }
  137. return true;
  138. }
  139. public bool UpdateItemDetails(LLUUID itemID, UpdateInventoryItemPacket.InventoryDataBlock packet)
  140. {
  141. System.Console.WriteLine("updating inventory item details");
  142. if (this.InventoryItems.ContainsKey(itemID))
  143. {
  144. System.Console.WriteLine("changing name to "+ Util.FieldToString(packet.Name));
  145. InventoryItem Item = this.InventoryItems[itemID];
  146. Item.Name = Util.FieldToString(packet.Name);
  147. System.Console.WriteLine("updated inventory item " + itemID.ToStringHyphenated());
  148. //TODO need to update the rest of the info
  149. }
  150. return true;
  151. }
  152. public LLUUID AddToInventory(LLUUID folderID, AssetBase asset)
  153. {
  154. if (this.InventoryFolders.ContainsKey(folderID))
  155. {
  156. LLUUID NewItemID = LLUUID.Random();
  157. InventoryItem Item = new InventoryItem();
  158. Item.FolderID = folderID;
  159. Item.OwnerID = AgentID;
  160. Item.AssetID = asset.FullID;
  161. Item.ItemID = NewItemID;
  162. Item.Type = asset.Type;
  163. Item.Name = asset.Name;
  164. Item.Description = asset.Description;
  165. Item.InvType = asset.InvType;
  166. this.InventoryItems.Add(Item.ItemID, Item);
  167. InventoryFolder Folder = InventoryFolders[Item.FolderID];
  168. Folder.Items.Add(Item);
  169. return (Item.ItemID);
  170. }
  171. else
  172. {
  173. return (null);
  174. }
  175. }
  176. public bool DeleteFromInventory(LLUUID itemID)
  177. {
  178. bool res = false;
  179. if (this.InventoryItems.ContainsKey(itemID))
  180. {
  181. InventoryItem item = this.InventoryItems[itemID];
  182. this.InventoryItems.Remove(itemID);
  183. foreach (InventoryFolder fold in InventoryFolders.Values)
  184. {
  185. if (fold.Items.Contains(item))
  186. {
  187. fold.Items.Remove(item);
  188. break;
  189. }
  190. }
  191. res = true;
  192. }
  193. return res;
  194. }
  195. }
  196. public class InventoryFolder
  197. {
  198. public List<InventoryItem> Items;
  199. //public List<InventoryFolder> Subfolders;
  200. public LLUUID FolderID;
  201. public LLUUID OwnerID;
  202. public LLUUID ParentID = LLUUID.Zero;
  203. public string FolderName;
  204. public ushort DefaultType;
  205. public ushort Version;
  206. public InventoryFolder()
  207. {
  208. Items = new List<InventoryItem>();
  209. //Subfolders = new List<InventoryFolder>();
  210. }
  211. }
  212. public class InventoryItem
  213. {
  214. public LLUUID FolderID;
  215. public LLUUID OwnerID;
  216. public LLUUID ItemID;
  217. public LLUUID AssetID;
  218. public LLUUID CreatorID;
  219. public sbyte InvType;
  220. public sbyte Type;
  221. public string Name ="";
  222. public string Description;
  223. public InventoryItem()
  224. {
  225. this.CreatorID = LLUUID.Zero;
  226. }
  227. public string ExportString()
  228. {
  229. string typ = "notecard";
  230. string result = "";
  231. result += "\tinv_object\t0\n\t{\n";
  232. result += "\t\tobj_id\t%s\n";
  233. result += "\t\tparent_id\t"+ ItemID.ToString() +"\n";
  234. result += "\t\ttype\t"+ typ +"\n";
  235. result += "\t\tname\t" + Name+"|\n";
  236. result += "\t}\n";
  237. return result;
  238. }
  239. }
  240. }