AgentAssetUpload.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using OpenSim.Assets;
  5. using OpenSim.Framework.Assets;
  6. using OpenSim.Framework.Utilities;
  7. using libsecondlife;
  8. using libsecondlife.Packets;
  9. namespace OpenSim
  10. {
  11. public class AgentAssetUpload
  12. {
  13. private Dictionary<LLUUID, AssetTransaction> transactions = new Dictionary<LLUUID, AssetTransaction>();
  14. private SimClient ourClient;
  15. private AssetCache m_assetCache;
  16. private InventoryCache m_inventoryCache;
  17. public AgentAssetUpload(SimClient client, AssetCache assetCache, InventoryCache inventoryCache)
  18. {
  19. this.ourClient = client;
  20. m_assetCache = assetCache;
  21. m_inventoryCache = inventoryCache;
  22. }
  23. public void AddUpload(LLUUID transactionID, AssetBase asset)
  24. {
  25. AssetTransaction upload = new AssetTransaction();
  26. lock (this.transactions)
  27. {
  28. upload.Asset = asset;
  29. upload.TransactionID = transactionID;
  30. this.transactions.Add(transactionID, upload);
  31. }
  32. if (upload.Asset.Data.Length > 2)
  33. {
  34. //is complete
  35. upload.UploadComplete = true;
  36. AssetUploadCompletePacket response = new AssetUploadCompletePacket();
  37. response.AssetBlock.Type = asset.Type;
  38. response.AssetBlock.Success = true;
  39. response.AssetBlock.UUID = transactionID.Combine(this.ourClient.SecureSessionID);
  40. this.ourClient.OutPacket(response);
  41. m_assetCache.AddAsset(asset);
  42. }
  43. else
  44. {
  45. upload.UploadComplete = false;
  46. upload.XferID = Util.GetNextXferID();
  47. RequestXferPacket xfer = new RequestXferPacket();
  48. xfer.XferID.ID = upload.XferID;
  49. xfer.XferID.VFileType = upload.Asset.Type;
  50. xfer.XferID.VFileID = transactionID.Combine(this.ourClient.SecureSessionID);
  51. xfer.XferID.FilePath = 0;
  52. xfer.XferID.Filename = new byte[0];
  53. this.ourClient.OutPacket(xfer);
  54. }
  55. }
  56. public AssetBase GetUpload(LLUUID transactionID)
  57. {
  58. if (this.transactions.ContainsKey(transactionID))
  59. {
  60. return this.transactions[transactionID].Asset;
  61. }
  62. return null;
  63. }
  64. public void HandleUploadPacket(AssetUploadRequestPacket pack, LLUUID assetID)
  65. {
  66. Console.Write("asset upload request , type = " + pack.AssetBlock.Type.ToString());
  67. AssetBase asset = null;
  68. if (pack.AssetBlock.Type == 0)
  69. {
  70. //first packet for transaction
  71. asset = new AssetBase();
  72. asset.FullID = assetID;
  73. asset.Type = pack.AssetBlock.Type;
  74. asset.InvType = asset.Type;
  75. asset.Name = "UploadedTexture" + Util.RandomClass.Next(1, 1000).ToString("000");
  76. asset.Data = pack.AssetBlock.AssetData;
  77. }
  78. else if (pack.AssetBlock.Type == 13 | pack.AssetBlock.Type == 5 | pack.AssetBlock.Type == 7)
  79. {
  80. asset = new AssetBase();
  81. asset.FullID = assetID;
  82. // Console.WriteLine("skin asset id is " + assetID.ToStringHyphenated());
  83. asset.Type = pack.AssetBlock.Type;
  84. asset.InvType = asset.Type;
  85. asset.Name = "NewClothing" + Util.RandomClass.Next(1, 1000).ToString("000");
  86. asset.Data = pack.AssetBlock.AssetData;
  87. }
  88. if (asset != null)
  89. {
  90. this.AddUpload(pack.AssetBlock.TransactionID, asset);
  91. }
  92. else
  93. {
  94. //currently we don't support this asset type
  95. //so lets just tell the client that the upload is complete
  96. AssetUploadCompletePacket response = new AssetUploadCompletePacket();
  97. response.AssetBlock.Type = pack.AssetBlock.Type;
  98. response.AssetBlock.Success = true;
  99. response.AssetBlock.UUID = pack.AssetBlock.TransactionID.Combine(this.ourClient.SecureSessionID);
  100. this.ourClient.OutPacket(response);
  101. }
  102. }
  103. #region Xfer packet system for larger uploads
  104. public void HandleXferPacket(SendXferPacketPacket xferPacket)
  105. {
  106. lock (this.transactions)
  107. {
  108. foreach (AssetTransaction trans in this.transactions.Values)
  109. {
  110. if (trans.XferID == xferPacket.XferID.ID)
  111. {
  112. if (trans.Asset.Data.Length > 1)
  113. {
  114. byte[] newArray = new byte[trans.Asset.Data.Length + xferPacket.DataPacket.Data.Length];
  115. Array.Copy(trans.Asset.Data, 0, newArray, 0, trans.Asset.Data.Length);
  116. Array.Copy(xferPacket.DataPacket.Data, 0, newArray, trans.Asset.Data.Length, xferPacket.DataPacket.Data.Length);
  117. trans.Asset.Data = newArray;
  118. }
  119. else
  120. {
  121. byte[] newArray = new byte[xferPacket.DataPacket.Data.Length - 4];
  122. Array.Copy(xferPacket.DataPacket.Data, 4, newArray, 0, xferPacket.DataPacket.Data.Length - 4);
  123. trans.Asset.Data = newArray;
  124. }
  125. if ((xferPacket.XferID.Packet & 2147483648) != 0)
  126. {
  127. //end of transfer
  128. trans.UploadComplete = true;
  129. AssetUploadCompletePacket response = new AssetUploadCompletePacket();
  130. response.AssetBlock.Type = trans.Asset.Type;
  131. response.AssetBlock.Success = true;
  132. response.AssetBlock.UUID = trans.TransactionID.Combine(this.ourClient.SecureSessionID);
  133. this.ourClient.OutPacket(response);
  134. m_assetCache.AddAsset(trans.Asset);
  135. //check if we should add it to inventory
  136. if (trans.AddToInventory)
  137. {
  138. // m_assetCache.AddAsset(trans.Asset);
  139. m_inventoryCache.AddNewInventoryItem(this.ourClient, trans.InventFolder, trans.Asset);
  140. }
  141. }
  142. break;
  143. }
  144. }
  145. }
  146. ConfirmXferPacketPacket confirmXfer = new ConfirmXferPacketPacket();
  147. confirmXfer.XferID.ID = xferPacket.XferID.ID;
  148. confirmXfer.XferID.Packet = xferPacket.XferID.Packet;
  149. this.ourClient.OutPacket(confirmXfer);
  150. }
  151. #endregion
  152. public AssetBase AddUploadToAssetCache(LLUUID transactionID)
  153. {
  154. AssetBase asset = null;
  155. if (this.transactions.ContainsKey(transactionID))
  156. {
  157. AssetTransaction trans = this.transactions[transactionID];
  158. if (trans.UploadComplete)
  159. {
  160. m_assetCache.AddAsset(trans.Asset);
  161. asset = trans.Asset;
  162. }
  163. }
  164. return asset;
  165. }
  166. public void CreateInventoryItem(CreateInventoryItemPacket packet)
  167. {
  168. if (this.transactions.ContainsKey(packet.InventoryBlock.TransactionID))
  169. {
  170. AssetTransaction trans = this.transactions[packet.InventoryBlock.TransactionID];
  171. trans.Asset.Description = Helpers.FieldToString(packet.InventoryBlock.Description);
  172. trans.Asset.Name = Helpers.FieldToString(packet.InventoryBlock.Name);
  173. trans.Asset.Type = packet.InventoryBlock.Type;
  174. trans.Asset.InvType = packet.InventoryBlock.InvType;
  175. if (trans.UploadComplete)
  176. {
  177. //already complete so we can add it to the inventory
  178. //m_assetCache.AddAsset(trans.Asset);
  179. m_inventoryCache.AddNewInventoryItem(this.ourClient, packet.InventoryBlock.FolderID, trans.Asset);
  180. }
  181. else
  182. {
  183. trans.AddToInventory = true;
  184. trans.InventFolder = packet.InventoryBlock.FolderID;
  185. }
  186. }
  187. }
  188. private class AssetTransaction
  189. {
  190. public uint XferID;
  191. public AssetBase Asset;
  192. public bool AddToInventory;
  193. public LLUUID InventFolder = LLUUID.Zero;
  194. public bool UploadComplete = false;
  195. public LLUUID TransactionID = LLUUID.Zero;
  196. public AssetTransaction()
  197. {
  198. }
  199. }
  200. }
  201. }