AgentAssetsTransactions.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 OpenSimulator 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 System.Reflection;
  29. using log4net;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Communications.Cache;
  33. using OpenSim.Region.Framework.Scenes;
  34. using OpenSim.Services.Interfaces;
  35. namespace OpenSim.Region.CoreModules.Agent.AssetTransaction
  36. {
  37. /// <summary>
  38. /// Manage asset transactions for a single agent.
  39. /// </summary>
  40. public class AgentAssetTransactions
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. // Fields
  44. private bool m_dumpAssetsToFile;
  45. public AssetTransactionModule Manager;
  46. public UUID UserID;
  47. public Dictionary<UUID, AssetXferUploader> XferUploaders = new Dictionary<UUID, AssetXferUploader>();
  48. // Methods
  49. public AgentAssetTransactions(UUID agentID, AssetTransactionModule manager, bool dumpAssetsToFile)
  50. {
  51. UserID = agentID;
  52. Manager = manager;
  53. m_dumpAssetsToFile = dumpAssetsToFile;
  54. }
  55. public AssetXferUploader RequestXferUploader(UUID transactionID)
  56. {
  57. if (!XferUploaders.ContainsKey(transactionID))
  58. {
  59. AssetXferUploader uploader = new AssetXferUploader(this, m_dumpAssetsToFile);
  60. lock (XferUploaders)
  61. {
  62. XferUploaders.Add(transactionID, uploader);
  63. }
  64. return uploader;
  65. }
  66. return null;
  67. }
  68. public void HandleXfer(ulong xferID, uint packetID, byte[] data)
  69. {
  70. lock (XferUploaders)
  71. {
  72. foreach (AssetXferUploader uploader in XferUploaders.Values)
  73. {
  74. if (uploader.XferID == xferID)
  75. {
  76. uploader.HandleXferPacket(xferID, packetID, data);
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. public void RequestCreateInventoryItem(IClientAPI remoteClient, UUID transactionID, UUID folderID,
  83. uint callbackID, string description, string name, sbyte invType,
  84. sbyte type, byte wearableType, uint nextOwnerMask)
  85. {
  86. if (XferUploaders.ContainsKey(transactionID))
  87. {
  88. XferUploaders[transactionID].RequestCreateInventoryItem(remoteClient, transactionID, folderID,
  89. callbackID, description, name, invType, type,
  90. wearableType, nextOwnerMask);
  91. }
  92. }
  93. /// <summary>
  94. /// Get an uploaded asset. If the data is successfully retrieved, the transaction will be removed.
  95. /// </summary>
  96. /// <param name="transactionID"></param>
  97. /// <returns>The asset if the upload has completed, null if it has not.</returns>
  98. public AssetBase GetTransactionAsset(UUID transactionID)
  99. {
  100. if (XferUploaders.ContainsKey(transactionID))
  101. {
  102. AssetXferUploader uploader = XferUploaders[transactionID];
  103. AssetBase asset = uploader.GetAssetData();
  104. lock (XferUploaders)
  105. {
  106. XferUploaders.Remove(transactionID);
  107. }
  108. return asset;
  109. }
  110. return null;
  111. }
  112. //private void CreateItemFromUpload(AssetBase asset, IClientAPI ourClient, UUID inventoryFolderID, uint nextPerms, uint wearableType)
  113. //{
  114. // Manager.MyScene.CommsManager.AssetCache.AddAsset(asset);
  115. // CachedUserInfo userInfo = Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(
  116. // ourClient.AgentId);
  117. // if (userInfo != null)
  118. // {
  119. // InventoryItemBase item = new InventoryItemBase();
  120. // item.Owner = ourClient.AgentId;
  121. // item.Creator = ourClient.AgentId;
  122. // item.ID = UUID.Random();
  123. // item.AssetID = asset.FullID;
  124. // item.Description = asset.Description;
  125. // item.Name = asset.Name;
  126. // item.AssetType = asset.Type;
  127. // item.InvType = asset.Type;
  128. // item.Folder = inventoryFolderID;
  129. // item.BasePermissions = 0x7fffffff;
  130. // item.CurrentPermissions = 0x7fffffff;
  131. // item.EveryOnePermissions = 0;
  132. // item.NextPermissions = nextPerms;
  133. // item.Flags = wearableType;
  134. // item.CreationDate = Util.UnixTimeSinceEpoch();
  135. // userInfo.AddItem(item);
  136. // ourClient.SendInventoryItemCreateUpdate(item);
  137. // }
  138. // else
  139. // {
  140. // m_log.ErrorFormat(
  141. // "[ASSET TRANSACTIONS]: Could not find user {0} for inventory item creation",
  142. // ourClient.AgentId);
  143. // }
  144. //}
  145. public void RequestUpdateTaskInventoryItem(
  146. IClientAPI remoteClient, SceneObjectPart part, UUID transactionID, TaskInventoryItem item)
  147. {
  148. if (XferUploaders.ContainsKey(transactionID))
  149. {
  150. AssetBase asset = XferUploaders[transactionID].GetAssetData();
  151. if (asset != null)
  152. {
  153. m_log.DebugFormat(
  154. "[ASSET TRANSACTIONS]: Updating task item {0} in {1} with asset in transaction {2}",
  155. item.Name, part.Name, transactionID);
  156. asset.Name = item.Name;
  157. asset.Description = item.Description;
  158. asset.Type = (sbyte)item.Type;
  159. item.AssetID = asset.FullID;
  160. Manager.MyScene.AssetService.Store(asset);
  161. if (part.Inventory.UpdateInventoryItem(item))
  162. part.GetProperties(remoteClient);
  163. }
  164. }
  165. }
  166. public void RequestUpdateInventoryItem(IClientAPI remoteClient, UUID transactionID,
  167. InventoryItemBase item)
  168. {
  169. if (XferUploaders.ContainsKey(transactionID))
  170. {
  171. CachedUserInfo userInfo = Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(
  172. remoteClient.AgentId);
  173. if (userInfo != null)
  174. {
  175. UUID assetID = UUID.Combine(transactionID, remoteClient.SecureSessionId);
  176. AssetBase asset = Manager.MyScene.AssetService.Get(assetID.ToString());
  177. if (asset == null)
  178. {
  179. asset = GetTransactionAsset(transactionID);
  180. }
  181. if (asset != null && asset.FullID == assetID)
  182. {
  183. // Assets never get updated, new ones get created
  184. asset.FullID = UUID.Random();
  185. asset.Name = item.Name;
  186. asset.Description = item.Description;
  187. asset.Type = (sbyte)item.AssetType;
  188. item.AssetID = asset.FullID;
  189. Manager.MyScene.AssetService.Store(asset);
  190. }
  191. userInfo.UpdateItem(item);
  192. }
  193. else
  194. {
  195. m_log.ErrorFormat(
  196. "[ASSET TRANSACTIONS]: Could not find user {0} for inventory item update",
  197. remoteClient.AgentId);
  198. }
  199. }
  200. }
  201. }
  202. }