From b0c9fccd2d6c5b05abeb773a0dd4c80f42661a25 Mon Sep 17 00:00:00 2001 From: toaster Date: Wed, 31 Aug 2022 22:52:06 +0100 Subject: [PATCH] Rework profile deletion - O(n) instead of O(4n) - Actually free the sacrificed profile's memory - Don't hop to profile 1 after deleting a later profile, to reduce the % of footguns/losing your place in the list. --- src/k_menufunc.c | 4 ++-- src/k_profiles.c | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/k_menufunc.c b/src/k_menufunc.c index 7bd0cee22..b37825586 100644 --- a/src/k_menufunc.c +++ b/src/k_menufunc.c @@ -5518,8 +5518,8 @@ static void M_EraseProfileResponse(INT32 choice) F_StartIntro(); M_ClearMenus(true); } - else - optionsmenu.eraseprofilen = 1; + else if (optionsmenu.eraseprofilen > PR_GetNumProfiles()-1) + optionsmenu.eraseprofilen--; } } diff --git a/src/k_profiles.c b/src/k_profiles.c index 482f748f3..19c39d502 100644 --- a/src/k_profiles.c +++ b/src/k_profiles.c @@ -96,34 +96,47 @@ profile_t* PR_GetProfile(INT32 num) boolean PR_DeleteProfile(INT32 num) { - UINT8 i, j; + UINT8 i; + profile_t* sacrifice; if (num <= 0 || num > numprofiles) { return false; } + sacrifice = profilesList[num]; + // If we're deleting inbetween profiles, move everything. if (num < numprofiles) { for (i = num; i < numprofiles-1; i++) { profilesList[i] = profilesList[i+1]; + } - // Make sure to move cv_lastprofile values as well - for (j = 0; j < MAXSPLITSCREENPLAYERS; j++) + // Make sure to move cv_lastprofile values as well! + for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + INT32 profileset = cv_lastprofile[i].value; + + if (profileset < num) { - if (cv_lastprofile[j].value == num) - { - // If we were on the deleted profile, default back to guest. - CV_StealthSetValue(&cv_lastprofile[j], PROFILE_GUEST); - } - else if (cv_lastprofile[j].value == i+1) - { - // Otherwise, shift our lastprofile number down to match the new order. - CV_StealthSetValue(&cv_lastprofile[j], cv_lastprofile[j].value-1); - } + // Not affected. + continue; } + + if (profileset > num) + { + // Shift our lastprofile number down to match the new order. + profileset--; + } + else + { + // There's no hope for it. If we were on the deleted profile, default back to guest. + profileset = PROFILE_GUEST; + } + + CV_StealthSetValue(&cv_lastprofile[i], profileset); } } @@ -132,6 +145,10 @@ boolean PR_DeleteProfile(INT32 num) numprofiles--; PR_SaveProfiles(); + + // Finally, clear up our memory! + Z_Free(sacrifice); + return true; }