-
Notifications
You must be signed in to change notification settings - Fork 4
/
Class1.cs
576 lines (540 loc) · 25.2 KB
/
Class1.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
using System;
using System.Collections.Generic;
using GeniePlugin.Interfaces;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
namespace InventoryView
{
public class Class1 : GeniePlugin.Interfaces.IPlugin
{
// Genie host.
public static IHost _host;
// Plugin Form
private Form _form;
// This contains all the of the inventory data.
public static List<CharacterData> characterData = new List<CharacterData>();
// Path to Genie config.
private static string basePath = Application.StartupPath;
// Whether or not InventoryView is currently scanning data, and what state it is in.
private string ScanMode = null;
// Keeps track of how many containers deep you are when scanning inventory in containers.
private int level = 1;
// The current character & source being scanned.
private CharacterData currentData = null;
// The last item tha was scanned.
private ItemData lastItem = null;
private bool Debug = false;
private string LastText = "";
public void Initialize(IHost host)
{
_host = host;
basePath = _host.get_Variable("PluginPath");
// Load inventory from the XML config if available.
LoadSettings(initial: true);
}
public void Show()
{
if (_form == null || _form.IsDisposed)
_form = new InventoryViewForm();
_form.Show();
}
public void VariableChanged(string variable)
{
}
public string ParseText(string text, string window)
{
if (ScanMode != null) // If a scan isn't in progress, do nothing here.
{
string trimtext = text.Trim(new char[] { '\n', '\r', ' ' }); // Trims spaces and newlines.
LastText = trimtext;
if (trimtext.StartsWith("XML") && trimtext.EndsWith("XML")) { } // Skip XML parser lines
else if (string.IsNullOrEmpty(trimtext)) { } // Skip blank lines
else if (ScanMode == "Start") // When a scan is initiated, it starts here.
{
if (trimtext == "You have:") // Text that appears at the beginning of "inventory list"
{
_host.EchoText("Scanning Inventory.");
ScanMode = "Inventory";
currentData = new CharacterData() { name = _host.get_Variable("charactername"), source = "Inventory" };
characterData.Add(currentData);
level = 1;
}
} //end if Start
else if (ScanMode == "Inventory")
{
if (text.StartsWith("[Use INVENTORY HELP"))
{
// Skip
}
else if (text.StartsWith("Roundtime:")) // text that appears at the end of "inventory list"
{
// Inventory List has a RT based on the number of items, so grab the number and pause the thread for that length.
Match match = Regex.Match(trimtext, @"^Roundtime:\s{1,3}(\d{1,3})\s{1,3}secs?\.$");
ScanMode = "VaultStart";
_host.EchoText(string.Format("Pausing {0} seconds for RT.", int.Parse(match.Groups[1].Value)));
System.Threading.Thread.Sleep(int.Parse(match.Groups[1].Value) * 1000);
_host.SendText("get my vault book");
}
else
{
// The first level of inventory has a padding of 2 spaces to the left, and each level adds an additional 3 spaces.
// 2, 5, 8, 11, 14, etc..
int spaces = text.Length - text.TrimStart().Length;
int newlevel = (spaces + 1) / 3;
string tap = trimtext;
// remove the - from the beginning if it exists.
if (tap.StartsWith("-")) tap = tap.Remove(0, 1);
// The logic below builds a tree of inventory items.
if (newlevel == 1) // If the item is in the first level, add to the root item list
{
lastItem = currentData.AddItem(new ItemData() { tap = tap });
}
else if (newlevel == level) // If this is the same level as the previous item, add to the previous item's parent's item list.
{
lastItem = lastItem.parent.AddItem(new ItemData() { tap = tap });
}
else if (newlevel == level + 1) // If this item is down a level from the previous, add it to the previous item's item list.
{
lastItem = lastItem.AddItem(new ItemData() { tap = tap });
}
else // Else, if the item is up a level, loop back until you reach the correct level.
{
for (int i = newlevel; i <= level; i++)
{
lastItem = lastItem.parent;
}
lastItem = lastItem.AddItem(new ItemData() { tap = tap });
}
level = newlevel;
}
} //end if Inventory
else if (ScanMode == "VaultStart")
{
// Get the vault book & read it.
Match match = Regex.Match(trimtext, @"^You get a.*vault book.*from");
if (match.Success || trimtext == "You are already holding that.")
{
_host.EchoText("Scanning Vault.");
_host.SendText("read my vault book");
}
else if (trimtext == "Vault Inventory:") // This text appears at the beginning of the vault list.
{
ScanMode = "Vault";
currentData = new CharacterData() { name = _host.get_Variable("charactername"), source = "Vault" };
characterData.Add(currentData);
level = 1;
}
// If you don't have a vault book or you can't read a vault book, it skips to checking your deed register.
else if (trimtext == "What were you referring to?" || trimtext == "The script that the vault book is written in is unfamiliar to you. You are unable to read it." || trimtext == "The vault book is filled with blank pages pre-printed with branch office letterhead. An advertisement touting the services of Rundmolen Bros. Storage Co. is pasted on the inside cover.")
{
_host.EchoText("Skipping Vault.");
ScanMode = "DeedStart";
_host.SendText("get my deed register");
}
} //end if VaultStart
else if (ScanMode == "Vault")
{
// This text indicates the end of the vault inventory list.
if (text.StartsWith("The last note in your book indicates that your vault contains"))
{
ScanMode = "DeedStart";
_host.SendText("stow my vault book");
_host.SendText("get my deed register");
}
else
{
// Determine how many levels down an item is based on the number of spaces before it.
// Anything greater than 4 levels down shows up at the same level as its parent.
int spaces = text.Length - text.TrimStart().Length;
int newlevel = 1;
if (spaces > 4)
newlevel += (spaces - 4) / 2;
//switch (spaces)
//{
// case 4:
// newlevel = 1;
// break;
// case 6:
// newlevel = 2;
// break;
// case 8:
// newlevel = 3;
// break;
// case 10:
// newlevel = 4;
// break;
//}
string tap = trimtext;
if (tap.StartsWith("-")) tap = tap.Remove(0, 1);
if (newlevel == 1)
{
lastItem = currentData.AddItem(new ItemData() { tap = tap, storage = true });
}
else if (newlevel == level)
{
lastItem = lastItem.parent.AddItem(new ItemData() { tap = tap });
}
else if (newlevel == level + 1)
{
lastItem = lastItem.AddItem(new ItemData() { tap = tap });
}
else
{
for (int i = newlevel; i <= level; i++)
{
lastItem = lastItem.parent;
}
lastItem = lastItem.AddItem(new ItemData() { tap = tap });
}
level = newlevel;
}
} //end if Vault
else if (ScanMode == "DeedStart")
{
// Get the vault book & read it.
Match match = Regex.Match(trimtext, @"^You get a.*deed register.*from");
if (match.Success || trimtext == "You are already holding that.")
{
_host.EchoText("Scanning Deed Register.");
_host.SendText("turn my deed register to contents");
_host.SendText("read my deed register");
}
else if (trimtext == "Page -- Deed") // This text appears at the beginning of the deed register list.
{
ScanMode = "Deed";
currentData = new CharacterData() { name = _host.get_Variable("charactername"), source = "Deed" };
characterData.Add(currentData);
level = 1;
}
// If you don't have a deed register or it is empty, it skips to checking your house.
else if (trimtext == "What were you referring to?" || trimtext.StartsWith("You haven't stored any deeds in this register."))
{
_host.EchoText("Skipping Deed Register.");
ScanMode = "HomeStart";
_host.SendText("home recall");
}
} //end if DeedStart
else if (ScanMode == "Deed")
{
if (trimtext.StartsWith("Currently stored"))
{
_host.SendText("stow my deed register");
ScanMode = "HomeStart";
_host.SendText("home recall");
}
else
{
string tap = trimtext.Substring(trimtext.IndexOf("--") + 3);
lastItem = currentData.AddItem(new ItemData() { tap = tap, storage = false });
}
} //end if Deed
else if (ScanMode == "HomeStart")
{
if (trimtext == "The home contains:") // This text appears at the beginning of the home list.
{
_host.EchoText("Scanning Home.");
ScanMode = "Home";
currentData = new CharacterData() { name = _host.get_Variable("charactername"), source = "Home" };
characterData.Add(currentData);
level = 1;
}
// This text appears if you don't have a home, skips and saves the results.
else if (trimtext.StartsWith("Your documentation filed with the Estate Holders"))
{
_host.EchoText("Skipping Home.");
if (_host.get_Variable("guild") == "Trader")
{
ScanMode = "TraderStart";
_host.SendText("get my storage book");
}
else
{
ScanMode = null;
_host.EchoText("Scan Complete.");
_host.SendText("#parse InventoryView scan complete");
SaveSettings();
}
}
else if (trimtext == "You shouldn't do that while inside of a home. Step outside if you need to check something.")
{
_host.EchoText("You cannot check the contents of your home while inside of a home. Step outside and try again.");
if (_host.get_Variable("guild") == "Trader")
{
ScanMode = "TraderStart";
_host.SendText("get my storage book");
}
else
{
ScanMode = null;
_host.EchoText("Scan Complete.");
_host.SendText("#parse InventoryView scan complete");
SaveSettings();
}
}
} //end if HomeStart
else if (ScanMode == "Home")
{
if (trimtext == ">") // There is no text after the home list, so watch for the next >
{
if (_host.get_Variable("guild") == "Trader")
{
ScanMode = "TraderStart";
_host.SendText("get my storage book");
}
else
{
ScanMode = null;
_host.EchoText("Scan Complete.");
_host.SendText("#parse InventoryView scan complete");
SaveSettings();
}
}
else if (trimtext.StartsWith("Attached:")) // If the item is attached, it is in/on/under/behind a piece of furniture.
{
string tap = trimtext.Replace("Attached: ", "");
lastItem = (lastItem.parent != null ? lastItem.parent : lastItem ).AddItem(new ItemData() { tap = tap });
}
else // Otherwise, it is a piece of furniture.
{
string tap = trimtext.Substring(trimtext.IndexOf(":")+2);
lastItem = currentData.AddItem(new ItemData() { tap = tap, storage = true });
}
} //end if Home
else if (ScanMode == "TraderStart")
{
// Get the storage book & read it.
Match match = Regex.Match(trimtext, @"^You get a.*storage book.*from");
if (match.Success || trimtext == "You are already holding that.")
{
_host.EchoText("Scanning Trader Storage.");
_host.SendText("read my storage book");
}
else if (trimtext == "in the known realms since 402.") // This text appears at the beginning of the storage book list.
{
ScanMode = "Trader";
currentData = new CharacterData() { name = _host.get_Variable("charactername"), source = "TraderStorage" };
characterData.Add(currentData);
level = 1;
}
// If you don't have a vault book or you can't read a vault book, it skips to checking your house.
else if (trimtext == "What were you referring to?" || trimtext == "The storage book is filled with complex lists of inventory that make little sense to you.")
{
ScanMode = null;
_host.EchoText("Skipping Trader Storage.");
_host.EchoText("Scan Complete.");
_host.SendText("#parse InventoryView scan complete");
SaveSettings();
}
} // end if trader start
else if (ScanMode == "Trader")
{
// This text indicates the end of the vault inventory list.
if (text.StartsWith("A notation at the bottom indicates"))
{
ScanMode = null;
_host.EchoText("Scan Complete.");
_host.SendText("#parse InventoryView scan complete");
SaveSettings();
}
else
{
// Determine how many levels down an item is based on the number of spaces before it.
// Anything greater than 4 levels down shows up at the same level as its parent.
int spaces = text.Length - text.TrimStart().Length;
int newlevel = 1;
switch (spaces)
{
case 4:
newlevel = 1;
break;
case 8:
newlevel = 2;
break;
case 12:
newlevel = 3;
break;
default:
newlevel = 3;
break;
}
string tap = trimtext;
if (tap.StartsWith("-")) tap = tap.Remove(0, 1);
if (newlevel == 1)
{
lastItem = currentData.AddItem(new ItemData() { tap = tap, storage = true });
}
else if (newlevel == level)
{
lastItem = lastItem.parent.AddItem(new ItemData() { tap = tap });
}
else if (newlevel == level + 1)
{
lastItem = lastItem.AddItem(new ItemData() { tap = tap });
}
else
{
for (int i = newlevel; i <= level; i++)
{
lastItem = lastItem.parent;
}
lastItem = lastItem.AddItem(new ItemData() { tap = tap });
}
level = newlevel;
}
} //end if Trader
}
return text;
}
public string ParseInput(string text)
{
if (text.ToLower().StartsWith("/inventoryview ") || text.ToLower().StartsWith("/iv "))
{
var SplitText = text.Split(' ');
if (SplitText.Length == 1 || SplitText[1].ToLower() == "help")
{
Help();
}
else if (SplitText[1].ToLower() == "scan")
{
if (_host.get_Variable("connected") == "0")
{
_host.EchoText("You must be connected to the server to do a scan.");
}
else
{
LoadSettings();
ScanMode = "Start";
while (characterData.Where(tbl => tbl.name == _host.get_Variable("charactername")).Count() > 0)
{
characterData.Remove(characterData.Where(tbl => tbl.name == _host.get_Variable("charactername")).First());
}
_host.SendText("inventory list");
}
}
else if (SplitText[1].ToLower() == "open")
{
Show();
}
else if (SplitText[1].ToLower() == "debug")
{
Debug = !Debug;
_host.EchoText("InventoryView Debug Mode " + (Debug ? "ON" : "OFF"));
}
else if (SplitText[1].ToLower() == "lasttext")
{
Debug = !Debug;
_host.EchoText("InventoryView Debug Last Text: " + LastText);
}
else
Help();
return string.Empty;
}
return text;
}
public void Help()
{
_host.EchoText("Inventory View plugin options:");
_host.EchoText("/InventoryView scan -- scan the items on the current character.");
_host.EchoText("/InventoryView open -- open the InventoryView Window to see items.");
}
public static void RemoveParents(List<ItemData> iList)
{
foreach (var iData in iList)
{
iData.parent = null;
RemoveParents(iData.items);
}
}
public static void AddParents(List<ItemData> iList, ItemData parent)
{
foreach (var iData in iList)
{
iData.parent = parent;
AddParents(iData.items, iData);
}
}
public void ParseXML(string xml)
{
}
public void ParentClosing()
{
}
public string Name
{
get { return "Inventory View"; }
}
public string Version
{
get { return "1.7"; }
}
public string Description
{
get { return "Stores your character inventory and allows you to search items across characters."; }
}
public string Author
{
get { return "Etherian <EtherianDR@gmail.com>"; }
}
private bool _enabled = true;
public bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
public static void LoadSettings(bool initial = false)
{
string configFile = Path.Combine(basePath, "InventoryView.xml");
if (File.Exists(configFile))
{
try
{
using (Stream stream = File.Open(configFile, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<CharacterData>));
characterData = (List<CharacterData>)serializer.Deserialize(stream);
}
foreach (var cData in characterData)
{
AddParents(cData.items, null);
}
if (!initial)
_host.EchoText("InventoryView data loaded.");
}
catch (IOException ex)
{
_host.EchoText("Error reading InventoryView file: " + ex.Message);
}
}
}
public static void SaveSettings()
{
string configFile = Path.Combine(basePath, "InventoryView.xml");
try
{
// Can't serialize a class with circular references, so I have to remove the parent links first.
foreach (var cData in characterData)
{
RemoveParents(cData.items);
}
FileStream writer = new FileStream(configFile, FileMode.Create);
XmlSerializer serializer = new XmlSerializer(typeof(List<CharacterData>));
serializer.Serialize(writer, characterData);
writer.Close();
// ..and add them back again afterwards.
foreach (var cData in characterData)
{
AddParents(cData.items, null);
}
}
catch (IOException ex)
{
_host.EchoText("Error writing to InventoryView file: " + ex.Message);
}
}
}
}