Refactor profiles code to use pointers

This commit is contained in:
James R 2022-02-19 14:25:34 -08:00
parent 446fe4ce32
commit c83803dd42
2 changed files with 47 additions and 42 deletions

View file

@ -11,9 +11,10 @@
/// \brief implements methods for profiles etc. /// \brief implements methods for profiles etc.
#include "k_profiles.h" #include "k_profiles.h"
#include "z_zone.h"
// List of all the profiles. // 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 static UINT8 numprofiles = 0; // # of loaded profiles
INT32 PR_GetNumProfiles(void) INT32 PR_GetNumProfiles(void)
@ -21,46 +22,46 @@ INT32 PR_GetNumProfiles(void)
return numprofiles; 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->skinname, sname);
strcpy(new.playername, pname); strcpy(new->playername, pname);
new.color = col; new->color = col;
strcpy(new.follower, fname); strcpy(new->follower, fname);
new.followercolor = fcol; new->followercolor = fcol;
// Copy from gamecontrol directly as we'll be setting controls up directly in the profile. // 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; 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. // 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: // Player bound cvars:
new.kickstartaccel = cv_kickstartaccel[pnum].value; new->kickstartaccel = cv_kickstartaccel[pnum].value;
return new; 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++; numprofiles++;
CONS_Printf("Profile '%s' added\n", p.profilename); CONS_Printf("Profile '%s' added\n", p->profilename);
return true; return true;
} }
@ -70,8 +71,8 @@ boolean PR_AddProfile(profile_t p)
profile_t* PR_GetProfile(INT32 num) profile_t* PR_GetProfile(INT32 num)
{ {
if (num < MAXPROFILES+1) if (num < numprofiles)
return &profilesList[num]; return profilesList[num];
else else
return NULL; return NULL;
} }
@ -79,7 +80,7 @@ profile_t* PR_GetProfile(INT32 num)
void PR_InitNewProfile(void) void PR_InitNewProfile(void)
{ {
char pname[PROFILENAMELEN+1] = "PRF"; char pname[PROFILENAMELEN+1] = "PRF";
profile_t dprofile; profile_t *dprofile;
strcpy(pname, va("PRF%c", 'A'+numprofiles-1)); strcpy(pname, va("PRF%c", 'A'+numprofiles-1));
@ -94,7 +95,15 @@ void PR_SaveProfiles(void)
f = fopen(PROFILESFILE, "w"); f = fopen(PROFILESFILE, "w");
if (f != NULL) 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); fclose(f);
} }
else else
@ -104,29 +113,25 @@ void PR_SaveProfiles(void)
void PR_LoadProfiles(void) void PR_LoadProfiles(void)
{ {
FILE *f = NULL; 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"); f = fopen(PROFILESFILE, "r");
if (f != NULL) if (f != NULL)
{ {
INT32 i; 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); fclose(f);
// Overwrite the first profile for the default profile to avoid letting anyone tamper with it. // Overwrite the first profile for the default profile to avoid letting anyone tamper with it.
memcpy(&profilesList[0], &dprofile, sizeof(profile_t)); profilesList[0] = dprofile;
// 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;
}
}
} }
else else
{ {
@ -146,4 +151,4 @@ void PR_ApplyProfile(UINT8 profilenum, UINT8 playernum)
// set controls... // set controls...
memcpy(&gamecontrol[playernum], p->controls, sizeof(gamecontroldefault)); memcpy(&gamecontrol[playernum], p->controls, sizeof(gamecontroldefault));
} }

View file

@ -74,17 +74,17 @@ INT32 PR_GetNumProfiles(void);
// PR_MakeProfile // PR_MakeProfile
// Makes a profile from the supplied profile name, player name, colour, follower, followercolour and controls. // Makes a profile from the supplied profile name, player name, colour, follower, followercolour and controls.
// The consvar values are left untouched. // 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 // PR_MakeProfileFromPlayer
// Makes a profile_t from the supplied profile name, player name, colour, follower and followercolour. // 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) // 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) // PR_AddProfile(profile_t p)
// Adds a profile to profilesList and increments numprofiles. // Adds a profile to profilesList and increments numprofiles.
// Returns true if succesful, false if not. // Returns true if succesful, false if not.
boolean PR_AddProfile(profile_t p); boolean PR_AddProfile(profile_t *p);
// PR_GetProfile(INT32 num) // PR_GetProfile(INT32 num)
// Returns a pointer to the profile you're asking for or NULL if the profile is uninitialized. // 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 #endif