From 952495ae085d900213f3f88d4bf7209733d06ab5 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Mon, 18 May 2020 23:31:19 +0300 Subject: [PATCH] clean up cliopts --- src/pc/cliopts.c | 93 +++++++++++++++++++++------------------------ src/pc/cliopts.h | 14 ++++--- src/pc/configfile.h | 2 + 3 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/pc/cliopts.c b/src/pc/cliopts.c index ee9bc13e..234660b2 100644 --- a/src/pc/cliopts.c +++ b/src/pc/cliopts.c @@ -1,4 +1,7 @@ #include "cliopts.h" +#include "configfile.h" +#include "pc_main.h" + #include #include #include @@ -6,53 +9,45 @@ struct PCCLIOptions gCLIOpts; -void parse_cli_opts(int argc, char* argv[]) -{ - // Initialize options with false values. - gCLIOpts.SkipIntro = 0; - gCLIOpts.FullScreen = 0; - gCLIOpts.ConfigFile = malloc(31); - strncpy(gCLIOpts.ConfigFile, "sm64config.txt", strlen("sm64config.txt")); - gCLIOpts.ConfigFile[strlen("sm64config.txt")] = '\0'; - - // Scan arguments for options - if (argc > 1) - { - int i; - for (i = 1; i < argc; i++) - { - if (strcmp(argv[i], "--skip-intro") == 0) // Skip Peach Intro - gCLIOpts.SkipIntro = 1; - - if (strcmp(argv[i], "--fullscreen") == 0) // Open game in fullscreen - gCLIOpts.FullScreen = 1; - - if (strcmp(argv[i], "--windowed") == 0) // Open game in windowed mode - gCLIOpts.FullScreen = 2; - - if (strcmp(argv[i], "--help") == 0) // Print help - { - printf("Super Mario 64 PC Port\n"); - printf("%-20s\tSkips the Peach and Castle intro when starting a new game.\n", "--skip-intro"); - printf("%-20s\tStarts the game in full screen mode.\n", "--fullscreen"); - printf("%-20s\tStarts the game in windowed mode.\n", "--windowed"); - printf("%-20s\tSaves the configuration file as CONFIGNAME.\n", "--configfile CONFIGNAME"); - exit(0); - } - - if (strncmp(argv[i], "--configfile", strlen("--configfile")) == 0) - { - if (i+1 < argc) - { - if (strlen(argv[i]) > 30) { - fprintf(stderr, "Configuration file supplied has a name too long.\n"); - } else { - memset(gCLIOpts.ConfigFile, 0, 30); - strncpy(gCLIOpts.ConfigFile, argv[i+1], strlen(argv[i+1])); - gCLIOpts.ConfigFile[strlen(argv[i+1])] = '\0'; - } - } - } - } - } +static void print_help(void) { + printf("Super Mario 64 PC Port\n"); + printf("%-20s\tSkips the Peach and Castle intro when starting a new game.\n", "--skip-intro"); + printf("%-20s\tStarts the game in full screen mode.\n", "--fullscreen"); + printf("%-20s\tStarts the game in windowed mode.\n", "--windowed"); + printf("%-20s\tSaves the configuration file as CONFIGNAME.\n", "--configfile CONFIGNAME"); +} + +void parse_cli_opts(int argc, char* argv[]) { + // Initialize options with false values. + memset(&gCLIOpts, 0, sizeof(gCLIOpts)); + strncpy(gCLIOpts.ConfigFile, CONFIGFILE_DEFAULT, sizeof(gCLIOpts.ConfigFile)); + + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--skip-intro") == 0) // Skip Peach Intro + gCLIOpts.SkipIntro = 1; + + else if (strcmp(argv[i], "--fullscreen") == 0) // Open game in fullscreen + gCLIOpts.FullScreen = 1; + + else if (strcmp(argv[i], "--windowed") == 0) // Open game in windowed mode + gCLIOpts.FullScreen = 2; + + // Print help + else if (strcmp(argv[i], "--help") == 0) { + print_help(); + game_exit(); + } + + else if (strcmp(argv[i], "--configfile") == 0) { + if (i+1 < argc) { + const unsigned int arglen = strlen(argv[i+1]); + if (arglen >= sizeof(gCLIOpts.ConfigFile)) { + fprintf(stderr, "Configuration file supplied has a name too long.\n"); + } else { + strncpy(gCLIOpts.ConfigFile, argv[i+1], arglen); + gCLIOpts.ConfigFile[arglen] = '\0'; + } + } + } + } } diff --git a/src/pc/cliopts.h b/src/pc/cliopts.h index 1844c44c..d20dcb4f 100644 --- a/src/pc/cliopts.h +++ b/src/pc/cliopts.h @@ -1,12 +1,14 @@ -#include "sm64.h" +#ifndef _CLIOPTS_H +#define _CLIOPTS_H -struct PCCLIOptions -{ - u8 SkipIntro; - u8 FullScreen; - char * ConfigFile; +struct PCCLIOptions { + unsigned int SkipIntro; + unsigned int FullScreen; + char ConfigFile[1024]; }; extern struct PCCLIOptions gCLIOpts; void parse_cli_opts(int argc, char* argv[]); + +#endif // _CLIOPTS_H diff --git a/src/pc/configfile.h b/src/pc/configfile.h index 5238b17c..cafd272f 100644 --- a/src/pc/configfile.h +++ b/src/pc/configfile.h @@ -3,6 +3,8 @@ #include +#define CONFIGFILE_DEFAULT "sm64config.txt" + #define MAX_BINDS 3 #define MAX_VOLUME 127 #define VOLUME_SHIFT 7