From 3af17f639646cda1382de0d43075238e750b7ec6 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sun, 18 Oct 2020 02:16:29 +0200 Subject: [PATCH] added support for loading and saving number of currently active user --- src/init.c | 2 ++ src/libgame/setup.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ src/libgame/setup.h | 3 +++ src/libgame/system.h | 4 +++ 4 files changed, 73 insertions(+) diff --git a/src/init.c b/src/init.c index 008db40c..38142326 100644 --- a/src/init.c +++ b/src/init.c @@ -5184,6 +5184,8 @@ static void Execute_Command(char *command) static void InitSetup(void) { + LoadUserSetup(); // global user number + LoadSetup(); // global setup info LoadSetup_AutoSetup(); // global auto setup info diff --git a/src/libgame/setup.c b/src/libgame/setup.c index 74c21fe3..5deb511f 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -2364,6 +2364,7 @@ SetupFileHash *loadSetupFileHash(char *filename) #define TOKEN_STR_LAST_LEVEL_SERIES "last_level_series" #define TOKEN_STR_LAST_PLAYED_LEVEL "last_played_level" #define TOKEN_STR_HANDICAP_LEVEL "handicap_level" +#define TOKEN_STR_LAST_USER "last_user" // level directory info #define LEVELINFO_TOKEN_IDENTIFIER 0 @@ -4726,3 +4727,66 @@ void LevelStats_incSolved(int nr) if (nr >= 0 && nr < MAX_LEVELS) level_stats[nr].solved++; } + +void LoadUserSetup(void) +{ + // -------------------------------------------------------------------------- + // ~/./usersetup.conf + // -------------------------------------------------------------------------- + + char *filename = getPath2(getMainUserGameDataDir(), USERSETUP_FILENAME); + SetupFileHash *user_setup_hash = NULL; + + // always start with reliable default values + user.nr = 0; + + if ((user_setup_hash = loadSetupFileHash(filename))) + { + char *token_value; + + // get last selected user number + token_value = getHashEntry(user_setup_hash, TOKEN_STR_LAST_USER); + + if (token_value) + user.nr = MIN(MAX(0, atoi(token_value)), MAX_PLAYER_NAMES - 1); + + freeSetupFileHash(user_setup_hash); + } + else + { + Debug("setup", "using default setup values"); + } + + free(filename); +} + +void SaveUserSetup(void) +{ + // -------------------------------------------------------------------------- + // ~/./usersetup.conf + // -------------------------------------------------------------------------- + + char *filename = getPath2(getMainUserGameDataDir(), USERSETUP_FILENAME); + FILE *file; + + InitMainUserDataDirectory(); + + if (!(file = fopen(filename, MODE_WRITE))) + { + Warn("cannot write setup file '%s'", filename); + + free(filename); + + return; + } + + fprintFileHeader(file, USERSETUP_FILENAME); + + fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_LAST_USER, + i_to_a(user.nr))); + fclose(file); + + SetFilePermissions(filename, PERMS_PRIVATE); + + free(filename); +} diff --git a/src/libgame/setup.h b/src/libgame/setup.h index 425c01fa..211d7614 100644 --- a/src/libgame/setup.h +++ b/src/libgame/setup.h @@ -356,4 +356,7 @@ void LevelStats_setSolved(int, int); void LevelStats_incPlayed(int); void LevelStats_incSolved(int); +void LoadUserSetup(void); +void SaveUserSetup(void); + #endif // MISC_H diff --git a/src/libgame/system.h b/src/libgame/system.h index 48a03fc1..2011aaac 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -510,6 +510,9 @@ // maximum number of parallel players supported by libgame functions #define MAX_PLAYERS 4 +// maximum number of player names +#define MAX_PLAYER_NAMES 10 + // maximum allowed length of player name #define MAX_PLAYER_NAME_LEN 10 @@ -646,6 +649,7 @@ // file names and filename extensions #define LEVELSETUP_DIRECTORY "levelsetup" #define SETUP_FILENAME "setup.conf" +#define USERSETUP_FILENAME "usersetup.conf" #define AUTOSETUP_FILENAME "autosetup.conf" #define LEVELSETUP_FILENAME "levelsetup.conf" #define EDITORSETUP_FILENAME "editorsetup.conf" -- 2.34.1