aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Scenes/SceneObjectPart.Inventory.cs
blob: d9ee94c0dddea64f412ad4cb2ec9c60c9c944353 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the OpenSim Project nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* 
*/

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

using libsecondlife;

using OpenSim.Framework;
using OpenSim.Framework.Console;
using OpenSim.Region.Environment.Interfaces;
using OpenSim.Region.Environment.Scenes.Scripting;

namespace OpenSim.Region.Environment.Scenes
{
    public partial class SceneObjectPart : IScriptHost
    {
        private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private string m_inventoryFileName = String.Empty;
        
        /// <summary>
        /// The inventory folder for this prim
        /// </summary>
        private LLUUID m_folderID = LLUUID.Zero;
        
        /// <summary>
        /// Exposing this is not particularly good, but it's one of the least evils at the moment to see
        /// folder id from prim inventory item data, since it's not (yet) actually stored with the prim.
        /// </summary>
        public LLUUID FolderID
        {
            get { return m_folderID; }
            set { m_folderID = value; }
        }        
                
        /// <summary>
        /// Serial count for inventory file , used to tell if inventory has changed
        /// no need for this to be part of Database backup
        /// </summary>
        protected uint m_inventorySerial = 0;

        public uint InventorySerial
        {
            get { return m_inventorySerial; }
            set { m_inventorySerial = value; }
        }        

        /// <summary>
        /// Holds in memory prim inventory
        /// </summary> 
        protected TaskInventoryDictionary m_taskInventory = new TaskInventoryDictionary();
        
        public TaskInventoryDictionary TaskInventory
        {
            get { return m_taskInventory; }
            set { m_taskInventory = value; }
        }
        
        /// <summary>
        /// Tracks whether inventory has changed since the last persistent backup
        /// </summary>
        private bool HasInventoryChanged;
        
        /// <summary>
        /// Reset LLUUIDs for all the items in the prim's inventory.  This involves either generating
        /// new ones or setting existing UUIDs to the correct parent UUIDs
        /// </summary>
        /// <param name="linkNum'>Link number for the part</param>
        public void ResetInventoryIDs()
        {
            lock (TaskInventory)
            {              
                IList<TaskInventoryItem> items = new List<TaskInventoryItem>(TaskInventory.Values);
                TaskInventory.Clear();
                
                foreach (TaskInventoryItem item in items)
                {
                    item.ResetIDs(UUID);
                    TaskInventory.Add(item.ItemID, item);
                }
            }
        }        
        
        /// <summary>
        /// Start all the scripts contained in this prim's inventory
        /// </summary>
        public void StartScripts()
        {
            lock (m_taskInventory)
            {
                foreach (TaskInventoryItem item in m_taskInventory.Values)
                {
                    // XXX more hardcoding badness.  Should be an enum in TaskInventoryItem
                    if (10 == item.Type)
                    {
                        StartScript(item);
                    }
                }
            }
        }
        
        /// <summary>
        /// Start a script which is in this prim's inventory.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public void StartScript(TaskInventoryItem item)
        {
//            m_log.Info(String.Format(
//                           "[PRIMINVENTORY]: " +
//                           "Starting script {0}, {1} in prim {2}, {3}", 
//                           item.Name, item.ItemID, Name, UUID));
            
            AssetBase rezAsset = m_parentGroup.Scene.AssetCache.GetAsset(item.AssetID, false);

            if (rezAsset != null)
            {
                string script = Helpers.FieldToUTF8String(rezAsset.Data);
                m_parentGroup.Scene.EventManager.TriggerRezScript(LocalID, item.ItemID, script);
            }     
            else
            {
                m_log.Error(String.Format(
                                "[PRIMINVENTORY]: " +
                                "Couldn't start script {0}, {1} since asset ID {2} could not be found", 
                                item.Name, item.ItemID, item.AssetID));
            }
        }   
        
        /// <summary>
        /// Start a script which is in this prim's inventory.
        /// </summary>
        /// <param name="itemId">
        /// A <see cref="LLUUID"/>
        /// </param>        
        public void StartScript(LLUUID itemId)
        {
            lock (m_taskInventory)
            {            
                if (m_taskInventory.ContainsKey(itemId))
                {
                    StartScript(m_taskInventory[itemId]);
                }            
                else
                {
                    m_log.Error(String.Format(
                                    "[PRIMINVENTORY]: " +
                                    "Couldn't start script with ID {0} since it couldn't be found for prim {1}, {2}", 
                                    itemId, Name, UUID));
                }                
            }
        }        
        
        /// <summary>
        /// Stop a script which is in this prim's inventory.
        /// </summary>
        /// <param name="itemId"></param>        
        public void StopScript(LLUUID itemId)
        {
            if (m_taskInventory.ContainsKey(itemId))
            {
                m_parentGroup.Scene.EventManager.TriggerRemoveScript(LocalID, itemId);
            }            
            else
            {
                m_log.Error(String.Format(
                                "[PRIMINVENTORY]: " +
                                "Couldn't stop script with ID {0} since it couldn't be found for prim {1}, {2}", 
                                itemId, Name, UUID));
            }                            
        }        

        /// <summary>
        /// Add an item to this prim's inventory.
        /// </summary>
        /// <param name="item"></param>
        public void AddInventoryItem(TaskInventoryItem item)
        {
            item.ParentID = m_folderID;
            item.CreationDate = 1000;
            item.ParentPartID = UUID;
            
            lock (m_taskInventory)
            {            
                m_taskInventory.Add(item.ItemID, item);
                TriggerScriptChangedEvent(Changed.INVENTORY);
            }
            
            m_inventorySerial++;
            HasInventoryChanged = true;
        }
        
        /// <summary>
        /// Restore a whole collection of items to the prim's inventory at once.  
        /// We assume that the items already have all their fields correctly filled out.
        /// The items are not flagged for persistence to the database, since they are being restored 
        /// from persistence rather than being newly added.
        /// </summary>
        /// <param name="items"></param>
        public void RestoreInventoryItems(ICollection<TaskInventoryItem> items)
        {
            lock (m_taskInventory)
            {                 
                foreach (TaskInventoryItem item in items)
                {           
                    m_taskInventory.Add(item.ItemID, item);
                    TriggerScriptChangedEvent(Changed.INVENTORY);
                }
            }
                        
            m_inventorySerial++;
        }
        
        /// <summary>
        /// Returns an existing inventory item.  Returns the original, so any changes will be live.
        /// </summary>
        /// <param name="itemID"></param>
        /// <returns>null if the item does not exist</returns>
        public TaskInventoryItem GetInventoryItem(LLUUID itemID)
        {
            lock (m_taskInventory)
            {              
                if (m_taskInventory.ContainsKey(itemID))
                {
                    return m_taskInventory[itemID];
                }
                else
                {
                    m_log.Error(String.Format(
                                    "[PRIMINVENTORY]: " +
                                    "Tried to retrieve item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
                                    itemID, Name, UUID));
                }
            }

            return null;
        }

        /// <summary>
        /// Update an existing inventory item.
        /// </summary>
        /// <param name="item">The updated item.  An item with the same id must already exist
        /// in this prim's inventory.</param>
        /// <returns>false if the item did not exist, true if the update occurred succesfully</returns>
        public bool UpdateInventoryItem(TaskInventoryItem item)
        {
            lock (m_taskInventory)
            {            
                if (m_taskInventory.ContainsKey(item.ItemID))
                {
                    m_taskInventory[item.ItemID] = item;
                    m_inventorySerial++;
                    TriggerScriptChangedEvent(Changed.INVENTORY);
                    
                    HasInventoryChanged = true;
                  
                    return true;
                }
                else
                {
                    m_log.Error(String.Format(
                                    "[PRIMINVENTORY]: " +
                                    "Tried to retrieve item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
                                    item.ItemID, Name, UUID));
                }   
            }
            
            return false;
        }

        /// <summary>
        /// Remove an item from this prim's inventory
        /// </summary>
        /// <param name="itemID"></param>
        /// <returns>Numeric asset type of the item removed.  Returns -1 if the item did not exist
        /// in this prim's inventory.</returns>
        public int RemoveInventoryItem(LLUUID itemID)
        {
            lock (m_taskInventory)
            {            
                if (m_taskInventory.ContainsKey(itemID))
                {
                    int type = m_taskInventory[itemID].InvType;
                    m_taskInventory.Remove(itemID);
                    m_inventorySerial++;
                    TriggerScriptChangedEvent(Changed.INVENTORY);
                    
                    HasInventoryChanged = true;
                    
                    return type;
                }
                else
                {
                    m_log.Error(String.Format(
                                    "[PRIMINVENTORY]: " +
                                    "Tried to remove item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
                                    itemID, Name, UUID));
                }            
            }

            return -1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="client"></param>
        /// <param name="localID"></param>
        public bool GetInventoryFileName(IClientAPI client, uint localID)
        {
            if (m_inventorySerial > 0)
            {
                client.SendTaskInventory(m_uuid, (short) m_inventorySerial,
                                         Helpers.StringToField(m_inventoryFileName));
                return true;
            }
            else
            {
                client.SendTaskInventory(m_uuid, 0, new byte[0]);
                return false;
            }
        }

        public void RequestInventoryFile(IXfer xferManager)
        {
            byte[] fileData = new byte[0];
            InventoryStringBuilder invString = new InventoryStringBuilder(m_folderID, UUID);
            
            lock (m_taskInventory)
            {            
                foreach (TaskInventoryItem item in m_taskInventory.Values)
                {
                    invString.AddItemStart();
                    invString.AddNameValueLine("item_id", item.ItemID.ToString());
                    invString.AddNameValueLine("parent_id", item.ParentID.ToString());
    
                    invString.AddPermissionsStart();
                    invString.AddNameValueLine("base_mask", "0x7FFFFFFF");
                    invString.AddNameValueLine("owner_mask", "0x7FFFFFFF");
                    invString.AddNameValueLine("group_mask", "0x7FFFFFFF");
                    invString.AddNameValueLine("everyone_mask", "0x7FFFFFFF");
                    invString.AddNameValueLine("next_owner_mask", "0x7FFFFFFF");
                    invString.AddNameValueLine("creator_id", item.CreatorID.ToString());
                    invString.AddNameValueLine("owner_id", item.OwnerID.ToString());
                    invString.AddNameValueLine("last_owner_id", item.LastOwnerID.ToString());
                    invString.AddNameValueLine("group_id", item.GroupID.ToString());
                    invString.AddSectionEnd();
    
                    invString.AddNameValueLine("asset_id", item.AssetID.ToString());
                    invString.AddNameValueLine("type", TaskInventoryItem.Types[item.Type]);
                    invString.AddNameValueLine("inv_type", TaskInventoryItem.InvTypes[item.InvType]);
                    invString.AddNameValueLine("flags", "0x00");
                    invString.AddNameValueLine("name", item.Name + "|");
                    invString.AddNameValueLine("desc", item.Description + "|");
                    invString.AddNameValueLine("creation_date", item.CreationDate.ToString());
                    invString.AddSectionEnd();
                }
            }
            
            fileData = Helpers.StringToField(invString.BuildString);
            
//            m_log.Info(String.Format(
//                           "[PRIMINVENTORY]: RequestInventoryFile fileData: {0}", Helpers.FieldToUTF8String(fileData)));
            
            if (fileData.Length > 2)
            {
                xferManager.AddNewFile(m_inventoryFileName, fileData);
            }
        }
        
        /// <summary>
        /// Process inventory backup
        /// </summary>
        /// <param name="datastore"></param>
        public void ProcessInventoryBackup(IRegionDataStore datastore)
        {
            if (HasInventoryChanged)
            {
                lock (TaskInventory)
                {
                    datastore.StorePrimInventory(UUID, TaskInventory.Values);
                }
                
                HasInventoryChanged = false;
            }
        }        

        public class InventoryStringBuilder
        {
            public string BuildString = String.Empty;

            public InventoryStringBuilder(LLUUID folderID, LLUUID parentID)
            {
                BuildString += "\tinv_object\t0\n\t{\n";
                AddNameValueLine("obj_id", folderID.ToString());
                AddNameValueLine("parent_id", parentID.ToString());
                AddNameValueLine("type", "category");
                AddNameValueLine("name", "Contents");
                AddSectionEnd();
            }

            public void AddItemStart()
            {
                BuildString += "\tinv_item\t0\n";
                BuildString += "\t{\n";
            }

            public void AddPermissionsStart()
            {
                BuildString += "\tpermissions 0\n";
                BuildString += "\t{\n";
            }

            public void AddSectionEnd()
            {
                BuildString += "\t}\n";
            }

            public void AddLine(string addLine)
            {
                BuildString += addLine;
            }

            public void AddNameValueLine(string name, string value)
            {
                BuildString += "\t\t";
                BuildString += name + "\t";
                BuildString += value + "\n";
            }

            public void Close()
            {
            }
        }        
    }
}