InventoryCache.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 bool CreateNewInventoryFolder(SimClient remoteClient, LLUUID folderID, ushort type, string folderName, LLUUID parent)
  105. {
  106. bool res = false;
  107. if (folderID != LLUUID.Zero) //don't create a folder with a zero id
  108. {
  109. if (this._agentsInventory.ContainsKey(remoteClient.AgentID))
  110. {
  111. res = this._agentsInventory[remoteClient.AgentID].CreateNewFolder(folderID, type, folderName, parent);
  112. }
  113. }
  114. return res;
  115. }
  116. public LLUUID AddNewInventoryItem(SimClient remoteClient, LLUUID folderID, OpenSim.Framework.Assets.AssetBase asset)
  117. {
  118. LLUUID newItem = null;
  119. if (this._agentsInventory.ContainsKey(remoteClient.AgentID))
  120. {
  121. newItem = this._agentsInventory[remoteClient.AgentID].AddToInventory(folderID, asset);
  122. if (newItem != null)
  123. {
  124. InventoryItem Item = this._agentsInventory[remoteClient.AgentID].InventoryItems[newItem];
  125. this.SendItemUpdateCreate(remoteClient, Item);
  126. }
  127. }
  128. return newItem;
  129. }
  130. public bool DeleteInventoryItem(SimClient remoteClient, LLUUID itemID)
  131. {
  132. bool res = false;
  133. if (this._agentsInventory.ContainsKey(remoteClient.AgentID))
  134. {
  135. res = this._agentsInventory[remoteClient.AgentID].DeleteFromInventory(itemID);
  136. if (res)
  137. {
  138. RemoveInventoryItemPacket remove = new RemoveInventoryItemPacket();
  139. remove.AgentData.AgentID = remoteClient.AgentID;
  140. remove.AgentData.SessionID = remoteClient.SessionID;
  141. remove.InventoryData = new RemoveInventoryItemPacket.InventoryDataBlock[1];
  142. remove.InventoryData[0] = new RemoveInventoryItemPacket.InventoryDataBlock();
  143. remove.InventoryData[0].ItemID = itemID;
  144. remoteClient.OutPacket(remove);
  145. }
  146. }
  147. return res;
  148. }
  149. public bool UpdateInventoryItemAsset(SimClient remoteClient, LLUUID itemID, OpenSim.Framework.Assets.AssetBase asset)
  150. {
  151. if (this._agentsInventory.ContainsKey(remoteClient.AgentID))
  152. {
  153. bool res = _agentsInventory[remoteClient.AgentID].UpdateItemAsset(itemID, asset);
  154. if (res)
  155. {
  156. InventoryItem Item = this._agentsInventory[remoteClient.AgentID].InventoryItems[itemID];
  157. this.SendItemUpdateCreate(remoteClient, Item);
  158. }
  159. return res;
  160. }
  161. return false;
  162. }
  163. public bool UpdateInventoryItemDetails(SimClient remoteClient, LLUUID itemID, UpdateInventoryItemPacket.InventoryDataBlock packet)
  164. {
  165. if (this._agentsInventory.ContainsKey(remoteClient.AgentID))
  166. {
  167. bool res = _agentsInventory[remoteClient.AgentID].UpdateItemDetails(itemID, packet);
  168. if (res)
  169. {
  170. InventoryItem Item = this._agentsInventory[remoteClient.AgentID].InventoryItems[itemID];
  171. this.SendItemUpdateCreate(remoteClient, Item);
  172. }
  173. return res;
  174. }
  175. return false;
  176. }
  177. public void FetchInventoryDescendents(SimClient userInfo, FetchInventoryDescendentsPacket FetchDescend)
  178. {
  179. if (this._agentsInventory.ContainsKey(userInfo.AgentID))
  180. {
  181. AgentInventory agentInventory = this._agentsInventory[userInfo.AgentID];
  182. if (FetchDescend.InventoryData.FetchItems)
  183. {
  184. if (agentInventory.InventoryFolders.ContainsKey(FetchDescend.InventoryData.FolderID))
  185. {
  186. InventoryFolder Folder = agentInventory.InventoryFolders[FetchDescend.InventoryData.FolderID];
  187. InventoryDescendentsPacket Descend = new InventoryDescendentsPacket();
  188. Descend.AgentData.AgentID = userInfo.AgentID;
  189. Descend.AgentData.OwnerID = Folder.OwnerID;
  190. Descend.AgentData.FolderID = FetchDescend.InventoryData.FolderID;
  191. Descend.AgentData.Descendents = Folder.Items.Count;
  192. Descend.AgentData.Version = Folder.Items.Count;
  193. Descend.ItemData = new InventoryDescendentsPacket.ItemDataBlock[Folder.Items.Count];
  194. for (int i = 0; i < Folder.Items.Count; i++)
  195. {
  196. InventoryItem Item = Folder.Items[i];
  197. Descend.ItemData[i] = new InventoryDescendentsPacket.ItemDataBlock();
  198. Descend.ItemData[i].ItemID = Item.ItemID;
  199. Descend.ItemData[i].AssetID = Item.AssetID;
  200. Descend.ItemData[i].CreatorID = Item.CreatorID;
  201. Descend.ItemData[i].BaseMask = FULL_MASK_PERMISSIONS;
  202. Descend.ItemData[i].CreationDate = 1000;
  203. Descend.ItemData[i].Description = _enc.GetBytes(Item.Description + "\0");
  204. Descend.ItemData[i].EveryoneMask = FULL_MASK_PERMISSIONS;
  205. Descend.ItemData[i].Flags = 1;
  206. Descend.ItemData[i].FolderID = Item.FolderID;
  207. Descend.ItemData[i].GroupID = new LLUUID("00000000-0000-0000-0000-000000000000");
  208. Descend.ItemData[i].GroupMask = FULL_MASK_PERMISSIONS;
  209. Descend.ItemData[i].InvType = Item.InvType;
  210. Descend.ItemData[i].Name = _enc.GetBytes(Item.Name + "\0");
  211. Descend.ItemData[i].NextOwnerMask = FULL_MASK_PERMISSIONS;
  212. Descend.ItemData[i].OwnerID = Item.OwnerID;
  213. Descend.ItemData[i].OwnerMask = FULL_MASK_PERMISSIONS;
  214. Descend.ItemData[i].SalePrice = 100;
  215. Descend.ItemData[i].SaleType = 0;
  216. Descend.ItemData[i].Type = Item.Type;
  217. 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);
  218. }
  219. userInfo.OutPacket(Descend);
  220. }
  221. }
  222. else
  223. {
  224. Console.WriteLine("fetch subfolders");
  225. }
  226. }
  227. }
  228. public void FetchInventory(SimClient userInfo, FetchInventoryPacket FetchItems)
  229. {
  230. if (this._agentsInventory.ContainsKey(userInfo.AgentID))
  231. {
  232. AgentInventory agentInventory = this._agentsInventory[userInfo.AgentID];
  233. for (int i = 0; i < FetchItems.InventoryData.Length; i++)
  234. {
  235. if (agentInventory.InventoryItems.ContainsKey(FetchItems.InventoryData[i].ItemID))
  236. {
  237. InventoryItem Item = agentInventory.InventoryItems[FetchItems.InventoryData[i].ItemID];
  238. FetchInventoryReplyPacket InventoryReply = new FetchInventoryReplyPacket();
  239. InventoryReply.AgentData.AgentID = userInfo.AgentID;
  240. InventoryReply.InventoryData = new FetchInventoryReplyPacket.InventoryDataBlock[1];
  241. InventoryReply.InventoryData[0] = new FetchInventoryReplyPacket.InventoryDataBlock();
  242. InventoryReply.InventoryData[0].ItemID = Item.ItemID;
  243. InventoryReply.InventoryData[0].AssetID = Item.AssetID;
  244. InventoryReply.InventoryData[0].CreatorID = Item.CreatorID;
  245. InventoryReply.InventoryData[0].BaseMask = FULL_MASK_PERMISSIONS;
  246. InventoryReply.InventoryData[0].CreationDate = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  247. InventoryReply.InventoryData[0].Description = _enc.GetBytes(Item.Description + "\0");
  248. InventoryReply.InventoryData[0].EveryoneMask = FULL_MASK_PERMISSIONS;
  249. InventoryReply.InventoryData[0].Flags = 0;
  250. InventoryReply.InventoryData[0].FolderID = Item.FolderID;
  251. InventoryReply.InventoryData[0].GroupID = new LLUUID("00000000-0000-0000-0000-000000000000");
  252. InventoryReply.InventoryData[0].GroupMask = FULL_MASK_PERMISSIONS;
  253. InventoryReply.InventoryData[0].InvType = Item.InvType;
  254. InventoryReply.InventoryData[0].Name = _enc.GetBytes(Item.Name + "\0");
  255. InventoryReply.InventoryData[0].NextOwnerMask = FULL_MASK_PERMISSIONS;
  256. InventoryReply.InventoryData[0].OwnerID = Item.OwnerID;
  257. InventoryReply.InventoryData[0].OwnerMask = FULL_MASK_PERMISSIONS;
  258. InventoryReply.InventoryData[0].SalePrice = 100;
  259. InventoryReply.InventoryData[0].SaleType = 0;
  260. InventoryReply.InventoryData[0].Type = Item.Type;
  261. 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);
  262. userInfo.OutPacket(InventoryReply);
  263. }
  264. }
  265. }
  266. }
  267. private void SendItemUpdateCreate(SimClient remoteClient, InventoryItem Item)
  268. {
  269. UpdateCreateInventoryItemPacket InventoryReply = new UpdateCreateInventoryItemPacket();
  270. InventoryReply.AgentData.AgentID = remoteClient.AgentID;
  271. InventoryReply.AgentData.SimApproved = true;
  272. InventoryReply.InventoryData = new UpdateCreateInventoryItemPacket.InventoryDataBlock[1];
  273. InventoryReply.InventoryData[0] = new UpdateCreateInventoryItemPacket.InventoryDataBlock();
  274. InventoryReply.InventoryData[0].ItemID = Item.ItemID;
  275. InventoryReply.InventoryData[0].AssetID = Item.AssetID;
  276. InventoryReply.InventoryData[0].CreatorID = Item.CreatorID;
  277. InventoryReply.InventoryData[0].BaseMask = FULL_MASK_PERMISSIONS;
  278. InventoryReply.InventoryData[0].CreationDate = 1000;
  279. InventoryReply.InventoryData[0].Description = _enc.GetBytes(Item.Description + "\0");
  280. InventoryReply.InventoryData[0].EveryoneMask = FULL_MASK_PERMISSIONS;
  281. InventoryReply.InventoryData[0].Flags = 0;
  282. InventoryReply.InventoryData[0].FolderID = Item.FolderID;
  283. InventoryReply.InventoryData[0].GroupID = new LLUUID("00000000-0000-0000-0000-000000000000");
  284. InventoryReply.InventoryData[0].GroupMask = FULL_MASK_PERMISSIONS;
  285. InventoryReply.InventoryData[0].InvType = Item.InvType;
  286. InventoryReply.InventoryData[0].Name = _enc.GetBytes(Item.Name + "\0");
  287. InventoryReply.InventoryData[0].NextOwnerMask = FULL_MASK_PERMISSIONS;
  288. InventoryReply.InventoryData[0].OwnerID = Item.OwnerID;
  289. InventoryReply.InventoryData[0].OwnerMask = FULL_MASK_PERMISSIONS;
  290. InventoryReply.InventoryData[0].SalePrice = 100;
  291. InventoryReply.InventoryData[0].SaleType = 0;
  292. InventoryReply.InventoryData[0].Type = Item.Type;
  293. 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);
  294. remoteClient.OutPacket(InventoryReply);
  295. }
  296. }
  297. public class UserServerRequest
  298. {
  299. public UserServerRequest()
  300. {
  301. }
  302. }
  303. }