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