From affc999d70cbbcd851766e2084e090015f471ae7 Mon Sep 17 00:00:00 2001 From: PatrickvL Date: Tue, 12 Feb 2019 11:58:25 +0100 Subject: [PATCH] Loader : Use CxbxMessageBox (a wrapper for Windows' MessageBox API) wherever possible, to avoid repeating the TEXT("Cxbx-Reloaded") caption everywhere, and a different argument-order allowing default argument values and thus more compact code. --- src/common/AddressRanges.h | 2 +- src/common/VerifyAddressRanges.cpp | 4 ++-- src/core/hle/XAPI/Xapi.cpp | 2 +- src/core/kernel/init/CxbxKrnl.cpp | 22 +++++++++++++---- src/core/kernel/init/CxbxKrnl.h | 6 ++++- src/core/kernel/support/Emu.cpp | 8 +++---- src/emulator/cxbxr-emu.cpp | 38 +++++++++++++++++++----------- src/gui/WinMain.cpp | 16 ++++++------- src/loader/cxbxr-ldr.cpp | 4 ---- 9 files changed, 62 insertions(+), 40 deletions(-) diff --git a/src/common/AddressRanges.h b/src/common/AddressRanges.h index 8529ec2a4..e1eb4b55d 100644 --- a/src/common/AddressRanges.h +++ b/src/common/AddressRanges.h @@ -26,7 +26,7 @@ // ****************************************************************** #pragma once -#ifndef CXBXR_EMU_EXPORTS // Only trim Windows symbols CxbxLoader, not in cxbxr-emu +#ifndef CXBXR_EMU_EXPORTS // Only trim Windows symbols in cxbxr-ldr.exe, not in cxbxr-emu.dll (TODO : What about cxbxr.exe and cxbx.exe?) #include #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #endif // CXBXR_EMU_EXPORTS diff --git a/src/common/VerifyAddressRanges.cpp b/src/common/VerifyAddressRanges.cpp index 9bcbe3dc8..ebbe5fc1a 100644 --- a/src/common/VerifyAddressRanges.cpp +++ b/src/common/VerifyAddressRanges.cpp @@ -31,8 +31,8 @@ bool VerifyBaseAddr() { /*! CXBX_BASE_ADDR is defined as 0x00010000, which is the base address of - the CxbxLoader.exe host executable. - Set in CxbxLoader.exe Project options, Linker, Advanced, Base Address */ + the cxbxr-ldr.exe host executable. + Set in cxbxr-ldr.exe Project options, Linker, Advanced, Base Address */ return ((UINT_PTR)GetModuleHandle(nullptr) == CXBX_BASE_ADDR); } diff --git a/src/core/hle/XAPI/Xapi.cpp b/src/core/hle/XAPI/Xapi.cpp index 33db5ef03..9a923360c 100644 --- a/src/core/hle/XAPI/Xapi.cpp +++ b/src/core/hle/XAPI/Xapi.cpp @@ -1188,7 +1188,7 @@ DWORD WINAPI XTL::EMUPATCH(XLaunchNewImageA) if (PathFileExists(szDashboardPath)) { - MessageBox(CxbxKrnl_hEmuParent, "The title is rebooting to dashboard", "Cxbx-Reloaded", 0); + (void)CxbxMessageBox("The title is rebooting to dashboard", MB_OK, CxbxKrnl_hEmuParent); lpTitlePath = "C:\\xboxdash.xbe"; xboxkrnl::LaunchDataPage->Header.dwLaunchDataType = LDT_FROM_DASHBOARD; // Other options include LDT_NONE, LDT_FROM_DEBUGGER_CMDLINE and LDT_FROM_UPDATE diff --git a/src/core/kernel/init/CxbxKrnl.cpp b/src/core/kernel/init/CxbxKrnl.cpp index 8da4a072f..18f4e4392 100644 --- a/src/core/kernel/init/CxbxKrnl.cpp +++ b/src/core/kernel/init/CxbxKrnl.cpp @@ -495,6 +495,18 @@ HANDLE CxbxRestorePageTablesMemory(char* szFilePath_page_tables) #pragma optimize("", off) +int CxbxMessageBox(const char* msg, UINT uType, HWND hWnd) +{ + return MessageBox(hWnd, msg, /*lpCaption=*/TEXT("Cxbx-Reloaded"), uType); +} + +void CxbxShowError(const char* msg, HWND hWnd) +{ + const UINT uType = MB_OK | MB_TOPMOST | MB_SETFOREGROUND | MB_ICONERROR; // Note : MB_ICONERROR == MB_ICONSTOP == MB_ICONHAND + + (void)CxbxMessageBox(msg, uType, hWnd); +} + void CxbxPopupMessageEx(CXBXR_MODULE cxbxr_module, LOG_LEVEL level, CxbxMsgDlgIcon icon, const char *message, ...) { char Buffer[1024]; @@ -527,7 +539,7 @@ void CxbxPopupMessageEx(CXBXR_MODULE cxbxr_module, LOG_LEVEL level, CxbxMsgDlgIc EmuLogEx(cxbxr_module, level, "Popup : %s", Buffer); - MessageBox(NULL, Buffer, TEXT("Cxbx-Reloaded"), uType); + (void)CxbxMessageBox(Buffer, uType); } void PrintCurrentConfigurationLog() @@ -903,7 +915,7 @@ bool CreateSettings() { g_Settings = new Settings(); if (g_Settings == nullptr) { - MessageBox(nullptr, szSettings_alloc_error, "Cxbx-Reloaded", MB_OK | MB_ICONERROR); + CxbxShowError(szSettings_alloc_error); return false; } @@ -928,9 +940,9 @@ bool HandleFirstLaunch() bool bElevated = CxbxIsElevated(); if (bElevated && !g_Settings->m_core.allowAdminPrivilege) { - int ret = MessageBox(NULL, "Cxbx-Reloaded has detected that it has been launched with Administrator rights.\n" + int ret = CxbxMessageBox("Cxbx-Reloaded has detected that it has been launched with Administrator rights.\n" "\nThis is dangerous, as a maliciously modified Xbox titles could take control of your system.\n" - "\nAre you sure you want to continue?", "Cxbx-Reloaded", MB_YESNO | MB_ICONWARNING); + "\nAre you sure you want to continue?", MB_YESNO | MB_ICONWARNING); if (ret != IDYES) { return false; } @@ -1054,7 +1066,7 @@ void CxbxKrnlMain(int argc, char* argv[]) } if (!isReady) { EmuLog(LOG_LEVEL::WARNING, "GUI process is not ready!"); - int mbRet = MessageBox(NULL, "GUI process is not ready, do you wish to retry?", TEXT("Cxbx-Reloaded"), + int mbRet = CxbxMessageBox("GUI process is not ready, do you wish to retry?", MB_ICONWARNING | MB_RETRYCANCEL | MB_TOPMOST | MB_SETFOREGROUND); if (mbRet == IDRETRY) { continue; diff --git a/src/core/kernel/init/CxbxKrnl.h b/src/core/kernel/init/CxbxKrnl.h index 42180ee91..1f5f3af53 100644 --- a/src/core/kernel/init/CxbxKrnl.h +++ b/src/core/kernel/init/CxbxKrnl.h @@ -205,6 +205,10 @@ typedef enum _CxbxMsgDlgIcon { CxbxMsgDlgIcon_Error, CxbxMsgDlgIcon_Unknown } CxbxMsgDlgIcon; + +int CxbxMessageBox(const char* msg, UINT uType = MB_OK, HWND hWnd = NULL); + +void CxbxShowError(const char* msg, HWND hWnd = NULL); void CxbxPopupMessageEx(CXBXR_MODULE cxbxr_module, LOG_LEVEL level, CxbxMsgDlgIcon icon, const char *message, ...); @@ -223,7 +227,7 @@ extern Xbe::Certificate *g_pCertificate; bool CxbxKrnlVerifyVersion(const char *szVersion); extern bool g_bIsDebugKernel; - + bool CheckLoadArgument(int argc, char* argv[], DWORD *pguiProcessID); bool CreateSettings(); diff --git a/src/core/kernel/support/Emu.cpp b/src/core/kernel/support/Emu.cpp index 33bf662d5..1a6b9a0df 100644 --- a/src/core/kernel/support/Emu.cpp +++ b/src/core/kernel/support/Emu.cpp @@ -170,7 +170,7 @@ bool EmuExceptionBreakpointAsk(LPEXCEPTION_POINTERS e) " Press Ignore to continue emulation.", EIPToString(e->ContextRecord->Eip).c_str()); - int ret = MessageBox(g_hEmuWindow, buffer, "Cxbx-Reloaded", MB_ICONSTOP | MB_ABORTRETRYIGNORE); + int ret = CxbxMessageBox(buffer, MB_ICONSTOP | MB_ABORTRETRYIGNORE, g_hEmuWindow); if (ret == IDABORT) { EmuExceptionExitProcess(); @@ -201,7 +201,7 @@ void EmuExceptionNonBreakpointUnhandledShow(LPEXCEPTION_POINTERS e) " Press \"Cancel\" to debug.", e->ExceptionRecord->ExceptionCode, EIPToString(e->ContextRecord->Eip).c_str()); - if (MessageBox(g_hEmuWindow, buffer, "Cxbx-Reloaded", MB_ICONSTOP | MB_OKCANCEL) == IDOK) + if (CxbxMessageBox(buffer, MB_ICONSTOP | MB_OKCANCEL, g_hEmuWindow) == IDOK) { EmuExceptionExitProcess(); } @@ -366,13 +366,13 @@ int ExitException(LPEXCEPTION_POINTERS e) fflush(stdout); - MessageBox(g_hEmuWindow, "Warning: Could not safely terminate process!", "Cxbx-Reloaded", MB_OK); + (void)CxbxMessageBox("Warning: Could not safely terminate process!", MB_OK, g_hEmuWindow); count++; if(count > 1) { - MessageBox(g_hEmuWindow, "Warning: Multiple Problems!", "Cxbx-Reloaded", MB_OK); + (void)CxbxMessageBox("Warning: Multiple Problems!", MB_OK, g_hEmuWindow); return EXCEPTION_CONTINUE_SEARCH; } diff --git a/src/emulator/cxbxr-emu.cpp b/src/emulator/cxbxr-emu.cpp index 444e9e383..b4f0ef442 100644 --- a/src/emulator/cxbxr-emu.cpp +++ b/src/emulator/cxbxr-emu.cpp @@ -124,43 +124,54 @@ DWORD WINAPI Emulate(int system) { FUNC_EXPORTS - /*! Verify our host executable, CxbxLoader.exe, is loaded to base address 0x00010000 */ + /*! Verify our host executable, cxbxr-ldr.exe, is loaded to base address 0x00010000 */ if (!VerifyBaseAddr()) { - MessageBox(NULL, "CxbxLoader.exe was not loaded to base address 0x00010000 (which is a requirement for Xbox emulation)", "Cxbx-Reloaded", - MB_OK | MB_ICONERROR); + CxbxShowError("cxbx-ldr.exe was not loaded to base address 0x00010000 (which is a requirement for Xbox emulation)"); return EXIT_FAILURE; } // Before doing anything else that might cause memory fragmentation, // verify that we still got control over all ranges the loader reserved if (!VerifyAddressRanges(system)) { - MessageBox(NULL, "Failed to claim required address ranges (which is a requirement for Xbox emulation)", "Cxbx-Reloaded", - MB_OK | MB_ICONERROR); + CxbxShowError("Failed to claim required address ranges (which is a requirement for Xbox emulation)"); return EXIT_FAILURE; } LPSTR CommandLine = GetCommandLine(); - int argc; + if (!CommandLine) { + CxbxShowError("Couldn't retrieve command line!"); + return EXIT_FAILURE; + } + + int argc = 0; PCHAR *argv = CommandLineToArgvA(CommandLine, &argc); + if (!argv) { + CxbxShowError("Couldn't parse command line!"); + return EXIT_FAILURE; + } DWORD guiProcessID = 0; bool bHasLoadArgument = CheckLoadArgument(argc, argv, &guiProcessID); + if (!bHasLoadArgument) { + CxbxShowError("No /load argument on command line!"); + return EXIT_FAILURE; + } /*! initialize shared memory */ if (!EmuShared::Init(guiProcessID)) { - MessageBox(NULL, "Could not map shared memory!", "Cxbx-Reloaded", MB_OK | MB_ICONERROR); + CxbxShowError("Could not map shared memory!"); LocalFree(argv); return EXIT_FAILURE; } - HandleFirstLaunch(); - - if (bHasLoadArgument) { - CxbxKrnlMain(argc, argv); - } else { - // TODO : Enter "2nd GUI" mode here, as done in Cxbx's WinMain() - but how? + if (!HandleFirstLaunch()) { + CxbxShowError("First launch failed!"); + EmuShared::Cleanup(); + return EXIT_FAILURE; } + CxbxKrnlMain(argc, argv); + LocalFree(argv); /*! cleanup shared memory */ @@ -175,4 +186,3 @@ DWORD WINAPI Emulate(int system) // This line will never be reached: return EXIT_FAILURE; } - diff --git a/src/gui/WinMain.cpp b/src/gui/WinMain.cpp index f0ed1e34f..a51bd45be 100644 --- a/src/gui/WinMain.cpp +++ b/src/gui/WinMain.cpp @@ -48,15 +48,13 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine // First detect if we are running on WoW64, if not, prevent Cxbx-Reloaded from starting // Cxbx-Relaoded needs access to high memory, only exposed to WoW64. if (!VerifyWow64()) { - MessageBox(NULL, "Cxbx-Reloaded can only run under WoW64\nThis means either a 64-bit version of Windows or Wine with a 64-bit prefix", "Cxbx-Reloaded", - MB_OK | MB_ICONERROR); + CxbxShowError("Cxbx-Reloaded can only run under WoW64\nThis means either a 64-bit version of Windows or Wine with a 64-bit prefix"); return EXIT_FAILURE; } /*! verify Cxbx.exe is loaded to base address 0x00010000 */ if (!VerifyBaseAddr()) { - MessageBox(NULL, "Cxbx.exe is not loaded to base address 0x00010000 (which is a requirement for Xbox emulation)", "Cxbx-Reloaded", - MB_OK | MB_ICONERROR); + CxbxShowError("Cxbx.exe is not loaded to base address 0x00010000 (which is a requirement for Xbox emulation)"); return EXIT_FAILURE; } @@ -65,7 +63,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine bool bHasLoadArgument = CheckLoadArgument(__argc, __argv, &guiProcessID); /*! initialize shared memory */ - EmuShared::Init(guiProcessID); + if (!EmuShared::Init(guiProcessID)) { + CxbxShowError("Could not map shared memory!"); + return EXIT_FAILURE; + } if (!HandleFirstLaunch()) { EmuShared::Cleanup(); @@ -121,9 +122,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } /*! if an error occurred, notify user */ - if(MainWindow->HasError()) - { - MessageBox(NULL, MainWindow->GetError().c_str(), "Cxbx-Reloaded", MB_ICONSTOP | MB_OK); + if(MainWindow->HasError()) { + CxbxShowError(MainWindow->GetError().c_str()); } delete MainWindow; diff --git a/src/loader/cxbxr-ldr.cpp b/src/loader/cxbxr-ldr.cpp index 87e8acea1..8a40fdd05 100644 --- a/src/loader/cxbxr-ldr.cpp +++ b/src/loader/cxbxr-ldr.cpp @@ -81,10 +81,6 @@ unsigned char virtual_memory_placeholder[VM_PLACEHOLDER_SIZE] = { 0 }; // = { OP void OutputMessage(const char *msg) { - if (!msg) { - return; - } - OutputDebugStringA(msg); // Send message to debugger output too HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);