Crusader_Decomp/rebuild/remorse/src/runtime.cpp

146 lines
4.7 KiB
C++
Raw Normal View History

#include "remorse/runtime.h"
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <crtdbg.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <exception>
#endif
namespace remorse {
namespace {
#ifdef _WIN32
void writeCrashMessage(const char* message)
{
OutputDebugStringA(message);
const HANDLE errorOutput = GetStdHandle(STD_ERROR_HANDLE);
if (errorOutput != nullptr && errorOutput != INVALID_HANDLE_VALUE) {
DWORD written = 0;
WriteFile(errorOutput, message, static_cast<DWORD>(std::strlen(message)), &written, nullptr);
}
}
LONG WINAPI reportUnhandledException(EXCEPTION_POINTERS* exception)
{
char message[160];
std::snprintf(message, sizeof(message), "Unhandled exception 0x%08lX at %p\n",
exception->ExceptionRecord->ExceptionCode, exception->ExceptionRecord->ExceptionAddress);
writeCrashMessage(message);
return EXCEPTION_EXECUTE_HANDLER;
}
void reportTermination() noexcept
{
try {
if (const auto error = std::current_exception()) {
std::rethrow_exception(error);
}
} catch (const std::exception& error) {
char message[512];
std::snprintf(message, sizeof(message), "Unhandled C++ exception: %s\n", error.what());
writeCrashMessage(message);
std::_Exit(EXIT_FAILURE);
} catch (...) {
writeCrashMessage("Unhandled C++ exception of unknown type\n");
std::_Exit(EXIT_FAILURE);
}
writeCrashMessage("Program terminated without an active exception\n");
std::_Exit(EXIT_FAILURE);
}
#endif
std::string readTextFile(const std::filesystem::path& path)
{
std::ifstream input(path, std::ios::binary);
if (!input) {
return {};
}
std::ostringstream buffer;
buffer << input.rdbuf();
return buffer.str();
}
std::optional<std::string> readJsonString(const std::string& json, const char* key)
{
const std::regex pattern(std::string("\\\"") + key + "\\\"\\s*:\\s*\\\"([^\\\"]*)\\\"");
std::smatch match;
if (!std::regex_search(json, match, pattern)) {
return std::nullopt;
}
return match[1].str();
}
u32 readJsonU32(const std::string& json, const char* key)
{
const std::regex pattern(std::string("\\\"") + key + "\\\"\\s*:\\s*([0-9]+)");
std::smatch match;
if (!std::regex_search(json, match, pattern)) {
return 0;
}
return static_cast<u32>(std::stoul(match[1].str()));
}
} // namespace
void installCrashDiagnostics()
{
#ifdef _WIN32
SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
_set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
_set_error_mode(_OUT_TO_STDERR);
#ifdef _DEBUG
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif
SetUnhandledExceptionFilter(reportUnhandledException);
std::set_terminate(reportTermination);
#endif
}
std::optional<ExportManifestSummary> loadExportManifestSummary(const std::filesystem::path& manifestPath)
{
const std::string json = readTextFile(manifestPath);
if (json.empty()) {
return std::nullopt;
}
ExportManifestSummary summary;
summary.program = readJsonString(json, "program").value_or("unknown");
summary.outputRoot = readJsonString(json, "outputRoot").value_or("");
summary.functionFiles = readJsonU32(json, "functionFiles");
summary.fallbackFunctionFiles = readJsonU32(json, "fallbackFunctionFiles");
summary.globalBuckets = readJsonU32(json, "globalBuckets");
summary.globalDefinitions = readJsonU32(json, "globalDefinitions");
return summary;
}
std::filesystem::path defaultManifestPath(const std::filesystem::path& currentPath)
{
return currentPath / "exports" / "src_remorse" / "export_manifest.json";
}
void printStartupReport(const ExportManifestSummary& manifest)
{
std::cout << "Crusader: No Remorse rebuild harness\n";
std::cout << "Program: " << manifest.program << '\n';
std::cout << "Export root: " << manifest.outputRoot.string() << '\n';
std::cout << "Function files: " << manifest.functionFiles << '\n';
std::cout << "Fallback function files: " << manifest.fallbackFunctionFiles << '\n';
std::cout << "Global buckets: " << manifest.globalBuckets << '\n';
std::cout << "Global definitions: " << manifest.globalDefinitions << '\n';
std::cout << "Status: StandProcess Run, registered timer, and keyboard diagnostic callback active; original key queue, NPC services, and game loop not linked.\n";
}
} // namespace remorse