Files
OpenRCT2-Web/src/core/Guard.cpp
T

114 lines
3.1 KiB
C++
Raw Normal View History

#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
2016-04-10 17:01:16 +01:00
#include <cassert>
2016-07-16 14:12:16 +01:00
#include <stdarg.h>
2016-04-10 17:01:16 +01:00
#include <stdio.h>
2016-07-16 14:49:41 +01:00
#ifdef __WINDOWS__
#define WIN32_LEAN_AND_MEAN
2016-07-16 14:12:16 +01:00
#include <windows.h>
2016-09-08 21:37:12 +01:00
#undef GetMessage
2016-07-16 14:49:41 +01:00
#endif
2016-04-10 17:01:16 +01:00
#include "Console.hpp"
#include "Diagnostics.hpp"
#include "Guard.hpp"
2016-07-16 14:12:16 +01:00
extern "C"
{
#include "../openrct2.h"
2016-07-17 23:54:12 +02:00
void openrct2_assert(bool expression, const char * message, ...)
{
va_list args;
va_start(args, message);
Guard::Assert_VA(expression, message, args);
va_end(args);
}
2016-07-16 14:12:16 +01:00
}
2016-04-10 17:01:16 +01:00
namespace Guard
{
2016-07-16 14:12:16 +01:00
void Assert(bool expression, const char * message, ...)
{
va_list args;
va_start(args, message);
Assert_VA(expression, message, args);
va_end(args);
}
void Assert_VA(bool expression, const char * message, va_list args)
2016-04-10 17:01:16 +01:00
{
if (expression) return;
if (message != nullptr)
{
2016-07-16 14:12:16 +01:00
Console::Error::WriteLine("Assertion failed:");
Console::Error::WriteLine_VA(message, args);
2016-04-10 17:01:16 +01:00
}
2016-07-16 14:34:10 +01:00
#ifdef DEBUG
2016-07-11 19:09:16 +01:00
Debug::Break();
#endif
2016-07-16 14:34:10 +01:00
#ifdef __WINDOWS__
2016-07-16 14:12:16 +01:00
char version[128];
openrct2_write_full_version_info(version, sizeof(version));
char buffer[512];
strcpy(buffer, "An assertion failed, please report this to the OpenRCT2 developers.\r\n\r\nVersion: ");
strcat(buffer, version);
2016-09-06 18:41:05 +01:00
if (message != nullptr)
{
strcat(buffer, "\r\n");
2016-09-25 21:24:29 -05:00
char *bufend = (char *)strchr(buffer, 0);
vsnprintf(bufend, sizeof(buffer) - (bufend - buffer), message, args);
2016-09-06 18:41:05 +01:00
}
2016-12-02 21:49:44 +00:00
#ifdef __TEST__
// Abort if we are building for testing
abort();
#else
// Show message box if we are not building for testing
2016-12-02 17:16:26 +00:00
int result = MessageBoxA(nullptr, buffer, OPENRCT2_NAME, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION);
2016-07-16 14:12:16 +01:00
if (result == IDABORT)
{
2016-07-16 14:34:10 +01:00
#ifdef USE_BREAKPAD
2016-07-16 14:12:16 +01:00
// Force a crash that breakpad will handle allowing us to get a dump
*((void**)0) = 0;
2016-07-16 14:34:10 +01:00
#else
assert(false);
#endif
2016-07-16 14:12:16 +01:00
}
2016-12-02 17:16:26 +00:00
#endif
2016-07-16 14:12:16 +01:00
#else
2016-07-11 19:09:16 +01:00
assert(false);
2016-07-16 14:12:16 +01:00
#endif
2016-07-11 19:09:16 +01:00
}
2016-07-16 14:12:16 +01:00
void Fail(const char * message, ...)
{
va_list args;
va_start(args, message);
2016-07-17 22:55:33 +01:00
Assert_VA(false, message, args);
2016-07-16 14:12:16 +01:00
va_end(args);
}
void Fail_VA(const char * message, va_list args)
2016-07-11 19:09:16 +01:00
{
2016-07-17 22:55:33 +01:00
Assert_VA(false, message, args);
2016-04-10 17:01:16 +01:00
}
}