From c83803dd42e9a087d9592660e0c0c6ca3d1fe03c Mon Sep 17 00:00:00 2001 From: James R Date: Sat, 19 Feb 2022 14:25:34 -0800 Subject: [PATCH] Refactor profiles code to use pointers --- src/k_profiles.c | 81 +++++++++++++++++++++++++----------------------- src/k_profiles.h | 8 ++--- 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/k_profiles.c b/src/k_profiles.c index 9d25a759f..72afd7420 100644 --- a/src/k_profiles.c +++ b/src/k_profiles.c @@ -11,9 +11,10 @@ /// \brief implements methods for profiles etc. #include "k_profiles.h" +#include "z_zone.h" // List of all the profiles. -static profile_t profilesList[MAXPROFILES+1]; // +1 because we're gonna add a default "GUEST' profile. +static profile_t *profilesList[MAXPROFILES+1]; // +1 because we're gonna add a default "GUEST' profile. static UINT8 numprofiles = 0; // # of loaded profiles INT32 PR_GetNumProfiles(void) @@ -21,46 +22,46 @@ INT32 PR_GetNumProfiles(void) return numprofiles; } -profile_t PR_MakeProfile(const char *prname, const char *pname, const char *sname, const UINT16 col, const char *fname, UINT16 fcol, INT32 controlarray[num_gamecontrols][MAXINPUTMAPPING]) +profile_t* PR_MakeProfile(const char *prname, const char *pname, const char *sname, const UINT16 col, const char *fname, UINT16 fcol, INT32 controlarray[num_gamecontrols][MAXINPUTMAPPING]) { - profile_t new; + profile_t *new = Z_Malloc(sizeof(profile_t), PU_STATIC, NULL); - new.version = PROFILEVER; + new->version = PROFILEVER; - strcpy(new.profilename, prname); + strcpy(new->profilename, prname); - strcpy(new.skinname, sname); - strcpy(new.playername, pname); - new.color = col; + strcpy(new->skinname, sname); + strcpy(new->playername, pname); + new->color = col; - strcpy(new.follower, fname); - new.followercolor = fcol; + strcpy(new->follower, fname); + new->followercolor = fcol; // Copy from gamecontrol directly as we'll be setting controls up directly in the profile. - memcpy(new.controls, controlarray, sizeof(new.controls)); + memcpy(new->controls, controlarray, sizeof(new->controls)); return new; } -profile_t PR_MakeProfileFromPlayer(const char *prname, const char *pname, const char *sname, const UINT16 col, const char *fname, UINT16 fcol, UINT8 pnum) +profile_t* PR_MakeProfileFromPlayer(const char *prname, const char *pname, const char *sname, const UINT16 col, const char *fname, UINT16 fcol, UINT8 pnum) { // Generate profile using the player's gamecontrol, as we set them directly when making profiles from menus. - profile_t new = PR_MakeProfile(prname, pname, sname, col, fname, fcol, gamecontrol[pnum]); + profile_t *new = PR_MakeProfile(prname, pname, sname, col, fname, fcol, gamecontrol[pnum]); // Player bound cvars: - new.kickstartaccel = cv_kickstartaccel[pnum].value; + new->kickstartaccel = cv_kickstartaccel[pnum].value; return new; } -boolean PR_AddProfile(profile_t p) +boolean PR_AddProfile(profile_t *p) { - if (numprofiles < MAXPROFILES) + if (numprofiles < MAXPROFILES+1) { - memcpy(&profilesList[numprofiles], &p, sizeof(profile_t)); + profilesList[numprofiles] = p; numprofiles++; - CONS_Printf("Profile '%s' added\n", p.profilename); + CONS_Printf("Profile '%s' added\n", p->profilename); return true; } @@ -70,8 +71,8 @@ boolean PR_AddProfile(profile_t p) profile_t* PR_GetProfile(INT32 num) { - if (num < MAXPROFILES+1) - return &profilesList[num]; + if (num < numprofiles) + return profilesList[num]; else return NULL; } @@ -79,7 +80,7 @@ profile_t* PR_GetProfile(INT32 num) void PR_InitNewProfile(void) { char pname[PROFILENAMELEN+1] = "PRF"; - profile_t dprofile; + profile_t *dprofile; strcpy(pname, va("PRF%c", 'A'+numprofiles-1)); @@ -94,7 +95,15 @@ void PR_SaveProfiles(void) f = fopen(PROFILESFILE, "w"); if (f != NULL) { - fwrite(profilesList, sizeof(profile_t), MAXPROFILES+1, f); + UINT8 i; + + fwrite(&numprofiles, sizeof numprofiles, 1, f); + + for (i = 1; i < numprofiles; ++i) + { + fwrite(profilesList[i], sizeof(profile_t), 1, f); + } + fclose(f); } else @@ -104,29 +113,25 @@ void PR_SaveProfiles(void) void PR_LoadProfiles(void) { FILE *f = NULL; - profile_t dprofile = PR_MakeProfile(PROFILEDEFAULTNAME, PROFILEDEFAULTPNAME, PROFILEDEFAULTSKIN, PROFILEDEFAULTCOLOR, PROFILEDEFAULTFOLLOWER, PROFILEDEFAULTFOLLOWERCOLOR, gamecontroldefault); + profile_t *dprofile = PR_MakeProfile(PROFILEDEFAULTNAME, PROFILEDEFAULTPNAME, PROFILEDEFAULTSKIN, PROFILEDEFAULTCOLOR, PROFILEDEFAULTFOLLOWER, PROFILEDEFAULTFOLLOWERCOLOR, gamecontroldefault); f = fopen(PROFILESFILE, "r"); if (f != NULL) { INT32 i; - fread(profilesList, sizeof(profile_t)*(MAXPROFILES+1), MAXPROFILES+1, f); + + fread(&numprofiles, sizeof numprofiles, 1, f); + + for (i = 1; i < numprofiles; ++i) + { + profilesList[i] = Z_Malloc(sizeof(profile_t), PU_STATIC, NULL); + fread(profilesList[i], sizeof(profile_t), 1, f); + } + fclose(f); // Overwrite the first profile for the default profile to avoid letting anyone tamper with it. - memcpy(&profilesList[0], &dprofile, sizeof(profile_t)); - - // Omega, count how many profiles there are in the list. - // WHY DID YOU ASK HIM TO DO THAT IT'S GOING TO TAKE FOR-EVER - for (i=0; i < MAXPROFILES; i++) - { - if (!profilesList[i].version) - { - numprofiles = i; - return; - } - } - + profilesList[0] = dprofile; } else { @@ -146,4 +151,4 @@ void PR_ApplyProfile(UINT8 profilenum, UINT8 playernum) // set controls... memcpy(&gamecontrol[playernum], p->controls, sizeof(gamecontroldefault)); -} \ No newline at end of file +} diff --git a/src/k_profiles.h b/src/k_profiles.h index 8eb7ec3e1..4cd0a5f1d 100644 --- a/src/k_profiles.h +++ b/src/k_profiles.h @@ -74,17 +74,17 @@ INT32 PR_GetNumProfiles(void); // PR_MakeProfile // Makes a profile from the supplied profile name, player name, colour, follower, followercolour and controls. // The consvar values are left untouched. -profile_t PR_MakeProfile(const char *prname, const char *pname, const char *sname, const UINT16 col, const char *fname, UINT16 fcol, INT32 controlarray[num_gamecontrols][MAXINPUTMAPPING]); +profile_t* PR_MakeProfile(const char *prname, const char *pname, const char *sname, const UINT16 col, const char *fname, UINT16 fcol, INT32 controlarray[num_gamecontrols][MAXINPUTMAPPING]); // PR_MakeProfileFromPlayer // Makes a profile_t from the supplied profile name, player name, colour, follower and followercolour. // The last argument is a player number to read cvars from; as for convenience, cvars will be set directly when making a profile (since loading another one will overwrite them, this will be inconsequential) -profile_t PR_MakeProfileFromPlayer(const char *prname, const char *pname, const char *sname, const UINT16 col, const char *fname, UINT16 fcol, UINT8 pnum); +profile_t* PR_MakeProfileFromPlayer(const char *prname, const char *pname, const char *sname, const UINT16 col, const char *fname, UINT16 fcol, UINT8 pnum); // PR_AddProfile(profile_t p) // Adds a profile to profilesList and increments numprofiles. // Returns true if succesful, false if not. -boolean PR_AddProfile(profile_t p); +boolean PR_AddProfile(profile_t *p); // PR_GetProfile(INT32 num) // Returns a pointer to the profile you're asking for or NULL if the profile is uninitialized. @@ -110,4 +110,4 @@ void PR_ApplyProfile(UINT8 profilenum, UINT8 playernum); -#endif \ No newline at end of file +#endif