-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathRTEError.cpp
More file actions
603 lines (513 loc) · 21.8 KB
/
RTEError.cpp
File metadata and controls
603 lines (513 loc) · 21.8 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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include "RTEError.h"
#include "WindowMan.h"
#include "FrameMan.h"
#include "ConsoleMan.h"
#include "ActivityMan.h"
#include "System.h"
#include "SDL_messagebox.h"
#ifdef _WIN32
#include "Windows.h"
#endif
#include <array>
#include <exception>
#include <regex>
#include <utility>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#elif defined(__linux__)
#include <cpuid.h>
#endif
#ifdef __linux__
#include <sys/utsname.h>
#include <fstream>
#include <filesystem>
#elif defined(__APPLE__) && defined(__MACH__)
#include <sys/sysctl.h>
#endif
#include "backward/backward.hpp"
using namespace RTE;
bool RTEError::s_CurrentlyAborting = false;
bool RTEError::s_IgnoreAllAsserts = false;
std::string RTEError::s_LastIgnoredAssertDescription = "";
std::source_location RTEError::s_LastIgnoredAssertLocation = {};
#if (defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)))
backward::SignalHandling sh;
#endif
#ifdef _WIN32
/// <summary>
/// Custom exception handler for Windows SEH.
/// Unfortunately this also intercepts any C++ exceptions and turns them into SE bullshit, meaning we can't get and rethrow the current C++ exception to get what() from it.
/// Even if we "translate" SE exceptions to C++ exceptions it's still ass and doesn't really work, so this is what it is and it is good enough.
/// </summary>
/// <param name="exceptPtr">Struct containing information about the exception. This will be provided by the OS exception handler.</param>
static LONG WINAPI RTEWindowsExceptionHandler([[maybe_unused]] EXCEPTION_POINTERS* exceptPtr) {
// This sorta half-assedly works in x86 because exception handling is slightly different, but since the main target is x64 we can just not care about it.
// Something something ESP. ESP is a guitar brand.
#ifndef TARGET_MACHINE_X86
// Returns the last Win32 error in string format. Returns an empty string if there is no error.
static auto getLastWinErrorAsString = []() -> std::string {
DWORD errorMessageID = GetLastError();
if (errorMessageID == 0) {
return "";
}
LPSTR messageBuffer = nullptr;
DWORD messageFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
// This bullshit makes the error string and returns the size because we can't know it in advance. Don't think we actually care about the size when we construct string from a buffer but whatever.
size_t messageSize = FormatMessage(messageFlags, nullptr, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPSTR>(&messageBuffer), 0, nullptr);
std::string message(messageBuffer, messageSize);
LocalFree(messageBuffer);
return message;
};
// Returns a string with the type of the exception from the passed in code.
static auto getExceptionDescriptionFromCode = [](const DWORD& exceptCode) -> std::string {
switch (exceptCode) {
case EXCEPTION_ACCESS_VIOLATION:
return "EXCEPTION_ACCESS_VIOLATION";
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
return "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
case EXCEPTION_BREAKPOINT:
return "EXCEPTION_BREAKPOINT";
case EXCEPTION_DATATYPE_MISALIGNMENT:
return "EXCEPTION_DATATYPE_MISALIGNMENT";
case EXCEPTION_FLT_DENORMAL_OPERAND:
return "EXCEPTION_FLT_DENORMAL_OPERAND";
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
return "EXCEPTION_FLT_DIVIDE_BY_ZERO";
case EXCEPTION_FLT_INEXACT_RESULT:
return "EXCEPTION_FLT_INEXACT_RESULT";
case EXCEPTION_FLT_INVALID_OPERATION:
return "EXCEPTION_FLT_INVALID_OPERATION";
case EXCEPTION_FLT_OVERFLOW:
return "EXCEPTION_FLT_OVERFLOW";
case EXCEPTION_FLT_STACK_CHECK:
return "EXCEPTION_FLT_STACK_CHECK";
case EXCEPTION_FLT_UNDERFLOW:
return "EXCEPTION_FLT_UNDERFLOW";
case EXCEPTION_ILLEGAL_INSTRUCTION:
return "EXCEPTION_ILLEGAL_INSTRUCTION";
case EXCEPTION_IN_PAGE_ERROR:
return "EXCEPTION_IN_PAGE_ERROR";
case EXCEPTION_INT_DIVIDE_BY_ZERO:
return "EXCEPTION_INT_DIVIDE_BY_ZERO";
case EXCEPTION_INT_OVERFLOW:
return "EXCEPTION_INT_OVERFLOW";
case EXCEPTION_INVALID_DISPOSITION:
return "EXCEPTION_INVALID_DISPOSITION";
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
return "EXCEPTION_NONCONTINUABLE_EXCEPTION";
case EXCEPTION_PRIV_INSTRUCTION:
return "EXCEPTION_PRIV_INSTRUCTION";
case EXCEPTION_SINGLE_STEP:
return "EXCEPTION_SINGLE_STEP";
case EXCEPTION_STACK_OVERFLOW:
return "EXCEPTION_STACK_OVERFLOW";
default:
return "UNKNOWN EXCEPTION";
}
};
// Attempts to get a symbol name from the exception address.
static auto getSymbolNameFromAddress = [](HANDLE& procHandle, const size_t& exceptAddr) {
if (SymInitialize(procHandle, nullptr, TRUE)) {
SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
char symbolBuffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(char)];
PSYMBOL_INFO symbolInfo = reinterpret_cast<PSYMBOL_INFO>(symbolBuffer);
symbolInfo->SizeOfStruct = sizeof(SYMBOL_INFO);
symbolInfo->MaxNameLen = MAX_SYM_NAME;
if (SymFromAddr(procHandle, exceptAddr, nullptr, symbolInfo)) {
std::string symbolName = symbolInfo->Name;
return "The symbol name at this address is" + (symbolName.empty() ? " empty for reasons unknown to man." : ": \"" + symbolName + "\"");
} else {
return "Unable to get symbol name at address because:\n\n" + getLastWinErrorAsString();
}
}
std::string error = getLastWinErrorAsString();
return "Unable to get symbol name at address.\nSymbol Handler failed to initialize " + (error.empty() ? "for reasons unknown to man." : "because\n\n" + error);
};
HANDLE processHandle = GetCurrentProcess();
std::stringstream exceptionDescription;
DWORD exceptionCode = exceptPtr->ExceptionRecord->ExceptionCode;
size_t exceptionAddress = reinterpret_cast<size_t>(exceptPtr->ExceptionRecord->ExceptionAddress);
if (exceptionCode == EXCEPTION_BREAKPOINT) {
// Advance to the next instruction otherwise this handler will be called for all eternity.
exceptPtr->ContextRecord->Rip++;
return EXCEPTION_CONTINUE_EXECUTION;
}
std::string symbolNameAtAddress = getSymbolNameFromAddress(processHandle, exceptionAddress);
RTEError::FormatFunctionSignature(symbolNameAtAddress);
exceptionDescription << getExceptionDescriptionFromCode(exceptionCode) << " at address 0x" << std::uppercase << std::hex << exceptionAddress << ".\n\n"
<< symbolNameAtAddress << std::endl;
backward::StackTrace st;
st.load_here(32, exceptPtr->ContextRecord);
backward::Printer printer;
std::ostringstream stack;
printer.print(st, stack);
RTEError::UnhandledExceptionFunc(exceptionDescription.str(), stack.str());
return EXCEPTION_EXECUTE_HANDLER;
#endif
}
#endif
void RTEError::SetExceptionHandlers() {
// Basic handling for C++ exceptions. Doesn't give us much meaningful information.
[[maybe_unused]] static const std::terminate_handler terminateHandler = []() {
std::exception_ptr currentException = std::current_exception();
if (currentException) {
try {
std::rethrow_exception(currentException);
} catch (const std::bad_exception& exception) {
RTEError::UnhandledExceptionFunc("Unable to get exception description because: " + std::string(exception.what()) + ".\n");
} catch (const std::exception& exception) {
RTEError::UnhandledExceptionFunc(std::string(exception.what()) + ".\n");
}
} else {
RTEError::UnhandledExceptionFunc("Terminate was called without an exception.\nMay god have mercy on us all.");
}
};
#ifdef _WIN32
#ifndef TARGET_MACHINE_X86
SetUnhandledExceptionFilter(RTEWindowsExceptionHandler);
#else
// This only works for C++ exceptions and doesn't catch and access violations and such, or provide much meaningful info.
std::set_terminate(terminateHandler);
#endif
#else
// TODO: Deal with segfaults and such on other systems. Probably need to use Unix signal junk to get any meaningful information. Good luck and godspeed to whoever deals with this.
std::set_terminate(terminateHandler);
#endif
}
void RTEError::ShowMessageBox(const std::string& message) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "RTE Warning! (>_<)", message.c_str(), nullptr);
}
bool RTEError::ShowAbortMessageBox(const std::string& message) {
enum AbortMessageButton {
ButtonInvalid,
ButtonExit,
ButtonRestart
};
std::vector<SDL_MessageBoxButtonData> abortMessageBoxButtons = {
SDL_MessageBoxButtonData(SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, AbortMessageButton::ButtonExit, "OK")};
// Don't even show the restart button in debug builds.
#ifdef RELEASE_BUILD
// Getting a junk path from argv[0] is, or should be, impossible but check anyway.
if (std::filesystem::exists(System::GetThisExePathAndName())) {
abortMessageBoxButtons.emplace_back(0, AbortMessageButton::ButtonRestart, "Restart Game");
}
#endif
SDL_MessageBoxData abortMessageBox = {
SDL_MESSAGEBOX_ERROR,
g_WindowMan.GetWindow(),
"RTE Aborted! (x_x)",
message.c_str(),
static_cast<int>(abortMessageBoxButtons.size()),
abortMessageBoxButtons.data(),
nullptr};
int pressedButton = AbortMessageButton::ButtonInvalid;
SDL_ShowMessageBox(&abortMessageBox, &pressedButton);
return pressedButton == AbortMessageButton::ButtonRestart;
}
bool RTEError::ShowAssertMessageBox(const std::string& message) {
enum AssertMessageButton {
ButtonInvalid,
ButtonAbort,
ButtonIgnore,
ButtonIgnoreAll
};
std::vector<SDL_MessageBoxButtonData> assertMessageBoxButtons = {
SDL_MessageBoxButtonData(SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, AssertMessageButton::ButtonAbort, "Abort"),
SDL_MessageBoxButtonData(0, AssertMessageButton::ButtonIgnore, "Ignore"),
SDL_MessageBoxButtonData(0, AssertMessageButton::ButtonIgnoreAll, "Ignore All")};
SDL_MessageBoxData assertMessageBox = {
SDL_MESSAGEBOX_ERROR,
g_WindowMan.GetWindow(),
"RTE Assert! (x_x)",
message.c_str(),
static_cast<int>(assertMessageBoxButtons.size()),
assertMessageBoxButtons.data(),
nullptr};
int pressedButton = AssertMessageButton::ButtonInvalid;
SDL_ShowMessageBox(&assertMessageBox, &pressedButton);
if (pressedButton == AssertMessageButton::ButtonIgnoreAll) {
s_IgnoreAllAsserts = true;
}
return pressedButton == AssertMessageButton::ButtonAbort;
}
void RTEError::UnhandledExceptionFunc(const std::string& description, const std::string& callstack) {
s_CurrentlyAborting = true;
std::string exceptionMessage = "Runtime Error due to unhandled exception!\n\n" + description;
if (!s_LastIgnoredAssertDescription.empty()) {
exceptionMessage += "\nThe last ignored Assertion was: " + s_LastIgnoredAssertDescription;
}
if (s_LastIgnoredAssertLocation.line() > 0) {
// This typically contains the absolute path to the file on whatever machine this was compiled on, so in that case get only the file name.
std::filesystem::path filePath = s_LastIgnoredAssertLocation.file_name();
std::string fileName = (filePath.has_root_name() || filePath.has_root_directory()) ? filePath.filename().generic_string() : s_LastIgnoredAssertLocation.file_name();
std::string srcLocation = "file '" + fileName + "', line " + std::to_string(s_LastIgnoredAssertLocation.line()) + ",\nin function '" + s_LastIgnoredAssertLocation.function_name() + "'";
if (!s_LastIgnoredAssertDescription.empty()) {
exceptionMessage += "\nIn " + srcLocation + ".\n";
} else {
exceptionMessage += "\nThe last ignored Assertion was in " + srcLocation + ".\n";
}
}
if (DumpAbortSave()) {
exceptionMessage += "\nThe game has saved to 'AbortSave'.";
}
if (DumpAbortScreen()) {
exceptionMessage += "\nThe last frame has been dumped to 'AbortScreen.png'.";
}
g_ConsoleMan.PrintString(exceptionMessage);
std::string consoleSaveMsg;
if (!callstack.empty()) {
g_ConsoleMan.PrintString(callstack);
consoleSaveMsg = "\nThe console and callstack have been dumped to 'AbortLog.txt'.";
} else {
consoleSaveMsg = "\nThe console has been dumped to 'AbortLog.txt'.";
}
if (g_ConsoleMan.SaveAllText("AbortLog.txt")) {
exceptionMessage += consoleSaveMsg;
}
System::PrintToCLI(exceptionMessage);
// Ditch the video mode so the message box appears without problems.
if (g_WindowMan.GetWindow()) {
SDL_SetWindowFullscreen(g_WindowMan.GetWindow(), 0);
}
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "RTE CATASTROPHIC ERROR!!! (X_X)", exceptionMessage.c_str(), nullptr);
AbortAction;
}
void RTEError::AbortFunc(const std::string& description, const std::source_location& srcLocation) {
s_CurrentlyAborting = true;
if (!System::IsInExternalModuleValidationMode()) {
// This typically contains the absolute path to the file on whatever machine this was compiled on, so in that case get only the file name.
std::filesystem::path filePath = srcLocation.file_name();
std::string fileName = (filePath.has_root_name() || filePath.has_root_directory()) ? filePath.filename().generic_string() : srcLocation.file_name();
std::string lineNum = std::to_string(srcLocation.line());
std::string funcName = srcLocation.function_name();
FormatFunctionSignature(funcName);
std::string abortMessage = "Runtime Error in file '" + fileName + "', line " + lineNum + ",\nin function '" + funcName + "'\nbecause:\n\n" + description + "\n";
if (DumpAbortSave()) {
abortMessage += "\nThe game has saved to 'AbortSave'.";
}
if (DumpAbortScreen()) {
abortMessage += "\nThe last frame has been dumped to 'AbortScreen.png'.";
}
g_ConsoleMan.PrintString(abortMessage);
DumpHardwareInfo();
std::string callstack = "";
backward::StackTrace st;
st.load_here();
backward::Printer printer;
std::ostringstream stack;
printer.print(st, stack);
callstack = stack.str();
std::string consoleSaveMsg;
if (!callstack.empty()) {
g_ConsoleMan.PrintString(callstack);
consoleSaveMsg = "\nThe console and callstack have been dumped to 'AbortLog.txt'.";
} else {
consoleSaveMsg = "\nThe console has been dumped to 'AbortLog.txt'.";
}
if (g_ConsoleMan.SaveAllText("AbortLog.txt")) {
abortMessage += consoleSaveMsg;
}
System::PrintToCLI(abortMessage);
// Ditch the video mode so the message box appears without problems.
if (g_WindowMan.GetWindow()) {
SDL_SetWindowFullscreen(g_WindowMan.GetWindow(), 0);
}
if (ShowAbortMessageBox(abortMessage)) {
// Enable restarting in release builds only.
// Once this exits the debugger is detached and while there does seem to be a way to programatically re-attach it to the new instance (at least in Windows), it is so incredibly ass and I cannot even begin to can.
// This will prevent your day from being ruined when your breakpoints don't trigger during a meltdown because you launched a new instance and didn't realize you're not attached to it.
#ifdef RELEASE_BUILD
#ifdef _WIN32
std::system(std::string(R"(start "" ")" + System::GetThisExePathAndName() + "\"").c_str());
#else
std::system(std::string("\"" + System::GetThisExePathAndName() + "\"").c_str());
#endif
#endif
}
}
s_CurrentlyAborting = false;
AbortAction;
}
void RTEError::AssertFunc(const std::string& description, const std::source_location& srcLocation) {
if (System::IsInExternalModuleValidationMode()) {
AbortFunc(description, srcLocation);
}
// This typically contains the absolute path to the file on whatever machine this was compiled on, so in that case get only the file name.
std::filesystem::path filePath = srcLocation.file_name();
std::string fileName = (filePath.has_root_name() || filePath.has_root_directory()) ? filePath.filename().generic_string() : srcLocation.file_name();
std::string lineNum = std::to_string(srcLocation.line());
std::string funcName = srcLocation.function_name();
g_ConsoleMan.PrintString("ERROR: Assertion in file '" + fileName + "', line " + lineNum + ", in function '" + funcName + "' because: " + description);
bool storeAssertInfo = false;
if (!s_IgnoreAllAsserts) {
std::string assertMessage =
"Assertion in file '" + fileName + "', line " + lineNum + ",\nin function '" + funcName + "'\nbecause:\n\n" + description + "\n\n" +
"You may choose to ignore this and crash immediately\nor at some unexpected point later on.\n\nProceed at your own risk!";
if (ShowAssertMessageBox(assertMessage)) {
AbortFunc(description, srcLocation);
} else {
storeAssertInfo = true;
}
} else {
storeAssertInfo = true;
}
if (storeAssertInfo) {
s_LastIgnoredAssertDescription = description;
s_LastIgnoredAssertLocation = srcLocation;
}
}
void RTEError::DumpHardwareInfo() {
std::string glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
std::string glVendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
std::string glRenderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
std::string hwInfo = "GL Version: " + glVersion + "\n" +
"GL Vendor: " + glVendor + "\n" +
"GL Renderer: " + glRenderer + "\n";
#if defined(_MSC_VER) || defined(__linux__)
int vendorRegs[4] = {0};
#ifdef _MSC_VER
__cpuid(vendorRegs, 0);
#else
__cpuid(0, vendorRegs[0], vendorRegs[1], vendorRegs[2], vendorRegs[3]);
#endif
std::string cpuVendor(reinterpret_cast<const char*>(&vendorRegs[1]), 4);
cpuVendor += std::string(reinterpret_cast<const char*>(&vendorRegs[3]), 4);
cpuVendor += std::string(reinterpret_cast<const char*>(&vendorRegs[2]), 4);
hwInfo += "CPU Manufacturer ID: " + cpuVendor + "\n";
std::string cpuModel;
int modelRegs[12];
#ifdef _MSC_VER
__cpuid(modelRegs, 0x80000000);
#else
__cpuid(0x80000000, modelRegs[0], modelRegs[1], modelRegs[2], modelRegs[3]);
#endif
if (modelRegs[0] >= 0x80000004) {
for (size_t i = 0; i <= 2; ++i) {
#ifdef _MSC_VER
__cpuid(&modelRegs[0] + i * 4, i + 0x80000002);
#else
__cpuid(i + 0x80000002, modelRegs[0 + i * 4], modelRegs[1 + i * 4], modelRegs[2 + i * 4], modelRegs[3 + i * 4]);
#endif
}
for (size_t i = 0; i < 12; ++i) {
cpuModel += std::string(reinterpret_cast<const char*>(&modelRegs[i]), 4);
}
hwInfo += "CPU Model: " + cpuModel + "\n";
}
#elif defined(__APPLE__) && defined(__MACH__)
char vendor[1024];
size_t vendorSize = sizeof(vendor);
int error = sysctlbyname("machdep.cpu.vendor", &vendor, &vendorSize, nullptr, 0);
if (!error) {
hwInfo += "CPU Vendor: " + std::string(vendor) + "\n";
}
char brand[1024];
size_t brandSize = sizeof(brand);
error = sysctlbyname("machdep.cpu.brand_string", &brand, &brandSize, nullptr, 0);
if (!error) {
hwInfo += "CPU Model: " + std::string(brand) + "\n";
}
#endif
g_ConsoleMan.PrintString(hwInfo);
#ifdef __unix__
struct utsname unameData;
if (uname(&unameData) == 0) {
std::string osInfo = "uname: " + std::string(unameData.sysname) + " " + std::string(unameData.release) + " " + std::string(unameData.version);
g_ConsoleMan.PrintString(osInfo);
}
#endif
#ifdef _MSC_VER
g_ConsoleMan.PrintString("OS: Windows");
#endif
#ifdef __linux__
// Read distribution info from /etc/os-release
if (std::filesystem::exists("/etc/os-release")) {
std::ifstream osReleaseFile("/etc/os-release");
if (osReleaseFile.is_open()) {
std::string line;
while (std::getline(osReleaseFile, line)) {
if (line.find("PRETTY_NAME") != std::string::npos) {
g_ConsoleMan.PrintString("OS: " + line.substr(line.find_first_of('"') + 1, line.find_last_of('"') - line.find_first_of('"') - 1));
break;
}
}
osReleaseFile.close();
}
} else {
g_ConsoleMan.PrintString("OS: Unknown Linux (/etc/os-release not found)");
}
#endif
#if defined(__APPLE__) && defined(__MACH__)
char osType[1024];
size_t osTypeSize = sizeof(osType);
error = sysctlbyname("kern.ostype", &osType, &osTypeSize, nullptr, 0);
if (!error) {
g_ConsoleMan.PrintString("OS Type: " + std::string(osType));
}
char osRelease[1024];
size_t osReleaseSize = sizeof(osRelease);
error = sysctlbyname("kern.osrelease", &osRelease, &osReleaseSize, nullptr, 0);
if (!error) {
g_ConsoleMan.PrintString("OS Release: " + std::string(osRelease));
}
char osVersion[1024];
size_t osVersionSize = sizeof(osVersion);
error = sysctlbyname("kern.osversion", &osVersion, &osVersionSize, nullptr, 0);
if (!error) {
g_ConsoleMan.PrintString("OS Version: " + std::string(osVersion));
}
#endif
}
bool RTEError::DumpAbortScreen() {
int success = -1;
if (glReadPixels != nullptr) {
int w, h;
SDL_GL_GetDrawableSize(g_WindowMan.GetWindow(), &w, &h);
if (!(w > 0 && h > 0)) {
return false;
}
BITMAP* readBuffer = create_bitmap_ex(24, w, h);
// Read screen from the front buffer since that is the only framebuffer guaranteed to exist at this point.
// Read twice because front buffer content is technically undefined, but most drivers still eventually give up the contents correctly.
glReadBuffer(GL_FRONT);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, readBuffer->line[0]);
glFinish();
glReadBuffer(GL_BACK);
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, readBuffer->line[0]);
glFinish();
BITMAP* flipBuffer = create_bitmap_ex(24, w, h);
draw_sprite_v_flip(flipBuffer, readBuffer, 0, 0);
success = save_png("AbortScreen.png", flipBuffer, nullptr);
}
return success == 0;
}
bool RTEError::DumpAbortSave() {
bool success = false;
if (g_ActivityMan.GetActivity() && g_ActivityMan.GetActivity()->CanBeUserSaved()) {
success = g_ActivityMan.SaveCurrentGame("AbortSave");
}
return success;
}
void RTEError::FormatFunctionSignature(std::string& symbolName) {
// TODO: Expand this with more dumb signatures, or make something that makes more sense.
static const std::array<std::pair<std::regex, std::string>, 3> stlSigs{
{{std::regex("( >)"), ">"},
{std::regex("(std::basic_string<char,std::char_traits<char>,std::allocator<char>>)"), "std::string"},
{std::regex("(class ?std::basic_string<char,struct ?std::char_traits<char>,class ?std::allocator<char>>)"), "std::string"}}};
for (const auto& [fullSig, simpleSig]: stlSigs) {
symbolName = std::regex_replace(symbolName, fullSig, simpleSig);
}
for (size_t pos = 0;;) {
pos += 100;
if (pos < symbolName.size()) {
if (size_t lastCommaPos = symbolName.find_last_of(',', pos); lastCommaPos != std::string::npos) {
symbolName.insert(lastCommaPos + 1, "\n");
}
} else {
break;
}
}
}