added support for loading and saving number of currently active user
authorHolger Schemel <info@artsoft.org>
Sun, 18 Oct 2020 00:16:29 +0000 (02:16 +0200)
committerHolger Schemel <info@artsoft.org>
Sun, 13 Dec 2020 23:57:57 +0000 (00:57 +0100)
src/init.c
src/libgame/setup.c
src/libgame/setup.h
src/libgame/system.h

index 008db40cde459a4aba01585457d4ebd8187d4055..381423265f235032ec96b0415628845eb0c30928 100644 (file)
@@ -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
 
index 74c21fe33bdad1fe05195ff196b84a3f20278f56..5deb511f9ab82f175904c1a67da3816114be9a2d 100644 (file)
@@ -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)
+{
+  // --------------------------------------------------------------------------
+  // ~/.<program>/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)
+{
+  // --------------------------------------------------------------------------
+  // ~/.<program>/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);
+}
index 425c01fa5d22a7caf1e50140acb8a0f3166f08db..211d7614bbf310a55a30f50c68e108f08cd037d4 100644 (file)
@@ -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
index 48a03fc1a80e43e8385a2177bb756646ea973571..2011aaacf3e6932985f32011b946555c7199b8e2 100644 (file)
 // 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
 
 // 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"