AgentAssetUpload.cs 9.1 KB

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