AgentInventory.cs 8.4 KB

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