AgentAssetsTransactions.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using System.IO;
  32. using libsecondlife;
  33. using libsecondlife.Packets;
  34. using OpenSim.Framework.Servers;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications.Cache;
  37. namespace OpenSim.Region.Environment.Modules
  38. {
  39. /// <summary>
  40. /// Manage asset transactions for a single agent.
  41. /// </summary>
  42. public class AgentAssetTransactions
  43. {
  44. //private static readonly log4net.ILog m_log
  45. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  46. // Fields
  47. public LLUUID UserID;
  48. public Dictionary<LLUUID, AssetXferUploader> XferUploaders = new Dictionary<LLUUID, AssetXferUploader>();
  49. public AgentAssetTransactionsManager Manager;
  50. private bool m_dumpAssetsToFile;
  51. // Methods
  52. public AgentAssetTransactions(LLUUID agentID, AgentAssetTransactionsManager manager, bool dumpAssetsToFile)
  53. {
  54. UserID = agentID;
  55. Manager = manager;
  56. m_dumpAssetsToFile = dumpAssetsToFile;
  57. }
  58. public AssetXferUploader RequestXferUploader(LLUUID transactionID)
  59. {
  60. if (!XferUploaders.ContainsKey(transactionID))
  61. {
  62. AssetXferUploader uploader = new AssetXferUploader(this, m_dumpAssetsToFile);
  63. lock (XferUploaders)
  64. {
  65. XferUploaders.Add(transactionID, uploader);
  66. }
  67. return uploader;
  68. }
  69. return null;
  70. }
  71. public void HandleXfer(ulong xferID, uint packetID, byte[] data)
  72. {
  73. AssetXferUploader uploaderFound = null;
  74. lock (XferUploaders)
  75. {
  76. foreach (AssetXferUploader uploader in XferUploaders.Values)
  77. {
  78. if (uploader.XferID == xferID)
  79. {
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. public void RequestCreateInventoryItem(IClientAPI remoteClient, LLUUID transactionID, LLUUID folderID,
  86. uint callbackID, string description, string name, sbyte invType,
  87. sbyte type, byte wearableType, uint nextOwnerMask)
  88. {
  89. if (XferUploaders.ContainsKey(transactionID))
  90. {
  91. XferUploaders[transactionID].RequestCreateInventoryItem(remoteClient, transactionID, folderID,
  92. callbackID, description, name, invType, type,
  93. wearableType, nextOwnerMask);
  94. }
  95. }
  96. public void RequestUpdateInventoryItem(IClientAPI remoteClient, LLUUID transactionID,
  97. InventoryItemBase item)
  98. {
  99. if (XferUploaders.ContainsKey(transactionID))
  100. {
  101. XferUploaders[transactionID].RequestUpdateInventoryItem(remoteClient, transactionID, item);
  102. }
  103. }
  104. /// <summary>
  105. /// Get an uploaded asset. If the data is successfully retrieved, the transaction will be removed.
  106. /// </summary>
  107. /// <param name="transactionID"></param>
  108. /// <returns>The asset if the upload has completed, null if it has not.</returns>
  109. public AssetBase GetTransactionAsset(LLUUID transactionID)
  110. {
  111. if (XferUploaders.ContainsKey(transactionID))
  112. {
  113. AssetXferUploader uploader = XferUploaders[transactionID];
  114. AssetBase asset = uploader.GetAssetData();
  115. lock (XferUploaders)
  116. {
  117. XferUploaders.Remove(transactionID);
  118. }
  119. return asset;
  120. }
  121. return null;
  122. }
  123. // Nested Types
  124. public class AssetXferUploader
  125. {
  126. // Fields
  127. public bool AddToInventory;
  128. public AssetBase Asset;
  129. public LLUUID InventFolder = LLUUID.Zero;
  130. private IClientAPI ourClient;
  131. public LLUUID TransactionID = LLUUID.Zero;
  132. public bool UploadComplete;
  133. public ulong XferID;
  134. private string m_name = String.Empty;
  135. private string m_description = String.Empty;
  136. private sbyte type = 0;
  137. private sbyte invType = 0;
  138. private uint nextPerm = 0;
  139. private bool m_finished = false;
  140. private bool m_createItem = false;
  141. private AgentAssetTransactions m_userTransactions;
  142. private bool m_storeLocal;
  143. private bool m_dumpAssetToFile;
  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. nextPerm = nextOwnerMask;
  291. Asset.Name = name;
  292. Asset.Description = description;
  293. Asset.Type = type;
  294. Asset.InvType = invType;
  295. m_createItem = true;
  296. if (m_finished)
  297. {
  298. DoCreateItem();
  299. }
  300. }
  301. }
  302. public void RequestUpdateInventoryItem(IClientAPI remoteClient, LLUUID transactionID,
  303. InventoryItemBase item)
  304. {
  305. if (TransactionID == transactionID)
  306. {
  307. CachedUserInfo userInfo =
  308. m_userTransactions.Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(
  309. remoteClient.AgentId);
  310. if (userInfo != null)
  311. {
  312. LLUUID assetID = LLUUID.Combine(transactionID, remoteClient.SecureSessionId);
  313. AssetBase asset
  314. = m_userTransactions.Manager.MyScene.CommsManager.AssetCache.GetAsset(
  315. assetID, (item.assetType == (int) AssetType.Texture ? true : false));
  316. if (asset == null)
  317. {
  318. asset = m_userTransactions.GetTransactionAsset(transactionID);
  319. }
  320. if (asset != null && asset.FullID == assetID)
  321. {
  322. asset.Name = item.inventoryName;
  323. asset.Description = item.inventoryDescription;
  324. asset.InvType = (sbyte) item.invType;
  325. asset.Type = (sbyte) item.assetType;
  326. item.assetID = asset.FullID;
  327. m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
  328. }
  329. userInfo.UpdateItem(remoteClient.AgentId, item);
  330. }
  331. }
  332. }
  333. private void DoCreateItem()
  334. {
  335. //really need to fix this call, if lbsa71 saw this he would die.
  336. m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
  337. CachedUserInfo userInfo =
  338. m_userTransactions.Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(ourClient.AgentId);
  339. if (userInfo != null)
  340. {
  341. InventoryItemBase item = new InventoryItemBase();
  342. item.avatarID = ourClient.AgentId;
  343. item.creatorsID = ourClient.AgentId;
  344. item.inventoryID = LLUUID.Random();
  345. item.assetID = Asset.FullID;
  346. item.inventoryDescription = m_description;
  347. item.inventoryName = m_name;
  348. item.assetType = type;
  349. item.invType = invType;
  350. item.parentFolderID = InventFolder;
  351. item.inventoryBasePermissions = 2147483647;
  352. item.inventoryCurrentPermissions = 2147483647;
  353. item.inventoryNextPermissions = nextPerm;
  354. userInfo.AddItem(ourClient.AgentId, item);
  355. ourClient.SendInventoryItemCreateUpdate(item);
  356. }
  357. }
  358. public AssetBase GetAssetData()
  359. {
  360. if (m_finished)
  361. {
  362. return Asset;
  363. }
  364. return null;
  365. }
  366. }
  367. }
  368. }