From 7611b04ad1f5a88ce5b96281ef0ecb2f8e5e500b Mon Sep 17 00:00:00 2001 From: Ted John Date: Sun, 23 Oct 2016 16:16:20 +0100 Subject: [PATCH] Add --console switch for Windows Windows subsystem does not work like console subsystem which makes it almost impossible to obtain `stdout` until the application has finished. This adds a `--console` switch to make the game either attach to an existing console or show a new one and redirect the C streams to it. --- src/cmdline/RootCommands.cpp | 35 ++++++++++++++++++++++++++--------- src/openrct2.c | 14 -------------- src/platform/platform.h | 1 + src/platform/windows.c | 11 +++++++++++ 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/cmdline/RootCommands.cpp b/src/cmdline/RootCommands.cpp index 25bf1ee15d..9f25711060 100644 --- a/src/cmdline/RootCommands.cpp +++ b/src/cmdline/RootCommands.cpp @@ -30,10 +30,26 @@ extern "C" #include "../network/network.h" #include "CommandLine.hpp" +#ifdef USE_BREAKPAD +#define IMPLIES_SILENT_BREAKPAD ", implies --silent-breakpad" +#else +#define IMPLIES_SILENT_BREAKPAD +#endif // USE_BREAKPAD + +#if defined(__WINDOWS__) && !defined(DEBUG) + #define __PROVIDE_CONSOLE__ 1 +#endif // defined(__WINDOWS__) && !defined(DEBUG) + #ifndef DISABLE_NETWORK int gNetworkStart = NETWORK_MODE_NONE; char gNetworkStartHost[128]; int gNetworkStartPort = NETWORK_DEFAULT_PORT; + +static uint32 _port = 0; +#endif + +#ifdef __PROVIDE_CONSOLE__ + static bool _provideConsole; #endif static bool _help = false; @@ -43,21 +59,12 @@ static bool _all = false; static bool _about = false; static bool _verbose = false; static bool _headless = false; -#ifndef DISABLE_NETWORK -static uint32 _port = 0; -#endif static utf8 * _password = nullptr; static utf8 * _userDataPath = nullptr; static utf8 * _openrctDataPath = nullptr; static utf8 * _rct2DataPath = nullptr; static bool _silentBreakpad = false; -#ifdef USE_BREAKPAD -#define IMPLIES_SILENT_BREAKPAD ", implies --silent-breakpad" -#else -#define IMPLIES_SILENT_BREAKPAD -#endif // USE_BREAKPAD - static const CommandLineOptionDefinition StandardOptions[] { { CMDLINE_TYPE_SWITCH, &_help, 'h', "help", "show this help message and exit" }, @@ -67,6 +74,9 @@ static const CommandLineOptionDefinition StandardOptions[] { CMDLINE_TYPE_SWITCH, &_about, NAC, "about", "show information about " OPENRCT2_NAME }, { CMDLINE_TYPE_SWITCH, &_verbose, NAC, "verbose", "log verbose messages" }, { CMDLINE_TYPE_SWITCH, &_headless, NAC, "headless", "run " OPENRCT2_NAME " headless" IMPLIES_SILENT_BREAKPAD }, +#ifdef __PROVIDE_CONSOLE__ + { CMDLINE_TYPE_SWITCH, &_provideConsole, NAC, "console", "creates a new or attaches to an existing console window for standard output" }, +#endif #ifndef DISABLE_NETWORK { CMDLINE_TYPE_INTEGER, &_port, NAC, "port", "port to use for hosting or joining a server" }, #endif @@ -144,6 +154,13 @@ exitcode_t CommandLine::HandleCommandDefault() { exitcode_t result = EXITCODE_CONTINUE; +#ifdef __PROVIDE_CONSOLE__ + if (_provideConsole) + { + platform_windows_open_console(); + } +#endif + if (_about) { PrintAbout(); diff --git a/src/openrct2.c b/src/openrct2.c index ce026ff2fb..9af19a5ffb 100644 --- a/src/openrct2.c +++ b/src/openrct2.c @@ -661,17 +661,3 @@ static void openrct2_setup_rct2_hooks() { // None for now } - -#if defined(_MSC_VER) && (_MSC_VER >= 1900) -/** - * Temporary fix for libraries not compiled with VS2015 - */ -FILE **__iob_func() -{ - static FILE* streams[3]; - streams[0] = stdin; - streams[1] = stdout; - streams[2] = stderr; - return streams; -} -#endif diff --git a/src/platform/platform.h b/src/platform/platform.h index c040e4794f..c3bcc90ae4 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -203,6 +203,7 @@ datetime64 platform_get_datetime_now_utc(); #include #undef GetMessage + void platform_windows_open_console(); int windows_get_registry_install_info(rct2_install_info *installInfo, char *source, char *font, uint8 charset); HWND windows_get_window_handle(); void platform_setup_file_associations(); diff --git a/src/platform/windows.c b/src/platform/windows.c index 192dba4278..7722b5b93d 100644 --- a/src/platform/windows.c +++ b/src/platform/windows.c @@ -143,6 +143,17 @@ __declspec(dllexport) int StartOpenRCT(HINSTANCE hInstance, HINSTANCE hPrevInsta #endif // NO_RCT2 +void platform_windows_open_console() +{ + if (!AttachConsole(ATTACH_PARENT_PROCESS)) { + AllocConsole(); + } + + freopen("CONIN$", "r", stdin); + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); +} + utf8 **windows_get_command_line_args(int *outNumArgs) { int argc;