forked from codeurzebs/fivem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchetypeDebug.cpp
More file actions
299 lines (251 loc) · 7.84 KB
/
Copy pathArchetypeDebug.cpp
File metadata and controls
299 lines (251 loc) · 7.84 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
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
#include <StdInc.h>
#include <CoreConsole.h>
#include <ConsoleHost.h>
#include <imgui.h>
#include <atHashMap.h>
#include <string>
#include <Hooking.h>
#include <EntitySystem.h>
#include <Streaming.h>
template<typename T>
class fwArchetypeDynamicFactory : public fwFactoryBase<T>
{
public:
atMultiHashMap<T> data;
};
struct FactoryInfo
{
FactoryInfo(const char* factoryName) : name(factoryName), count(0), usage(0.0f) {}
std::string name;
size_t count;
float usage;
};
struct FactoryBucket
{
FactoryBucket() : count(0) {}
std::string name;
size_t count;
};
static void* rage__fwArchetypeManager__ms_ArchetypePool;
// Taken from CloneExperiments.cpp
static hook::cdecl_stub<const char* (int, uint32_t)> rage__atHashStringNamespaceSupport__GetString([]
{
return hook::get_pattern("85 D2 75 04 33 C0 EB 61", -0x18);
});
static HookFunction hookFunction([]()
{
auto location = hook::get_call(hook::get_pattern<char>("E8 ? ? ? ? EB 0B 45 33 C0 48 8B CB"));
rage__fwArchetypeManager__ms_ArchetypePool = hook::get_address<void*>(location + 0x2B);
});
static std::string ResolveHashString(uint32_t hash)
{
const char* resolvedName = rage__atHashStringNamespaceSupport__GetString(1, hash);
if (resolvedName)
{
return resolvedName;
}
std::string fileName = streaming::GetStreamingBaseNameForHash(hash);
if (!fileName.empty())
{
return fileName;
}
return fmt::sprintf("Unknown 0x%08X", hash);
}
static std::string GetFriendlyNameFromBucketHash(uint32_t hash)
{
if (hash == 0xFFFF)
{
return "Base Game Content";
}
else if (hash == 0xF000)
{
return "Other Game Content";
}
else if (hash < 0xFFFF)
{
static auto mapTypesStore = streaming::Manager::GetInstance()->moduleMgr.GetStreamingModule("ytyp");
void* mapTypesContent = mapTypesStore->GetPtr(hash);
if (mapTypesContent)
{
hash = *(uint32_t*)((char*)mapTypesContent + 0x8);
}
}
return ResolveHashString(hash);
}
template<typename T>
static int SortCompare(const T& lhs, const T& rhs)
{
if (lhs < rhs)
{
return -1;
}
else if (lhs > rhs)
{
return 1;
}
else
{
return 0;
}
}
static InitFunction initFunction([]()
{
static bool archetypeListEnabled;
static ConVar<bool> archetypeListVar("archetypelist", ConVar_Archive | ConVar_UserPref, false, &archetypeListEnabled);
ConHost::OnShouldDrawGui.Connect([](bool* should)
{
*should = *should || archetypeListEnabled;
});
ConHost::OnDrawGui.Connect([]()
{
if (!archetypeListEnabled)
{
return;
}
size_t archetypePoolSize = *(uint32_t*)(((char*)rage__fwArchetypeManager__ms_ArchetypePool) + 0x8);
size_t archetypePoolFree = *(uint32_t*)(((char*)rage__fwArchetypeManager__ms_ArchetypePool) + 0x10);
// Wait for some archetypes to exist
if (archetypePoolFree == archetypePoolSize)
{
return;
}
ImGui::SetNextWindowSize(ImVec2(500.0f, 300.0f), ImGuiCond_FirstUseEver);
std::vector<FactoryInfo> factoryInfos = {
"CBaseModelInfo",
"CMloModelInfo",
"CTimeModelInfo",
"CWeaponModelInfo",
"CVehicleModelInfo",
"CPedModelInfo",
"CCompEntityModelInfo",
};
std::unordered_map<uint32_t, FactoryBucket> factoryBucketsLookup;
for (int i = 0; i < factoryInfos.size(); i++)
{
auto& factoryInfo = factoryInfos[i];
auto factory = reinterpret_cast<fwArchetypeDynamicFactory<fwArchetype>*>(g_archetypeFactories->Get(i + 1));
factory->data.ForAllEntriesWithHash([&](uint32_t hash, atArray<fwArchetype>* entries)
{
factoryInfo.count += entries->GetCount();
auto& factoryBucket = factoryBucketsLookup[hash];
factoryBucket.name = GetFriendlyNameFromBucketHash(hash);
factoryBucket.count += entries->GetCount();
});
factoryInfo.usage = (float(factoryInfo.count) / float(archetypePoolSize)) * 100.0f;
}
if (ImGui::Begin("Archetype List", &archetypeListEnabled))
{
ImGui::Text("Available Archetypes: %d / %d", archetypePoolFree, archetypePoolSize);
ImGui::Text("Registered Archetypes: %d (%.02f%%)", archetypePoolSize - archetypePoolFree, (1.0 - (float(archetypePoolFree) / float(archetypePoolSize))) * 100.0f);
if (ImGui::BeginTable("##archetypedistribution", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Sortable))
{
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("Factory Type");
ImGui::TableSetupColumn("Num Archetypes Registered", ImGuiTableColumnFlags_PreferSortDescending | ImGuiTableColumnFlags_DefaultSort);
ImGui::TableSetupColumn("Total Contribution", ImGuiTableColumnFlags_PreferSortDescending);
ImGui::TableHeadersRow();
auto sortSpecs = ImGui::TableGetSortSpecs();
if (sortSpecs && sortSpecs->SpecsCount > 0)
{
std::sort(factoryInfos.begin(), factoryInfos.end(), [sortSpecs](const FactoryInfo& left, const FactoryInfo& right)
{
for (int n = 0; n < sortSpecs->SpecsCount; n++)
{
const ImGuiTableColumnSortSpecs* sortSpec = &sortSpecs->Specs[n];
int delta = 0;
switch (sortSpec->ColumnIndex)
{
case 0:
delta = SortCompare(left.name, right.name);
break;
case 1:
delta = SortCompare(left.count, right.count);
break;
case 2:
delta = SortCompare(left.usage, right.usage);
break;
}
if (delta > 0)
{
return (sortSpec->SortDirection == ImGuiSortDirection_Ascending) ? false : true;
}
else if (delta < 0)
{
return (sortSpec->SortDirection == ImGuiSortDirection_Ascending) ? true : false;
}
}
return left.count > right.count;
});
}
for (const auto& factoryInfo : factoryInfos)
{
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text(factoryInfo.name.c_str());
ImGui::TableSetColumnIndex(1);
ImGui::Text("%d", factoryInfo.count);
ImGui::TableSetColumnIndex(2);
ImGui::Text("%.02f%%", factoryInfo.usage);
}
ImGui::EndTable();
}
static char filterText[512];
ImGui::InputText("Filter", filterText, sizeof(filterText));
std::vector<FactoryBucket> factoryBuckets;
for (const auto& factoryBucketKvp : factoryBucketsLookup)
{
factoryBuckets.push_back(factoryBucketKvp.second);
}
if (ImGui::BeginTable("##archetypebuckets", 2, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Resizable | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Sortable))
{
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("Bucket Name");
ImGui::TableSetupColumn("Num Archetypes Defined", ImGuiTableColumnFlags_PreferSortDescending | ImGuiTableColumnFlags_DefaultSort);
ImGui::TableHeadersRow();
auto sortSpecs = ImGui::TableGetSortSpecs();
if (sortSpecs && sortSpecs->SpecsCount > 0)
{
std::sort(factoryBuckets.begin(), factoryBuckets.end(), [sortSpecs](const FactoryBucket& left, const FactoryBucket& right)
{
for (int n = 0; n < sortSpecs->SpecsCount; n++)
{
const ImGuiTableColumnSortSpecs* sortSpec = &sortSpecs->Specs[n];
int delta = 0;
switch (sortSpec->ColumnIndex)
{
case 0:
delta = SortCompare(left.name, right.name);
break;
case 1:
delta = SortCompare(left.count, right.count);
break;
}
if (delta > 0)
{
return (sortSpec->SortDirection == ImGuiSortDirection_Ascending) ? false : true;
}
else if (delta < 0)
{
return (sortSpec->SortDirection == ImGuiSortDirection_Ascending) ? true : false;
}
}
return left.count > right.count;
});
}
for (const auto& factoryBucket : factoryBuckets)
{
if (factoryBucket.name.find(filterText) != std::string::npos)
{
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text(factoryBucket.name.c_str());
ImGui::TableSetColumnIndex(1);
ImGui::Text("%d", factoryBucket.count);
}
}
ImGui::EndTable();
}
}
ImGui::End();
});
});