Skip to content

Commit a391a43

Browse files
committed
fix: cache get heroes lookup to make much more performant
1 parent 1969b7a commit a391a43

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

DataTool/DataModels/Hero/Talent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void Init(STUTalentBase stu, ulong key = default) {
5555

5656
Loadout = new HeroLoadout {
5757
GUID = talent.m_loadout,
58-
Name = LoadoutVM.GetName(talent.m_loadout),
58+
Name = Helpers.GetLoadoutById(talent.m_loadout)?.Name,
5959
HeroGUID = hero?.GUID ?? null,
6060
HeroName = hero?.Name,
6161
};
@@ -67,13 +67,13 @@ public void Init(STUTalentBase stu, ulong key = default) {
6767

6868
Loadout = new HeroLoadout {
6969
GUID = perk.m_loadout,
70-
Name = LoadoutVM.GetName(perk.m_loadout),
70+
Name = Helpers.GetLoadoutById(perk.m_loadout)?.Name,
7171
HeroGUID = hero?.GUID ?? null,
7272
HeroName = hero?.Name,
7373
};
7474
}
7575

76-
Name = LoadoutVM.GetName(perk.m_loadout);
76+
Name = Helpers.GetLoadoutById(perk.m_loadout)?.Name;
7777
Level = (int) perk.m_4DDE5023;
7878
Major = perk.m_D60C9EA2 != 0;
7979
}

DataTool/Helper/Helpers.cs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,63 @@
44
using System.Linq;
55
using DataTool.DataModels.Hero;
66
using TankLib;
7-
using TankLib.STU.Types;
87

98
namespace DataTool.Helper;
109

1110
public static class Helpers {
11+
private static Dictionary<teResourceGUID, HeroVM>? _heroesCache;
12+
private static Dictionary<teResourceGUID, LoadoutVM>? _loadoutsCache;
13+
1214
/// <summary>
13-
/// Returns dictionary of all Heroes by GUID using the <see cref="Hero"/> data model.
15+
/// Returns dictionary of all Heroes by GUID using the <see cref="HeroVM"/> data model.
1416
/// </summary>
1517
public static Dictionary<teResourceGUID, Hero> GetHeroes() {
18+
if (_heroesCache != null) {
19+
return _heroesCache;
20+
}
21+
1622
var @return = new Dictionary<teResourceGUID, Hero>();
1723
foreach (teResourceGUID key in Program.TrackedFiles[0x75]) {
18-
var hero = STUHelper.GetInstance<STUHero>(key);
24+
var hero = HeroVM.Load(key);
1925
if (hero == null) continue;
20-
@return[key] = new Hero(hero, key);
26+
@return[key] = hero;
2127
}
2228

29+
_heroesCache = @return;
2330
return @return;
2431
}
2532

33+
/// <summary>
34+
/// Returns dictionary of all Loadouts by GUID using the <see cref="LoadoutVM"/> data model.
35+
/// </summary>
36+
public static Dictionary<teResourceGUID, Loadout> GetLoadouts() {
37+
if (_loadoutsCache != null) {
38+
return _loadoutsCache;
39+
}
40+
41+
var @return = new Dictionary<teResourceGUID, Loadout>();
42+
foreach (teResourceGUID key in Program.TrackedFiles[0x9E]) {
43+
var loadout = LoadoutVM.Load(key);
44+
if (loadout == null) continue;
45+
@return[key] = loadout;
46+
}
47+
48+
_loadoutsCache = @return;
49+
return @return;
50+
}
51+
52+
/// <summary>Returns a Loadout by its GUID</summary>
53+
public static LoadoutVM? GetLoadoutById(ulong guid) {
54+
var loadouts = GetLoadouts();
55+
return loadouts.GetValueOrDefault((teResourceGUID) guid);
56+
}
57+
58+
/// <summary>Returns a Hero by its GUID</summary>
59+
public static HeroVM? GetHeroById(ulong guid) {
60+
var heroes = GetHeroes();
61+
return heroes.GetValueOrDefault((teResourceGUID) guid);
62+
}
63+
2664
/// <summary>
2765
/// Returns a mapping of hero names to their GUIDs in lowercase.
2866
/// </summary>
@@ -42,7 +80,7 @@ public static IgnoreCaseDict<string> GetHeroNameLocaleOverrides(Dictionary<teRes
4280
if (!namesForThisLocale.TryGetValue(localizedName.Value, out var nameForThisLocale)) {
4381
continue;
4482
}
45-
83+
4684
if (localizedName.Key.Equals(nameForThisLocale, StringComparison.OrdinalIgnoreCase)) {
4785
// identical, don't bother mapping
4886
continue;

0 commit comments

Comments
 (0)