add geode::terminate and geode::unreachable

This commit is contained in:
HJfod 2024-04-13 21:38:26 +03:00
parent b1f33db497
commit d107f3386f
2 changed files with 68 additions and 0 deletions
loader
include/Geode/utils
src/utils

View file

@ -0,0 +1,54 @@
#pragma once
#include "../DefaultInclude.hpp"
#include "../loader/Log.hpp"
#include <source_location>
namespace geode {
class Mod;
}
namespace geode::utils {
namespace detail {
// This needs to do stuff with `Mod*` which is not included in the file
GEODE_DLL std::string fmtTerminateError(const char* reason, Mod* mod, std::source_location loc);
}
template <class = void>
[[noreturn]]
void terminate(
std::string const& reason,
Mod* mod = getMod(),
std::source_location loc = std::source_location::current()
) {
auto fullError = detail::fmtTerminateError(reason.c_str(), mod, loc);
// Add the error to the logfile
log::error("{}", fullError);
#ifdef GEODE_IS_WINDOWS
// If a debugger is attached, start debugging
if (IsDebuggerPresent()) {
OutputDebugStringA(reason.c_str());
DebugBreak();
}
// Otherwise just terminate
else {
MessageBoxA(nullptr, "A Mod Crashed", fullError.c_str(), MB_ICONERROR);
std::terminate();
}
#else
std::terminate();
#endif
}
template <class = void>
[[noreturn]]
void unreachable(
std::string const& reason = "Unspecified",
Mod* mod = getMod(),
std::source_location loc = std::source_location::current()
) {
return terminate(reason + " (Unreachable code path)", mod, loc);
}
}

View file

@ -0,0 +1,14 @@
#include <Geode/utils/terminate.hpp>
#include <Geode/loader/Mod.hpp>
using namespace geode::prelude;
std::string geode::utils::detail::fmtTerminateError(const char* reason, Mod* mod, std::source_location loc) {
return fmt::format(
"The mod '{}' by {} has deliberately asked the game to crash.\n"
"Reason: {}\n"
"Source: {}::{}():{}:{}",
mod->getID(), mod->getDeveloper(), reason,
loc.file_name(), loc.function_name(), loc.line(), loc.column()
);
}