AgentInventory.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. Console.WriteLine("creating new folder called " + folderName + " in agents inventory");
  72. InventoryFolder Folder = new InventoryFolder();
  73. Folder.FolderID = folderID;
  74. Folder.OwnerID = this.AgentID;
  75. Folder.DefaultType = type;
  76. Folder.FolderName = folderName;
  77. Folder.ParentID = parent;
  78. this.InventoryFolders.Add(Folder.FolderID, Folder);
  79. return (true);
  80. }
  81. public bool HasFolder(LLUUID folderID)
  82. {
  83. if (this.InventoryFolders.ContainsKey(folderID))
  84. {
  85. return true;
  86. }
  87. return false;
  88. }
  89. public bool UpdateItemAsset(LLUUID itemID, AssetBase asset)
  90. {
  91. if(this.InventoryItems.ContainsKey(itemID))
  92. {
  93. InventoryItem Item = this.InventoryItems[itemID];
  94. Item.AssetID = asset.FullID;
  95. Console.WriteLine("updated inventory item " + itemID.ToStringHyphenated() + " so it now is set to asset " + asset.FullID.ToStringHyphenated());
  96. //TODO need to update the rest of the info
  97. }
  98. return true;
  99. }
  100. public bool UpdateItemDetails(LLUUID itemID, UpdateInventoryItemPacket.InventoryDataBlock packet)
  101. {
  102. Console.WriteLine("updating inventory item details");
  103. if (this.InventoryItems.ContainsKey(itemID))
  104. {
  105. Console.WriteLine("changing name to "+ Util.FieldToString(packet.Name));
  106. InventoryItem Item = this.InventoryItems[itemID];
  107. Item.Name = Util.FieldToString(packet.Name);
  108. Console.WriteLine("updated inventory item " + itemID.ToStringHyphenated());
  109. //TODO need to update the rest of the info
  110. }
  111. return true;
  112. }
  113. public LLUUID AddToInventory(LLUUID folderID, AssetBase asset)
  114. {
  115. if (this.InventoryFolders.ContainsKey(folderID))
  116. {
  117. LLUUID NewItemID = LLUUID.Random();
  118. InventoryItem Item = new InventoryItem();
  119. Item.FolderID = folderID;
  120. Item.OwnerID = AgentID;
  121. Item.AssetID = asset.FullID;
  122. Item.ItemID = NewItemID;
  123. Item.Type = asset.Type;
  124. Item.Name = asset.Name;
  125. Item.Description = asset.Description;
  126. Item.InvType = asset.InvType;
  127. this.InventoryItems.Add(Item.ItemID, Item);
  128. InventoryFolder Folder = InventoryFolders[Item.FolderID];
  129. Folder.Items.Add(Item);
  130. return (Item.ItemID);
  131. }
  132. else
  133. {
  134. return (null);
  135. }
  136. }
  137. public bool DeleteFromInventory(LLUUID itemID)
  138. {
  139. bool res = false;
  140. if (this.InventoryItems.ContainsKey(itemID))
  141. {
  142. InventoryItem item = this.InventoryItems[itemID];
  143. this.InventoryItems.Remove(itemID);
  144. foreach (InventoryFolder fold in InventoryFolders.Values)
  145. {
  146. if (fold.Items.Contains(item))
  147. {
  148. fold.Items.Remove(item);
  149. break;
  150. }
  151. }
  152. res = true;
  153. }
  154. return res;
  155. }
  156. }
  157. public class InventoryFolder
  158. {
  159. public List<InventoryItem> Items;
  160. //public List<InventoryFolder> Subfolders;
  161. public LLUUID FolderID;
  162. public LLUUID OwnerID;
  163. public LLUUID ParentID = LLUUID.Zero;
  164. public string FolderName;
  165. public ushort DefaultType;
  166. public ushort Version;
  167. public InventoryFolder()
  168. {
  169. Items = new List<InventoryItem>();
  170. //Subfolders = new List<InventoryFolder>();
  171. }
  172. }
  173. public class InventoryItem
  174. {
  175. public LLUUID FolderID;
  176. public LLUUID OwnerID;
  177. public LLUUID ItemID;
  178. public LLUUID AssetID;
  179. public LLUUID CreatorID;
  180. public sbyte InvType;
  181. public sbyte Type;
  182. public string Name ="";
  183. public string Description;
  184. public InventoryItem()
  185. {
  186. this.CreatorID = LLUUID.Zero;
  187. }
  188. public string ExportString()
  189. {
  190. string typ = "notecard";
  191. string result = "";
  192. result += "\tinv_object\t0\n\t{\n";
  193. result += "\t\tobj_id\t%s\n";
  194. result += "\t\tparent_id\t"+ ItemID.ToString() +"\n";
  195. result += "\t\ttype\t"+ typ +"\n";
  196. result += "\t\tname\t" + Name+"|\n";
  197. result += "\t}\n";
  198. return result;
  199. }
  200. }
  201. public class AvatarWearable
  202. {
  203. public LLUUID AssetID = new LLUUID("00000000-0000-0000-0000-000000000000");
  204. public LLUUID ItemID = new LLUUID("00000000-0000-0000-0000-000000000000");
  205. public AvatarWearable()
  206. {
  207. }
  208. }
  209. }