AgentAssetsTransactions.cs 9.2 KB

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