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