AgentInventory.cs 9.2 KB

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