-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandleExtractor.cs
246 lines (223 loc) · 8.22 KB
/
HandleExtractor.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace BG3LocalizationMerger
{
internal static partial class HandleExtractor
{
[GeneratedRegex("""<attribute[ \w="]* handle="(\w+)"[ \w="]*/>""")]
private static partial Regex BookHandle();
[GeneratedRegex("""<attribute(?:[\s\w="]*\s(?:id="Type"|value="item")){2}\s*/>""")]
private static partial Regex TemplateItemFilter();
[GeneratedRegex("""<attribute(?:[\s\w="]*\s(?:id="Type"|value="character")){2}\s*/>""")]
private static partial Regex TemplateCharacterFilter();
[GeneratedRegex(
"""<attribute(?:(?:[\s\w="]*\s(?:id="DisplayName"|handle="(?<Name>\w{30,})")){2}|(?:[\s\w="]*\s(?:id="Description"|handle="(?<Description>\w{30,})")){2})[\s\w="]*/>"""
)]
private static partial Regex TemplateHandles();
[GeneratedRegex(
"""<attribute(?:[\s\w="]*\s(?:id="(?:DisplayName|Title)"|handle="(\w{30,})")){2}[\s\w="]*/>"""
)]
private static partial Regex CharacterHandles();
[GeneratedRegex("""<\w+ .*=['"](\w{37})["'].*/>""")]
private static partial Regex GenericHandle();
[GeneratedRegex("""<\w+ handle=['"](\w{37})["'].*/>""")]
private static partial Regex HandleHandle();
[GeneratedRegex(
"""data "(?<Type>(?:Extra)?Description|DisplayName)" "(?<Handle>\w{37});\d+"\s?"""
)]
private static partial Regex StatusHandle();
[GeneratedRegex(
"""<attribute id="(?<Type>\w+)" type="TranslatedString" handle="(?<Handle>\w{37})".*/>"""
)]
private static partial Regex QuestHandle();
[GeneratedRegex(
"""(?:<(?:TextBlock x:Name|Setter TargetName)="Description")\s?""",
RegexOptions.IgnoreCase
)]
private static partial Regex TooltipFilter();
public static IEnumerable<string>? ExtractCharacterHandles(string path)
{
try
{
var text = ReadText(path);
if (!TemplateCharacterFilter().IsMatch(text))
return Enumerable.Empty<string>();
return ExtractHandles(text, CharacterHandles());
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static IEnumerable<string>? ExtractWaypointHandles(string path)
{
try
{
return ExtractGenericHandles(path);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static IEnumerable<string>? ExtractQuestMarkerHandles(string path)
{
try
{
return ExtractGenericHandles(path);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static IEnumerable<string>? ExtractBookHandles(string path)
{
try
{
return ExtractGenericHandles(path);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static (IEnumerable<string>, IEnumerable<string>)? ExtractQuestHandles(string path)
{
try
{
string text = ReadText(path);
MatchCollection matches = QuestHandle().Matches(text);
var group = matches.GroupBy(x => x.Groups["Type"].Value);
return (
group
.FirstOrDefault(x => x.Key == "QuestTitle")
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>(),
group
.Where(x => x.Key.EndsWith("Description"))
.SelectMany(x => x)
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>()
);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static (IEnumerable<string>, IEnumerable<string>)? ExtractStatusHandles(string path)
{
try
{
string text = ReadText(path);
MatchCollection matches = StatusHandle().Matches(text);
var group = matches.GroupBy(x => x.Groups["Type"].Value);
return (
group
.FirstOrDefault(x => x.Key == "DisplayName")
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>(),
group
.Where(x => x.Key.EndsWith("Description"))
.SelectMany(x => x)
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>()
);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static IEnumerable<Group>? ExtractItemGroup(string path)
{
try
{
string text = ReadText(path);
if (!TemplateItemFilter().IsMatch(text))
return Enumerable.Empty<Group>();
MatchCollection matches = TemplateHandles().Matches(text);
return matches
.SelectMany(x => x.Groups.Values.Skip(1))
.Where(x => !string.IsNullOrEmpty(x.Value));
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static IEnumerable<string>? ExtractTooltipHandles(string path)
{
try
{
string text = ReadText(path);
MatchCollection matches = GenericHandle().Matches(text);
return matches
.Where(x => TooltipFilter().IsMatch(x.Groups[0].Value))
.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value)
.Where(x => !string.IsNullOrEmpty(x));
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static IEnumerable<string>? ExtractHintHandles(string path)
{
try
{
return ExtractGenericHandles(path);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
public static IEnumerable<string>? ExtractGenericHandles(string path)
{
try
{
return ExtractHandles(ReadText(path), GenericHandle());
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}
private static string ReadText(string path)
{
using var sr = new StreamReader(path);
return sr.ReadToEnd();
}
private static IEnumerable<string> ExtractHandles(string text, Regex regex)
{
MatchCollection matches = regex.Matches(text);
return matches
.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value)
.Where(x => !string.IsNullOrEmpty(x));
}
private static IEnumerable<Group> ExtractGroups(string text, Regex regex)
{
MatchCollection matches = regex.Matches(text);
return matches
.SelectMany(x => x.Groups.Values.Skip(1))
.Where(x => !string.IsNullOrEmpty(x.Value));
}
}
}