From 8946bf9e01d88a3fccd2c563bf6f99b4e7057684 Mon Sep 17 00:00:00 2001 From: toaster Date: Sat, 3 Sep 2022 00:16:41 +0100 Subject: [PATCH] Refactor PR_ApplyProfile and its ilk - Reduces copypasted code. - Preperation for the next commit. --- src/k_profiles.c | 64 +++++++++++++++++++++++++++++++++++------------- src/k_profiles.h | 5 ++++ 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/k_profiles.c b/src/k_profiles.c index 32cba641a..5067b98ae 100644 --- a/src/k_profiles.c +++ b/src/k_profiles.c @@ -402,18 +402,8 @@ skincolornum_t PR_GetProfileColor(profile_t *p) return p->color; } -void PR_ApplyProfile(UINT8 profilenum, UINT8 playernum) +static void PR_ApplyProfile_Appearance(profile_t *p, UINT8 playernum) { - profile_t *p = PR_GetProfile(profilenum); - - // this CAN happen!! - if (p == NULL) - { - CONS_Printf("Profile '%d' could not be loaded as it does not exist. Guest Profile will be loaded instead.\n", profilenum); - profilenum = 0; // make sure to set this so that the cvar is set properly. - p = PR_GetProfile(0); // Use guest profile instead if things went south somehow. - } - CV_StealthSet(&cv_skin[playernum], p->skinname); CV_StealthSetValue(&cv_playercolor[playernum], PR_GetProfileColor(p)); CV_StealthSet(&cv_playername[playernum], p->playername); @@ -421,13 +411,19 @@ void PR_ApplyProfile(UINT8 profilenum, UINT8 playernum) // Followers CV_StealthSet(&cv_follower[playernum], p->follower); CV_StealthSetValue(&cv_followercolor[playernum], p->followercolor); +} +static void PR_ApplyProfile_Settings(profile_t *p, UINT8 playernum) +{ // toggles CV_StealthSetValue(&cv_kickstartaccel[playernum], p->kickstartaccel); // set controls... memcpy(&gamecontrol[playernum], p->controls, sizeof(gamecontroldefault)); +} +static void PR_ApplyProfile_Memory(UINT8 profilenum, UINT8 playernum) +{ // set memory cvar CV_StealthSetValue(&cv_lastprofile[playernum], profilenum); @@ -438,17 +434,51 @@ void PR_ApplyProfile(UINT8 profilenum, UINT8 playernum) } } +void PR_ApplyProfile(UINT8 profilenum, UINT8 playernum) +{ + profile_t *p = PR_GetProfile(profilenum); + + // this CAN happen!! + if (p == NULL) + { + CONS_Printf("Profile '%d' could not be loaded as it does not exist. Guest Profile will be loaded instead.\n", profilenum); + profilenum = 0; // make sure to set this so that the cvar is set properly. + p = PR_GetProfile(profilenum); + } + + PR_ApplyProfile_Appearance(p, playernum); + PR_ApplyProfile_Settings(p, playernum); + PR_ApplyProfile_Memory(profilenum, playernum); +} + void PR_ApplyProfileLight(UINT8 profilenum, UINT8 playernum) { profile_t *p = PR_GetProfile(profilenum); - CV_StealthSet(&cv_skin[playernum], p->skinname); - CV_StealthSetValue(&cv_playercolor[playernum], p->color); - CV_StealthSet(&cv_playername[playernum], p->playername); + // this CAN happen!! + if (p == NULL) + { + // no need to be as loud... + profilenum = 0; // make sure to set this so that the cvar is set properly. + p = PR_GetProfile(profilenum); + } - // Followers - CV_StealthSet(&cv_follower[playernum], p->follower); - CV_StealthSetValue(&cv_followercolor[playernum], p->followercolor); + PR_ApplyProfile_Appearance(p, playernum); +} + +void PR_ApplyProfilePretend(UINT8 profilenum, UINT8 playernum) +{ + profile_t *p = PR_GetProfile(profilenum); + + // this CAN happen!! + if (p == NULL) + { + CONS_Printf("Profile '%d' could not be loaded as it does not exist. Guest Profile will be loaded instead.\n", profilenum); + profilenum = 0; // make sure to set this so that the cvar is set properly. + p = PR_GetProfile(profilenum); + } + + PR_ApplyProfile_Memory(profilenum, playernum); } UINT8 PR_GetProfileNum(profile_t *p) diff --git a/src/k_profiles.h b/src/k_profiles.h index a099e1ca2..0a254bef5 100644 --- a/src/k_profiles.h +++ b/src/k_profiles.h @@ -136,6 +136,11 @@ void PR_ApplyProfile(UINT8 profilenum, UINT8 playernum); // Controls, kickstartaccel and "current profile" data is *not* modified. void PR_ApplyProfileLight(UINT8 profilenum, UINT8 playernum); +// PR_ApplyProfilePretend(UINT8 profilenum, UINT8 playernum) +// ONLY modifies "current profile" data. +// Exists because any other option inteferes with rapid testing. +void PR_ApplyProfilePretend(UINT8 profilenum, UINT8 playernum); + // PR_GetProfileNum(profile_t *p) // Gets the profile's index # in profilesList UINT8 PR_GetProfileNum(profile_t *p);