InventoryCache.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using libsecondlife;
  30. using OpenSim;
  31. using libsecondlife.Packets;
  32. //using OpenSim.GridServers;
  33. using OpenSim.Framework.Inventory;
  34. using OpenSim.Framework.Assets;
  35. using OpenSim.Framework.Interfaces;
  36. namespace OpenSim.Assets
  37. {
  38. /// <summary>
  39. /// Description of InventoryManager.
  40. /// </summary>
  41. public class InventoryCache
  42. {
  43. private Dictionary<LLUUID, AgentInventory> _agentsInventory;
  44. private List<UserServerRequest> _serverRequests; //list of requests made to user server.
  45. private System.Text.Encoding _enc = System.Text.Encoding.ASCII;
  46. private const uint FULL_MASK_PERMISSIONS = 2147483647;
  47. public InventoryCache()
  48. {
  49. _agentsInventory = new Dictionary<LLUUID, AgentInventory>();
  50. _serverRequests = new List<UserServerRequest>();
  51. }
  52. public void AddNewAgentsInventory(AgentInventory agentInventory)
  53. {
  54. if (!this._agentsInventory.ContainsKey(agentInventory.AgentID))
  55. {
  56. this._agentsInventory.Add(agentInventory.AgentID, agentInventory);
  57. }
  58. }
  59. public AgentInventory FetchAgentsInventory(LLUUID agentID, IUserServer userserver)
  60. {
  61. AgentInventory res = null;
  62. if (!this._agentsInventory.ContainsKey(agentID))
  63. {
  64. res = userserver.RequestAgentsInventory(agentID);
  65. this._agentsInventory.Add(agentID,res);
  66. }
  67. return res;
  68. }
  69. public AgentInventory GetAgentsInventory(LLUUID agentID)
  70. {
  71. if (this._agentsInventory.ContainsKey(agentID))
  72. {
  73. return this._agentsInventory[agentID];
  74. }
  75. return null;
  76. }
  77. public void ClientLeaving(LLUUID clientID, IUserServer userserver)
  78. {
  79. if (this._agentsInventory.ContainsKey(clientID))
  80. {
  81. if (userserver != null)
  82. {
  83. userserver.UpdateAgentsInventory(clientID, this._agentsInventory[clientID]);
  84. }
  85. this._agentsInventory.Remove(clientID);
  86. }
  87. }
  88. public bool CreateNewInventoryFolder(SimClient remoteClient, LLUUID folderID)
  89. {
  90. return this.CreateNewInventoryFolder(remoteClient, folderID, 0);
  91. }
  92. public bool CreateNewInventoryFolder(SimClient remoteClient, LLUUID folderID, ushort type)
  93. {
  94. bool res = false;
  95. if (folderID != LLUUID.Zero) //don't create a folder with a zero id
  96. {
  97. if (this._agentsInventory.ContainsKey(remoteClient.AgentID))
  98. {
  99. res = this._agentsInventory[remoteClient.AgentID].CreateNewFolder(folderID, type);
  100. }
  101. }
  102. return res;
  103. }
  104. public LLUUID AddNewInventoryItem(SimClient remoteClient, LLUUID folderID, OpenSim.Framework.Assets.AssetBase asset)
  105. {
  106. LLUUID newItem = null;
  107. if (this._agentsInventory.ContainsKey(remoteClient.AgentID))
  108. {
  109. newItem = this._agentsInventory[remoteClient.AgentID].AddToInventory(folderID, asset);
  110. if (newItem != null)
  111. {
  112. InventoryItem Item = this._agentsInventory[remoteClient.AgentID].InventoryItems[newItem];
  113. this.SendItemUpdateCreate(remoteClient, Item);
  114. }
  115. }
  116. return newItem;
  117. }
  118. public bool UpdateInventoryItem(SimClient remoteClient, LLUUID itemID, OpenSim.Framework.Assets.AssetBase asset)
  119. {
  120. if (this._agentsInventory.ContainsKey(remoteClient.AgentID))
  121. {
  122. bool res = _agentsInventory[remoteClient.AgentID].UpdateItem(itemID, asset);
  123. if (res)
  124. {
  125. InventoryItem Item = this._agentsInventory[remoteClient.AgentID].InventoryItems[itemID];
  126. this.SendItemUpdateCreate(remoteClient, Item);
  127. }
  128. return res;
  129. }
  130. return false;
  131. }
  132. public void FetchInventoryDescendents(SimClient userInfo, FetchInventoryDescendentsPacket FetchDescend)
  133. {
  134. if (this._agentsInventory.ContainsKey(userInfo.AgentID))
  135. {
  136. AgentInventory agentInventory = this._agentsInventory[userInfo.AgentID];
  137. if (FetchDescend.InventoryData.FetchItems)
  138. {
  139. if (agentInventory.InventoryFolders.ContainsKey(FetchDescend.InventoryData.FolderID))
  140. {
  141. InventoryFolder Folder = agentInventory.InventoryFolders[FetchDescend.InventoryData.FolderID];
  142. InventoryDescendentsPacket Descend = new InventoryDescendentsPacket();
  143. Descend.AgentData.AgentID = userInfo.AgentID;
  144. Descend.AgentData.OwnerID = Folder.OwnerID;
  145. Descend.AgentData.FolderID = FetchDescend.InventoryData.FolderID;
  146. Descend.AgentData.Descendents = Folder.Items.Count;
  147. Descend.AgentData.Version = Folder.Items.Count;
  148. Descend.ItemData = new InventoryDescendentsPacket.ItemDataBlock[Folder.Items.Count];
  149. for (int i = 0; i < Folder.Items.Count; i++)
  150. {
  151. InventoryItem Item = Folder.Items[i];
  152. Descend.ItemData[i] = new InventoryDescendentsPacket.ItemDataBlock();
  153. Descend.ItemData[i].ItemID = Item.ItemID;
  154. Descend.ItemData[i].AssetID = Item.AssetID;
  155. Descend.ItemData[i].CreatorID = Item.CreatorID;
  156. Descend.ItemData[i].BaseMask = FULL_MASK_PERMISSIONS;
  157. Descend.ItemData[i].CreationDate = 1000;
  158. Descend.ItemData[i].Description = _enc.GetBytes(Item.Description + "\0");
  159. Descend.ItemData[i].EveryoneMask = FULL_MASK_PERMISSIONS;
  160. Descend.ItemData[i].Flags = 1;
  161. Descend.ItemData[i].FolderID = Item.FolderID;
  162. Descend.ItemData[i].GroupID = new LLUUID("00000000-0000-0000-0000-000000000000");
  163. Descend.ItemData[i].GroupMask = FULL_MASK_PERMISSIONS;
  164. Descend.ItemData[i].InvType = Item.InvType;
  165. Descend.ItemData[i].Name = _enc.GetBytes(Item.Name + "\0");
  166. Descend.ItemData[i].NextOwnerMask = FULL_MASK_PERMISSIONS;
  167. Descend.ItemData[i].OwnerID = Item.OwnerID;
  168. Descend.ItemData[i].OwnerMask = FULL_MASK_PERMISSIONS;
  169. Descend.ItemData[i].SalePrice = 100;
  170. Descend.ItemData[i].SaleType = 0;
  171. Descend.ItemData[i].Type = Item.Type;
  172. Descend.ItemData[i].CRC = libsecondlife.Helpers.InventoryCRC(1000, 0, Descend.ItemData[i].InvType, Descend.ItemData[i].Type, Descend.ItemData[i].AssetID, Descend.ItemData[i].GroupID, 100, Descend.ItemData[i].OwnerID, Descend.ItemData[i].CreatorID, Descend.ItemData[i].ItemID, Descend.ItemData[i].FolderID, FULL_MASK_PERMISSIONS, 1, FULL_MASK_PERMISSIONS, FULL_MASK_PERMISSIONS, FULL_MASK_PERMISSIONS);
  173. }
  174. userInfo.OutPacket(Descend);
  175. }
  176. }
  177. else
  178. {
  179. Console.WriteLine("fetch subfolders");
  180. }
  181. }
  182. }
  183. public void FetchInventory(SimClient userInfo, FetchInventoryPacket FetchItems)
  184. {
  185. if (this._agentsInventory.ContainsKey(userInfo.AgentID))
  186. {
  187. AgentInventory agentInventory = this._agentsInventory[userInfo.AgentID];
  188. for (int i = 0; i < FetchItems.InventoryData.Length; i++)
  189. {
  190. if (agentInventory.InventoryItems.ContainsKey(FetchItems.InventoryData[i].ItemID))
  191. {
  192. InventoryItem Item = agentInventory.InventoryItems[FetchItems.InventoryData[i].ItemID];
  193. FetchInventoryReplyPacket InventoryReply = new FetchInventoryReplyPacket();
  194. InventoryReply.AgentData.AgentID = userInfo.AgentID;
  195. InventoryReply.InventoryData = new FetchInventoryReplyPacket.InventoryDataBlock[1];
  196. InventoryReply.InventoryData[0] = new FetchInventoryReplyPacket.InventoryDataBlock();
  197. InventoryReply.InventoryData[0].ItemID = Item.ItemID;
  198. InventoryReply.InventoryData[0].AssetID = Item.AssetID;
  199. InventoryReply.InventoryData[0].CreatorID = Item.CreatorID;
  200. InventoryReply.InventoryData[0].BaseMask = FULL_MASK_PERMISSIONS;
  201. InventoryReply.InventoryData[0].CreationDate = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  202. InventoryReply.InventoryData[0].Description = _enc.GetBytes(Item.Description + "\0");
  203. InventoryReply.InventoryData[0].EveryoneMask = FULL_MASK_PERMISSIONS;
  204. InventoryReply.InventoryData[0].Flags = 0;
  205. InventoryReply.InventoryData[0].FolderID = Item.FolderID;
  206. InventoryReply.InventoryData[0].GroupID = new LLUUID("00000000-0000-0000-0000-000000000000");
  207. InventoryReply.InventoryData[0].GroupMask = FULL_MASK_PERMISSIONS;
  208. InventoryReply.InventoryData[0].InvType = Item.InvType;
  209. InventoryReply.InventoryData[0].Name = _enc.GetBytes(Item.Name + "\0");
  210. InventoryReply.InventoryData[0].NextOwnerMask = FULL_MASK_PERMISSIONS;
  211. InventoryReply.InventoryData[0].OwnerID = Item.OwnerID;
  212. InventoryReply.InventoryData[0].OwnerMask = FULL_MASK_PERMISSIONS;
  213. InventoryReply.InventoryData[0].SalePrice = 100;
  214. InventoryReply.InventoryData[0].SaleType = 0;
  215. InventoryReply.InventoryData[0].Type = Item.Type;
  216. InventoryReply.InventoryData[0].CRC = libsecondlife.Helpers.InventoryCRC(1000, 0, InventoryReply.InventoryData[0].InvType, InventoryReply.InventoryData[0].Type, InventoryReply.InventoryData[0].AssetID, InventoryReply.InventoryData[0].GroupID, 100, InventoryReply.InventoryData[0].OwnerID, InventoryReply.InventoryData[0].CreatorID, InventoryReply.InventoryData[0].ItemID, InventoryReply.InventoryData[0].FolderID, FULL_MASK_PERMISSIONS, 1, FULL_MASK_PERMISSIONS, FULL_MASK_PERMISSIONS, FULL_MASK_PERMISSIONS);
  217. userInfo.OutPacket(InventoryReply);
  218. }
  219. }
  220. }
  221. }
  222. private void SendItemUpdateCreate(SimClient remoteClient, InventoryItem Item)
  223. {
  224. UpdateCreateInventoryItemPacket InventoryReply = new UpdateCreateInventoryItemPacket();
  225. InventoryReply.AgentData.AgentID = remoteClient.AgentID;
  226. InventoryReply.AgentData.SimApproved = true;
  227. InventoryReply.InventoryData = new UpdateCreateInventoryItemPacket.InventoryDataBlock[1];
  228. InventoryReply.InventoryData[0] = new UpdateCreateInventoryItemPacket.InventoryDataBlock();
  229. InventoryReply.InventoryData[0].ItemID = Item.ItemID;
  230. InventoryReply.InventoryData[0].AssetID = Item.AssetID;
  231. InventoryReply.InventoryData[0].CreatorID = Item.CreatorID;
  232. InventoryReply.InventoryData[0].BaseMask = FULL_MASK_PERMISSIONS;
  233. InventoryReply.InventoryData[0].CreationDate = 1000;
  234. InventoryReply.InventoryData[0].Description = _enc.GetBytes(Item.Description + "\0");
  235. InventoryReply.InventoryData[0].EveryoneMask = FULL_MASK_PERMISSIONS;
  236. InventoryReply.InventoryData[0].Flags = 0;
  237. InventoryReply.InventoryData[0].FolderID = Item.FolderID;
  238. InventoryReply.InventoryData[0].GroupID = new LLUUID("00000000-0000-0000-0000-000000000000");
  239. InventoryReply.InventoryData[0].GroupMask = FULL_MASK_PERMISSIONS;
  240. InventoryReply.InventoryData[0].InvType = Item.InvType;
  241. InventoryReply.InventoryData[0].Name = _enc.GetBytes(Item.Name + "\0");
  242. InventoryReply.InventoryData[0].NextOwnerMask = FULL_MASK_PERMISSIONS;
  243. InventoryReply.InventoryData[0].OwnerID = Item.OwnerID;
  244. InventoryReply.InventoryData[0].OwnerMask = FULL_MASK_PERMISSIONS;
  245. InventoryReply.InventoryData[0].SalePrice = 100;
  246. InventoryReply.InventoryData[0].SaleType = 0;
  247. InventoryReply.InventoryData[0].Type = Item.Type;
  248. InventoryReply.InventoryData[0].CRC = libsecondlife.Helpers.InventoryCRC(1000, 0, InventoryReply.InventoryData[0].InvType, InventoryReply.InventoryData[0].Type, InventoryReply.InventoryData[0].AssetID, InventoryReply.InventoryData[0].GroupID, 100, InventoryReply.InventoryData[0].OwnerID, InventoryReply.InventoryData[0].CreatorID, InventoryReply.InventoryData[0].ItemID, InventoryReply.InventoryData[0].FolderID, FULL_MASK_PERMISSIONS, 1, FULL_MASK_PERMISSIONS, FULL_MASK_PERMISSIONS, FULL_MASK_PERMISSIONS);
  249. remoteClient.OutPacket(InventoryReply);
  250. }
  251. }
  252. public class UserServerRequest
  253. {
  254. public UserServerRequest()
  255. {
  256. }
  257. }
  258. }