AgentInventory.cs 8.2 KB

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