forked from RyanJGray/AlienIsolation.DevTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDEBUG_TEXT.cpp
More file actions
399 lines (344 loc) · 12.2 KB
/
Copy pathDEBUG_TEXT.cpp
File metadata and controls
399 lines (344 loc) · 12.2 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
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
#include "DEBUG_TEXT.h"
#include "DEBUG_MARKER.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <imgui.h>
#include <cfloat>
#include <cstdio>
#include <mutex>
#include <string>
#include <vector>
namespace
{
struct TextEntry
{
void* owner;
std::string text;
int alignment;
float size;
ImU32 colour;
bool stacking;
ULONGLONG expiresAt;
ULONGLONG refreshedAt;
};
std::mutex g_mutex;
std::vector<TextEntry> g_entries;
constexpr int kDefaultSize = 20;
constexpr uint32_t kInvalidString = 0xFFFFFFFF;
constexpr size_t kMaxStackEntries = 5;
constexpr ULONGLONG kRefreshIntervalMs = 100;
struct ParameterGuids
{
uintptr_t text, size, colour;
uintptr_t textInput, intInput, floatInput, boolInput, vectorInput, enumInput;
};
const ParameterGuids kDebugText = {
0x01352244, 0x0135224c, 0x01352250,
0x01352240, 0x01352230, 0x0135222c, 0x01352234, 0x01352238, 0x0135223c,
};
const ParameterGuids kDebugTextStacking = {
0x0135220c, 0x01352214, 0x01352218,
0, 0x013521fc, 0x013521f8, 0x01352200, 0x01352204, 0x01352208,
};
const void* Guid(uintptr_t offset)
{
return reinterpret_cast<const void*>(DEVTOOLS_RELATIVE_ADDRESS(offset));
}
std::string ResolveString(uint32_t offset)
{
if (offset == kInvalidString)
return "";
void* table = *DEBUG_TEXT::string_table_instance;
if (!table)
return "";
const char* text = DEBUG_TEXT::string_from_offset(table, offset);
return text ? text : "";
}
std::string ReadText(void* entityInterface, const void* entity, const void* guid)
{
uint32_t offset = kInvalidString;
if (!DEBUG_TEXT::find_parameter_string(entityInterface, entity, guid, &offset))
return "";
return ResolveString(offset);
}
int ReadSize(void* entityInterface, const void* entity, const void* guid)
{
int size = kDefaultSize;
DEBUG_TEXT::find_parameter_int(entityInterface, entity, guid, &size);
return size > 0 ? size : kDefaultSize;
}
ImU32 ReadColour(void* entityInterface, const void* entity, const void* guid)
{
alignas(16) float value[4] = { 255.0f, 255.0f, 255.0f, 0.0f };
if (!DEBUG_TEXT::find_parameter_vector(entityInterface, entity, guid, value))
return IM_COL32_WHITE;
auto channel = [](float v) { return static_cast<int>(v < 0.0f ? 0.0f : v > 255.0f ? 255.0f : v); };
return IM_COL32(channel(value[0]), channel(value[1]), channel(value[2]), 255);
}
bool IsLinked(void* entityInterface, const void* entity, const void* guid)
{
alignas(16) unsigned char lock[16] = {};
DEBUG_TEXT::cache_lock_ctor(lock, entity);
const void* entry = DEBUG_TEXT::find_entry(entityInterface, entity, guid);
const bool linked = entry != nullptr && *reinterpret_cast<void* const*>(static_cast<const char*>(entry) + 8) != nullptr;
DEBUG_TEXT::cache_lock_dtor(lock);
return linked;
}
std::string FormatFloat(float value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%g", value);
return buffer;
}
std::string ReadInputs(void* entityInterface, const void* entity, const ParameterGuids& guids)
{
std::string out;
auto append = [&out](const std::string& value) { out += " ["; out += value; out += "]"; };
if (guids.textInput)
{
uint32_t offset = kInvalidString;
if (DEBUG_TEXT::find_parameter_string(entityInterface, entity, Guid(guids.textInput), &offset))
{
const std::string value = ResolveString(offset);
if (!value.empty() || IsLinked(entityInterface, entity, Guid(guids.textInput)))
append(value);
}
}
{
int value = 0;
if (DEBUG_TEXT::find_parameter_int(entityInterface, entity, Guid(guids.intInput), &value))
if (value != 0 || IsLinked(entityInterface, entity, Guid(guids.intInput)))
append(std::to_string(value));
}
{
float value = 0.0f;
if (DEBUG_TEXT::find_parameter_float(entityInterface, entity, Guid(guids.floatInput), &value))
if (value != 0.0f || IsLinked(entityInterface, entity, Guid(guids.floatInput)))
append(FormatFloat(value));
}
{
bool value = false;
if (DEBUG_TEXT::find_parameter_bool(entityInterface, entity, Guid(guids.boolInput), &value))
if (value || IsLinked(entityInterface, entity, Guid(guids.boolInput)))
append(value ? "true" : "false");
}
{
alignas(16) float value[4] = {};
if (DEBUG_TEXT::find_parameter_vector(entityInterface, entity, Guid(guids.vectorInput), value))
if (value[0] != 0.0f || value[1] != 0.0f || value[2] != 0.0f || IsLinked(entityInterface, entity, Guid(guids.vectorInput)))
append(FormatFloat(value[0]) + ", " + FormatFloat(value[1]) + ", " + FormatFloat(value[2]));
}
{
DEBUG_TEXT::Enum value = { 0, -1 };
if (DEBUG_TEXT::find_parameter_enum(entityInterface, entity, Guid(guids.enumInput), &value))
if (value.index != -1 || IsLinked(entityInterface, entity, Guid(guids.enumInput)))
append(std::to_string(value.index));
}
return out;
}
void RemoveEntry(void* owner)
{
std::lock_guard<std::mutex> lock(g_mutex);
for (auto it = g_entries.begin(); it != g_entries.end();)
it = (it->owner == owner) ? g_entries.erase(it) : it + 1;
}
void RemoveAnchored(int alignment /* -1 for all */)
{
std::lock_guard<std::mutex> lock(g_mutex);
for (auto it = g_entries.begin(); it != g_entries.end();)
it = (!it->stacking && (alignment < 0 || it->alignment == alignment)) ? g_entries.erase(it) : it + 1;
}
void RemoveStacking(bool lastOnly)
{
std::lock_guard<std::mutex> lock(g_mutex);
for (auto it = g_entries.end(); it != g_entries.begin();)
{
--it;
if (!it->stacking)
continue;
it = g_entries.erase(it);
if (lastOnly)
return;
}
}
}
bool DEBUG_TEXT::EnableEntities(bool text, bool stacking)
{
bool ok = true;
if (text)
ok = DevTools::EnableEntity(0x00e57768) && ok;
if (stacking)
ok = DevTools::EnableEntity(0x00e57280) && ok;
return ok;
}
void DEBUG_TEXT::ClearAll()
{
std::lock_guard<std::mutex> lock(g_mutex);
g_entries.clear();
}
__declspec(noinline)
void __fastcall DEBUG_TEXT::h_level_close(void* _this, void* /*_EDX*/)
{
ClearAll();
DEBUG_MARKER::ClearAll();
level_close(_this);
}
__declspec(noinline)
void __fastcall DEBUG_TEXT::h_destructor(void* _this, void* /*_EDX*/)
{
RemoveEntry(_this);
destructor(_this);
}
__declspec(noinline)
void __fastcall DEBUG_TEXT::h_stacking_destructor(void* _this, void* /*_EDX*/)
{
RemoveEntry(_this);
stacking_destructor(_this);
}
__declspec(noinline)
bool __fastcall DEBUG_TEXT::h_on_start(void* _this, void* /*_EDX*/, const void* entity, const void* trigger)
{
const std::string text = ReadText(_this, entity, Guid(kDebugText.text)) + ReadInputs(_this, entity, kDebugText);
const int alignment = get_alignment(_this, entity);
const int size = ReadSize(_this, entity, Guid(kDebugText.size));
const ImU32 colour = ReadColour(_this, entity, Guid(kDebugText.colour));
const float duration = get_duration(_this, entity);
RemoveEntry(_this);
{
std::lock_guard<std::mutex> lock(g_mutex);
const ULONGLONG now = GetTickCount64();
const ULONGLONG expiresAt = duration > 0.0f ? now + static_cast<ULONGLONG>(duration * 1000.0f) : 0; // <= 0: stays until stopped.
g_entries.push_back({ _this, text, alignment, static_cast<float>(size), colour, false, expiresAt, now });
}
const bool result = on_start(_this, entity, trigger);
start_updating(static_cast<char*>(_this) + kEntityStateOffset, entity, trigger, true);
return result;
}
__declspec(noinline)
bool __fastcall DEBUG_TEXT::h_on_update(void* _this, void* /*_EDX*/, const void* entity, const void* trigger)
{
bool refresh = false;
{
std::lock_guard<std::mutex> lock(g_mutex);
for (const TextEntry& entry : g_entries)
if (entry.owner == _this && GetTickCount64() - entry.refreshedAt >= kRefreshIntervalMs)
refresh = true;
}
if (refresh)
{
const std::string text = ReadText(_this, entity, Guid(kDebugText.text)) + ReadInputs(_this, entity, kDebugText);
const int alignment = get_alignment(_this, entity);
const int size = ReadSize(_this, entity, Guid(kDebugText.size));
const ImU32 colour = ReadColour(_this, entity, Guid(kDebugText.colour));
std::lock_guard<std::mutex> lock(g_mutex);
for (TextEntry& entry : g_entries)
{
if (entry.owner != _this)
continue;
entry.text = text;
entry.alignment = alignment;
entry.size = static_cast<float>(size);
entry.colour = colour;
entry.refreshedAt = GetTickCount64();
}
}
return on_update(_this, entity, trigger);
}
__declspec(noinline)
bool __fastcall DEBUG_TEXT::h_on_stop(void* _this, void* /*_EDX*/, const void* entity, const void* trigger)
{
RemoveEntry(_this);
return on_stop(_this, entity, trigger);
}
__declspec(noinline)
bool __fastcall DEBUG_TEXT::h_on_clear_all(void* _this, void* /*_EDX*/, const void* entity, const void* trigger)
{
RemoveAnchored(-1);
return on_clear_all(_this, entity, trigger);
}
__declspec(noinline)
bool __fastcall DEBUG_TEXT::h_on_clear_of_alignment(void* _this, void* /*_EDX*/, const void* entity, const void* trigger)
{
RemoveAnchored(get_alignment(_this, entity));
return on_clear_of_alignment(_this, entity, trigger);
}
__declspec(noinline)
bool __fastcall DEBUG_TEXT::h_stacking_on_start(void* _this, void* /*_EDX*/, const void* entity, const void* trigger)
{
const std::string text = ReadText(_this, entity, Guid(kDebugTextStacking.text)) + ReadInputs(_this, entity, kDebugTextStacking);
const int size = ReadSize(_this, entity, Guid(kDebugTextStacking.size));
const ImU32 colour = ReadColour(_this, entity, Guid(kDebugTextStacking.colour));
{
std::lock_guard<std::mutex> lock(g_mutex);
const ULONGLONG now = GetTickCount64();
g_entries.push_back({ _this, text, 3 /* LEFT */, static_cast<float>(size), colour, true, 0, now });
// Keep the stack to its last few entries, dropping the oldest.
size_t stacked = 0;
for (const TextEntry& entry : g_entries)
stacked += entry.stacking ? 1 : 0;
for (auto it = g_entries.begin(); it != g_entries.end() && stacked > kMaxStackEntries;)
{
if (!it->stacking) { ++it; continue; }
it = g_entries.erase(it);
--stacked;
}
}
return stacking_on_start(_this, entity, trigger);
}
__declspec(noinline)
bool __fastcall DEBUG_TEXT::h_stacking_on_clear_all(void* _this, void* /*_EDX*/, const void* entity, const void* trigger)
{
RemoveStacking(false);
return stacking_on_clear_all(_this, entity, trigger);
}
__declspec(noinline)
bool __fastcall DEBUG_TEXT::h_stacking_on_clear_last(void* _this, void* /*_EDX*/, const void* entity, const void* trigger)
{
RemoveStacking(true);
return stacking_on_clear_last(_this, entity, trigger);
}
void DEBUG_TEXT::DrawOverlay()
{
std::lock_guard<std::mutex> lock(g_mutex);
const ULONGLONG now = GetTickCount64();
for (auto it = g_entries.begin(); it != g_entries.end();)
it = (it->expiresAt != 0 && now >= it->expiresAt) ? g_entries.erase(it) : it + 1;
if (g_entries.empty())
return;
ImDrawList* drawList = ImGui::GetForegroundDrawList();
ImFont* font = ImGui::GetFont();
const ImVec2 screen = ImGui::GetIO().DisplaySize;
constexpr float margin = 40.0f;
constexpr float lineGap = 4.0f;
auto drawText = [&](const ImVec2& pos, const TextEntry& entry)
{
drawList->AddText(font, entry.size, ImVec2(pos.x + 1.0f, pos.y + 1.0f), IM_COL32(0, 0, 0, 200), entry.text.c_str());
drawList->AddText(font, entry.size, pos, entry.colour, entry.text.c_str());
};
float cellOffset[9] = {};
for (const TextEntry& entry : g_entries)
{
if (entry.stacking)
continue;
const int cell = (entry.alignment >= 0 && entry.alignment < 9) ? entry.alignment : 0;
const int column = cell % 3;
const int row = cell / 3;
const ImVec2 textSize = font->CalcTextSizeA(entry.size, FLT_MAX, 0.0f, entry.text.c_str());
const float x = column == 0 ? margin : column == 1 ? (screen.x - textSize.x) * 0.5f : screen.x - margin - textSize.x;
const float y = row == 0 ? margin : row == 1 ? (screen.y - textSize.y) * 0.5f : screen.y - margin - textSize.y;
drawText(ImVec2(x, y + cellOffset[cell]), entry);
cellOffset[cell] += textSize.y + lineGap;
}
float stackHeight = 0.0f;
for (const TextEntry& entry : g_entries)
if (entry.stacking)
stackHeight += font->CalcTextSizeA(entry.size, FLT_MAX, 0.0f, entry.text.c_str()).y + lineGap;
float stackY = (screen.y - stackHeight) * 0.5f + cellOffset[3];
for (const TextEntry& entry : g_entries)
{
if (!entry.stacking)
continue;
drawText(ImVec2(margin, stackY), entry);
stackY += font->CalcTextSizeA(entry.size, FLT_MAX, 0.0f, entry.text.c_str()).y + lineGap;
}
}