From dd15d67e62a17028ecb95e27d7c0e674e67a4577 Mon Sep 17 00:00:00 2001 From: Misa Date: Sun, 2 Jun 2024 11:09:22 -0700 Subject: [PATCH] Fix info args not working with -console The intention of the `-console` argument was to enable seeing console output on Windows without having to use workarounds. However, this didn't actually work for arguments like `-addresses` and `-version`, because the program would exit first before it could get the chance to create the console. The other issue is that the console closes too quickly before output can be read by the user. So to fix that, we must hold it open and let the user close it when they want to by waiting for an enter press from STDIN. --- desktop_version/src/Vlogging.c | 6 ++ desktop_version/src/main.cpp | 106 +++++++++++++++++++++++---------- 2 files changed, 80 insertions(+), 32 deletions(-) diff --git a/desktop_version/src/Vlogging.c b/desktop_version/src/Vlogging.c index 2b00095c..c0d19781 100644 --- a/desktop_version/src/Vlogging.c +++ b/desktop_version/src/Vlogging.c @@ -220,6 +220,12 @@ void vlog_open_console(void) vlog_error("Could not redirect STDERR to console."); } + handle = freopen("CON", "r", stdin); + if (handle == NULL) + { + vlog_error("Could not redirect STDIN to console."); + } + check_color_support(); if (!SetConsoleOutputCP(CP_UTF8)) diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 1dc9f989..cdae341f 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -363,6 +363,23 @@ static void emscriptenloop(void) } #endif +static void keep_console_open(const bool open_console) +{ + if (!open_console) + { + return; + } + + printf("Press ENTER to quit."); + + int c; + do + { + c = getchar(); + } + while (c != '\n' && c != EOF); +} + int main(int argc, char *argv[]) { char* baseDir = NULL; @@ -370,9 +387,11 @@ int main(int argc, char *argv[]) char* langDir = NULL; char* fontsDir = NULL; bool seed_use_sdl_getticks = false; -#ifdef _WIN32 bool open_console = false; -#endif + bool print_version = false; + bool print_addresses = false; + int invalid_arg = 0; + int invalid_partial_arg = 0; vlog_init(); @@ -386,41 +405,16 @@ int main(int argc, char *argv[]) } \ else \ { \ - vlog_error("%s option requires one argument.", argv[i]); \ - VVV_exit(1); \ + invalid_partial_arg = i; \ } if (ARG("-version")) { - /* Just print the version and exit. No vlogging. */ - puts( - "VVVVVV " RELEASE_VERSION -#ifdef MAKEANDPLAY - " [M&P]" -#endif - ); -#ifdef INTERIM_VERSION_EXISTS - puts(COMMIT_DATE); - puts(INTERIM_COMMIT); - puts(BRANCH_NAME); -#endif - VVV_exit(0); + print_version = true; } else if (ARG("-addresses")) { - printf("cl : %p\n", (void*) &cl); - printf("ed : %p\n", (void*) &ed); - printf("game : %p\n", (void*) &game); - printf("gameScreen : %p\n", (void*) &gameScreen); - printf("graphics : %p\n", (void*) &graphics); - printf("help : %p\n", (void*) &help); - printf("key : %p\n", (void*) &key); - printf("map : %p\n", (void*) &map); - printf("music : %p\n", (void*) &music); - printf("obj : %p\n", (void*) &obj); - printf("script : %p\n", (void*) &script); - - VVV_exit(0); + print_addresses = true; } else if (ARG("-renderer")) { @@ -541,8 +535,7 @@ int main(int argc, char *argv[]) #undef ARG else { - vlog_error("Error: invalid option: %s", argv[i]); - VVV_exit(1); + invalid_arg = i; } } @@ -557,6 +550,54 @@ int main(int argc, char *argv[]) } #endif + if (invalid_arg > 0) + { + vlog_error("Error: invalid option: %s", argv[invalid_arg]); + keep_console_open(open_console); + VVV_exit(1); + } + else if (invalid_partial_arg > 0) + { + vlog_error("%s option requires one argument.", argv[invalid_partial_arg]); + keep_console_open(open_console); + VVV_exit(1); + } + + if (print_version) + { + /* Just print the version and exit. No vlogging. */ + puts( + "VVVVVV " RELEASE_VERSION +#ifdef MAKEANDPLAY + " [M&P]" +#endif + ); +#ifdef INTERIM_VERSION_EXISTS + puts(COMMIT_DATE); + puts(INTERIM_COMMIT); + puts(BRANCH_NAME); +#endif + keep_console_open(open_console); + VVV_exit(0); + } + else if (print_addresses) + { + printf("cl : %p\n", (void*) &cl); + printf("ed : %p\n", (void*) &ed); + printf("game : %p\n", (void*) &game); + printf("gameScreen : %p\n", (void*) &gameScreen); + printf("graphics : %p\n", (void*) &graphics); + printf("help : %p\n", (void*) &help); + printf("key : %p\n", (void*) &key); + printf("map : %p\n", (void*) &map); + printf("music : %p\n", (void*) &music); + printf("obj : %p\n", (void*) &obj); + printf("script : %p\n", (void*) &script); + + keep_console_open(open_console); + VVV_exit(0); + } + SDL_SetHintWithPriority(SDL_HINT_IME_SHOW_UI, "1", SDL_HINT_OVERRIDE); /* We already do the button swapping in ButtonGlyphs, disable SDL's swapping */ @@ -774,6 +815,7 @@ int main(int argc, char *argv[]) cl.ListOfMetaData.push_back(meta); } else { vlog_error("Level not found"); + keep_console_open(open_console); VVV_exit(1); } }