AgentAssetsTransactions.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 System.IO;
  30. using libsecondlife;
  31. using libsecondlife.Packets;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications.Cache;
  34. namespace OpenSim.Region.Environment.Modules
  35. {
  36. /// <summary>
  37. /// Manage asset transactions for a single agent.
  38. /// </summary>
  39. public class AgentAssetTransactions
  40. {
  41. //private static readonly log4net.ILog m_log
  42. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  43. // Fields
  44. public LLUUID UserID;
  45. public Dictionary<LLUUID, AssetXferUploader> XferUploaders = new Dictionary<LLUUID, AssetXferUploader>();
  46. public AgentAssetTransactionsManager Manager;
  47. private bool m_dumpAssetsToFile;
  48. // Methods
  49. public AgentAssetTransactions(LLUUID agentID, AgentAssetTransactionsManager manager, bool dumpAssetsToFile)
  50. {
  51. UserID = agentID;
  52. Manager = manager;
  53. m_dumpAssetsToFile = dumpAssetsToFile;
  54. }
  55. public AssetXferUploader RequestXferUploader(LLUUID transactionID)
  56. {
  57. if (!XferUploaders.ContainsKey(transactionID))
  58. {
  59. AssetXferUploader uploader = new AssetXferUploader(this, m_dumpAssetsToFile);
  60. lock (XferUploaders)
  61. {
  62. XferUploaders.Add(transactionID, uploader);
  63. }
  64. return uploader;
  65. }
  66. return null;
  67. }
  68. public void HandleXfer(ulong xferID, uint packetID, byte[] data)
  69. {
  70. // AssetXferUploader uploaderFound = null;
  71. lock (XferUploaders)
  72. {
  73. foreach (AssetXferUploader uploader in XferUploaders.Values)
  74. {
  75. if (uploader.XferID == xferID)
  76. {
  77. uploader.HandleXferPacket(xferID, packetID, data);
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. public void RequestCreateInventoryItem(IClientAPI remoteClient, LLUUID transactionID, LLUUID folderID,
  84. uint callbackID, string description, string name, sbyte invType,
  85. sbyte type, byte wearableType, uint nextOwnerMask)
  86. {
  87. if (XferUploaders.ContainsKey(transactionID))
  88. {
  89. XferUploaders[transactionID].RequestCreateInventoryItem(remoteClient, transactionID, folderID,
  90. callbackID, description, name, invType, type,
  91. wearableType, nextOwnerMask);
  92. }
  93. }
  94. public void RequestUpdateInventoryItem(IClientAPI remoteClient, LLUUID transactionID,
  95. InventoryItemBase item)
  96. {
  97. if (XferUploaders.ContainsKey(transactionID))
  98. {
  99. XferUploaders[transactionID].RequestUpdateInventoryItem(remoteClient, transactionID, item);
  100. }
  101. }
  102. /// <summary>
  103. /// Get an uploaded asset. If the data is successfully retrieved, the transaction will be removed.
  104. /// </summary>
  105. /// <param name="transactionID"></param>
  106. /// <returns>The asset if the upload has completed, null if it has not.</returns>
  107. public AssetBase GetTransactionAsset(LLUUID transactionID)
  108. {
  109. if (XferUploaders.ContainsKey(transactionID))
  110. {
  111. AssetXferUploader uploader = XferUploaders[transactionID];
  112. AssetBase asset = uploader.GetAssetData();
  113. lock (XferUploaders)
  114. {
  115. XferUploaders.Remove(transactionID);
  116. }
  117. return asset;
  118. }
  119. return null;
  120. }
  121. // Nested Types
  122. public class AssetXferUploader
  123. {
  124. // Fields
  125. public bool AddToInventory;
  126. public AssetBase Asset;
  127. public LLUUID InventFolder = LLUUID.Zero;
  128. private IClientAPI ourClient;
  129. public LLUUID TransactionID = LLUUID.Zero;
  130. public bool UploadComplete;
  131. public ulong XferID;
  132. private string m_name = String.Empty;
  133. private string m_description = String.Empty;
  134. private sbyte type = 0;
  135. private sbyte invType = 0;
  136. private uint nextPerm = 0;
  137. private bool m_finished = false;
  138. private bool m_createItem = false;
  139. private AgentAssetTransactions m_userTransactions;
  140. private bool m_storeLocal;
  141. private bool m_dumpAssetToFile;
  142. public AssetXferUploader(AgentAssetTransactions transactions, bool dumpAssetToFile)
  143. {
  144. m_userTransactions = transactions;
  145. m_dumpAssetToFile = dumpAssetToFile;
  146. }
  147. /// <summary>
  148. /// Process transfer data received from the client.
  149. /// </summary>
  150. /// <param name="xferID"></param>
  151. /// <param name="packetID"></param>
  152. /// <param name="data"></param>
  153. /// <returns>True if the transfer is complete, false otherwise or if the xferID was not valid</returns>
  154. public bool HandleXferPacket(ulong xferID, uint packetID, byte[] data)
  155. {
  156. if (XferID == xferID)
  157. {
  158. if (Asset.Data.Length > 1)
  159. {
  160. byte[] destinationArray = new byte[Asset.Data.Length + data.Length];
  161. Array.Copy(Asset.Data, 0, destinationArray, 0, Asset.Data.Length);
  162. Array.Copy(data, 0, destinationArray, Asset.Data.Length, data.Length);
  163. Asset.Data = destinationArray;
  164. }
  165. else
  166. {
  167. byte[] buffer2 = new byte[data.Length - 4];
  168. Array.Copy(data, 4, buffer2, 0, data.Length - 4);
  169. Asset.Data = buffer2;
  170. }
  171. ConfirmXferPacketPacket newPack = new ConfirmXferPacketPacket();
  172. newPack.XferID.ID = xferID;
  173. newPack.XferID.Packet = packetID;
  174. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  175. if ((packetID & 0x80000000) != 0)
  176. {
  177. SendCompleteMessage();
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. /// <summary>
  184. /// Initialise asset transfer from the client
  185. /// </summary>
  186. /// <param name="xferID"></param>
  187. /// <param name="packetID"></param>
  188. /// <param name="data"></param>
  189. /// <returns>True if the transfer is complete, false otherwise</returns>
  190. public bool Initialise(IClientAPI remoteClient, LLUUID assetID, LLUUID transaction, sbyte type, byte[] data,
  191. bool storeLocal, bool tempFile)
  192. {
  193. ourClient = remoteClient;
  194. Asset = new AssetBase();
  195. Asset.FullID = assetID;
  196. Asset.InvType = type;
  197. Asset.Type = type;
  198. Asset.Data = data;
  199. Asset.Name = "blank";
  200. Asset.Description = "empty";
  201. Asset.Local = storeLocal;
  202. Asset.Temporary = tempFile;
  203. TransactionID = transaction;
  204. m_storeLocal = storeLocal;
  205. if (Asset.Data.Length > 2)
  206. {
  207. SendCompleteMessage();
  208. return true;
  209. }
  210. else
  211. {
  212. RequestStartXfer();
  213. }
  214. return false;
  215. }
  216. protected void RequestStartXfer()
  217. {
  218. UploadComplete = false;
  219. XferID = Util.GetNextXferID();
  220. RequestXferPacket newPack = new RequestXferPacket();
  221. newPack.XferID.ID = XferID;
  222. newPack.XferID.VFileType = Asset.Type;
  223. newPack.XferID.VFileID = Asset.FullID;
  224. newPack.XferID.FilePath = 0;
  225. newPack.XferID.Filename = new byte[0];
  226. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  227. }
  228. protected void SendCompleteMessage()
  229. {
  230. UploadComplete = true;
  231. AssetUploadCompletePacket newPack = new AssetUploadCompletePacket();
  232. newPack.AssetBlock.Type = Asset.Type;
  233. newPack.AssetBlock.Success = true;
  234. newPack.AssetBlock.UUID = Asset.FullID;
  235. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  236. m_finished = true;
  237. if (m_createItem)
  238. {
  239. DoCreateItem();
  240. }
  241. else if (m_storeLocal)
  242. {
  243. m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
  244. }
  245. // Console.WriteLine("upload complete "+ this.TransactionID);
  246. if (m_dumpAssetToFile)
  247. {
  248. DateTime now = DateTime.Now;
  249. string filename =
  250. String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat", now.Year, now.Month, now.Day,
  251. now.Hour, now.Minute, now.Second, Asset.Name, Asset.Type);
  252. SaveAssetToFile(filename, Asset.Data);
  253. }
  254. }
  255. ///Left this in and commented in case there are unforseen issues
  256. //private void SaveAssetToFile(string filename, byte[] data)
  257. //{
  258. // FileStream fs = File.Create(filename);
  259. // BinaryWriter bw = new BinaryWriter(fs);
  260. // bw.Write(data);
  261. // bw.Close();
  262. // fs.Close();
  263. //}
  264. private void SaveAssetToFile(string filename, byte[] data)
  265. {
  266. string assetPath = "UserAssets";
  267. if (!Directory.Exists(assetPath))
  268. {
  269. Directory.CreateDirectory(assetPath);
  270. }
  271. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  272. BinaryWriter bw = new BinaryWriter(fs);
  273. bw.Write(data);
  274. bw.Close();
  275. fs.Close();
  276. }
  277. public void RequestCreateInventoryItem(IClientAPI remoteClient, LLUUID transactionID, LLUUID folderID,
  278. uint callbackID, string description, string name, sbyte invType,
  279. sbyte type, byte wearableType, uint nextOwnerMask)
  280. {
  281. if (TransactionID == transactionID)
  282. {
  283. InventFolder = folderID;
  284. m_name = name;
  285. m_description = description;
  286. this.type = type;
  287. this.invType = invType;
  288. nextPerm = nextOwnerMask;
  289. Asset.Name = name;
  290. Asset.Description = description;
  291. Asset.Type = type;
  292. Asset.InvType = invType;
  293. m_createItem = true;
  294. if (m_finished)
  295. {
  296. DoCreateItem();
  297. }
  298. }
  299. }
  300. public void RequestUpdateInventoryItem(IClientAPI remoteClient, LLUUID transactionID,
  301. InventoryItemBase item)
  302. {
  303. if (TransactionID == transactionID)
  304. {
  305. CachedUserInfo userInfo =
  306. m_userTransactions.Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(
  307. remoteClient.AgentId);
  308. if (userInfo != null)
  309. {
  310. LLUUID assetID = LLUUID.Combine(transactionID, remoteClient.SecureSessionId);
  311. AssetBase asset
  312. = m_userTransactions.Manager.MyScene.CommsManager.AssetCache.GetAsset(
  313. assetID, (item.assetType == (int) AssetType.Texture ? true : false));
  314. if (asset == null)
  315. {
  316. asset = m_userTransactions.GetTransactionAsset(transactionID);
  317. }
  318. if (asset != null && asset.FullID == assetID)
  319. {
  320. asset.Name = item.inventoryName;
  321. asset.Description = item.inventoryDescription;
  322. asset.InvType = (sbyte) item.invType;
  323. asset.Type = (sbyte) item.assetType;
  324. item.assetID = asset.FullID;
  325. m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
  326. }
  327. userInfo.UpdateItem(remoteClient.AgentId, item);
  328. }
  329. }
  330. }
  331. private void DoCreateItem()
  332. {
  333. //really need to fix this call, if lbsa71 saw this he would die.
  334. m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
  335. CachedUserInfo userInfo =
  336. m_userTransactions.Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(ourClient.AgentId);
  337. if (userInfo != null)
  338. {
  339. InventoryItemBase item = new InventoryItemBase();
  340. item.avatarID = ourClient.AgentId;
  341. item.creatorsID = ourClient.AgentId;
  342. item.inventoryID = LLUUID.Random();
  343. item.assetID = Asset.FullID;
  344. item.inventoryDescription = m_description;
  345. item.inventoryName = m_name;
  346. item.assetType = type;
  347. item.invType = invType;
  348. item.parentFolderID = InventFolder;
  349. item.inventoryBasePermissions = 2147483647;
  350. item.inventoryCurrentPermissions = 2147483647;
  351. item.inventoryNextPermissions = nextPerm;
  352. userInfo.AddItem(ourClient.AgentId, item);
  353. ourClient.SendInventoryItemCreateUpdate(item);
  354. }
  355. }
  356. public AssetBase GetAssetData()
  357. {
  358. if (m_finished)
  359. {
  360. return Asset;
  361. }
  362. return null;
  363. }
  364. }
  365. }
  366. }