AgentAssetsTransactions.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.Agent.AssetTransaction
  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. private bool m_dumpAssetsToFile;
  45. public AgentAssetTransactionsManager Manager;
  46. public LLUUID UserID;
  47. public Dictionary<LLUUID, AssetXferUploader> XferUploaders = new Dictionary<LLUUID, AssetXferUploader>();
  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. #region Nested type: AssetXferUploader
  123. public class AssetXferUploader
  124. {
  125. // Fields
  126. public bool AddToInventory;
  127. public AssetBase Asset;
  128. public LLUUID InventFolder = LLUUID.Zero;
  129. private sbyte invType = 0;
  130. private bool m_createItem = false;
  131. private string m_description = String.Empty;
  132. private bool m_dumpAssetToFile;
  133. private bool m_finished = false;
  134. private string m_name = String.Empty;
  135. private bool m_storeLocal;
  136. private AgentAssetTransactions m_userTransactions;
  137. private uint nextPerm = 0;
  138. private IClientAPI ourClient;
  139. public LLUUID TransactionID = LLUUID.Zero;
  140. private sbyte type = 0;
  141. public bool UploadComplete;
  142. private byte wearableType = 0;
  143. public ulong XferID;
  144. public AssetXferUploader(AgentAssetTransactions transactions, bool dumpAssetToFile)
  145. {
  146. m_userTransactions = transactions;
  147. m_dumpAssetToFile = dumpAssetToFile;
  148. }
  149. /// <summary>
  150. /// Process transfer data received from the client.
  151. /// </summary>
  152. /// <param name="xferID"></param>
  153. /// <param name="packetID"></param>
  154. /// <param name="data"></param>
  155. /// <returns>True if the transfer is complete, false otherwise or if the xferID was not valid</returns>
  156. public bool HandleXferPacket(ulong xferID, uint packetID, byte[] data)
  157. {
  158. if (XferID == xferID)
  159. {
  160. if (Asset.Data.Length > 1)
  161. {
  162. byte[] destinationArray = new byte[Asset.Data.Length + data.Length];
  163. Array.Copy(Asset.Data, 0, destinationArray, 0, Asset.Data.Length);
  164. Array.Copy(data, 0, destinationArray, Asset.Data.Length, data.Length);
  165. Asset.Data = destinationArray;
  166. }
  167. else
  168. {
  169. byte[] buffer2 = new byte[data.Length - 4];
  170. Array.Copy(data, 4, buffer2, 0, data.Length - 4);
  171. Asset.Data = buffer2;
  172. }
  173. ConfirmXferPacketPacket newPack = new ConfirmXferPacketPacket();
  174. newPack.XferID.ID = xferID;
  175. newPack.XferID.Packet = packetID;
  176. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  177. if ((packetID & 0x80000000) != 0)
  178. {
  179. SendCompleteMessage();
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. /// <summary>
  186. /// Initialise asset transfer from the client
  187. /// </summary>
  188. /// <param name="xferID"></param>
  189. /// <param name="packetID"></param>
  190. /// <param name="data"></param>
  191. /// <returns>True if the transfer is complete, false otherwise</returns>
  192. public bool Initialise(IClientAPI remoteClient, LLUUID assetID, LLUUID transaction, sbyte type, byte[] data,
  193. bool storeLocal, bool tempFile)
  194. {
  195. ourClient = remoteClient;
  196. Asset = new AssetBase();
  197. Asset.FullID = assetID;
  198. Asset.InvType = type;
  199. Asset.Type = type;
  200. Asset.Data = data;
  201. Asset.Name = "blank";
  202. Asset.Description = "empty";
  203. Asset.Local = storeLocal;
  204. Asset.Temporary = tempFile;
  205. TransactionID = transaction;
  206. m_storeLocal = storeLocal;
  207. if (Asset.Data.Length > 2)
  208. {
  209. SendCompleteMessage();
  210. return true;
  211. }
  212. else
  213. {
  214. RequestStartXfer();
  215. }
  216. return false;
  217. }
  218. protected void RequestStartXfer()
  219. {
  220. UploadComplete = false;
  221. XferID = Util.GetNextXferID();
  222. RequestXferPacket newPack = new RequestXferPacket();
  223. newPack.XferID.ID = XferID;
  224. newPack.XferID.VFileType = Asset.Type;
  225. newPack.XferID.VFileID = Asset.FullID;
  226. newPack.XferID.FilePath = 0;
  227. newPack.XferID.Filename = new byte[0];
  228. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  229. }
  230. protected void SendCompleteMessage()
  231. {
  232. UploadComplete = true;
  233. AssetUploadCompletePacket newPack = new AssetUploadCompletePacket();
  234. newPack.AssetBlock.Type = Asset.Type;
  235. newPack.AssetBlock.Success = true;
  236. newPack.AssetBlock.UUID = Asset.FullID;
  237. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  238. m_finished = true;
  239. if (m_createItem)
  240. {
  241. DoCreateItem();
  242. }
  243. else if (m_storeLocal)
  244. {
  245. m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
  246. }
  247. // Console.WriteLine("upload complete "+ this.TransactionID);
  248. if (m_dumpAssetToFile)
  249. {
  250. DateTime now = DateTime.Now;
  251. string filename =
  252. String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat", now.Year, now.Month, now.Day,
  253. now.Hour, now.Minute, now.Second, Asset.Name, Asset.Type);
  254. SaveAssetToFile(filename, Asset.Data);
  255. }
  256. }
  257. ///Left this in and commented in case there are unforseen issues
  258. //private void SaveAssetToFile(string filename, byte[] data)
  259. //{
  260. // FileStream fs = File.Create(filename);
  261. // BinaryWriter bw = new BinaryWriter(fs);
  262. // bw.Write(data);
  263. // bw.Close();
  264. // fs.Close();
  265. //}
  266. private void SaveAssetToFile(string filename, byte[] data)
  267. {
  268. string assetPath = "UserAssets";
  269. if (!Directory.Exists(assetPath))
  270. {
  271. Directory.CreateDirectory(assetPath);
  272. }
  273. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  274. BinaryWriter bw = new BinaryWriter(fs);
  275. bw.Write(data);
  276. bw.Close();
  277. fs.Close();
  278. }
  279. public void RequestCreateInventoryItem(IClientAPI remoteClient, LLUUID transactionID, LLUUID folderID,
  280. uint callbackID, string description, string name, sbyte invType,
  281. sbyte type, byte wearableType, uint nextOwnerMask)
  282. {
  283. if (TransactionID == transactionID)
  284. {
  285. InventFolder = folderID;
  286. m_name = name;
  287. m_description = description;
  288. this.type = type;
  289. this.invType = invType;
  290. this.wearableType = wearableType;
  291. nextPerm = nextOwnerMask;
  292. Asset.Name = name;
  293. Asset.Description = description;
  294. Asset.Type = type;
  295. Asset.InvType = invType;
  296. m_createItem = true;
  297. if (m_finished)
  298. {
  299. DoCreateItem();
  300. }
  301. }
  302. }
  303. public void RequestUpdateInventoryItem(IClientAPI remoteClient, LLUUID transactionID,
  304. InventoryItemBase item)
  305. {
  306. if (TransactionID == transactionID)
  307. {
  308. CachedUserInfo userInfo =
  309. m_userTransactions.Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(
  310. remoteClient.AgentId);
  311. if (userInfo != null)
  312. {
  313. LLUUID assetID = LLUUID.Combine(transactionID, remoteClient.SecureSessionId);
  314. AssetBase asset
  315. = m_userTransactions.Manager.MyScene.CommsManager.AssetCache.GetAsset(
  316. assetID, (item.AssetType == (int) AssetType.Texture ? true : false));
  317. if (asset == null)
  318. {
  319. asset = m_userTransactions.GetTransactionAsset(transactionID);
  320. }
  321. if (asset != null && asset.FullID == assetID)
  322. {
  323. asset.Name = item.Name;
  324. asset.Description = item.Description;
  325. asset.InvType = (sbyte) item.InvType;
  326. asset.Type = (sbyte) item.AssetType;
  327. item.AssetID = asset.FullID;
  328. m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
  329. }
  330. userInfo.UpdateItem(remoteClient.AgentId, item);
  331. }
  332. }
  333. }
  334. private void DoCreateItem()
  335. {
  336. //really need to fix this call, if lbsa71 saw this he would die.
  337. m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
  338. CachedUserInfo userInfo =
  339. m_userTransactions.Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(ourClient.AgentId);
  340. if (userInfo != null)
  341. {
  342. InventoryItemBase item = new InventoryItemBase();
  343. item.Owner = ourClient.AgentId;
  344. item.Creator = ourClient.AgentId;
  345. item.ID = LLUUID.Random();
  346. item.AssetID = Asset.FullID;
  347. item.Description = m_description;
  348. item.Name = m_name;
  349. item.AssetType = type;
  350. item.InvType = invType;
  351. item.Folder = InventFolder;
  352. item.BasePermissions = 2147483647;
  353. item.CurrentPermissions = 2147483647;
  354. item.NextPermissions = nextPerm;
  355. item.Flags = (uint) wearableType;
  356. userInfo.AddItem(ourClient.AgentId, item);
  357. ourClient.SendInventoryItemCreateUpdate(item);
  358. }
  359. }
  360. public AssetBase GetAssetData()
  361. {
  362. if (m_finished)
  363. {
  364. return Asset;
  365. }
  366. return null;
  367. }
  368. }
  369. #endregion
  370. }
  371. }