From daffa99825ddb9d30b400f94d9f6d8254178b2d5 Mon Sep 17 00:00:00 2001 From: PatrickvL Date: Fri, 8 Feb 2019 18:29:01 +0100 Subject: [PATCH] Refactorings preparing actual emulation start for DLL (just need to move two functions to a shared file) --- src/emulator/CxbxEmulator.cpp | 7 +- src/gui/WinMain.cpp | 149 ++++++++++++++++++---------------- 2 files changed, 81 insertions(+), 75 deletions(-) diff --git a/src/emulator/CxbxEmulator.cpp b/src/emulator/CxbxEmulator.cpp index 01f84229c..da909eff2 100644 --- a/src/emulator/CxbxEmulator.cpp +++ b/src/emulator/CxbxEmulator.cpp @@ -143,20 +143,19 @@ DWORD WINAPI Emulate(int system) return EXIT_FAILURE; } - /* Initialize Cxbx File Paths */ -// CxbxInitFilePaths(); - /*! initialize shared memory */ if (!EmuShared::Init(0)) { MessageBox(NULL, "Could not map shared memory!", "Cxbx-Reloaded", MB_OK); return EXIT_FAILURE; } + // TODO : Call HandleFirstLaunch(); + LPSTR CommandLine = GetCommandLine(); int argc; PCHAR *argv = CommandLineToArgvA(CommandLine, &argc); -// CxbxKrnlMain(argc, argv); + // TODO : Call LaunchEmulation(argc, argv); LocalFree(argv); diff --git a/src/gui/WinMain.cpp b/src/gui/WinMain.cpp index 0deeeba9f..a794d4a96 100644 --- a/src/gui/WinMain.cpp +++ b/src/gui/WinMain.cpp @@ -40,6 +40,72 @@ name = 'Microsoft.Windows.Common-Controls' version = '6.0.0.0' \ processorArchitecture = '*' publicKeyToken = '6595b64144ccf1df' language = '*'\"") +// TODO : Move HandleFirstLaunch() to a file shared with the CxbxEmulator project +bool HandleFirstLaunch() +{ + bool bFirstLaunch; + g_EmuShared->GetIsFirstLaunch(&bFirstLaunch); + + /* check if process is launch with elevated access then prompt for continue on or not. */ + if (!bFirstLaunch) { + g_Settings = new Settings(); + if (g_Settings == nullptr) { + MessageBox(nullptr, szSettings_alloc_error, "Cxbx-Reloaded", MB_OK); + return false; + } + + if (!g_Settings->Init()) { + return false; + } + + log_get_settings(); + + 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" + "\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); + if (ret != IDYES) { + return false; + } + } + + g_EmuShared->SetIsFirstLaunch(true); + } + + return true; +} + +// TODO : Move LaunchEmulation() to a file shared with the CxbxEmulator project +void LaunchEmulation(int argc, char* argv[]) +{ + // NOTE: This is designated for standalone kernel mode launch without GUI + if (g_Settings != nullptr) { + + // Reset to default + g_EmuShared->Reset(); + + g_Settings->Verify(); + g_Settings->SyncToEmulator(); + + // We don't need to keep Settings open plus allow emulator to use unused memory. + delete g_Settings; + g_Settings = nullptr; + + // Perform identical to what GUI will do to certain EmuShared's variable before launch. + g_EmuShared->SetIsEmulating(true); + + // NOTE: This setting the ready status is optional. Internal kernel process is checking if GUI is running. + // Except if enforce check, then we need to re-set ready status every time for non-GUI. + //g_EmuShared->SetIsReady(true); + } + + /* Initialize Cxbx File Paths */ + CxbxInitFilePaths(); + + CxbxKrnlMain(argc, argv); +} + /*! program entry point */ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { @@ -60,112 +126,53 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine return EXIT_FAILURE; } - bool bRet, bKernel; + bool bHasLoadArgument; HWND hWnd = nullptr; DWORD guiProcessID = 0; // TODO: Convert ALL __argc & __argv to use main(int argc, char** argv) method. if (__argc >= 2 && std::strcmp(__argv[1], "/load") == 0 && std::strlen(__argv[2]) > 0) { - bKernel = true; - + bHasLoadArgument = true; // Perform check if command line contain gui's hWnd value. if (__argc > 3) { hWnd = (HWND)std::stoi(__argv[3], nullptr, 10); - hWnd = IsWindow(hWnd) ? hWnd : nullptr; if (hWnd != nullptr) { // We don't need thread ID from window handle. GetWindowThreadProcessId(hWnd, &guiProcessID); } } - } - else { - bKernel = false; + } else { + bHasLoadArgument = false; guiProcessID = GetCurrentProcessId(); } + g_exec_filepath = __argv[0]; // NOTE: Workaround solution until simulated "main" function is made. /*! initialize shared memory */ EmuShared::Init(guiProcessID); - bool bFirstLaunch; - g_EmuShared->GetIsFirstLaunch(&bFirstLaunch); - - /* check if process is launch with elevated access then prompt for continue on or not. */ - if (!bFirstLaunch) { - - g_Settings = new Settings(); - - if (g_Settings == nullptr) { - MessageBox(nullptr, szSettings_alloc_error, "Cxbx-Reloaded", MB_OK); - EmuShared::Cleanup(); - return EXIT_FAILURE; - } - - bRet = g_Settings->Init(); - if (!bRet) { - EmuShared::Cleanup(); - return EXIT_FAILURE; - } - - log_get_settings(); - - 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" - "\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); - if (ret != IDYES) { - EmuShared::Cleanup(); - return EXIT_FAILURE; - } - } - g_EmuShared->SetIsFirstLaunch(true); + if (!HandleFirstLaunch()) { + EmuShared::Cleanup(); + return EXIT_FAILURE; } - if (bKernel) { - - // NOTE: This is designated for standalone kernel mode launch without GUI - if (g_Settings != nullptr) { - - // Reset to default - g_EmuShared->Reset(); - - g_Settings->Verify(); - g_Settings->SyncToEmulator(); - - // We don't need to keep Settings open plus allow emulator to use unused memory. - delete g_Settings; - g_Settings = nullptr; - - // Perform identical to what GUI will do to certain EmuShared's variable before launch. - g_EmuShared->SetIsEmulating(true); - - // NOTE: This setting the ready status is optional. Internal kernel process is checking if GUI is running. - // Except if enforce check, then we need to re-set ready status every time for non-GUI. - //g_EmuShared->SetIsReady(true); - } - - /* Initialize Cxbx File Paths */ - CxbxInitFilePaths(); - - CxbxKrnlMain(__argc, __argv); + if (bHasLoadArgument) { + LaunchEmulation(__argc, __argv); + EmuShared::Cleanup(); return EXIT_SUCCESS; } // If 2nd GUI executable is launched, load settings file for GUI for editable support. if (g_Settings == nullptr) { g_Settings = new Settings(); - if (g_Settings == nullptr) { MessageBox(nullptr, szSettings_alloc_error, "Cxbx-Reloaded", MB_OK); EmuShared::Cleanup(); return EXIT_FAILURE; } - bRet = g_Settings->Init(); - if (!bRet) { + if (!g_Settings->Init()) { EmuShared::Cleanup(); return EXIT_FAILURE; }