forked from EtherianDR/InventoryView
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInventoryTextSearch.cs
More file actions
139 lines (114 loc) · 5.06 KB
/
InventoryTextSearch.cs
File metadata and controls
139 lines (114 loc) · 5.06 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Windows.Forms;
namespace InventoryView
{
internal class InventoryTextSearch
{
private static string basePath = Application.StartupPath;
public static void PerformSearch(string searchText, string style)
{
if (string.IsNullOrEmpty(searchText))
{
Class1.Host.EchoText("Provide a tap to use.");
return;
}
var matchingItems = SearchXmlData(searchText, style);
if (matchingItems.Count > 0)
{
if (style == "path") Class1.Host.EchoText($"\nFull path for '{searchText}':\n");
else Class1.Host.EchoText($"\nFound {matchingItems.Count} items matching '{searchText}':\n");
foreach (var item in matchingItems)
{
if (style == "path") Class1.Host.EchoText(item);
else Class1.Host.SendText("#link {" + item + "} {#put /iv path " + Regex.Replace(item, @"\w+ - ", "") + "}");
}
}
else
{
Class1.Host.EchoText($"No matches found for '{searchText}'.");
}
}
public static List<string> SearchXmlData(string searchText, string style)
{
basePath = Class1.Host.get_Variable("PluginPath");
var matchingItems = new List<string>();
string xmlPath = Path.Combine(basePath, "InventoryView.xml");
if (!File.Exists(xmlPath))
{
Class1.Host.EchoText("InventoryView.xml not found.");
return matchingItems;
}
XmlDocument doc = new();
doc.Load(xmlPath);
Regex regex = new(searchText, RegexOptions.IgnoreCase);
// Navigate to the CharacterData elements
XmlNodeList characterDataElements = doc.GetElementsByTagName("CharacterData");
foreach (XmlNode characterDataNode in characterDataElements)
{
string characterName = characterDataNode.SelectSingleNode("name")?.InnerText;
if (characterName == null)
continue;
// Get the items section for this character
XmlNode itemsNodes = characterDataNode.SelectSingleNode("items");
if (itemsNodes == null)
continue;
XmlNodeList itemNodes = itemsNodes.SelectNodes(".//ItemData");
foreach (XmlNode itemNode in itemNodes)
{
string itemText = itemNode.SelectSingleNode("tap")?.InnerText;
if (itemText == null)
continue;
// Search for the searchText using a case-insensitive regex pattern
if (regex.IsMatch(itemText))
{
string single = Regex.Replace(itemText.TrimEnd('.'), @"\(\d+\)\s|\s\(closed\)", "");
string fullPath = GetItemPath(itemNode);
if (style == "path")
{
if (single.Equals(searchText, StringComparison.OrdinalIgnoreCase))
{
matchingItems.Add($"{characterName}\n{fullPath}\n");
}
}
else
{
matchingItems.Add($"{characterName} - {single}"); // Single tap line
}
}
}
}
return matchingItems;
}
private static string GetItemPath(XmlNode itemNode)
{
var path = new List<string>();
var current = itemNode;
while (current != null)
{
string source = current.SelectSingleNode("source")?.InnerText;
string tap = current.SelectSingleNode("tap")?.InnerText;
string tapText = !string.IsNullOrEmpty(tap) ? Regex.Replace(tap.TrimEnd('.'), @"\(\d+\)\s", "") : string.Empty;
if (!string.IsNullOrEmpty(source))
{
path.Insert(0, source);
}
if (!string.IsNullOrEmpty(tap))
{
path.Insert(0, tapText);
}
current = current.ParentNode;
// Check if it's the final <tap> node within <items> and it's a single-word item
if (current != null && current.Name == "items" && !current.PreviousSibling.HasChildNodes && !tapText.Contains(' '))
{
break;
}
}
return string.Join(Environment.NewLine, path.Select((part, index) => new string('-', index + 1) + " " + part));
}
}
}