diff --git a/src/pc/configfile.c b/src/pc/configfile.c index 05693ed4..0bf121b0 100644 --- a/src/pc/configfile.c +++ b/src/pc/configfile.c @@ -357,10 +357,11 @@ static const struct FunctionConfigOption functionOptions[] = { // Reads an entire line from a file (excluding the newline character) and returns an allocated string // Returns NULL if no lines could be read from the file -static char *read_file_line(fs_file_t *file) { +static char *read_file_line(fs_file_t *file, bool* error) { char *buffer; size_t bufferSize = 8; size_t offset = 0; // offset in buffer to write + *error = false; buffer = malloc(bufferSize); buffer[0] = '\0'; @@ -373,6 +374,7 @@ static char *read_file_line(fs_file_t *file) { offset = strlen(buffer); if (offset <= 0) { LOG_ERROR("Configfile offset <= 0"); + *error = true; return NULL; } @@ -440,11 +442,16 @@ const char *configfile_name(void) { return (gCLIOpts.ConfigFile[0]) ? gCLIOpts.ConfigFile : CONFIGFILE_DEFAULT; } +const char *configfile_backup_name(void) { + return CONFIGFILE_BACKUP; +} + // Loads the config file specified by 'filename' -void configfile_load(const char *filename) { +void configfile_load(const char *filename, bool* error) { fs_file_t *file; char *line; unsigned int temp; + *error = false; printf("Loading configuration from '%s'\n", filename); @@ -457,7 +464,7 @@ void configfile_load(const char *filename) { } // Go through each line in the file - while ((line = read_file_line(file)) != NULL) { + while ((line = read_file_line(file, error)) != NULL && !*error) { char *p = line; char *tokens[20]; int numTokens; @@ -539,6 +546,11 @@ void configfile_load(const char *filename) { } NEXT_OPTION: free(line); + line = NULL; + } + + if (line) { + free(line); } fs_close(file); diff --git a/src/pc/configfile.h b/src/pc/configfile.h index dbe9a9e9..484c851e 100644 --- a/src/pc/configfile.h +++ b/src/pc/configfile.h @@ -6,6 +6,7 @@ #include "game/characters.h" #define CONFIGFILE_DEFAULT "sm64config.txt" +#define CONFIGFILE_BACKUP "sm64config-backup.txt" #define MAX_BINDS 3 #define MAX_VOLUME 127 @@ -111,8 +112,9 @@ extern bool configDebugInfo; extern bool configDebugError; extern char configLanguage[]; -void configfile_load(const char *filename); +void configfile_load(const char *filename, bool* error); void configfile_save(const char *filename); const char *configfile_name(void); +const char *configfile_backup_name(void); #endif diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index f4caa7cc..a1ce19f8 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -278,7 +278,16 @@ void main_func(void) { sync_objects_init_system(); djui_unicode_init(); mods_init(); - configfile_load(configfile_name()); + + // load config + bool configReadError = false; + configfile_load(configfile_name(), &configReadError); + if (configReadError) { + configfile_load(configfile_backup_name(), &configReadError); + } else { + configfile_save(configfile_backup_name()); + } + if (!djui_language_init(configLanguage)) { snprintf(configLanguage, MAX_CONFIG_STRING, "%s", ""); }