AgentInventory.cs 10 KB

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