Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Engine/Source/Runtime/Core/Private/Core/Assertion/Assertion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RavenStorm Copyright @ 2025-2025

#include "Core/Assertion/Assertion.hpp"

#include <type_traits>
#include <Windows.h>

#include "Core/Assertion/ExceptionHandler.hpp"
#include "Core/Logging/LogManager.hpp"

DEFINE_LOG_CHANNEL(Assert, All)

void Assertion::Assert(FString&& Condition, FString&& Message, const long CustomCode, const FSourceLocation& Location)
{
#ifdef CORVUS_MODE_DEBUG
if (IsDebuggerPresent())
{
__debugbreak();
}
#endif
FExceptionMetadata Metadata;
Metadata.ReportCode = ERROR_CODE_ASSERTION;
Metadata.AssertionCondition = std::move(Condition);
Metadata.Message = std::move(Message);
Metadata.SourceLocation = Location;
Metadata.CustomCode = CustomCode;
FExceptionHandler::Report(Metadata);
}

void Assertion::Assert(const FExceptionMetadata& Metadata)
{
#ifdef CORVUS_MODE_DEBUG
if (IsDebuggerPresent())
{
__debugbreak();
}
#endif
FExceptionHandler::Report(Metadata);
}
102 changes: 102 additions & 0 deletions Engine/Source/Runtime/Core/Private/Core/Assertion/ExceptionHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// RavenStorm Copyright @ 2025-2025

#include "Core/Assertion/ExceptionHandler.hpp"

#include <cstdlib>
#include <Windows.h>

#include "Core/Logging/LogManager.hpp"

int32 FExceptionHandler::ExitCode = EXIT_SUCCESS;

FString FormatSourceLocation(const FSourceLocation& SourceLocation)
{
FString Result;
FString FileName = SourceLocation.file_name();
const size64 LastSlashIndex = FileName.find_last_of('\\');
if (LastSlashIndex != FString::npos)
{
FileName = FileName.substr(LastSlashIndex + 1);
}
Result += "File: " + FileName + "\n";
Result += "Line: " + std::to_string(SourceLocation.line()) + "\n";
Result += "Function: " + FString(SourceLocation.function_name()) + "\n";
return Result;
}

long FExceptionHandler::HandleException(const EXCEPTION_POINTERS* ExceptionInfo)
{
ExitCode = EXIT_FAILURE;

const HRESULT ExceptionCode = static_cast<HRESULT>(ExceptionInfo->ExceptionRecord->ExceptionCode);

FString MessageBoxCaption;
FString MessageBoxText;
uint32 MessageBoxType;
if (ExceptionCode == ERROR_CODE_ASSERTION)
{
const FExceptionMetadata* Metadata = reinterpret_cast<FExceptionMetadata*>(ExceptionInfo->ExceptionRecord->ExceptionInformation[0]);

MessageBoxCaption = "Corvus | Assertion";
MessageBoxText = "Assertion Failure!\n\n";
if (Metadata->Message.empty() && Metadata->CustomCode != 0)
{
MessageBoxText += GetResultDescription(Metadata->CustomCode) + "\n\n";
}
else
{
MessageBoxText += Metadata->Message + "\n\n";
}
MessageBoxText += "Condition: " + Metadata->AssertionCondition + "\n";
MessageBoxText += FormatSourceLocation(Metadata->SourceLocation);
if (!Metadata->Message.empty() && Metadata->CustomCode != 0)
{
MessageBoxText += "Result: " + GetResultDescription(Metadata->CustomCode) + "\n";
}
MessageBoxType = MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST;
}
else
{
MessageBoxCaption = "Corvus | Exception";
MessageBoxText = "Unhandled Exception!\n\n";
MessageBoxText += "Result: " + GetResultDescription(ExceptionCode) + "\n";
MessageBoxType = MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST;
}
MessageBoxEx(nullptr, MessageBoxText.c_str(), MessageBoxCaption.c_str(), MessageBoxType, MAKELANGID(LANG_NEUTRAL, SUBLANG_ENGLISH_US));
return EXCEPTION_EXECUTE_HANDLER;
}

void FExceptionHandler::Report(const FExceptionMetadata& Metadata)
{
ULONG_PTR Parameters[] = {reinterpret_cast<ULONG_PTR>(&Metadata)};
RaiseException(Metadata.ReportCode, EXCEPTION_NONCONTINUABLE, _countof(Parameters), Parameters);
}

FString FExceptionHandler::GetResultDescription(const HRESULT Result)
{
if (HRESULT_FACILITY(Result) == FACILITY_ITF)
{
return "Unknown";
}
LPSTR Buffer = nullptr;
const size64 Size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, Result,
MAKELANGID(LANG_NEUTRAL, SUBLANG_ENGLISH_US), reinterpret_cast<LPSTR>(&Buffer), 0, nullptr);
FString Message(Buffer, Size);
LocalFree(Buffer);
if (!Message.empty())
{
Message = Message.substr(0, Message.size() - 2);
return Message;
}
return {};
}

int32 FExceptionHandler::GetExitCode()
{
return ExitCode;
}

long HandleException(const EXCEPTION_POINTERS* ExceptionInfo)
{
return FExceptionHandler::HandleException(ExceptionInfo);
}
35 changes: 35 additions & 0 deletions Engine/Source/Runtime/Core/Public/Core/Assertion/Assertion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RavenStorm Copyright @ 2025-2025

#pragma once

#include "Core/Containers/String.hpp"
#include "Core/Logging/LogChannel.hpp"
#include "Core/Utility/SourceLocation.hpp"
#include "Core/Utility/StringUtils.hpp"

struct FExceptionMetadata;
CORE_API DECLARE_LOG_CHANNEL_EXTERN(Assert)

namespace Assertion
{
CORE_API void Assert(FString&& Condition, FString&& Message = "", long CustomCode = 0, const FSourceLocation& Location = FSourceLocation::current());
CORE_API void Assert(const FExceptionMetadata& Metadata);
}

#define ASSERT_IF(Condition, ...) \
do \
{ \
if(!(Condition)) \
{ \
Assertion::Assert(FExceptionMetadata{ .AssertionCondition = FString(TEXT(#Condition)) __VA_OPT__(, .Message = StringUtils::Format(__VA_ARGS__)), .SourceLocation = FSourceLocation::current() }); \
} \
} while (false)

#define ASSERT_RESULT(Result, ...) \
do \
{ \
if(FAILED(Result)) \
{ \
Assertion::Assert(FExceptionMetadata{ .ReportCode = ERROR_CODE_ASSERTION, .AssertionCondition = FString(TEXT(#Result)) __VA_OPT__(, .Message = StringUtils::Format(__VA_ARGS__)), .CustomCode = Result, .SourceLocation = FSourceLocation::current() }); \
} \
} while (false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// RavenStorm Copyright @ 2025-2025

#pragma once

#include <Windows.h>

#include "Core/Containers/String.hpp"
#include "Core/Utility/SourceLocation.hpp"

#define ERROR_CODE_UNKNOWN MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0001)
#define ERROR_CODE_ASSERTION MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x0002)

struct CORE_API FExceptionMetadata
{
HRESULT ReportCode = ERROR_CODE_UNKNOWN;
FString AssertionCondition; // Only valid if report code is ERROR_CODE_ASSERTION
FString Message;
HRESULT CustomCode = 0;
FSourceLocation SourceLocation;
};

class CORE_API FExceptionHandler
{
public:
[[nodiscard]] static long HandleException(const EXCEPTION_POINTERS* ExceptionInfo);
static void Report(const FExceptionMetadata& Metadata);
[[nodiscard]] static FString GetResultDescription(HRESULT Result);

[[nodiscard]] static int32 GetExitCode();

private:
static int32 ExitCode;
};

[[nodiscard]] CORE_API long HandleException(const EXCEPTION_POINTERS* ExceptionInfo);

#define TRY __try
#define CATCH() __except(HandleException(GetExceptionInformation()))
117 changes: 117 additions & 0 deletions Engine/Source/Runtime/Core/Public/Core/Utility/EnumFlags.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// RavenStorm Copyright @ 2025-2025

#pragma once

#include <type_traits>

#define REGISTER_AS_ENUM_FLAG(EnumName) \
template<> \
[[nodiscard]] constexpr bool8 IsEnumFlag(EnumName) { return true; }

template <typename T>
[[nodiscard]] constexpr bool8 IsEnumFlag(T)
{
return false;
}

template <typename T>
concept CIsUnsignedEnum = std::is_enum_v<T> && std::is_unsigned_v<std::underlying_type_t<T>>;

template <typename T>
concept CIsEnumFlag = CIsUnsignedEnum<T> && IsEnumFlag(T{});

template <typename T>
[[nodiscard]] bool8 HasEnumFlag(T Value, T Flag)
{
return (Value & Flag) == Flag;
}

template <typename T>
[[nodiscard]] bool8 HasEnumFlag(T Value, std::underlying_type_t<T> Flag)
{
return (static_cast<std::underlying_type_t<T>>(Value) & Flag) == Flag;
}

template <typename T>
[[nodiscard]] bool8 HasEnumFlag(std::underlying_type_t<T> Value, T Flag)
{
return (Value & static_cast<std::underlying_type_t<T>>(Flag)) == static_cast<std::underlying_type_t<T>>(Flag);
}

template <typename T> requires CIsEnumFlag<T>
constexpr T operator~(T Value)
{
return static_cast<T>(~static_cast<std::underlying_type_t<T>>(Value));
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator|(T Left, T Right)
{
return static_cast<T>(static_cast<std::underlying_type_t<T>>(Left) | static_cast<std::underlying_type_t<T>>(Right));
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator|(std::underlying_type_t<T> Left, T Right)
{
return static_cast<std::underlying_type_t<T>>(Left | static_cast<std::underlying_type_t<T>>(Right));
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator|(T Left, std::underlying_type_t<T> Right)
{
return static_cast<T>(static_cast<std::underlying_type_t<T>>(Left) | Right);
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator|(std::underlying_type_t<T>& Left, const T Right)
{
return Left = Left | Right;
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator&(T Left, T Right)
{
return static_cast<T>(static_cast<std::underlying_type_t<T>>(Left) & static_cast<std::underlying_type_t<T>>(Right));
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator&(std::underlying_type_t<T> Left, T Right)
{
return static_cast<std::underlying_type_t<T>>(Left & static_cast<std::underlying_type_t<T>>(Right));
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator&(T Left, std::underlying_type_t<T> Right)
{
return static_cast<T>(static_cast<std::underlying_type_t<T>>(Left) & Right);
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator&=(std::underlying_type_t<T>& Left, const T Right)
{
return Left = Left & Right;
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator^(T Left, T Right)
{
return static_cast<T>(static_cast<std::underlying_type_t<T>>(Left) ^ static_cast<std::underlying_type_t<T>>(Right));
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator^(std::underlying_type_t<T> Left, T Right)
{
return static_cast<std::underlying_type_t<T>>(Left ^ static_cast<std::underlying_type_t<T>>(Right));
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator^(T Left, std::underlying_type_t<T> Right)
{
return static_cast<T>(static_cast<std::underlying_type_t<T>>(Left) ^ Right);
}

template <typename T> requires CIsEnumFlag<T>
constexpr auto operator^=(std::underlying_type_t<T>& Left, const T Right)
{
return Left = Left ^ Right;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RavenStorm Copyright @ 2025-2025

#pragma once

#include <source_location>

using FSourceLocation = std::source_location;
Loading