added setup values to store system and player UUID
[rocksndiamonds.git] / src / libgame / misc.c
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
 // ----------------------------------------------------------------------------