forked from sruon/ffxivlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceParser.cs
511 lines (463 loc) · 19.7 KB
/
ResourceParser.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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ffxivlib
{
public static class ResourceParser
{
private static readonly Dictionary<string, Dictionary<int, string>> Cache =
new Dictionary<string, Dictionary<int, string>>();
/// <summary>
/// Generator to stream the XML.
/// </summary>
/// <param name="fileName">Filename to be streamed</param>
/// <param name="elementName">The name of a node</param>
/// <returns>Yields next element</returns>
private static IEnumerable<XElement> StreamElements(string fileName, string elementName)
{
using (var sr = new StreamReader(fileName, Encoding.GetEncoding("UTF-8")))
{
using (var rdr = XmlTextReader.Create(sr))
{
rdr.MoveToContent();
while (rdr.Read())
{
if ((rdr.NodeType == XmlNodeType.Element) && (rdr.Name == elementName))
{
var e = XElement.ReadFrom(rdr) as XElement;
yield return e;
}
}
rdr.Close();
}
sr.Close();
}
}
/// <summary>
/// Attempts to retrieve the key in internal cache (we iterate over the Dictionary
/// as it is less costly than running XML parsing again and again)
/// or run the Linq query
/// The value is then added to the cache.
/// </summary>
/// <param name="filename">Resource file to parse</param>
/// <param name="node">Type of resource</param>
/// <param name="lookupKey">On what field to match</param>
/// <param name="resultKey">What field do we need to get back</param>
/// <param name="search">The search parameter</param>
/// <returns>The key (ID) or -1</returns>
private static int RunLinqQuery(string filename, string node, string lookupKey, string resultKey, string search)
{
if (search == "")
return -1;
if (Cache.ContainsKey(node))
{
foreach (KeyValuePair<int, string> pair in Cache[node])
{
if (pair.Value == search)
return pair.Key;
}
}
else
Cache.Add(node, new Dictionary<int, string>());
var result = (from item in StreamElements(filename, node)
where item.Element(lookupKey).Value == search
select item.Element(resultKey).Value).FirstOrDefault();
int key = int.Parse(result);
if (key > 0)
Cache[node][key] = search;
return key;
}
/// <summary>
/// Attempts to retrieve the value in internal cache or run the Linq query
/// The value is then added to the cache.
/// </summary>
/// <param name="filename">Resource file to parse</param>
/// <param name="node">Type of resource</param>
/// <param name="lookupKey">On what field to match</param>
/// <param name="resultKey">What field do we need to get back</param>
/// <param name="search">The search parameter</param>
/// <returns>The value (string) or string.empty</returns>
private static string RunLinqQuery(string filename, string node, string lookupKey, string resultKey, int search)
{
if (search == 0)
return string.Empty;
if (Cache.ContainsKey(node))
{
if (Cache[node].ContainsKey(search))
return Cache[node][search];
}
else
Cache.Add(node, new Dictionary<int, string>());
var result = (from item in StreamElements(filename, node)
where int.Parse(item.Element(lookupKey).Value) == search
select item.Element(resultKey).Value).FirstOrDefault();
if (result != null)
Cache[node][search] = result;
return result;
}
/// <summary>
/// Attempts to retrieve the value in internal cache or run the Linq query
/// The value is then added to the cache.
/// This needs to be reworked to handle multiple search parameters easily.
/// </summary>
/// <param name="filename">Resource file to parse</param>
/// <param name="node">Type of resource</param>
/// <param name="lookupKeys"></param>
/// <param name="resultKey">What field do we need to get back</param>
/// <param name="searchValues"></param>
/// <returns>The value (string) or string.empty</returns>
private static string RunLinqQuery(string filename, string node, string[] lookupKeys, string resultKey,
int[] searchValues)
{
if (searchValues.First() == 0)
return string.Empty;
if (Cache.ContainsKey(node))
{
int key = int.Parse(string.Format("{0}{1}", searchValues.First(), searchValues.Last()));
if (Cache[node].ContainsKey(key))
return Cache[node][key];
}
else
Cache.Add(node, new Dictionary<int, string>());
var result = (from item in StreamElements(filename, node)
where int.Parse(item.Element(lookupKeys[0]).Value) == searchValues[0] &&
int.Parse(item.Element(lookupKeys[1]).Value) == searchValues[1]
select item.Element(resultKey).Value).FirstOrDefault();
if (result != null)
{
int key = int.Parse(string.Format("{0}{1}", searchValues.First(), searchValues.Last()));
Cache[node][key] = result;
}
return result;
}
#region Autotranslate lookups
/// <summary>
/// Retrieves Autotranslate corresponding to ID
/// </summary>
/// <param name="id">Item ID</param>
/// <returns>Item Name</returns>
public static string GetAutotranslate(int CategoryId, int Id)
{
string[] lookupKeys = {"CategoryId", "Id"};
int[] searchValues = {CategoryId, Id};
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.AUTOTRANSLATE_FILE), "Autotranslate", lookupKeys, "Content",
searchValues);
}
#endregion
#region Zone lookups
/// <summary>
/// Retrieves Zone Name corresponding to ID.
/// </summary>
/// <param name="id">Zone ID</param>
/// <returns>Zone name</returns>
public static string GetZoneName(int id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.ZONE_FILE), "PlaceName", "Key",
"SGL_" + Constants.ResourceParser.RESOURCES_LANGUAGE, id);
}
/// <summary>
/// Retrieves Zone ID corresponding to Name.
/// </summary>
/// <param name="name">Zone name</param>
/// <returns>Zone ID</returns>
public static int GetZoneID(string name)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.ZONE_FILE), "PlaceName",
"SGL_" + Constants.ResourceParser.RESOURCES_LANGUAGE, "Key", name);
}
#endregion
#region Item lookups
/// <summary>
/// Retrieves Item name corresponding to ID
/// </summary>
/// <param name="id">Item ID</param>
/// <returns>Item Name</returns>
public static string GetItemName(int id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.ITEM_FILE), "Item", "Key",
"UIName_" + Constants.ResourceParser.RESOURCES_LANGUAGE, id);
}
/// <summary>
/// Retrieves Item name corresponding to ID in ITEM structure
/// </summary>
/// <param name="item">Item structure</param>
/// <returns>Item Name</returns>
public static string GetItemName(Item item)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.ITEM_FILE), "Item", "Key",
"UIName_" + Constants.ResourceParser.RESOURCES_LANGUAGE, (int) item.Id);
}
/// <summary>
/// Retrieves Item ID corresponding to name
/// </summary>
/// <param name="name">Item Name</param>
/// <returns>Item ID</returns>
public static int GetItemID(string name)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.ITEM_FILE), "Item",
"UIName_" + Constants.ResourceParser.RESOURCES_LANGUAGE, "Key", name);
}
#endregion
#region Buff lookups
/// <summary>
/// Retrieves Buff name corresponding to ID
/// </summary>
/// <param name="id">Buff ID</param>
/// <returns>Buff Name</returns>
public static string GetBuffName(short id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.BUFF_FILE), "Buff", "id", "name", id);
}
/// <summary>
/// Retrieves Buff name corresponding to ID in BUFF structure
/// </summary>
/// <returns>Buff Name</returns>
public static string GetBuffName(Buff item)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.BUFF_FILE), "Buff", "id", "name", item.Id);
}
/// <summary>
/// Retrieves Buff ID corresponding to name.
/// </summary>
/// <param name="name">Buff name</param>
/// <returns>Buff ID</returns>
public static int GetBuffID(string name)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.BUFF_FILE), "Buff", "name", "id", name);
}
#endregion
#region Title lookups
/// <summary>
/// Retrieves Title Name corresponding to ID.
/// </summary>
/// <param name="id">Title ID</param>
/// <returns>Title name</returns>
public static string GetTitleName(byte id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.TITLE_FILE), "Title", "Key",
"Male_" + Constants.ResourceParser.RESOURCES_LANGUAGE, id);
}
/// <summary>
/// Retrieves Title Name corresponding to ID in Entity.
/// </summary>
/// <param name="e">Entity</param>
/// <returns>Title name</returns>
public static string GetTitleName(Entity e)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.TITLE_FILE), "Title", "Key",
"Male_" + Constants.ResourceParser.RESOURCES_LANGUAGE, e.Structure.Title);
}
#endregion
#region Job lookups
/// <summary>
/// Retrieves Job Name corresponding to ID.
/// </summary>
/// <param name="id">Job ID</param>
/// <returns>Job name</returns>
public static string GetJobName(JOB id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.JOB_FILE), "ClassJob", "Key",
"Name_" + Constants.ResourceParser.RESOURCES_LANGUAGE, (byte) id);
}
/// <summary>
/// Retrieves Job Name corresponding to ID in Entity.
/// </summary>
/// <param name="e">Entity</param>
/// <returns>Job name</returns>
public static string GetJobName(Entity e)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.JOB_FILE), "ClassJob", "Key",
"Name_" + Constants.ResourceParser.RESOURCES_LANGUAGE, (byte) e.Job);
}
/// <summary>
/// Retrieves Job shortname corresponding to ID.
/// </summary>
/// <param name="id">Job ID</param>
/// <returns>Job shortname</returns>
public static string GetJobShortname(JOB id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.JOB_FILE), "ClassJob", "Key",
"Abbreviation_" + Constants.ResourceParser.RESOURCES_LANGUAGE, (byte) id);
}
/// <summary>
/// Retrieves Job shortname corresponding to ID in Entity.
/// </summary>
/// <param name="e">Entity</param>
/// <returns>Job shortname</returns>
public static string GetJobShortname(Entity e)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.JOB_FILE), "ClassJob", "Key",
"Abbreviation_" + Constants.ResourceParser.RESOURCES_LANGUAGE, (byte) e.Job);
}
/// <summary>
/// Retrieves Job ID corresponding to shortname.
/// </summary>
/// <param name="name">Job shortname</param>
/// <returns>Job ID</returns>
public static int GetJobID(string name)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.JOB_FILE), "ClassJob",
"Abbreviation_" + Constants.ResourceParser.RESOURCES_LANGUAGE, "Key", name);
}
#endregion
#region Grand Company lookups
/// <summary>
/// Retrieves Grand Company name corresponding to ID
/// </summary>
/// <param name="id">GC ID</param>
/// <returns>GC Name</returns>
public static string GetGrandCompany(byte id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.GRAND_COMPANY_FILE), "GrandCompany", "Key",
"SGL_" + Constants.ResourceParser.RESOURCES_LANGUAGE, id);
}
/// <summary>
/// Retrieves Grand Company name corresponding to ID in Entity
/// </summary>
/// <param name="e">Entity</param>
/// <returns>GC Name</returns>
public static string GetGrandCompany(Entity e)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.GRAND_COMPANY_FILE), "GrandCompany", "Key",
"SGL_" + Constants.ResourceParser.RESOURCES_LANGUAGE, e.Structure.GrandCompany);
}
/// <summary>
/// Retrieves Grand Company rank corresponding to ID
/// Remarks: This returns only Uldah ranks at the moment
/// </summary>
/// <param name="id">GC Rank ID</param>
/// <returns>GC Rank Name</returns>
public static string GetGrandCompanyRank(byte id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.GRAND_COMPANY_RANK_FILE), "GCRankUldahMaleText", "Key",
"SGL_" + Constants.ResourceParser.RESOURCES_LANGUAGE, id);
}
/// <summary>
/// Retrieves Grand Company rank corresponding to ID in Entity
/// Remarks: This returns only Uldah ranks at the moment
/// </summary>
/// <param name="e">Entity</param>
/// <returns>GC Rank Name</returns>
public static string GetGrandCompanyRank(Entity e)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.GRAND_COMPANY_RANK_FILE), "GCRankUldahMaleText", "Key",
"SGL_" + Constants.ResourceParser.RESOURCES_LANGUAGE, e.Structure.GrandCompanyRank);
}
#endregion
#region Quests lookups
/// <summary>
/// Retrieves Quest Name corresponding to ID.
/// </summary>
/// <param name="id">Quest ID</param>
/// <returns>Quest name</returns>
public static string GetQuestName(int id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.QUEST_FILE), "Quest", "Key",
"Name_" + Constants.ResourceParser.RESOURCES_LANGUAGE, id);
}
#endregion
#region Actions lookups
/// <summary>
/// Retrieves Action name corresponding to ID
/// </summary>
/// <param name="id">Action ID</param>
/// <returns>Action Name</returns>
public static string GetActionName(int id)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.ACTION_FILE), "Action", "Key",
"Name_" + Constants.ResourceParser.RESOURCES_LANGUAGE, id);
}
/// <summary>
/// Retrieves Action name corresponding to ID
/// </summary>
/// <param name="id">Action ID</param>
/// <returns>Action Name</returns>
public static string GetActionName(short id)
{
return GetActionName((int)id);
}
/// <summary>
/// Retrieves Action ID corresponding to name
/// </summary>
/// <param name="name">Action Name</param>
/// <returns>Action ID</returns>
public static int GetActionID(string name)
{
return
RunLinqQuery(
string.Format("{0}/{1}", Constants.ResourceParser.RESOURCES_FOLDER,
Constants.ResourceParser.ACTION_FILE), "Action",
"Name_" + Constants.ResourceParser.RESOURCES_LANGUAGE, "Key", name);
}
#endregion
}
}