From d79e0fbfab492dc22fa0fa8fecaa2bdba77d6b4f Mon Sep 17 00:00:00 2001 From: louist103 Date: Tue, 25 Oct 2022 11:59:52 -0400 Subject: Cleanup crash handler. --- ZAPD/CrashHandler.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ ZAPD/CrashHandler.h | 28 +--------------------------- ZAPD/Main.cpp | 28 ++-------------------------- 3 files changed, 43 insertions(+), 53 deletions(-) diff --git a/ZAPD/CrashHandler.cpp b/ZAPD/CrashHandler.cpp index 3a45260..b1b4a47 100644 --- a/ZAPD/CrashHandler.cpp +++ b/ZAPD/CrashHandler.cpp @@ -1,6 +1,32 @@ #include "CrashHandler.h" #include "Utils/StringHelper.h" +#if __has_include() +#define HAS_POSIX 1 +#else +#define HAS_POSIX 0 +#endif + +#include +#include +#include +#include + +#if HAS_POSIX == 1 +#include +#include // for __cxa_demangle +#include // for dladdr +#include +#include +#elif defined(_MSC_VER) +#include +#include + +#include + +#pragma comment(lib, "Dbghelp.lib") +#endif + // Feel free to add more crash messages. static std::array crashEasterEgg = { @@ -164,5 +190,19 @@ LONG seh_filter(_EXCEPTION_POINTERS* ex) printStack(ex->ContextRecord); return EXCEPTION_EXECUTE_HANDLER; } +#endif +void CrashHandler_Init() +{ +#if HAS_POSIX == 1 + signal(SIGSEGV, ErrorHandler); + signal(SIGABRT, ErrorHandler); +#elif defined(_MSC_VER) + SetUnhandledExceptionFilter(seh_filter); +#else + HANDLE_WARNING(WarningType::Always, + "tried to set error handler, but this ZAPD build lacks support for one", ""); #endif +} + + diff --git a/ZAPD/CrashHandler.h b/ZAPD/CrashHandler.h index 86e4930..102778b 100644 --- a/ZAPD/CrashHandler.h +++ b/ZAPD/CrashHandler.h @@ -1,32 +1,6 @@ #ifndef CRASH_HANDLER_H #define CRASH_HANDLER_H -#if __has_include() -#define HAS_POSIX 1 -#else -#define HAS_POSIX 0 -#endif - - -#include -#include -#include -#include -#if HAS_POSIX == 1 -#include -#include // for __cxa_demangle -#include // for dladdr -#include -#include -void ErrorHandler(int sig); -#elif defined(_MSC_VER) -#include -#include - -#include -LONG seh_filter(_EXCEPTION_POINTERS* ex); - -#pragma comment(lib, "Dbghelp.lib") -#endif +void CrashHandler_Init(); #endif diff --git a/ZAPD/Main.cpp b/ZAPD/Main.cpp index a7b9660..2563154 100644 --- a/ZAPD/Main.cpp +++ b/ZAPD/Main.cpp @@ -24,7 +24,7 @@ void BuildAssetBlob(const fs::path& blobFilePath, const fs::path& outPath); extern const char gBuildHash[]; -int Main(int argc, char* argv[]) +int main(int argc, char* argv[]) { // Syntax: ZAPD.out [mode (btex/bovl/e)] (Arbritrary Number of Arguments) @@ -116,14 +116,7 @@ int Main(int argc, char* argv[]) } else if (arg == "-eh") // Enable Error Handler { -#if HAS_POSIX == 1 - signal(SIGSEGV, ErrorHandler); - signal(SIGABRT, ErrorHandler); -#elif !defined(_MSC_VER) - HANDLE_WARNING(WarningType::Always, - "tried to set error handler, but this ZAPD build lacks support for one", - ""); -#endif + CrashHandler_Init(); } else if (arg == "-v") // Verbose { @@ -246,23 +239,6 @@ int Main(int argc, char* argv[]) return 0; } -// Windows doesn't make it easy to get a stack trace from just a signal. So we need to do this messy -// stuff for just windows. -int main(int argc, char* argv[]) -{ -#ifdef _MSC_VER - __try - { - return Main(argc, argv); - } - __except (seh_filter(GetExceptionInformation())) - { - } -#else - return Main(argc, argv); -#endif -} - bool Parse(const fs::path& xmlFilePath, const fs::path& basePath, const fs::path& outPath, ZFileMode fileMode) { -- cgit v1.2.3