From 224535bfe9ef9bfae63168c8ed99c2b70d2f44b1 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sat, 7 Aug 2021 21:29:06 +0200 Subject: [PATCH] added setup values to store system and player UUID --- src/files.c | 28 +++++++++++++++++++++- src/libgame/misc.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/libgame/misc.h | 4 ++++ src/libgame/system.c | 2 ++ src/libgame/system.h | 5 ++++ 5 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/files.c b/src/files.c index e9cd8113..8e51f5ad 100644 --- a/src/files.c +++ b/src/files.c @@ -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 || diff --git a/src/libgame/misc.c b/src/libgame/misc.c index ed4c2606..bd93e1ce 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -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 // ---------------------------------------------------------------------------- diff --git a/src/libgame/misc.h b/src/libgame/misc.h index cb27fa4e..1665a246 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -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); diff --git a/src/libgame/system.c b/src/libgame/system.c index 28d8ebae..f186c143 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -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; } diff --git a/src/libgame/system.h b/src/libgame/system.h index 636240d5..bf7ec1a4 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -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; -- 2.34.1