ClientView.AgentAssetUpload.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using libsecondlife;
  31. using libsecondlife.Packets;
  32. using OpenSim.Assets;
  33. using OpenSim.Framework.Interfaces;
  34. using OpenSim.Framework.Types;
  35. using OpenSim.Framework.Utilities;
  36. using OpenSim.Region.Caches;
  37. namespace OpenSim.Region.ClientStack
  38. {
  39. partial class ClientView
  40. {
  41. public class AgentAssetUpload
  42. {
  43. private Dictionary<LLUUID, AssetTransaction> transactions = new Dictionary<LLUUID, AssetTransaction>();
  44. private ClientView ourClient;
  45. private AssetCache m_assetCache;
  46. private InventoryCache m_inventoryCache;
  47. public AgentAssetUpload(ClientView client, AssetCache assetCache, InventoryCache inventoryCache)
  48. {
  49. this.ourClient = client;
  50. m_assetCache = assetCache;
  51. m_inventoryCache = inventoryCache;
  52. }
  53. public void AddUpload(LLUUID transactionID, AssetBase asset)
  54. {
  55. AssetTransaction upload = new AssetTransaction();
  56. lock (this.transactions)
  57. {
  58. upload.Asset = asset;
  59. upload.TransactionID = transactionID;
  60. this.transactions.Add(transactionID, upload);
  61. }
  62. if (upload.Asset.Data.Length > 2)
  63. {
  64. //is complete
  65. upload.UploadComplete = true;
  66. AssetUploadCompletePacket response = new AssetUploadCompletePacket();
  67. response.AssetBlock.Type = asset.Type;
  68. response.AssetBlock.Success = true;
  69. response.AssetBlock.UUID = transactionID.Combine(this.ourClient.SecureSessionID);
  70. this.ourClient.OutPacket(response);
  71. m_assetCache.AddAsset(asset);
  72. }
  73. else
  74. {
  75. upload.UploadComplete = false;
  76. upload.XferID = Util.GetNextXferID();
  77. RequestXferPacket xfer = new RequestXferPacket();
  78. xfer.XferID.ID = upload.XferID;
  79. xfer.XferID.VFileType = upload.Asset.Type;
  80. xfer.XferID.VFileID = transactionID.Combine(this.ourClient.SecureSessionID);
  81. xfer.XferID.FilePath = 0;
  82. xfer.XferID.Filename = new byte[0];
  83. this.ourClient.OutPacket(xfer);
  84. }
  85. }
  86. public AssetBase GetUpload(LLUUID transactionID)
  87. {
  88. if (this.transactions.ContainsKey(transactionID))
  89. {
  90. return this.transactions[transactionID].Asset;
  91. }
  92. return null;
  93. }
  94. public void HandleUploadPacket(AssetUploadRequestPacket pack, LLUUID assetID)
  95. {
  96. // Console.Write("asset upload request , type = " + pack.AssetBlock.Type.ToString());
  97. AssetBase asset = null;
  98. if (pack.AssetBlock.Type == 0)
  99. {
  100. //first packet for transaction
  101. asset = new AssetBase();
  102. asset.FullID = assetID;
  103. asset.Type = pack.AssetBlock.Type;
  104. asset.InvType = asset.Type;
  105. asset.Name = "UploadedTexture" + Util.RandomClass.Next(1, 1000).ToString("000");
  106. asset.Data = pack.AssetBlock.AssetData;
  107. }
  108. else if (pack.AssetBlock.Type == 13 | pack.AssetBlock.Type == 5 | pack.AssetBlock.Type == 7)
  109. {
  110. asset = new AssetBase();
  111. asset.FullID = assetID;
  112. // Console.WriteLine("skin asset id is " + assetID.ToStringHyphenated());
  113. asset.Type = pack.AssetBlock.Type;
  114. asset.InvType = asset.Type;
  115. asset.Name = "NewClothing" + Util.RandomClass.Next(1, 1000).ToString("000");
  116. asset.Data = pack.AssetBlock.AssetData;
  117. }
  118. if (asset != null)
  119. {
  120. this.AddUpload(pack.AssetBlock.TransactionID, asset);
  121. }
  122. else
  123. {
  124. //currently we don't support this asset type
  125. //so lets just tell the client that the upload is complete
  126. AssetUploadCompletePacket response = new AssetUploadCompletePacket();
  127. response.AssetBlock.Type = pack.AssetBlock.Type;
  128. response.AssetBlock.Success = true;
  129. response.AssetBlock.UUID = pack.AssetBlock.TransactionID.Combine(this.ourClient.SecureSessionID);
  130. this.ourClient.OutPacket(response);
  131. }
  132. }
  133. #region Xfer packet system for larger uploads
  134. public void HandleXferPacket(SendXferPacketPacket xferPacket)
  135. {
  136. lock (this.transactions)
  137. {
  138. foreach (AssetTransaction trans in this.transactions.Values)
  139. {
  140. if (trans.XferID == xferPacket.XferID.ID)
  141. {
  142. if (trans.Asset.Data.Length > 1)
  143. {
  144. byte[] newArray = new byte[trans.Asset.Data.Length + xferPacket.DataPacket.Data.Length];
  145. Array.Copy(trans.Asset.Data, 0, newArray, 0, trans.Asset.Data.Length);
  146. Array.Copy(xferPacket.DataPacket.Data, 0, newArray, trans.Asset.Data.Length, xferPacket.DataPacket.Data.Length);
  147. trans.Asset.Data = newArray;
  148. }
  149. else
  150. {
  151. byte[] newArray = new byte[xferPacket.DataPacket.Data.Length - 4];
  152. Array.Copy(xferPacket.DataPacket.Data, 4, newArray, 0, xferPacket.DataPacket.Data.Length - 4);
  153. trans.Asset.Data = newArray;
  154. }
  155. if ((xferPacket.XferID.Packet & 2147483648) != 0)
  156. {
  157. //end of transfer
  158. trans.UploadComplete = true;
  159. AssetUploadCompletePacket response = new AssetUploadCompletePacket();
  160. response.AssetBlock.Type = trans.Asset.Type;
  161. response.AssetBlock.Success = true;
  162. response.AssetBlock.UUID = trans.TransactionID.Combine(this.ourClient.SecureSessionID);
  163. this.ourClient.OutPacket(response);
  164. m_assetCache.AddAsset(trans.Asset);
  165. //check if we should add it to inventory
  166. if (trans.AddToInventory)
  167. {
  168. // m_assetCache.AddAsset(trans.Asset);
  169. m_inventoryCache.AddNewInventoryItem(this.ourClient, trans.InventFolder, trans.Asset);
  170. }
  171. }
  172. break;
  173. }
  174. }
  175. }
  176. ConfirmXferPacketPacket confirmXfer = new ConfirmXferPacketPacket();
  177. confirmXfer.XferID.ID = xferPacket.XferID.ID;
  178. confirmXfer.XferID.Packet = xferPacket.XferID.Packet;
  179. this.ourClient.OutPacket(confirmXfer);
  180. }
  181. #endregion
  182. public AssetBase AddUploadToAssetCache(LLUUID transactionID)
  183. {
  184. AssetBase asset = null;
  185. if (this.transactions.ContainsKey(transactionID))
  186. {
  187. AssetTransaction trans = this.transactions[transactionID];
  188. if (trans.UploadComplete)
  189. {
  190. m_assetCache.AddAsset(trans.Asset);
  191. asset = trans.Asset;
  192. }
  193. }
  194. return asset;
  195. }
  196. public void CreateInventoryItem(CreateInventoryItemPacket packet)
  197. {
  198. if (this.transactions.ContainsKey(packet.InventoryBlock.TransactionID))
  199. {
  200. AssetTransaction trans = this.transactions[packet.InventoryBlock.TransactionID];
  201. trans.Asset.Description = Util.FieldToString(packet.InventoryBlock.Description);
  202. trans.Asset.Name = Util.FieldToString(packet.InventoryBlock.Name);
  203. trans.Asset.Type = packet.InventoryBlock.Type;
  204. trans.Asset.InvType = packet.InventoryBlock.InvType;
  205. if (trans.UploadComplete)
  206. {
  207. //already complete so we can add it to the inventory
  208. //m_assetCache.AddAsset(trans.Asset);
  209. m_inventoryCache.AddNewInventoryItem(this.ourClient, packet.InventoryBlock.FolderID, trans.Asset);
  210. }
  211. else
  212. {
  213. trans.AddToInventory = true;
  214. trans.InventFolder = packet.InventoryBlock.FolderID;
  215. }
  216. }
  217. }
  218. private class AssetTransaction
  219. {
  220. public uint XferID;
  221. public AssetBase Asset;
  222. public bool AddToInventory;
  223. public LLUUID InventFolder = LLUUID.Zero;
  224. public bool UploadComplete = false;
  225. public LLUUID TransactionID = LLUUID.Zero;
  226. public AssetTransaction()
  227. {
  228. }
  229. }
  230. //new class , not currently used.
  231. public class AssetXferUploader
  232. {
  233. private IClientAPI ourClient;
  234. public bool UploadComplete = false;
  235. public bool AddToInventory;
  236. public LLUUID InventFolder = LLUUID.Zero;
  237. public uint XferID;
  238. public AssetBase Asset;
  239. public LLUUID TransactionID = LLUUID.Zero;
  240. public AssetXferUploader(IClientAPI remoteClient, LLUUID assetID, LLUUID transaction, sbyte type, byte[] data)
  241. {
  242. ourClient = remoteClient;
  243. Asset = new AssetBase();
  244. Asset.FullID = assetID;
  245. Asset.InvType = type;
  246. Asset.Type = type;
  247. Asset.Data = data;
  248. Asset.Name = "blank";
  249. Asset.Description = "empty";
  250. TransactionID = transaction;
  251. if (Asset.Data.Length > 2)
  252. {
  253. //data block should only have data in it, if there is no more data to be uploaded
  254. this.SendCompleteMessage();
  255. }
  256. else
  257. {
  258. this.ReqestStartXfer();
  259. }
  260. }
  261. protected void SendCompleteMessage()
  262. {
  263. UploadComplete = true;
  264. AssetUploadCompletePacket response = new AssetUploadCompletePacket();
  265. response.AssetBlock.Type = Asset.Type;
  266. response.AssetBlock.Success = true;
  267. response.AssetBlock.UUID = Asset.FullID;
  268. this.ourClient.OutPacket(response);
  269. //TODO trigger event
  270. }
  271. protected void ReqestStartXfer()
  272. {
  273. UploadComplete = false;
  274. XferID = Util.GetNextXferID();
  275. RequestXferPacket xfer = new RequestXferPacket();
  276. xfer.XferID.ID = XferID;
  277. xfer.XferID.VFileType = Asset.Type;
  278. xfer.XferID.VFileID = Asset.FullID;
  279. xfer.XferID.FilePath = 0;
  280. xfer.XferID.Filename = new byte[0];
  281. this.ourClient.OutPacket(xfer);
  282. }
  283. public void HandleXferPacket(uint xferID, uint packetID, byte[] data)
  284. {
  285. if (XferID == xferID)
  286. {
  287. if (Asset.Data.Length > 1)
  288. {
  289. byte[] newArray = new byte[Asset.Data.Length + data.Length];
  290. Array.Copy(Asset.Data, 0, newArray, 0, Asset.Data.Length);
  291. Array.Copy(data, 0, newArray, Asset.Data.Length, data.Length);
  292. Asset.Data = newArray;
  293. }
  294. else
  295. {
  296. byte[] newArray = new byte[data.Length - 4];
  297. Array.Copy(data, 4, newArray, 0, data.Length - 4);
  298. Asset.Data = newArray;
  299. }
  300. ConfirmXferPacketPacket confirmXfer = new ConfirmXferPacketPacket();
  301. confirmXfer.XferID.ID = xferID;
  302. confirmXfer.XferID.Packet = packetID;
  303. this.ourClient.OutPacket(confirmXfer);
  304. if ((packetID & 2147483648) != 0)
  305. {
  306. this.SendCompleteMessage();
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. }