mirror of
https://github.com/geode-sdk/geode.git
synced 2025-04-10 12:04:36 -04:00
add geode::terminate and geode::unreachable
This commit is contained in:
parent
b1f33db497
commit
d107f3386f
2 changed files with 68 additions and 0 deletions
loader
54
loader/include/Geode/utils/terminate.hpp
Normal file
54
loader/include/Geode/utils/terminate.hpp
Normal 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);
|
||||
}
|
||||
}
|
14
loader/src/utils/terminate.cpp
Normal file
14
loader/src/utils/terminate.cpp
Normal 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()
|
||||
);
|
||||
}
|
Loading…
Add table
Reference in a new issue