-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObjectIndentification.cs
488 lines (423 loc) · 18.7 KB
/
ObjectIndentification.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
using System.ComponentModel;
using System.Text;
using Dalamud.Utility;
using Lumina;
using Lumina.Data.Parsing;
using Lumina.Excel.GeneratedSheets;
using Action = Lumina.Excel.GeneratedSheets.Action;
namespace PathMapper;
internal class ObjectIdentification {
private readonly List<(ulong, HashSet<Item>)> _weapons;
private readonly List<(ulong, HashSet<Item>)> _equipment;
private readonly Dictionary<string, HashSet<Action>> _actions;
private readonly GamePathParser _parser;
private readonly GameData _gameData;
private readonly BNpcContainer _bnpcs;
private static bool Add(IDictionary<ulong, HashSet<Item>> dict, ulong key, Item item) {
if (dict.TryGetValue(key, out var list)) {
return list.Add(item);
}
dict[key] = new HashSet<Item> { item };
return true;
}
private static ulong EquipmentKey(Item i) {
var model = (ulong) ((Quad) i.ModelMain).A;
var variant = (ulong) ((Quad) i.ModelMain).B;
var slot = (ulong) ((EquipSlot) i.EquipSlotCategory.Row).ToSlot();
return (model << 32) | (slot << 16) | variant;
}
private static ulong WeaponKey(Item i, bool offhand) {
var quad = offhand ? (Quad) i.ModelSub : (Quad) i.ModelMain;
var model = (ulong) quad.A;
var type = (ulong) quad.B;
var variant = (ulong) quad.C;
return (model << 32) | (type << 16) | variant;
}
private void AddAction(string key, Action action) {
if (key.Length == 0) {
return;
}
key = key.ToLowerInvariant();
if (this._actions.TryGetValue(key, out var actions)) {
actions.Add(action);
} else {
this._actions[key] = new HashSet<Action> { action };
}
}
public ObjectIdentification(GameData dataManager, GamePathParser parser, BNpcContainer bnpcs) {
this._gameData = dataManager;
this._parser = parser;
this._bnpcs = bnpcs;
var items = dataManager.GetExcelSheet<Item>()!;
SortedList<ulong, HashSet<Item>> weapons = new();
SortedList<ulong, HashSet<Item>> equipment = new();
foreach (var item in items) {
switch ((EquipSlot) item.EquipSlotCategory.Row) {
case EquipSlot.MainHand:
case EquipSlot.OffHand:
case EquipSlot.BothHand:
if (item.ModelMain != 0) {
Add(weapons, WeaponKey(item, false), item);
}
if (item.ModelSub != 0) {
Add(weapons, WeaponKey(item, true), item);
}
break;
// Accessories
case EquipSlot.RFinger:
case EquipSlot.Wrists:
case EquipSlot.Ears:
case EquipSlot.Neck:
Add(equipment, EquipmentKey(item), item);
break;
// Equipment
case EquipSlot.Head:
case EquipSlot.Body:
case EquipSlot.Hands:
case EquipSlot.Legs:
case EquipSlot.Feet:
case EquipSlot.BodyHands:
case EquipSlot.BodyHandsLegsFeet:
case EquipSlot.BodyLegsFeet:
case EquipSlot.FullBody:
case EquipSlot.HeadBody:
case EquipSlot.LegsFeet:
Add(equipment, EquipmentKey(item), item);
break;
default: continue;
}
}
this._actions = new Dictionary<string, HashSet<Action>>();
foreach (var action in dataManager.GetExcelSheet<Action>()!
.Where(a => a.Name.ToString().Any())) {
var startKey = action.AnimationStart?.Value?.Name?.Value?.Key.ToString() ?? string.Empty;
var endKey = action.AnimationEnd?.Value?.Key.ToString() ?? string.Empty;
var hitKey = action.ActionTimelineHit?.Value?.Key.ToString() ?? string.Empty;
this.AddAction(startKey, action);
this.AddAction(endKey, action);
this.AddAction(hitKey, action);
}
this._weapons = weapons.Select(kvp => (kvp.Key, kvp.Value)).ToList();
this._equipment = equipment.Select(kvp => (kvp.Key, kvp.Value)).ToList();
}
private class Comparer : IComparer<(ulong, HashSet<Item>)> {
public int Compare((ulong, HashSet<Item>) x, (ulong, HashSet<Item>) y) => x.Item1.CompareTo(y.Item1);
}
private static (int, int) FindIndexRange(List<(ulong, HashSet<Item>)> list, ulong key, ulong mask) {
var maskedKey = key & mask;
var idx = list.BinarySearch(0, list.Count, (key, null!), new Comparer());
if (idx < 0) {
if (~idx == list.Count || maskedKey != (list[~idx].Item1 & mask)) {
return (-1, -1);
}
idx = ~idx;
}
var endIdx = idx + 1;
while (endIdx < list.Count && maskedKey == (list[endIdx].Item1 & mask)) {
++endIdx;
}
return (idx, endIdx);
}
private void FindEquipment(IDictionary<string, object?> set, GameObjectInfo info) {
var key = (ulong) info.PrimaryId << 32;
var mask = 0xFFFF00000000ul;
if (info.EquipSlot != EquipSlot.Unknown) {
key |= (ulong) info.EquipSlot.ToSlot() << 16;
mask |= 0xFFFF0000;
}
if (info.Variant != 0) {
key |= info.Variant;
mask |= 0xFFFF;
}
var (start, end) = FindIndexRange(this._equipment, key, mask);
if (start == -1) {
return;
}
for (; start < end; ++start) {
foreach (var item in this._equipment[start].Item2) {
var name = item.Name.ToString();
if (string.IsNullOrWhiteSpace(name) || (item.RowId != 17557 && name.StartsWith("Dated "))) {
continue;
}
set[name] = item;
}
}
}
private void FindWeapon(IDictionary<string, object?> set, GameObjectInfo info) {
var key = (ulong) info.PrimaryId << 32;
var mask = 0xFFFF00000000ul;
if (info.SecondaryId != 0) {
key |= (ulong) info.SecondaryId << 16;
mask |= 0xFFFF0000;
}
if (info.Variant != 0) {
key |= info.Variant;
mask |= 0xFFFF;
}
var (start, end) = FindIndexRange(this._weapons, key, mask);
if (start == -1) {
return;
}
for (; start < end; ++start) {
foreach (var item in this._weapons[start].Item2) {
var name = item.Name.ToString();
if (string.IsNullOrWhiteSpace(name) || (item.RowId != 17557 && name.StartsWith("Dated "))) {
continue;
}
set[name] = item;
}
}
}
private static void AddCounterString(IDictionary<string, object?> set, string data) {
if (set.TryGetValue(data, out var obj) && obj is int counter) {
set[data] = counter + 1;
} else {
set[data] = 1;
}
}
private readonly Dictionary<(FileType, uint[]), HashSet<string>> _cachedBNpcs = new();
private readonly Dictionary<(FileType, uint[]), HashSet<string>> _cachedMonsters = new();
private void IdentifyParsed(IDictionary<string, object?> set, GameObjectInfo info) {
switch (info.ObjectType) {
case ObjectType.Unknown:
switch (info.FileType) {
case FileType.Sound:
AddCounterString(set, FileType.Sound.ToString());
break;
case FileType.Animation:
case FileType.Pap:
AddCounterString(set, FileType.Animation.ToString());
break;
case FileType.Shader:
AddCounterString(set, FileType.Shader.ToString());
break;
}
break;
case ObjectType.LoadingScreen:
case ObjectType.Interface:
case ObjectType.Vfx:
case ObjectType.World:
case ObjectType.Housing:
case ObjectType.Font:
AddCounterString(set, info.ObjectType.ToString());
break;
case ObjectType.Map: {
var id = string.Join("", new[] {
(char) info.MapC1,
(char) info.MapC2,
(char) info.MapC3,
(char) info.MapC4,
});
var realId = $"{id}/{info.Variant:00}";
var names = this._gameData.GetExcelSheet<Map>()!
.Where(row => row.Id == realId)
.Select(row => (row.PlaceNameRegion.Value!.Name.ToDalamudString().TextValue.Trim(), row.PlaceName.Value!.Name.ToDalamudString().TextValue.Trim(), row.PlaceNameSub.Value!.Name.ToDalamudString().TextValue.Trim()))
.Select(pn => {
var sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(pn.Item1)) {
sb.Append(pn.Item1);
}
if (!string.IsNullOrWhiteSpace(pn.Item2)) {
if (sb.Length > 0) {
sb.Append(" - ");
}
sb.Append(pn.Item2);
}
if (!string.IsNullOrWhiteSpace(pn.Item3) && pn.Item2 != pn.Item3) {
var empty = sb.Length == 0;
if (!empty) {
sb.Append(" (");
}
sb.Append(pn.Item3);
if (!empty) {
sb.Append(')');
}
}
return $"Map: {sb}";
})
.ToHashSet();
foreach (var name in names) {
set[name] = null;
}
break;
}
case ObjectType.DemiHuman: {
var matchers = ExtractMatchers(info);
if (!this._cachedBNpcs.TryGetValue((info.FileType, matchers), out var names)) {
names = this._gameData.GetExcelSheet<BNpcBase>()!
.Where(row => row.ModelChara.Value!.Type == 2 && Matches(row.ModelChara.Value, matchers))
.SelectMany(row => this._bnpcs.bnpc.Where(bnpc => bnpc.bnpcBase == row.RowId))
.Select(e => this._gameData.GetExcelSheet<BNpcName>()!.GetRow(e.bnpcName)?.Singular.ToDalamudString().TextValue.Trim())
.Where(name => !string.IsNullOrWhiteSpace(name))
.Cast<string>()
.ToHashSet();
this._cachedBNpcs[(info.FileType, matchers)] = names;
}
foreach (var name in names) {
set[name] = null;
}
break;
}
case ObjectType.Monster: {
var matchers = ExtractMatchers(info);
if (!this._cachedMonsters.TryGetValue((info.FileType, matchers), out var names)) {
names = this._gameData.GetExcelSheet<ModelChara>()!
.Where(row => row.RowId != 0 && row.Type == 3 && Matches(row, matchers))
.SelectMany(row => {
var minions = this._gameData.GetExcelSheet<Companion>()!
.Where(com => com.Model.Row == row.RowId)
.Select(com => com.Singular.ToDalamudString().TextValue.Trim())
.Where(name => !string.IsNullOrWhiteSpace(name))
.Select(name => $"Minion: {name}")
.ToHashSet();
if (minions.Count > 0) {
return minions;
}
var mounts = this._gameData.GetExcelSheet<Mount>()!
.Where(com => com.ModelChara.Row == row.RowId)
.Select(com => com.Singular.ToDalamudString().TextValue.Trim())
.Where(name => !string.IsNullOrWhiteSpace(name))
.Select(name => $"Mount: {name}")
.ToHashSet();
if (mounts.Count > 0) {
return mounts;
}
var battleNpcs = this._gameData.GetExcelSheet<BNpcBase>()!
.Where(b => b.ModelChara.Row == row.RowId)
.SelectMany(b => this._bnpcs.bnpc.Where(bn => bn.bnpcBase == b.RowId))
.Select(e => this._gameData.GetExcelSheet<BNpcName>()!.GetRow(e.bnpcName)?.Singular.ToDalamudString().TextValue.Trim())
.Where(name => !string.IsNullOrWhiteSpace(name))
.Select(name => $"Battle NPC: {name}");
return battleNpcs;
})
.ToHashSet();
this._cachedMonsters[(info.FileType, matchers)] = names;
}
foreach (var name in names) {
set[name] = null;
}
break;
}
case ObjectType.Icon:
set[$"Icon: {info.IconId}"] = null;
break;
case ObjectType.Accessory:
case ObjectType.Equipment:
this.FindEquipment(set, info);
break;
case ObjectType.Weapon:
this.FindWeapon(set, info);
break;
case ObjectType.Character:
var (gender, race) = info.GenderRace.Split();
var raceString = race != ModelRace.Unknown ? race.ToName() + " " : "";
var genderString = gender != Gender.Unknown ? gender.ToName() + " " : "Player ";
switch (info.CustomizationType) {
case CustomizationType.Skin:
set[$"{raceString}{genderString}Skin Textures"] = null;
break;
case CustomizationType.DecalFace:
set[$"Face Decal {info.PrimaryId}"] = null;
break;
case CustomizationType.Iris when race == ModelRace.Unknown:
set["All Eyes (Catchlight)"] = null;
break;
default: {
var customizationString = race == ModelRace.Unknown
|| info.BodySlot == BodySlot.Unknown
|| info.CustomizationType == CustomizationType.Unknown
? "Customization: Unknown"
: $"{race.ToName()} {gender} {info.BodySlot} ({info.CustomizationType}) {info.PrimaryId}";
var isSkel = info.BodySlot == BodySlot.Unknown
&& info.CustomizationType != CustomizationType.Unknown
&& race != ModelRace.Unknown
&& info.FileType == FileType.Skeleton;
if (isSkel) {
// FIXME: met/m0188 surely is an item id, right
// Goatskin Pothelm (Midlander Male Skeleton)
customizationString = $"{race.ToName()} {gender} {info.CustomizationType} Skeleton";
}
set[customizationString] = null;
break;
}
}
break;
default:
throw new InvalidEnumArgumentException();
}
}
private void IdentifyVfx(IDictionary<string, object?> set, string path) {
var key = this._parser.VfxToKey(path);
if (key.Length == 0 || !this._actions.TryGetValue(key, out var actions)) {
return;
}
foreach (var action in actions) {
set[$"Action: {action.Name}"] = action;
}
}
public void Identify(IDictionary<string, object?> set, string path) {
if (path.EndsWith(".pap") || path.EndsWith(".tmb")) {
this.IdentifyVfx(set, path);
} else {
var infos = this._parser.GetFileInfo(path);
foreach (var info in infos) {
this.IdentifyParsed(set, info);
}
}
}
public Dictionary<string, object?> Identify(string path) {
Dictionary<string, object?> ret = new();
this.Identify(ret, path);
return ret;
}
public Item? Identify(SetId setId, WeaponType weaponType, ushort variant, EquipSlot slot) {
switch (slot) {
case EquipSlot.MainHand:
case EquipSlot.OffHand: {
var (begin, _) = FindIndexRange(this._weapons, ((ulong) setId << 32) | ((ulong) weaponType << 16) | variant,
0xFFFFFFFFFFFF);
return begin >= 0 ? this._weapons[begin].Item2.FirstOrDefault() : null;
}
default: {
var (begin, _) = FindIndexRange(this._equipment,
((ulong) setId << 32) | ((ulong) slot.ToSlot() << 16) | variant,
0xFFFFFFFFFFFF);
return begin >= 0 ? this._equipment[begin].Item2.FirstOrDefault() : null;
}
}
}
private static uint[] ExtractMatchers(GameObjectInfo info) {
var match = info.FileType switch {
FileType.Material => 3,
FileType.Texture => 3,
FileType.Vfx => 3,
FileType.Model => 2,
FileType.Skeleton => 1,
FileType.SkeletonParameter => 1,
FileType.ElementId => 1,
FileType.SkeletonPhysicsBinary => 1,
_ => 0,
};
if (match == 0) {
return Array.Empty<uint>();
}
var inQuestion = new uint[] {
info.PrimaryId,
info.SecondaryId,
info.Variant,
};
return inQuestion[..match];
}
private static bool Matches(ModelChara chara, uint[] matchers) {
var match = matchers.Length;
if (match == 0) {
return false;
}
var parts = new uint[] {
chara.Model,
chara.Base,
chara.Variant,
};
return parts[..match].SequenceEqual(matchers);
}
}