forked from cinderblocks/libremetaverse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryExplorer.cs
More file actions
375 lines (319 loc) · 14 KB
/
Copy pathInventoryExplorer.cs
File metadata and controls
375 lines (319 loc) · 14 KB
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
/*
* Copyright (c) 2025-2026, Sjofn LLC.
* 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 openmetaverse.co 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.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenMetaverse;
#nullable enable
namespace InventoryExplorer
{
/// <summary>
/// A tool to explore and export inventory contents.
/// Demonstrates inventory navigation, searching, and data export.
/// </summary>
internal class InventoryExplorer
{
private static GridClient? client;
private static bool inventoryComplete = false;
static async Task<int> Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("InventoryExplorer - Explore and export inventory data");
Console.WriteLine();
Console.WriteLine("Usage: InventoryExplorer [firstname] [lastname] [password] [options]");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --search <term> Search for items by name");
Console.WriteLine(" --type <type> Filter by type (texture, object, notecard, etc.)");
Console.WriteLine(" --export <file> Export inventory tree to file");
Console.WriteLine(" --stats Show inventory statistics");
Console.WriteLine();
Console.WriteLine("Examples:");
Console.WriteLine(" InventoryExplorer John Doe password123 --stats");
Console.WriteLine(" InventoryExplorer John Doe password123 --search \"sword\"");
Console.WriteLine(" InventoryExplorer John Doe password123 --export inventory.txt");
return 1;
}
var options = ParseOptions(args);
client = new GridClient();
client.Network.LoginProgress += Network_LoginProgress;
client.Network.Disconnected += Network_Disconnected;
Console.WriteLine("Logging in...");
var loginParams = client.Network.DefaultLoginParams(args[0], args[1], args[2],
"InventoryExplorer", "1.0.0");
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
try
{
var success = await client.Network.LoginAsync(loginParams, cts.Token);
if (!success)
{
Console.WriteLine($"Login failed: {client.Network.LoginMessage}");
return 1;
}
Console.WriteLine("Logged in successfully");
Console.WriteLine("Downloading inventory...");
// Wait for inventory to download
var timeout = DateTime.UtcNow.AddSeconds(30);
while (!inventoryComplete && DateTime.UtcNow < timeout)
{
await Task.Delay(100, cts.Token);
if ((client!.Inventory?.Store?.Count ?? 0) > 0)
{
inventoryComplete = true;
}
}
if (!inventoryComplete)
{
Console.WriteLine("Warning: Inventory download may be incomplete");
}
Console.WriteLine($"Inventory loaded: {client!.Inventory?.Store?.Count ?? 0} items");
Console.WriteLine();
// Process based on options
if (options.ShowStats)
ShowStatistics();
if (!string.IsNullOrEmpty(options.SearchTerm))
SearchInventory(options.SearchTerm, options.FilterType);
if (!string.IsNullOrEmpty(options.ExportFile))
ExportInventory(options.ExportFile);
if (!options.ShowStats && string.IsNullOrEmpty(options.SearchTerm)
&& string.IsNullOrEmpty(options.ExportFile))
{
ShowInventoryTree();
}
client.Network.Logout();
return 0;
}
catch (OperationCanceledException)
{
Console.WriteLine("Login timed out");
return 1;
}
}
private static void ShowStatistics()
{
if (client == null) return;
// Collect all inventory items by traversing folders
var allItems = CollectAllInventoryBases().OfType<InventoryItem>().ToList();
var store = client!.Inventory?.Store;
if (store == null) return;
var root = store.RootFolder;
var rootContents = root == null ? Enumerable.Empty<InventoryFolder>() : store.GetContents(root).OfType<InventoryFolder>();
Console.WriteLine("=== Inventory Statistics ===");
Console.WriteLine($"Total Items: {allItems.Count}");
Console.WriteLine($"Root Folders: {rootContents.Count()}");
Console.WriteLine();
var byType = allItems.GroupBy(i => i.AssetType)
.OrderByDescending(g => g.Count())
.Take(10);
Console.WriteLine("Top Item Types:");
foreach (var group in byType)
{
Console.WriteLine($" {group.Key,-20} {group.Count(),6} items");
}
Console.WriteLine();
}
private static void SearchInventory(string searchTerm, AssetType? filterType)
{
if (client == null) return;
var allBases = CollectAllInventoryBases();
var results = allBases
.Where(i => i.Name != null && i.Name.Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
.Where(i => filterType == null || (i is InventoryItem ii && ii.AssetType == filterType))
.OrderBy(i => i.Name)
.ToList();
Console.WriteLine($"=== Search Results for '{searchTerm}' ===");
Console.WriteLine($"Found {results.Count} matching items");
Console.WriteLine();
foreach (var item in results.Take(50))
{
string folderName = "Unknown";
if (client.Inventory?.Store != null && client.Inventory.Store.TryGetValue(item.ParentUUID, out InventoryBase? parent) && parent != null)
{
folderName = (parent as InventoryFolder)?.Name ?? "Unknown";
}
Console.WriteLine($"{item.Name}");
if (item is InventoryItem itemI)
{
Console.WriteLine($" Type: {itemI.AssetType}");
}
Console.WriteLine($" Folder: {folderName}");
Console.WriteLine($" UUID: {item.UUID}");
Console.WriteLine();
}
if (results.Count > 50)
Console.WriteLine($"... and {results.Count - 50} more results");
}
private static void ShowInventoryTree()
{
if (client == null) return;
Console.WriteLine("=== Inventory Tree (Top Level) ===");
Console.WriteLine();
var store = client!.Inventory?.Store;
if (store == null) return;
var rootFolder = store.RootFolder;
if (rootFolder == null) return;
var rootContents = store.GetContents(rootFolder);
foreach (var item in rootContents.OrderBy(i => i.Name))
{
if (item is InventoryFolder folder)
{
var contents = store.GetContents(folder);
Console.WriteLine($"?? {folder.Name} ({contents.Count} items)");
}
else
{
if (item is InventoryItem ii)
Console.WriteLine($"?? {item.Name} ({ii.AssetType})");
else
Console.WriteLine($"?? {item.Name}");
}
}
}
private static void ExportInventory(string filename)
{
if (client == null) return;
Console.WriteLine($"Exporting inventory to {filename}...");
var sb = new StringBuilder();
sb.AppendLine("LibreMetaverse Inventory Export");
sb.AppendLine($"Date: {DateTime.Now}");
sb.AppendLine($"Items: {CollectAllInventoryBases().Count}");
sb.AppendLine();
var store = client.Inventory?.Store;
var rootFolder = store?.RootFolder;
if (rootFolder != null)
{
ExportFolder(sb, rootFolder, 0);
}
File.WriteAllText(filename, sb.ToString());
Console.WriteLine($"Exported {CollectAllInventoryBases().Count} items to {filename}");
}
private static void ExportFolder(StringBuilder sb, InventoryFolder? folder, int depth)
{
if (client == null || folder == null) return;
var indent = new string(' ', depth * 2);
var store = client!.Inventory?.Store;
if (store == null) return;
var contents = store.GetContents(folder);
foreach (var item in contents.OrderBy(i => i is InventoryFolder ? 0 : 1).ThenBy(i => i.Name))
{
if (item is InventoryFolder subfolder)
{
sb.AppendLine($"{indent}[Folder] {subfolder.Name}");
if (depth < 10) // Prevent too deep recursion
ExportFolder(sb, subfolder, depth + 1);
}
else
{
if (item is InventoryItem ii)
sb.AppendLine($"{indent}{item.Name} ({ii.AssetType}) - {item.UUID}");
else
sb.AppendLine($"{indent}{item.Name} - {item.UUID}");
}
}
}
private static List<InventoryBase> CollectAllInventoryBases()
{
var list = new List<InventoryBase>();
if (client == null) return list;
var store = client.Inventory?.Store;
if (store == null) return list;
var root = store.RootFolder;
if (root == null) return list;
var stack = new Stack<InventoryFolder>();
stack.Push(root);
while (stack.Count > 0)
{
var folder = stack.Pop();
var contents = store.GetContents(folder);
foreach (var entry in contents)
{
list.Add(entry);
if (entry is InventoryFolder sub)
{
stack.Push(sub);
}
}
}
return list;
}
private static CommandOptions ParseOptions(string[] args)
{
var options = new CommandOptions();
for (int i = 3; i < args.Length; i++)
{
switch (args[i].ToLower())
{
case "--stats":
options.ShowStats = true;
break;
case "--search":
if (i + 1 < args.Length)
options.SearchTerm = args[++i];
break;
case "--type":
if (i + 1 < args.Length && Enum.TryParse<AssetType>(args[i + 1], true, out var type))
{
options.FilterType = type;
i++;
}
break;
case "--export":
if (i + 1 < args.Length)
options.ExportFile = args[++i];
break;
}
}
return options;
}
private static void Network_LoginProgress(object? sender, LoginProgressEventArgs e)
{
if (e.Status == LoginStatus.Success)
{
Console.WriteLine("Login successful");
}
else if (e.Status == LoginStatus.Failed)
{
Console.WriteLine($"Login failed: {e.Message}");
}
}
private static void Network_Disconnected(object? sender, DisconnectedEventArgs e)
{
Console.WriteLine($"Disconnected: {e.Reason}");
}
private class CommandOptions
{
public bool ShowStats { get; set; }
public string SearchTerm { get; set; } = string.Empty;
public AssetType? FilterType { get; set; }
public string ExportFile { get; set; } = string.Empty;
}
}
}