#include "remorse/file_system.h" #include "remorse/runtime.h" #include #include #include #include #include #include #include #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include #ifdef _DEBUG #include #endif #endif int main(int argc, char** argv) { #ifdef _WIN32 if (argc == 2 && std::strcmp(argv[1], "--crash-probe") == 0) { remorse::installCrashDiagnostics(); try { throw std::runtime_error("diagnostic probe"); } catch (...) { std::terminate(); } } #ifdef _DEBUG if (argc == 2 && std::strcmp(argv[1], "--assert-probe") == 0) { assert(false && "diagnostic assert probe"); return 0; } _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_WNDW); _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW); #endif remorse::installCrashDiagnostics(); assert((GetErrorMode() & SEM_NOGPFAULTERRORBOX) != 0); #ifdef _DEBUG assert((_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE) & _CRTDBG_MODE_WNDW) == 0); assert((_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_REPORT_MODE) & _CRTDBG_MODE_DEBUG) != 0); assert((_CrtSetReportMode(_CRT_ERROR, _CRTDBG_REPORT_MODE) & _CRTDBG_MODE_WNDW) == 0); #endif wchar_t executable[MAX_PATH]{}; if (GetModuleFileNameW(nullptr, executable, MAX_PATH) == 0) { return 1; } auto runProbe = [&](const wchar_t* option, const char* expectedOutput, bool expectExitOne) { std::wstring commandLine = L"\"" + std::wstring(executable) + L"\" " + option; SECURITY_ATTRIBUTES pipeSecurity{sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE}; HANDLE readPipe = nullptr; HANDLE writePipe = nullptr; if (!CreatePipe(&readPipe, &writePipe, &pipeSecurity, 0)) { return false; } if (!SetHandleInformation(readPipe, HANDLE_FLAG_INHERIT, 0)) { CloseHandle(readPipe); CloseHandle(writePipe); return false; } STARTUPINFOW startup{}; startup.cb = sizeof(startup); startup.dwFlags = STARTF_USESTDHANDLES; startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE); startup.hStdOutput = writePipe; startup.hStdError = writePipe; PROCESS_INFORMATION child{}; if (!CreateProcessW(nullptr, commandLine.data(), nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &startup, &child)) { CloseHandle(readPipe); CloseHandle(writePipe); return false; } CloseHandle(writePipe); const DWORD waitResult = WaitForSingleObject(child.hProcess, 5000); if (waitResult == WAIT_TIMEOUT) { TerminateProcess(child.hProcess, 2); WaitForSingleObject(child.hProcess, INFINITE); } char buffer[512]; DWORD count = 0; std::string crashOutput; while (ReadFile(readPipe, buffer, sizeof(buffer), &count, nullptr) && count != 0) { crashOutput.append(buffer, count); } DWORD exitCode = 0; const BOOL gotExitCode = GetExitCodeProcess(child.hProcess, &exitCode); CloseHandle(readPipe); CloseHandle(child.hThread); CloseHandle(child.hProcess); const bool passed = gotExitCode && waitResult == WAIT_OBJECT_0 && (expectExitOne ? exitCode == 1 : exitCode != 0) && crashOutput.find(expectedOutput) != std::string::npos; if (!passed) { std::fprintf(stderr, "Probe %ls wait=%lu exit=0x%08lX output=%s\n", option, waitResult, exitCode, crashOutput.c_str()); } return passed; }; if (!runProbe(L"--crash-probe", "Unhandled C++ exception: diagnostic probe", true) #ifdef _DEBUG || !runProbe(L"--assert-probe", "diagnostic assert probe", false) #endif ) { return 1; } #endif const auto root = remorse::findGameDataRoot(std::filesystem::current_path()); assert(root.has_value()); assert(root->hasExecutable); assert(root->hasStaticData); assert(root->hasUsecode); const auto palettePath = remorse::resolveGameDataPath(*root, std::filesystem::path("static") / "gamepal.pal"); assert(palettePath.has_value()); const auto paletteBytes = remorse::readBinaryFile(*palettePath); assert(paletteBytes.has_value()); assert(!paletteBytes->empty()); const auto blockedParent = remorse::resolveGameDataPath(*root, std::filesystem::path("..") / "CRUSADER.EXE"); assert(!blockedParent.has_value()); return 0; }