added setup values to store system and player UUID
authorHolger Schemel <info@artsoft.org>
Sat, 7 Aug 2021 19:29:06 +0000 (21:29 +0200)
committerHolger Schemel <info@artsoft.org>
Sun, 8 Aug 2021 22:54:34 +0000 (00:54 +0200)
src/files.c
src/libgame/misc.c
src/libgame/misc.h
src/libgame/system.c
src/libgame/system.h

index e9cd81135f9ed2a2950777a26e26f539c9cdbdd3..8e51f5ad801cd1edd8bfe89bb42de8bc8d71dbcd 100644 (file)
@@ -9509,6 +9509,14 @@ static struct TokenInfo global_setup_tokens[] =
     TYPE_STRING,
     &setup.player_name,                                "player_name"
   },
+  {
+    TYPE_STRING,
+    &setup.player_uuid,                                "player_uuid"
+  },
+  {
+    TYPE_STRING,
+    &setup.system_uuid,                                "system_uuid"
+  },
   {
     TYPE_SWITCH,
     &setup.multiple_users,                     "multiple_users"
@@ -10321,6 +10329,9 @@ static void setSetupInfoToDefaults(struct SetupInfo *si)
 
   si->player_name = getStringCopy(getDefaultUserName(user.nr));
 
+  si->player_uuid = NULL;      // (will be set later)
+  si->system_uuid = NULL;      // (will be set later)
+
   si->multiple_users = TRUE;
 
   si->sound = TRUE;
@@ -10866,6 +10877,20 @@ static void LoadSetup_SpecialPostProcessing(void)
   // make sure that scroll delay value stays inside valid range
   setup.scroll_delay_value =
     MIN(MAX(MIN_SCROLL_DELAY, setup.scroll_delay_value), MAX_SCROLL_DELAY);
+
+  if (setup.player_uuid == NULL ||
+      setup.system_uuid == NULL)
+  {
+    if (setup.player_uuid == NULL)
+      setup.player_uuid = getStringCopy(GetPlayerUUID());
+
+    if (setup.system_uuid == NULL)
+      setup.system_uuid = getStringCopy(GetSystemUUID());
+
+    SaveSetup();
+  }
+
+  SetSystemUUID(setup.system_uuid);
 }
 
 void LoadSetup(void)
@@ -10998,7 +11023,8 @@ void SaveSetup(void)
   for (i = 0; i < ARRAY_SIZE(global_setup_tokens); i++)
   {
     // just to make things nicer :)
-    if (global_setup_tokens[i].value == &setup.multiple_users          ||
+    if (global_setup_tokens[i].value == &setup.player_uuid             ||
+       global_setup_tokens[i].value == &setup.multiple_users           ||
        global_setup_tokens[i].value == &setup.sound                    ||
        global_setup_tokens[i].value == &setup.graphics_set             ||
        global_setup_tokens[i].value == &setup.volume_simple            ||
index ed4c2606e81026f94c307d9da347eab39f3db3b8..bd93e1ce170af1cc4f74a2a2a37b5869f11e50f6 100644 (file)
@@ -549,6 +549,61 @@ boolean getTokenValueFromString(char *string, char **token, char **value)
 }
 
 
+// ----------------------------------------------------------------------------
+// UUID functions
+// ----------------------------------------------------------------------------
+
+#define UUID_BYTES             16
+#define UUID_CHARS             (UUID_BYTES * 2)
+#define UUID_LENGTH            (UUID_CHARS + 4)
+
+static char *getUUID(void)
+{
+  static char uuid[UUID_LENGTH + 1];
+  int data[UUID_BYTES];
+  int count = 0;
+  int i;
+
+  for (i = 0; i < UUID_BYTES; i++)
+    data[i] = GetSimpleRandom(256);
+
+  data[6] = 0x40 | (data[6] & 0x0f);
+  data[8] = 0x80 | (data[8] & 0x3f);
+
+  for (i = 0; i < UUID_BYTES; i++)
+  {
+    sprintf(&uuid[count], "%02x", data[i]);
+    count += 2;
+
+    if (i == 3 || i == 5 || i == 7 || i == 9)
+      strcat(&uuid[count++], "-");
+  }
+
+  return uuid;
+}
+
+char *GetPlayerUUID(void)
+{
+  return getUUID();
+}
+
+char *GetSystemUUID(void)
+{
+  if (program.system_uuid != NULL)
+    return program.system_uuid;
+
+  return getUUID();
+}
+
+void SetSystemUUID(char *uuid)
+{
+  if (program.system_uuid != NULL)
+    checked_free(program.system_uuid);
+
+  program.system_uuid = getStringCopy(uuid);
+}
+
+
 // ----------------------------------------------------------------------------
 // counter functions
 // ----------------------------------------------------------------------------
index cb27fa4e9aadd78bbffd09421e34e04fd849ceb4..1665a2469f32cb34a9640a84bd1a73b043712b20 100644 (file)
@@ -120,6 +120,10 @@ int log_2(unsigned int);
 
 boolean getTokenValueFromString(char *, char **, char **);
 
+char *GetPlayerUUID(void);
+char *GetSystemUUID(void);
+void SetSystemUUID(char *);
+
 void InitCounter(void);
 unsigned int Counter(void);
 void Delay(unsigned int);
index 28d8ebae635b09027136b57abed0035f377ec8c9..f186c143d298cfd4c933e19938e8b226d13b5d41 100644 (file)
@@ -103,6 +103,8 @@ void InitProgramInfo(char *argv0, char *config_filename, char *userdata_subdir,
   program.log_file[LOG_OUT_ID] = program.log_file_default[LOG_OUT_ID] = stdout;
   program.log_file[LOG_ERR_ID] = program.log_file_default[LOG_ERR_ID] = stderr;
 
+  program.system_uuid = NULL;
+
   program.headless = FALSE;
 }
 
index 636240d58e196af73beb329acdb23e04a97a9ef5..bf7ec1a4306200bf146ed2e6047eab6925aa2bf9 100644 (file)
@@ -1013,6 +1013,8 @@ struct ProgramInfo
   void (*exit_message_function)(char *, va_list);
   void (*exit_function)(int);
 
+  char *system_uuid;           // initialized when reading first setup file
+
   boolean headless;
 };
 
@@ -1431,6 +1433,9 @@ struct SetupInfo
 {
   char *player_name;
 
+  char *player_uuid;
+  char *system_uuid;
+
   boolean multiple_users;
 
   boolean sound;