2016-05-04 19:24:41 +02:00
|
|
|
/*****************************************************************************
|
2022-10-01 09:42:14 +02:00
|
|
|
* Copyright (c) 2014-2022 OpenRCT2 developers
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-05-04 19:24:41 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2017-06-12 19:00:15 +02:00
|
|
|
#ifdef _WIN32
|
2018-07-21 16:17:06 +02:00
|
|
|
# include <windows.h>
|
2016-07-16 15:49:41 +02:00
|
|
|
#endif
|
2016-04-10 18:01:16 +02:00
|
|
|
|
2017-02-03 14:11:58 +01:00
|
|
|
#include "../Version.h"
|
2018-06-18 18:46:49 +02:00
|
|
|
#include "../common.h"
|
2016-04-10 18:01:16 +02:00
|
|
|
#include "Console.hpp"
|
|
|
|
#include "Diagnostics.hpp"
|
|
|
|
#include "Guard.hpp"
|
2022-08-14 22:16:53 +02:00
|
|
|
#include "StringBuilder.h"
|
2016-04-10 18:01:16 +02:00
|
|
|
|
2018-06-18 18:46:49 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2022-08-14 22:16:53 +02:00
|
|
|
#include <string>
|
2018-06-18 18:46:49 +02:00
|
|
|
|
|
|
|
void openrct2_assert_fwd(bool expression, const char* message, ...)
|
2016-07-16 15:12:16 +02:00
|
|
|
{
|
2018-02-01 18:49:14 +01:00
|
|
|
va_list va;
|
|
|
|
va_start(va, message);
|
|
|
|
Guard::Assert_VA(expression, message, va);
|
|
|
|
va_end(va);
|
2016-07-16 15:12:16 +02:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:01:16 +02:00
|
|
|
namespace Guard
|
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
constexpr const utf8* ASSERTION_MESSAGE = "An assertion failed, please report this to the OpenRCT2 developers.";
|
2017-02-02 00:16:22 +01:00
|
|
|
|
2017-02-02 00:11:47 +01:00
|
|
|
// The default behaviour when an assertion is raised.
|
|
|
|
static ASSERT_BEHAVIOUR _assertBehaviour =
|
2017-06-12 19:00:15 +02:00
|
|
|
#ifdef _WIN32
|
2017-02-02 00:11:47 +01:00
|
|
|
ASSERT_BEHAVIOUR::MESSAGE_BOX
|
|
|
|
#else
|
|
|
|
ASSERT_BEHAVIOUR::CASSERT
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2020-01-13 19:22:45 +01:00
|
|
|
static std::optional<std::string> _lastAssertMessage = std::nullopt;
|
|
|
|
|
2017-06-12 19:00:15 +02:00
|
|
|
#ifdef _WIN32
|
2022-10-05 23:47:07 +02:00
|
|
|
[[nodiscard]] static std::wstring CreateDialogAssertMessage(std::string_view);
|
2017-02-02 00:11:47 +01:00
|
|
|
static void ForceCrash();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ASSERT_BEHAVIOUR GetAssertBehaviour()
|
|
|
|
{
|
|
|
|
return _assertBehaviour;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAssertBehaviour(ASSERT_BEHAVIOUR behaviour)
|
|
|
|
{
|
|
|
|
_assertBehaviour = behaviour;
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
void Assert(bool expression, const char* message, ...)
|
2016-07-16 15:12:16 +02:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, message);
|
|
|
|
Assert_VA(expression, message, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
void Assert_VA(bool expression, const char* message, va_list args)
|
2016-04-10 18:01:16 +02:00
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
if (expression)
|
|
|
|
return;
|
2016-04-10 18:01:16 +02:00
|
|
|
|
2017-02-02 00:16:22 +01:00
|
|
|
Console::Error::WriteLine(ASSERTION_MESSAGE);
|
2017-04-18 04:38:35 +02:00
|
|
|
Console::Error::WriteLine("Version: %s", gVersionInfoFull);
|
2017-02-02 00:11:47 +01:00
|
|
|
|
|
|
|
// This is never freed, but acceptable considering we are about to crash out
|
2022-06-23 05:46:34 +02:00
|
|
|
std::string formattedMessage;
|
2016-04-10 18:01:16 +02:00
|
|
|
if (message != nullptr)
|
|
|
|
{
|
2017-02-02 00:11:47 +01:00
|
|
|
formattedMessage = String::Format_VA(message, args);
|
2022-06-23 05:46:34 +02:00
|
|
|
Console::Error::WriteLine(formattedMessage.c_str());
|
2020-01-13 19:22:45 +01:00
|
|
|
_lastAssertMessage = std::make_optional(formattedMessage);
|
|
|
|
}
|
|
|
|
|
2016-07-16 15:34:10 +02:00
|
|
|
#ifdef DEBUG
|
2016-07-11 20:09:16 +02:00
|
|
|
Debug::Break();
|
|
|
|
#endif
|
2016-07-16 15:12:16 +02:00
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
switch (_assertBehaviour)
|
2016-09-06 19:41:05 +02:00
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
case ASSERT_BEHAVIOUR::ABORT:
|
|
|
|
abort();
|
|
|
|
default:
|
|
|
|
case ASSERT_BEHAVIOUR::CASSERT:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
#ifdef _WIN32
|
|
|
|
case ASSERT_BEHAVIOUR::MESSAGE_BOX:
|
2017-02-02 00:11:47 +01:00
|
|
|
{
|
2018-06-22 22:58:39 +02:00
|
|
|
// Show message box if we are not building for testing
|
2022-08-14 22:16:53 +02:00
|
|
|
auto buffer = CreateDialogAssertMessage(formattedMessage);
|
2022-10-05 23:47:07 +02:00
|
|
|
int32_t result = MessageBoxW(
|
|
|
|
nullptr, buffer.c_str(), L"" OPENRCT2_NAME, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION);
|
2018-06-22 22:58:39 +02:00
|
|
|
if (result == IDABORT)
|
|
|
|
{
|
|
|
|
ForceCrash();
|
|
|
|
}
|
|
|
|
break;
|
2017-02-02 00:11:47 +01:00
|
|
|
}
|
2016-07-16 15:34:10 +02:00
|
|
|
#endif
|
2016-07-16 15:12:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
void Fail(const char* message, ...)
|
2016-07-16 15:12:16 +02:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, message);
|
2016-07-17 23:55:33 +02:00
|
|
|
Assert_VA(false, message, args);
|
2016-07-16 15:12:16 +02:00
|
|
|
va_end(args);
|
2016-07-11 20:09:16 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
void Fail_VA(const char* message, va_list args)
|
2016-07-11 20:09:16 +02:00
|
|
|
{
|
2016-07-17 23:55:33 +02:00
|
|
|
Assert_VA(false, message, args);
|
2016-04-10 18:01:16 +02:00
|
|
|
}
|
2017-02-02 00:11:47 +01:00
|
|
|
|
2020-01-13 19:22:45 +01:00
|
|
|
std::optional<std::string> GetLastAssertMessage()
|
|
|
|
{
|
|
|
|
return _lastAssertMessage;
|
|
|
|
}
|
|
|
|
|
2017-06-12 19:00:15 +02:00
|
|
|
#ifdef _WIN32
|
2022-10-05 23:47:07 +02:00
|
|
|
[[nodiscard]] static std::wstring CreateDialogAssertMessage(std::string_view formattedMessage)
|
2017-02-02 00:11:47 +01:00
|
|
|
{
|
2022-08-14 22:16:53 +02:00
|
|
|
StringBuilder sb;
|
|
|
|
sb.Append(ASSERTION_MESSAGE);
|
2022-10-05 23:47:07 +02:00
|
|
|
sb.Append("\n\n");
|
2022-08-14 22:16:53 +02:00
|
|
|
sb.Append("Version: ");
|
|
|
|
sb.Append(gVersionInfoFull);
|
|
|
|
if (!formattedMessage.empty())
|
2017-02-02 00:11:47 +01:00
|
|
|
{
|
2022-10-05 23:47:07 +02:00
|
|
|
sb.Append("\n");
|
2022-08-14 22:16:53 +02:00
|
|
|
sb.Append(formattedMessage);
|
2017-02-02 00:11:47 +01:00
|
|
|
}
|
2022-10-05 23:47:07 +02:00
|
|
|
return String::ToWideChar({ sb.GetBuffer(), sb.GetLength() });
|
2017-02-02 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ForceCrash()
|
|
|
|
{
|
2018-07-21 16:17:06 +02:00
|
|
|
# ifdef USE_BREAKPAD
|
2017-02-02 00:11:47 +01:00
|
|
|
// Force a crash that breakpad will handle allowing us to get a dump
|
|
|
|
*((void**)0) = 0;
|
2018-07-21 16:17:06 +02:00
|
|
|
# else
|
2017-02-02 00:11:47 +01:00
|
|
|
assert(false);
|
2018-07-21 16:17:06 +02:00
|
|
|
# endif
|
2017-02-02 00:11:47 +01:00
|
|
|
}
|
|
|
|
#endif
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace Guard
|