aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim.RegionServer/AgentAssetUpload.cs
blob: cdf49c545e1368eb1dc7db4b8a4f42aa0e4a5d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections.Generic;
using System.Text;
using OpenSim.Assets;
using OpenSim.Framework.Types;
using OpenSim.Framework.Utilities;
using libsecondlife;
using libsecondlife.Packets;

namespace OpenSim
{
    public class AgentAssetUpload
    {
        private Dictionary<LLUUID, AssetTransaction> transactions = new Dictionary<LLUUID, AssetTransaction>();
        private SimClient ourClient;
        private AssetCache m_assetCache;
        private InventoryCache m_inventoryCache;

        public AgentAssetUpload(SimClient client, AssetCache assetCache, InventoryCache inventoryCache)
        {
            this.ourClient = client;
            m_assetCache = assetCache;
            m_inventoryCache = inventoryCache;
        }

        public void AddUpload(LLUUID transactionID, AssetBase asset)
        {
            AssetTransaction upload = new AssetTransaction();
            lock (this.transactions)
            {
                upload.Asset = asset;
                upload.TransactionID = transactionID;
                this.transactions.Add(transactionID, upload);
            }
            if (upload.Asset.Data.Length > 2)
            {
                //is complete
                upload.UploadComplete = true;
                AssetUploadCompletePacket response = new AssetUploadCompletePacket();
                response.AssetBlock.Type = asset.Type;
                response.AssetBlock.Success = true;
                response.AssetBlock.UUID = transactionID.Combine(this.ourClient.SecureSessionID);
                this.ourClient.OutPacket(response);
                m_assetCache.AddAsset(asset);
            }
            else
            {
                upload.UploadComplete = false;
                upload.XferID = Util.GetNextXferID();
                RequestXferPacket xfer = new RequestXferPacket();
                xfer.XferID.ID = upload.XferID;
                xfer.XferID.VFileType = upload.Asset.Type;
                xfer.XferID.VFileID = transactionID.Combine(this.ourClient.SecureSessionID);
                xfer.XferID.FilePath = 0;
                xfer.XferID.Filename = new byte[0];
                this.ourClient.OutPacket(xfer);
            }

        }

        public AssetBase GetUpload(LLUUID transactionID)
        {
            if (this.transactions.ContainsKey(transactionID))
            {
                return this.transactions[transactionID].Asset;
            }

            return null;
        }

        public void HandleUploadPacket(AssetUploadRequestPacket pack, LLUUID assetID)
        {
           // Console.Write("asset upload request , type = " + pack.AssetBlock.Type.ToString());
            AssetBase asset = null;
            if (pack.AssetBlock.Type == 0)
            {

                //first packet for transaction
                asset = new AssetBase();
                asset.FullID = assetID;
                asset.Type = pack.AssetBlock.Type;
                asset.InvType = asset.Type;
                asset.Name = "UploadedTexture" + Util.RandomClass.Next(1, 1000).ToString("000");
                asset.Data = pack.AssetBlock.AssetData;


            }
            else if (pack.AssetBlock.Type == 13 | pack.AssetBlock.Type == 5 | pack.AssetBlock.Type == 7)
            {

                asset = new AssetBase();
                asset.FullID = assetID;
                //  Console.WriteLine("skin asset id is " + assetID.ToStringHyphenated());
                asset.Type = pack.AssetBlock.Type;
                asset.InvType = asset.Type;
                asset.Name = "NewClothing" + Util.RandomClass.Next(1, 1000).ToString("000");
                asset.Data = pack.AssetBlock.AssetData;


            }

            if (asset != null)
            {
                this.AddUpload(pack.AssetBlock.TransactionID, asset);
            }
            else
            {

                //currently we don't support this asset type 
                //so lets just tell the client that the upload is complete
                AssetUploadCompletePacket response = new AssetUploadCompletePacket();
                response.AssetBlock.Type = pack.AssetBlock.Type;
                response.AssetBlock.Success = true;
                response.AssetBlock.UUID = pack.AssetBlock.TransactionID.Combine(this.ourClient.SecureSessionID);
                this.ourClient.OutPacket(response);
            }

        }

        #region Xfer packet system for larger uploads

        public void HandleXferPacket(SendXferPacketPacket xferPacket)
        {
            lock (this.transactions)
            {
                foreach (AssetTransaction trans in this.transactions.Values)
                {
                    if (trans.XferID == xferPacket.XferID.ID)
                    {
                        if (trans.Asset.Data.Length > 1)
                        {
                            byte[] newArray = new byte[trans.Asset.Data.Length + xferPacket.DataPacket.Data.Length];
                            Array.Copy(trans.Asset.Data, 0, newArray, 0, trans.Asset.Data.Length);
                            Array.Copy(xferPacket.DataPacket.Data, 0, newArray, trans.Asset.Data.Length, xferPacket.DataPacket.Data.Length);
                            trans.Asset.Data = newArray;
                        }
                        else
                        {
                            byte[] newArray = new byte[xferPacket.DataPacket.Data.Length - 4];
                            Array.Copy(xferPacket.DataPacket.Data, 4, newArray, 0, xferPacket.DataPacket.Data.Length - 4);
                            trans.Asset.Data = newArray;
                        }

                        if ((xferPacket.XferID.Packet & 2147483648) != 0)
                        {
                            //end of transfer
                            trans.UploadComplete = true;
                            AssetUploadCompletePacket response = new AssetUploadCompletePacket();
                            response.AssetBlock.Type = trans.Asset.Type;
                            response.AssetBlock.Success = true;
                            response.AssetBlock.UUID = trans.TransactionID.Combine(this.ourClient.SecureSessionID);
                            this.ourClient.OutPacket(response);

                            m_assetCache.AddAsset(trans.Asset);
                            //check if we should add it to inventory 
                            if (trans.AddToInventory)
                            {
                                // m_assetCache.AddAsset(trans.Asset);
                                m_inventoryCache.AddNewInventoryItem(this.ourClient, trans.InventFolder, trans.Asset);
                            }


                        }
                        break;
                    }

                }
            }

            ConfirmXferPacketPacket confirmXfer = new ConfirmXferPacketPacket();
            confirmXfer.XferID.ID = xferPacket.XferID.ID;
            confirmXfer.XferID.Packet = xferPacket.XferID.Packet;
            this.ourClient.OutPacket(confirmXfer);
        }

        #endregion

        public AssetBase AddUploadToAssetCache(LLUUID transactionID)
        {
            AssetBase asset = null;
            if (this.transactions.ContainsKey(transactionID))
            {
                AssetTransaction trans = this.transactions[transactionID];
                if (trans.UploadComplete)
                {
                    m_assetCache.AddAsset(trans.Asset);
                    asset = trans.Asset;
                }
            }

            return asset;
        }

        public void CreateInventoryItem(CreateInventoryItemPacket packet)
        {
            if (this.transactions.ContainsKey(packet.InventoryBlock.TransactionID))
            {
                AssetTransaction trans = this.transactions[packet.InventoryBlock.TransactionID];
                trans.Asset.Description = Util.FieldToString(packet.InventoryBlock.Description);
                trans.Asset.Name = Util.FieldToString(packet.InventoryBlock.Name);
                trans.Asset.Type = packet.InventoryBlock.Type;
                trans.Asset.InvType = packet.InventoryBlock.InvType;
                if (trans.UploadComplete)
                {
                    //already complete so we can add it to the inventory
                    //m_assetCache.AddAsset(trans.Asset);
                    m_inventoryCache.AddNewInventoryItem(this.ourClient, packet.InventoryBlock.FolderID, trans.Asset);
                }
                else
                {
                    trans.AddToInventory = true;
                    trans.InventFolder = packet.InventoryBlock.FolderID;
                }
            }
        }

        private class AssetTransaction
        {
            public uint XferID;
            public AssetBase Asset;
            public bool AddToInventory;
            public LLUUID InventFolder = LLUUID.Zero;
            public bool UploadComplete = false;
            public LLUUID TransactionID = LLUUID.Zero;

            public AssetTransaction()
            {

            }
        }
    }
}