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