AgentInventory.cs 9.2 KB

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