From 3b9959a70749cc6bd8d4cf734d00d7fcda212011 Mon Sep 17 00:00:00 2001 From: toaster Date: Sun, 25 Sep 2022 22:47:22 +0100 Subject: [PATCH] Replay hut code smell fix - Properly account for non-loaded maps (missed in big-large-map-markers) - Don't have a million copypasted extrasmenu.demolist indexarooies - Make one bad replay handler in `G_LoadDemoInfo(` instead of fifty copypastes (Hmn I am smelling a THEME) --- src/g_demo.c | 27 ++++++--------- src/k_menu.h | 1 - src/k_menudraw.c | 88 +++++++++++++++++++++++------------------------- 3 files changed, 54 insertions(+), 62 deletions(-) diff --git a/src/g_demo.c b/src/g_demo.c index 09e5a9bdd..bc1f86cc5 100644 --- a/src/g_demo.c +++ b/src/g_demo.c @@ -2553,10 +2553,8 @@ void G_LoadDemoInfo(menudemo_t *pdemo) if (!FIL_ReadFile(pdemo->filepath, &infobuffer)) { CONS_Alert(CONS_ERROR, M_GetText("Failed to read file '%s'.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - - return; + infobuffer = NULL; + goto badreplay; } info_p = infobuffer; @@ -2564,10 +2562,7 @@ void G_LoadDemoInfo(menudemo_t *pdemo) if (memcmp(info_p, DEMOHEADER, 12)) { CONS_Alert(CONS_ERROR, M_GetText("%s is not a Ring Racers replay file.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - Z_Free(infobuffer); - return; + goto badreplay; } pdemo->type = MD_LOADED; @@ -2589,10 +2584,7 @@ void G_LoadDemoInfo(menudemo_t *pdemo) // too old, cannot support. default: CONS_Alert(CONS_ERROR, M_GetText("%s is an incompatible replay format and cannot be played.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - Z_Free(infobuffer); - return; + goto badreplay; } if (version != VERSION || subversion != SUBVERSION) @@ -2602,10 +2594,7 @@ void G_LoadDemoInfo(menudemo_t *pdemo) if (memcmp(info_p, "PLAY", 4)) { CONS_Alert(CONS_ERROR, M_GetText("%s is the wrong type of recording and cannot be played.\n"), pdemo->filepath); - pdemo->type = MD_INVALID; - sprintf(pdemo->title, "INVALID REPLAY"); - Z_Free(infobuffer); - return; + goto badreplay; } info_p += 4; // "PLAY" READSTRINGN(info_p, mapname, sizeof(mapname)); @@ -2704,6 +2693,12 @@ void G_LoadDemoInfo(menudemo_t *pdemo) // I think that's everything we need? Z_Free(infobuffer); + return; + +badreplay: + pdemo->type = MD_INVALID; + sprintf(pdemo->title, "INVALID REPLAY"); + Z_Free(infobuffer); } // diff --git a/src/k_menu.h b/src/k_menu.h index a55756788..234882446 100644 --- a/src/k_menu.h +++ b/src/k_menu.h @@ -1062,7 +1062,6 @@ void M_DrawExtrasMovingButton(void); void M_DrawExtras(void); void M_DrawReplayHut(void); void M_DrawReplayStartMenu(void); -void M_DrawReplayHutReplayInfo(void); // Misc menus: #define LOCATIONSTRING1 "Visit \x83SRB2.ORG/MODS\x80 to get & make addons!" diff --git a/src/k_menudraw.c b/src/k_menudraw.c index 9e18e181f..b83728ed3 100644 --- a/src/k_menudraw.c +++ b/src/k_menudraw.c @@ -3807,13 +3807,13 @@ void M_DrawPlaybackMenu(void) #define SCALEDVIEWWIDTH (vid.width/vid.dupx) #define SCALEDVIEWHEIGHT (vid.height/vid.dupy) -void M_DrawReplayHutReplayInfo(void) +static void M_DrawReplayHutReplayInfo(menudemo_t *demoref) { patch_t *patch = NULL; UINT8 *colormap; INT32 x, y, w, h; - switch (extrasmenu.demolist[dir_on[menudepthleft]].type) + switch (demoref->type) { case MD_NOTLOADED: V_DrawCenteredString(160, 40, V_SNAPTOTOP, "Loading replay information..."); @@ -3834,22 +3834,20 @@ void M_DrawReplayHutReplayInfo(void) x = 15; y = 15; // A 160x100 image of the level as entry MAPxxP - //CONS_Printf("%d %s\n", extrasmenu.demolist[dir_on[menudepthleft]].map, G_BuildMapName(extrasmenu.demolist[dir_on[menudepthleft]].map)); - - if (mapheaderinfo[extrasmenu.demolist[dir_on[menudepthleft]].map]) + if (demoref->map < nummapheaders && mapheaderinfo[demoref->map]) { - patch = mapheaderinfo[extrasmenu.demolist[dir_on[menudepthleft]].map]->thumbnailPic; + patch = mapheaderinfo[demoref->map]->thumbnailPic; if (!patch) { patch = blanklvl; } } - else if (!patch) + else { patch = W_CachePatchName("M_NOLVL", PU_CACHE); } - if (!(extrasmenu.demolist[dir_on[menudepthleft]].kartspeed & DF_ENCORE)) + if (!(demoref->kartspeed & DF_ENCORE)) V_DrawSmallScaledPatch(x, y, V_SNAPTOTOP, patch); else { @@ -3867,43 +3865,42 @@ void M_DrawReplayHutReplayInfo(void) x += 85; - if (mapheaderinfo[extrasmenu.demolist[dir_on[menudepthleft]].map-1]) - V_DrawString(x, y, V_SNAPTOTOP, G_BuildMapTitle(extrasmenu.demolist[dir_on[menudepthleft]].map)); + if (demoref->map < nummapheaders && mapheaderinfo[demoref->map]) + V_DrawString(x, y, V_SNAPTOTOP, G_BuildMapTitle(demoref->map+1)); else V_DrawString(x, y, V_SNAPTOTOP|V_ALLOWLOWERCASE|V_TRANSLUCENT, "Level is not loaded."); - if (extrasmenu.demolist[dir_on[menudepthleft]].numlaps) - V_DrawThinString(x, y+9, V_SNAPTOTOP|V_ALLOWLOWERCASE, va("(%d laps)", extrasmenu.demolist[dir_on[menudepthleft]].numlaps)); + if (demoref->numlaps) + V_DrawThinString(x, y+9, V_SNAPTOTOP|V_ALLOWLOWERCASE, va("(%d laps)", demoref->numlaps)); - V_DrawString(x, y+20, V_SNAPTOTOP|V_ALLOWLOWERCASE, extrasmenu.demolist[dir_on[menudepthleft]].gametype == GT_RACE ? - va("Race (%s speed)", kartspeed_cons_t[(extrasmenu.demolist[dir_on[menudepthleft]].kartspeed & ~DF_ENCORE) + 1].strvalue) : + V_DrawString(x, y+20, V_SNAPTOTOP|V_ALLOWLOWERCASE, demoref->gametype == GT_RACE ? + va("Race (%s speed)", kartspeed_cons_t[(demoref->kartspeed & ~DF_ENCORE) + 1].strvalue) : "Battle Mode"); - if (!extrasmenu.demolist[dir_on[menudepthleft]].standings[0].ranking) + if (!demoref->standings[0].ranking) { // No standings were loaded! V_DrawString(x, y+39, V_SNAPTOTOP|V_ALLOWLOWERCASE|V_TRANSLUCENT, "No standings available."); - break; } V_DrawThinString(x, y+29, V_SNAPTOTOP|highlightflags, "WINNER"); - V_DrawString(x+38, y+30, V_SNAPTOTOP|V_ALLOWLOWERCASE, extrasmenu.demolist[dir_on[menudepthleft]].standings[0].name); + V_DrawString(x+38, y+30, V_SNAPTOTOP|V_ALLOWLOWERCASE, demoref->standings[0].name); - if (extrasmenu.demolist[dir_on[menudepthleft]].gametype == GT_RACE) + if (demoref->gametype == GT_RACE) { V_DrawThinString(x, y+39, V_SNAPTOTOP|highlightflags, "TIME"); V_DrawRightAlignedString(x+84, y+40, V_SNAPTOTOP, va("%d'%02d\"%02d", - G_TicsToMinutes(extrasmenu.demolist[dir_on[menudepthleft]].standings[0].timeorscore, true), - G_TicsToSeconds(extrasmenu.demolist[dir_on[menudepthleft]].standings[0].timeorscore), - G_TicsToCentiseconds(extrasmenu.demolist[dir_on[menudepthleft]].standings[0].timeorscore) + G_TicsToMinutes(demoref->standings[0].timeorscore, true), + G_TicsToSeconds(demoref->standings[0].timeorscore), + G_TicsToCentiseconds(demoref->standings[0].timeorscore) )); } else { V_DrawThinString(x, y+39, V_SNAPTOTOP|highlightflags, "SCORE"); - V_DrawString(x+32, y+40, V_SNAPTOTOP, va("%d", extrasmenu.demolist[dir_on[menudepthleft]].standings[0].timeorscore)); + V_DrawString(x+32, y+40, V_SNAPTOTOP, va("%d", demoref->standings[0].timeorscore)); } // Character face! @@ -3911,12 +3908,12 @@ void M_DrawReplayHutReplayInfo(void) // Lat: 08/06/2020: For some reason missing skins have their value set to 255 (don't even ask me why I didn't write this) // and for an even STRANGER reason this passes the first check below, so we're going to make sure that the skin here ISN'T 255 before we do anything stupid. - if (extrasmenu.demolist[dir_on[menudepthleft]].standings[0].skin != 0xFF) + if (demoref->standings[0].skin != 0xFF) { - patch = faceprefix[extrasmenu.demolist[dir_on[menudepthleft]].standings[0].skin][FACE_WANTED]; + patch = faceprefix[demoref->standings[0].skin][FACE_WANTED]; colormap = R_GetTranslationColormap( - extrasmenu.demolist[dir_on[menudepthleft]].standings[0].skin, - extrasmenu.demolist[dir_on[menudepthleft]].standings[0].color, + demoref->standings[0].skin, + demoref->standings[0].color, GTC_MENUCACHE); } else @@ -3924,7 +3921,7 @@ void M_DrawReplayHutReplayInfo(void) patch = W_CachePatchName("M_NOWANT", PU_CACHE); colormap = R_GetTranslationColormap( TC_RAINBOW, - extrasmenu.demolist[dir_on[menudepthleft]].standings[0].color, + demoref->standings[0].color, GTC_MENUCACHE); } @@ -4073,7 +4070,7 @@ void M_DrawReplayHut(void) if (itemOn == replaylistitem) { - M_DrawReplayHutReplayInfo(); + M_DrawReplayHutReplayInfo(&extrasmenu.demolist[dir_on[menudepthleft]]); } } @@ -4081,42 +4078,43 @@ void M_DrawReplayStartMenu(void) { const char *warning; UINT8 i; + menudemo_t *demoref = &extrasmenu.demolist[dir_on[menudepthleft]]; M_DrawEggaChannel(); M_DrawGenericMenu(); #define STARTY 62-(extrasmenu.replayScrollTitle>>1) // Draw rankings beyond first - for (i = 1; i < MAXPLAYERS && extrasmenu.demolist[dir_on[menudepthleft]].standings[i].ranking; i++) + for (i = 1; i < MAXPLAYERS && demoref->standings[i].ranking; i++) { patch_t *patch; UINT8 *colormap; - V_DrawRightAlignedString(BASEVIDWIDTH-100, STARTY + i*20, V_SNAPTOTOP|highlightflags, va("%2d", extrasmenu.demolist[dir_on[menudepthleft]].standings[i].ranking)); - V_DrawThinString(BASEVIDWIDTH-96, STARTY + i*20, V_SNAPTOTOP|V_ALLOWLOWERCASE, extrasmenu.demolist[dir_on[menudepthleft]].standings[i].name); + V_DrawRightAlignedString(BASEVIDWIDTH-100, STARTY + i*20, V_SNAPTOTOP|highlightflags, va("%2d", demoref->standings[i].ranking)); + V_DrawThinString(BASEVIDWIDTH-96, STARTY + i*20, V_SNAPTOTOP|V_ALLOWLOWERCASE, demoref->standings[i].name); - if (extrasmenu.demolist[dir_on[menudepthleft]].standings[i].timeorscore == UINT32_MAX-1) + if (demoref->standings[i].timeorscore == UINT32_MAX-1) V_DrawThinString(BASEVIDWIDTH-92, STARTY + i*20 + 9, V_SNAPTOTOP, "NO CONTEST"); - else if (extrasmenu.demolist[dir_on[menudepthleft]].gametype == GT_RACE) + else if (demoref->gametype == GT_RACE) V_DrawRightAlignedString(BASEVIDWIDTH-40, STARTY + i*20 + 9, V_SNAPTOTOP, va("%d'%02d\"%02d", - G_TicsToMinutes(extrasmenu.demolist[dir_on[menudepthleft]].standings[i].timeorscore, true), - G_TicsToSeconds(extrasmenu.demolist[dir_on[menudepthleft]].standings[i].timeorscore), - G_TicsToCentiseconds(extrasmenu.demolist[dir_on[menudepthleft]].standings[i].timeorscore) + G_TicsToMinutes(demoref->standings[i].timeorscore, true), + G_TicsToSeconds(demoref->standings[i].timeorscore), + G_TicsToCentiseconds(demoref->standings[i].timeorscore) )); else - V_DrawString(BASEVIDWIDTH-92, STARTY + i*20 + 9, V_SNAPTOTOP, va("%d", extrasmenu.demolist[dir_on[menudepthleft]].standings[i].timeorscore)); + V_DrawString(BASEVIDWIDTH-92, STARTY + i*20 + 9, V_SNAPTOTOP, va("%d", demoref->standings[i].timeorscore)); // Character face! // Lat: 08/06/2020: For some reason missing skins have their value set to 255 (don't even ask me why I didn't write this) // and for an even STRANGER reason this passes the first check below, so we're going to make sure that the skin here ISN'T 255 before we do anything stupid. - if (extrasmenu.demolist[dir_on[menudepthleft]].standings[i].skin != 0xFF) + if (demoref->standings[i].skin != 0xFF) { - patch = faceprefix[extrasmenu.demolist[dir_on[menudepthleft]].standings[i].skin][FACE_RANK]; + patch = faceprefix[demoref->standings[i].skin][FACE_RANK]; colormap = R_GetTranslationColormap( - extrasmenu.demolist[dir_on[menudepthleft]].standings[i].skin, - extrasmenu.demolist[dir_on[menudepthleft]].standings[i].color, + demoref->standings[i].skin, + demoref->standings[i].color, GTC_MENUCACHE); } else @@ -4124,7 +4122,7 @@ void M_DrawReplayStartMenu(void) patch = W_CachePatchName("M_NORANK", PU_CACHE); colormap = R_GetTranslationColormap( TC_RAINBOW, - extrasmenu.demolist[dir_on[menudepthleft]].standings[i].color, + demoref->standings[i].color, GTC_MENUCACHE); } @@ -4157,12 +4155,12 @@ void M_DrawReplayStartMenu(void) } V_DrawFill(10, 10, 300, 60, V_SNAPTOTOP|159); - M_DrawReplayHutReplayInfo(); + M_DrawReplayHutReplayInfo(demoref); - V_DrawString(10, 72, V_SNAPTOTOP|highlightflags|V_ALLOWLOWERCASE, extrasmenu.demolist[dir_on[menudepthleft]].title); + V_DrawString(10, 72, V_SNAPTOTOP|highlightflags|V_ALLOWLOWERCASE, demoref->title); // Draw a warning prompt if needed - switch (extrasmenu.demolist[dir_on[menudepthleft]].addonstatus) + switch (demoref->addonstatus) { case DFILE_ERROR_CANNOTLOAD: warning = "Some addons in this replay cannot be loaded.\nYou can watch anyway, but desyncs may occur.";