coop now restores a backup configuration file if loading the config file threw an error

This commit is contained in:
MysterD 2023-04-01 17:01:50 -07:00
parent 972687de72
commit 0b6e087936
3 changed files with 28 additions and 5 deletions

View file

@ -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);

View file

@ -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

View file

@ -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", "");
}