forked from kow/gridsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryManager.cs
2623 lines (2282 loc) · 113 KB
/
InventoryManager.cs
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2006-2007, Second Life Reverse Engineering Team
* All rights reserved.
*
* - 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.
* - Neither the name of the Second Life Reverse Engineering Team 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR 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.Text.RegularExpressions;
using System.Threading;
using System.Text;
using libsecondlife;
using libsecondlife.Packets;
namespace libsecondlife
{
#region Enums
public enum InventoryType : sbyte
{
Unknown = -1,
Texture = 0,
Sound = 1,
CallingCard = 2,
Landmark = 3,
[Obsolete] Script = 4,
[Obsolete] Clothing = 5,
Object = 6,
Notecard = 7,
Category = 8,
Folder = 8,
RootCategory = 0,
LSL = 10,
[Obsolete] LSLBytecode = 11,
[Obsolete] TextureTGA = 12,
[Obsolete] Bodypart = 13,
[Obsolete] Trash = 14,
Snapshot = 15,
[Obsolete] LostAndFound = 16,
Attachment = 17,
Wearable = 18,
Animation = 19,
Gesture = 20
}
/// <summary>
///
/// </summary>
public enum SaleType : byte
{
/// <summary>Not for sale</summary>
Not = 0,
/// <summary>The original is for sale</summary>
Original = 1,
/// <summary>Copies are for sale</summary>
Copy = 2,
/// <summary>The contents of the object are for sale</summary>
Contents = 3
}
[Flags]
public enum InventorySortOrder : int
{
/// <summary>Sort by name</summary>
ByName = 0,
/// <summary>Sort by date</summary>
ByDate = 1,
/// <summary>Sort folders by name, regardless of whether items are
/// sorted by name or date</summary>
FoldersByName = 2,
/// <summary>Place system folders at the top</summary>
SystemFoldersToTop = 4
}
#endregion Enums
#region Inventory Object Classes
public abstract class InventoryBase
{
public readonly LLUUID UUID;
public LLUUID ParentUUID;
public string Name;
public LLUUID OwnerID;
public InventoryBase(LLUUID itemID)
{
if (itemID == LLUUID.Zero)
throw new ArgumentException("Inventory item ID cannot be NULL_KEY (LLUUID.Zero)");
UUID = itemID;
}
public override int GetHashCode()
{
return UUID.GetHashCode() ^ ParentUUID.GetHashCode() ^ Name.GetHashCode() ^ OwnerID.GetHashCode();
}
public override bool Equals(object o)
{
InventoryBase inv = o as InventoryBase;
return inv != null && Equals(inv);
}
public virtual bool Equals(InventoryBase o)
{
return o.UUID == UUID
&& o.ParentUUID == ParentUUID
&& o.Name == Name
&& o.OwnerID == OwnerID;
}
}
public class InventoryItem : InventoryBase
{
public LLUUID AssetUUID;
public Permissions Permissions;
public AssetType AssetType;
public InventoryType InventoryType;
public LLUUID CreatorID;
public string Description;
public LLUUID GroupID;
public bool GroupOwned;
public int SalePrice;
public SaleType SaleType;
public uint Flags;
/// <summary>Time and date this inventory item was created, stored as
/// UTC (Coordinated Universal Time)</summary>
public DateTime CreationDate;
public InventoryItem(LLUUID itemID)
: base(itemID) { }
public InventoryItem(InventoryType type, LLUUID itemID) : base(itemID) { InventoryType = type; }
public override int GetHashCode()
{
return AssetUUID.GetHashCode() ^ Permissions.GetHashCode() ^ AssetType.GetHashCode() ^
InventoryType.GetHashCode() ^ Description.GetHashCode() ^ GroupID.GetHashCode() ^
GroupOwned.GetHashCode() ^ SalePrice.GetHashCode() ^ SaleType.GetHashCode() ^
Flags.GetHashCode() ^ CreationDate.GetHashCode();
}
public override bool Equals(object o)
{
InventoryItem item = o as InventoryItem;
return item != null && Equals(item);
}
public override bool Equals(InventoryBase o)
{
InventoryItem item = o as InventoryItem;
return item != null && Equals(item);
}
public bool Equals(InventoryItem o)
{
return base.Equals(o as InventoryBase)
&& o.AssetType == AssetType
&& o.AssetUUID == AssetUUID
&& o.CreationDate == CreationDate
&& o.Description == Description
&& o.Flags == Flags
&& o.GroupID == GroupID
&& o.GroupOwned == GroupOwned
&& o.InventoryType == InventoryType
&& o.Permissions.Equals(Permissions)
&& o.SalePrice == SalePrice
&& o.SaleType == SaleType;
}
}
public class InventoryTexture : InventoryItem { public InventoryTexture(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Texture; } }
public class InventorySound : InventoryItem { public InventorySound(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Sound; } }
public class InventoryCallingCard : InventoryItem { public InventoryCallingCard(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.CallingCard; } }
public class InventoryLandmark : InventoryItem { public InventoryLandmark(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Landmark; } }
public class InventoryObject : InventoryItem { public InventoryObject(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Object; } }
public class InventoryNotecard : InventoryItem { public InventoryNotecard(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Notecard; } }
public class InventoryCategory : InventoryItem { public InventoryCategory(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Category; } }
public class InventoryLSL : InventoryItem { public InventoryLSL(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.LSL; } }
public class InventorySnapshot : InventoryItem { public InventorySnapshot(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Snapshot; } }
public class InventoryAttachment : InventoryItem { public InventoryAttachment(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Attachment; } }
public class InventoryWearable : InventoryItem
{
public InventoryWearable(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Wearable; }
public WearableType WearableType
{
get { return (WearableType)Flags; }
set { Flags = (uint)value; }
}
}
public class InventoryAnimation : InventoryItem { public InventoryAnimation(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Animation; } }
public class InventoryGesture : InventoryItem { public InventoryGesture(LLUUID itemID) : base(itemID) { InventoryType = InventoryType.Gesture; } }
public class InventoryFolder : InventoryBase
{
public AssetType PreferredType;
public int Version;
public int DescendentCount;
public InventoryFolder(LLUUID itemID)
: base(itemID) { }
public override int GetHashCode()
{
return PreferredType.GetHashCode() ^ Version.GetHashCode() ^ DescendentCount.GetHashCode();
}
public override bool Equals(object o)
{
InventoryFolder folder = o as InventoryFolder;
return folder != null && Equals(folder);
}
public override bool Equals(InventoryBase o)
{
InventoryFolder folder = o as InventoryFolder;
return folder != null && Equals(folder);
}
public bool Equals(InventoryFolder o)
{
return base.Equals(o as InventoryBase)
&& o.DescendentCount == DescendentCount
&& o.PreferredType == PreferredType
&& o.Version == Version;
}
}
#endregion Inventory Object Classes
public class InventoryManager
{
protected struct InventorySearch
{
public LLUUID Folder;
public LLUUID Owner;
public string[] Path;
public int Level;
}
/// <summary>
/// Callback for inventory item creation finishing
/// </summary>
/// <param name="success">Whether the request to create an inventory
/// item succeeded or not</param>
/// <param name="item">Inventory item being created. If success is
/// false this will be null</param>
public delegate void ItemCreatedCallback(bool success, InventoryItem item);
/// <summary>
///
/// </summary>
/// <param name="success"></param>
/// <param name="status"></param>
/// <param name="itemID"></param>
/// <param name="assetID"></param>
public delegate void ItemCreatedFromAssetCallback(bool success, string status, LLUUID itemID, LLUUID assetID);
/// <summary>
///
/// </summary>
/// <param name="items"></param>
public delegate void ItemCopiedCallback(InventoryBase item);
/// <summary>
///
/// </summary>
/// <param name="item"></param>
public delegate void ItemReceivedCallback(InventoryItem item);
/// <summary>
/// Callback for an inventory folder updating
/// </summary>
/// <param name="folderID">UUID of the folder that was updated</param>
public delegate void FolderUpdatedCallback(LLUUID folderID);
/// <summary>
/// Callback when an inventory object is received from another avatar
/// or a primitive
/// </summary>
/// <param name="fromAgentID"></param>
/// <param name="fromAgentName"></param>
/// <param name="parentEstateID"></param>
/// <param name="regionID"></param>
/// <param name="position"></param>
/// <param name="timestamp"></param>
/// <param name="type"></param>
/// <param name="objectID">Will be null if offered from a primitive</param>
/// <param name="fromTask"></param>
/// <returns>True to accept the inventory offer, false to reject it</returns>
public delegate bool ObjectOfferedCallback(LLUUID fromAgentID, string fromAgentName, uint parentEstateID,
LLUUID regionID, LLVector3 position, DateTime timestamp, AssetType type, LLUUID objectID, bool fromTask);
/// <summary>
/// Callback when an inventory object is accepted and received from a
/// task inventory. This is the callback in which you actually get
/// the ItemID, as in ObjectOfferedCallback it is null when received
/// from a task.
/// </summary>
/// <param name="ItemID"></param>
/// <param name="FolderID"></param>
/// <param name="CreatorID"></param>
/// <param name="AssetID"></param>
public delegate void TaskItemReceivedCallback(LLUUID itemID, LLUUID folderID, LLUUID creatorID,
LLUUID assetID, InventoryType type);
/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <param name="inventoryObjectID"></param>
public delegate void FindObjectByPathCallback(string path, LLUUID inventoryObjectID);
/// <summary>
/// Reply received after calling <code>RequestTaskInventory</code>,
/// contains a filename that can be used in an asset download request
/// </summary>
/// <param name="itemID">UUID of the inventory item</param>
/// <param name="serial">Version number of the task inventory asset</param>
/// <param name="assetFilename">Filename of the task inventory asset</param>
public delegate void TaskInventoryReplyCallback(LLUUID itemID, short serial, string assetFilename);
public event ItemReceivedCallback OnItemReceived;
public event FolderUpdatedCallback OnFolderUpdated;
public event ObjectOfferedCallback OnObjectOffered;
public event TaskItemReceivedCallback OnTaskItemReceived;
public event FindObjectByPathCallback OnFindObjectByPath;
public event TaskInventoryReplyCallback OnTaskInventoryReply;
private SecondLife _Client;
private Inventory _Store;
private Random _RandNumbers = new Random();
private object _CallbacksLock = new object();
private uint _CallbackPos;
private Dictionary<uint, ItemCreatedCallback> _ItemCreatedCallbacks = new Dictionary<uint, ItemCreatedCallback>();
private Dictionary<uint, ItemCopiedCallback> _ItemCopiedCallbacks = new Dictionary<uint,ItemCopiedCallback>();
private List<InventorySearch> _Searches = new List<InventorySearch>();
#region String Arrays
/// <summary>Partial mapping of AssetTypes to folder names</summary>
private static readonly string[] _NewFolderNames = new string[]
{
"Textures",
"Sounds",
"Calling Cards",
"Landmarks",
"Scripts",
"Clothing",
"Objects",
"Notecards",
"New Folder",
"Inventory",
"Scripts",
"Scripts",
"Uncompressed Images",
"Body Parts",
"Trash",
"Photo Album",
"Lost And Found",
"Uncompressed Sounds",
"Uncompressed Images",
"Uncompressed Images",
"Animations",
"Gestures"
};
private static readonly string[] _AssetTypeNames = new string[]
{
"texture",
"sound",
"callcard",
"landmark",
"script",
"clothing",
"object",
"notecard",
"category",
"root",
"lsltext",
"lslbyte",
"txtr_tga",
"bodypart",
"trash",
"snapshot",
"lstndfnd",
"snd_wav",
"img_tga",
"jpeg",
"animatn",
"gesture",
"simstate"
};
private static readonly string[] _InventoryTypeNames = new string[]
{
"texture",
"sound",
"callcard",
"landmark",
String.Empty,
String.Empty,
"object",
"notecard",
"category",
"root",
"script",
String.Empty,
String.Empty,
String.Empty,
String.Empty,
"snapshot",
String.Empty,
"attach",
"wearable",
"animation",
"gesture",
};
private static readonly string[] _SaleTypeNames = new string[]
{
"not",
"orig",
"copy",
"cntn"
};
#endregion String Arrays
#region Properties
public Inventory Store { get { return _Store; } }
#endregion Properties
/// <summary>
/// Default constructor
/// </summary>
/// <param name="client">Reference to the SecondLife client</param>
public InventoryManager(SecondLife client)
{
_Client = client;
_Store = new Inventory(client, this);
_Client.Network.RegisterCallback(PacketType.UpdateCreateInventoryItem, new NetworkManager.PacketCallback(UpdateCreateInventoryItemHandler));
_Client.Network.RegisterCallback(PacketType.SaveAssetIntoInventory, new NetworkManager.PacketCallback(SaveAssetIntoInventoryHandler));
_Client.Network.RegisterCallback(PacketType.BulkUpdateInventory, new NetworkManager.PacketCallback(BulkUpdateInventoryHandler));
_Client.Network.RegisterCallback(PacketType.MoveInventoryItem, new NetworkManager.PacketCallback(MoveInventoryItemHandler));
_Client.Network.RegisterCallback(PacketType.InventoryDescendents, new NetworkManager.PacketCallback(InventoryDescendentsHandler));
_Client.Network.RegisterCallback(PacketType.FetchInventoryReply, new NetworkManager.PacketCallback(FetchInventoryReplyHandler));
_Client.Network.RegisterCallback(PacketType.ReplyTaskInventory, new NetworkManager.PacketCallback(ReplyTaskInventoryHandler));
// Watch for inventory given to us through instant message
_Client.Self.OnInstantMessage += new AgentManager.InstantMessageCallback(Self_OnInstantMessage);
// Register extra parameters with login and parse the inventory data that comes back
_Client.Network.RegisterLoginResponseCallback(new NetworkManager.LoginResponseCallback(Network_OnLoginResponse), new string[] {"inventory-root", "inventory-skeleton", "inventory-lib-root", "inventory-lib-owner", "inventory-skel-lib"} );
}
//public IAsyncResult BeginFindObjects(LLUUID baseFolder, string regex, bool recurse, bool refresh, bool firstOnly, AsyncCallback callback, object asyncState)
//{
// return BeginFindObjects(baseFolder, new Regex(regex), recurse, refresh, firstOnly, callback, asyncState);
//}
//public IAsyncResult BeginFindObjects(LLUUID baseFolder, Regex regexp, bool recurse, bool refresh, bool firstOnly, AsyncCallback callback, object asyncState)
//{
// FindResult result = new FindResult(regexp, recurse, callback);
// result.FirstOnly = firstOnly;
// result.AsyncState = asyncState;
// result.FoldersWaiting = 1;
// if (refresh)
// {
// lock (FindDescendantsMap)
// {
// IAsyncResult descendReq = BeginRequestFolderContents(baseFolder, _Client.Self.AgentID, true, true, recurse && !firstOnly, InventorySortOrder.ByName, new AsyncCallback(SearchDescendantsCallback), baseFolder);
// FindDescendantsMap.Add(descendReq, result);
// }
// }
// else
// {
// result.Result = LocalFind(baseFolder, regexp, recurse, firstOnly);
// result.CompletedSynchronously = true;
// result.IsCompleted = true;
// }
// return result;
//}
//private List<InventoryBase> LocalFind(LLUUID baseFolder, Regex regexp, bool recurse, bool firstOnly)
//{
// List<InventoryBase> objects = new List<InventoryBase>();
// List<InventoryFolder> folders = new List<InventoryFolder>();
// List<InventoryBase> contents = _Store.GetContents(baseFolder);
// foreach (InventoryBase inv in contents)
// {
// if (regexp.IsMatch(inv.Name))
// {
// objects.Add(inv);
// if (firstOnly)
// return objects;
// }
// if (inv is InventoryFolder)
// {
// folders.Add(inv as InventoryFolder);
// }
// }
// // Recurse outside of the loop because subsequent calls to FindObjects may
// // modify the baseNode.Nodes collection.
// // FIXME: I'm pretty sure this is not necessary
// if (recurse)
// {
// foreach (InventoryFolder folder in folders)
// {
// objects.AddRange(LocalFind(folder.UUID, regexp, true, firstOnly));
// }
// }
// return objects;
//}
//public List<InventoryBase> FindObjectsByPath(LLUUID baseFolder, string[] path, bool refresh, bool firstOnly)
//{
// IAsyncResult r = BeginFindObjectsByPath(baseFolder, path, refresh, firstOnly, null, null, true);
// return EndFindObjects(r);
//}
//public IAsyncResult BeginFindObjectsByPath(LLUUID baseFolder, string[] path, bool refresh, bool firstOnly, AsyncCallback callback, object asyncState, bool recurse)
//{
// if (path.Length == 0)
// throw new ArgumentException("Empty path is not supported");
// FindResult result = new FindResult(new Regex(String.Join("/",path)), recurse, callback);
// result.FirstOnly = firstOnly;
// result.AsyncState = asyncState;
// if (refresh)
// {
// result.FoldersWaiting = 1;
// BeginRequestFolderContents(
// baseFolder,
// _Client.Self.AgentID,
// true,
// true,
// false,
// InventorySortOrder.ByName,
// new AsyncCallback(FindObjectsByPathCallback),
// new FindObjectsByPathState(result, baseFolder, 0));
// }
// else
// {
// result.Result = LocalFind(baseFolder, path, 0, firstOnly);
// result.CompletedSynchronously = true;
// result.IsCompleted = true;
// }
// return result;
//}
#region Fetch
public InventoryItem FetchItem(LLUUID itemID, LLUUID ownerID, int timeoutMS)
{
AutoResetEvent fetchEvent = new AutoResetEvent(false);
InventoryItem fetchedItem = null;
ItemReceivedCallback callback =
delegate(InventoryItem item)
{
if (item.UUID == itemID)
{
fetchedItem = item;
fetchEvent.Set();
}
};
OnItemReceived += callback;
RequestFetchInventory(itemID, ownerID);
fetchEvent.WaitOne(timeoutMS, false);
OnItemReceived -= callback;
return fetchedItem;
}
public void RequestFetchInventory(LLUUID itemID, LLUUID ownerID)
{
FetchInventoryPacket fetch = new FetchInventoryPacket();
fetch.AgentData = new FetchInventoryPacket.AgentDataBlock();
fetch.AgentData.AgentID = _Client.Self.AgentID;
fetch.AgentData.SessionID = _Client.Self.SessionID;
fetch.InventoryData = new FetchInventoryPacket.InventoryDataBlock[1];
fetch.InventoryData[0] = new FetchInventoryPacket.InventoryDataBlock();
fetch.InventoryData[0].ItemID = itemID;
fetch.InventoryData[0].OwnerID = ownerID;
_Client.Network.SendPacket(fetch);
}
/// <summary>
/// Request inventory items
/// </summary>
/// <param name="itemIDs">Inventory items to request</param>
/// <param name="ownerIDs">Owners of the inventory items</param>
public void RequestFetchInventory(List<LLUUID> itemIDs, List<LLUUID> ownerIDs)
{
if (itemIDs.Count != ownerIDs.Count)
throw new ArgumentException("itemIDs and ownerIDs must contain the same number of entries");
FetchInventoryPacket fetch = new FetchInventoryPacket();
fetch.AgentData = new FetchInventoryPacket.AgentDataBlock();
fetch.AgentData.AgentID = _Client.Self.AgentID;
fetch.AgentData.SessionID = _Client.Self.SessionID;
fetch.InventoryData = new FetchInventoryPacket.InventoryDataBlock[itemIDs.Count];
for (int i = 0; i < itemIDs.Count; i++)
{
fetch.InventoryData[i] = new FetchInventoryPacket.InventoryDataBlock();
fetch.InventoryData[i].ItemID = itemIDs[i];
fetch.InventoryData[i].OwnerID = ownerIDs[i];
}
_Client.Network.SendPacket(fetch);
}
public List<InventoryBase> FolderContents(LLUUID folder, LLUUID owner, bool folders, bool items,
InventorySortOrder order, int timeoutMS)
{
List<InventoryBase> objects = null;
AutoResetEvent fetchEvent = new AutoResetEvent(false);
FolderUpdatedCallback callback =
delegate(LLUUID folderID)
{
if (folderID == folder)
fetchEvent.Set();
};
OnFolderUpdated += callback;
RequestFolderContents(folder, owner, folders, items, order);
if (fetchEvent.WaitOne(timeoutMS, false))
objects = _Store.GetContents(folder);
OnFolderUpdated -= callback;
return objects;
}
public void RequestFolderContents(LLUUID folder, LLUUID owner, bool folders, bool items,
InventorySortOrder order)
{
FetchInventoryDescendentsPacket fetch = new FetchInventoryDescendentsPacket();
fetch.AgentData.AgentID = _Client.Self.AgentID;
fetch.AgentData.SessionID = _Client.Self.SessionID;
fetch.InventoryData.FetchFolders = folders;
fetch.InventoryData.FetchItems = items;
fetch.InventoryData.FolderID = folder;
fetch.InventoryData.OwnerID = owner;
fetch.InventoryData.SortOrder = (int)order;
_Client.Network.SendPacket(fetch);
}
#endregion Fetch
#region Find
/// <summary>
/// Returns the UUID of the folder (category) that defaults to
/// containing 'type'. The folder is not necessarily only for that
/// type
/// </summary>
/// <remarks>This will return the root folder if one does not exist</remarks>
/// <param name="type"></param>
/// <returns>The UUID of the desired folder if found, the UUID of the RootFolder
/// if not found, or LLUUID.Zero on failure</returns>
public LLUUID FindFolderForType(AssetType type)
{
if (_Store == null)
{
_Client.Log("Inventory is null, FindFolderForType() lookup cannot continue",
Helpers.LogLevel.Error);
return LLUUID.Zero;
}
// Folders go in the root
if (type == AssetType.Folder)
return _Store.RootFolder.UUID;
// Loop through each top-level directory and check if PreferredType
// matches the requested type
List<InventoryBase> contents = _Store.GetContents(_Store.RootFolder.UUID);
foreach (InventoryBase inv in contents)
{
if (inv is InventoryFolder)
{
InventoryFolder folder = inv as InventoryFolder;
if (folder.PreferredType == type)
return folder.UUID;
}
}
// No match found, return Root Folder ID
return _Store.RootFolder.UUID;
}
public LLUUID FindObjectByPath(LLUUID baseFolder, LLUUID inventoryOwner, string path, int timeoutMS)
{
AutoResetEvent findEvent = new AutoResetEvent(false);
LLUUID foundItem = LLUUID.Zero;
FindObjectByPathCallback callback =
delegate(string thisPath, LLUUID inventoryObjectID)
{
if (thisPath == path)
{
foundItem = inventoryObjectID;
findEvent.Set();
}
};
OnFindObjectByPath += callback;
RequestFindObjectByPath(baseFolder, inventoryOwner, path);
findEvent.WaitOne(timeoutMS, false);
OnFindObjectByPath -= callback;
return foundItem;
}
public void RequestFindObjectByPath(LLUUID baseFolder, LLUUID inventoryOwner, string path)
{
if (path == null || path.Length == 0)
throw new ArgumentException("Empty path is not supported");
// Store this search
InventorySearch search;
search.Folder = baseFolder;
search.Owner = inventoryOwner;
search.Path = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
search.Level = 0;
lock (_Searches) _Searches.Add(search);
// Start the search
RequestFolderContents(baseFolder, inventoryOwner, true, true, InventorySortOrder.ByName);
}
public List<InventoryBase> LocalFind(LLUUID baseFolder, string[] path, int level, bool firstOnly)
{
List<InventoryBase> objects = new List<InventoryBase>();
//List<InventoryFolder> folders = new List<InventoryFolder>();
List<InventoryBase> contents = _Store.GetContents(baseFolder);
foreach (InventoryBase inv in contents)
{
if (inv.Name.CompareTo(path[level]) == 0)
{
if (level == path.Length - 1)
{
objects.Add(inv);
if (firstOnly) return objects;
}
else if (inv is InventoryFolder)
objects.AddRange(LocalFind(inv.UUID, path, level + 1, firstOnly));
}
}
return objects;
}
#endregion Find
#region Move
public void Move(InventoryBase item, InventoryFolder newParent)
{
if (item is InventoryFolder)
MoveFolder(item.UUID, newParent.UUID);
else
MoveItem(item.UUID, newParent.UUID);
}
public void MoveFolder(LLUUID folder, LLUUID newParent)
{
lock (Store)
{
if (_Store.Contains(folder))
{
InventoryBase inv = Store[folder];
inv.ParentUUID = newParent;
_Store.UpdateNodeFor(inv);
}
}
MoveInventoryFolderPacket move = new MoveInventoryFolderPacket();
move.AgentData.AgentID = _Client.Self.AgentID;
move.AgentData.SessionID = _Client.Self.SessionID;
move.AgentData.Stamp = false; //FIXME: ??
move.InventoryData = new MoveInventoryFolderPacket.InventoryDataBlock[1];
move.InventoryData[0] = new MoveInventoryFolderPacket.InventoryDataBlock();
move.InventoryData[0].FolderID = folder;
move.InventoryData[0].ParentID = newParent;
_Client.Network.SendPacket(move);
}
/// <summary>
/// Moves the folders, the keys in the Dictionary parameter,
/// to a new parents, the value of that folder's key.
/// </summary>
/// <param name="FoldersNewParents"></param>
public void MoveFolders(Dictionary<LLUUID, LLUUID> foldersNewParents)
{
// FIXME: Use two List<LLUUID> to stay consistent
lock (Store)
{
foreach (KeyValuePair<LLUUID, LLUUID> entry in foldersNewParents)
{
if (_Store.Contains(entry.Key))
{
InventoryBase inv = _Store[entry.Key];
inv.ParentUUID = entry.Value;
_Store.UpdateNodeFor(inv);
}
}
}
//TODO: Test if this truly supports multiple-folder move
MoveInventoryFolderPacket move = new MoveInventoryFolderPacket();
move.AgentData.AgentID = _Client.Self.AgentID;
move.AgentData.SessionID = _Client.Self.SessionID;
move.AgentData.Stamp = false; //FIXME: ??
move.InventoryData = new MoveInventoryFolderPacket.InventoryDataBlock[foldersNewParents.Count];
int index = 0;
foreach (KeyValuePair<LLUUID, LLUUID> folder in foldersNewParents)
{
MoveInventoryFolderPacket.InventoryDataBlock block = new MoveInventoryFolderPacket.InventoryDataBlock();
block.FolderID = folder.Key;
block.ParentID = folder.Value;
move.InventoryData[index++] = block;
}
_Client.Network.SendPacket(move);
}
public void MoveItem(LLUUID item, LLUUID folder)
{
MoveItem(item, folder, String.Empty);
}
public void MoveItem(LLUUID item, LLUUID folder, string newItemName)
{
lock (Store)
{
if (Store.Contains(item))
{
InventoryBase inv = _Store[item];
inv.ParentUUID = folder;
_Store.UpdateNodeFor(inv);
}
}
MoveInventoryItemPacket move = new MoveInventoryItemPacket();
move.AgentData.AgentID = _Client.Self.AgentID;
move.AgentData.SessionID = _Client.Self.SessionID;
move.AgentData.Stamp = false; //FIXME: ??
move.InventoryData = new MoveInventoryItemPacket.InventoryDataBlock[1];
move.InventoryData[0] = new MoveInventoryItemPacket.InventoryDataBlock();
move.InventoryData[0].ItemID = item;
move.InventoryData[0].FolderID = folder;
move.InventoryData[0].NewName = Helpers.StringToField(newItemName);
_Client.Network.SendPacket(move);
}
public void MoveItems(Dictionary<LLUUID, LLUUID> itemsNewParents)
{
lock (Store)
{
foreach (KeyValuePair<LLUUID, LLUUID> entry in itemsNewParents)
{
if (Store.Contains(entry.Key))
{
InventoryBase inv = Store[entry.Key];
inv.ParentUUID = entry.Value;
Store.UpdateNodeFor(inv);
}
}
}
MoveInventoryItemPacket move = new MoveInventoryItemPacket();
move.AgentData.AgentID = _Client.Self.AgentID;
move.AgentData.SessionID = _Client.Self.SessionID;
move.AgentData.Stamp = false; //FIXME: ??
move.InventoryData = new MoveInventoryItemPacket.InventoryDataBlock[itemsNewParents.Count];
int index = 0;
foreach (KeyValuePair<LLUUID, LLUUID> entry in itemsNewParents)
{
MoveInventoryItemPacket.InventoryDataBlock block = new MoveInventoryItemPacket.InventoryDataBlock();
block.ItemID = entry.Key;
block.FolderID = entry.Value;
block.NewName = new byte[0];
move.InventoryData[index++] = block;
}
_Client.Network.SendPacket(move);
}
#endregion Move
#region Remove
public void RemoveDescendants(LLUUID folder)
{
PurgeInventoryDescendentsPacket purge = new PurgeInventoryDescendentsPacket();
purge.AgentData.AgentID = _Client.Self.AgentID;
purge.AgentData.SessionID = _Client.Self.SessionID;
purge.InventoryData.FolderID = folder;
_Client.Network.SendPacket(purge);
// Update our local copy
lock (Store)
{
if (Store.Contains(folder))
{
List<InventoryBase> contents = Store.GetContents(folder);
foreach (InventoryBase obj in contents)
{
Store.RemoveNodeFor(obj);
}
}
}
}
public void RemoveItem(LLUUID item)
{
List<LLUUID> items = new List<LLUUID>(1);
items.Add(item);
Remove(items, null);
}
public void RemoveFolder(LLUUID folder)
{
List<LLUUID> folders = new List<LLUUID>(1);
folders.Add(folder);
Remove(null, folders);
}
public void Remove(List<LLUUID> items, List<LLUUID> folders)
{
if ((items == null || items.Count == 0) && (folders == null || folders.Count == 0))
return;
RemoveInventoryObjectsPacket rem = new RemoveInventoryObjectsPacket();
rem.AgentData.AgentID = _Client.Self.AgentID;
rem.AgentData.SessionID = _Client.Self.SessionID;
if (items == null || items.Count == 0)
{
// To indicate that we want no items removed:
rem.ItemData = new RemoveInventoryObjectsPacket.ItemDataBlock[1];
rem.ItemData[0] = new RemoveInventoryObjectsPacket.ItemDataBlock();
rem.ItemData[0].ItemID = LLUUID.Zero;
}
else
{
lock (_Store)
{
rem.ItemData = new RemoveInventoryObjectsPacket.ItemDataBlock[items.Count];
for (int i = 0; i < items.Count; i++)
{
rem.ItemData[i] = new RemoveInventoryObjectsPacket.ItemDataBlock();
rem.ItemData[i].ItemID = items[i];