From d25c6fa0b261a877d1532767838493763294893e Mon Sep 17 00:00:00 2001 From: Indev Date: Thu, 8 Aug 2024 16:51:39 +0300 Subject: [PATCH 01/10] Test replay search --- src/filesrch.c | 5 +++ src/filesrch.h | 1 + src/m_menu.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/filesrch.c b/src/filesrch.c index 3a3a576ce..623dbcaa2 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -326,6 +326,7 @@ consvar_t cv_addons_search_type = {"addons_search_type", "Anywhere", CV_SAVE, ad char menupath[1024]; size_t menupathindex[menudepth]; size_t menudepthleft = menudepth; +char *replayhutquery = NULL; char menusearch[MAXSTRINGLENGTH+1]; @@ -691,6 +692,8 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (replayhut) { if (strcasecmp(".lmp", dent->d_name+len-5)) continue; // Not a replay + if (replayhutquery != NULL && strcasestr(dent->d_name, replayhutquery) == NULL) + continue; } else if (!cv_addons_showall.value) { @@ -765,6 +768,8 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (replayhut) { if (strcasecmp(".lmp", dent->d_name+len-5)) continue; // Not a replay + if (replayhutquery != NULL && strcasestr(dent->d_name, replayhutquery) == NULL) + continue; ext = EXT_TXT; // This isn't used anywhere but better safe than sorry for messing with this... } else diff --git a/src/filesrch.h b/src/filesrch.h index dd0fb404a..9bf4caa24 100644 --- a/src/filesrch.h +++ b/src/filesrch.h @@ -33,6 +33,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want extern char menupath[1024]; extern size_t menupathindex[menudepth]; extern size_t menudepthleft; +extern char *replayhutquery; extern char menusearch[MAXSTRINGLENGTH+1]; diff --git a/src/m_menu.c b/src/m_menu.c index baf13e6ec..e864ad492 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6711,6 +6711,11 @@ static void M_HandleAddons(INT32 choice) // ---- REPLAY HUT ----- menudemo_t *demolist; +#define MAXREPLAYQUERY 30 +char replayqueryinput[MAXREPLAYQUERY+1]; +size_t replayquerypos = 0; +boolean replayqueryopen = false; + #define DF_ENCORE 0x40 static INT16 replayScrollTitle = 0; static SINT8 replayScrollDelay = TICRATE, replayScrollDir = 1; @@ -6756,6 +6761,13 @@ void M_ReplayHut(INT32 choice) snprintf(menupath, 1024, "%s"PATHSEP"replay"PATHSEP"online"PATHSEP, srb2home); menupathindex[(menudepthleft = menudepth-1)] = strlen(menupath); } + + // Reset + replayhutquery = NULL; + memset(replayqueryinput, 0, 30); + replayquerypos = 0; + replayqueryopen = false; + if (!preparefilemenu(false, true)) { M_StartMessage("No replays found.\n\n(Press a key)\n", NULL, MM_NOTHING); @@ -6779,10 +6791,71 @@ void M_ReplayHut(INT32 choice) S_ChangeMusicInternal("replst", true); } -static void M_HandleReplayHutList(INT32 choice) +static boolean M_HandleReplayHutQuery(INT32 choice) { + if (!replayqueryopen) + return false; + switch (choice) { + case KEY_ENTER: + case KEY_ESCAPE: + replayqueryopen = false; + break; + + case KEY_BACKSPACE: + if (replayquerypos == 0) + break; + + S_StartSound(NULL,sfx_menu1); + + replayquerypos--; + replayqueryinput[replayquerypos] = 0; + + if (!replayquerypos) + replayhutquery = NULL; + + preparefilemenu(false, true); + dir_on[menudepthleft] = 0; + PrepReplayList(); + + break; + + default: + if (choice < 32 || choice > 127) + break; + + if (replayquerypos < MAXREPLAYQUERY) + { + S_StartSound(NULL,sfx_menu1); + + replayqueryinput[replayquerypos] = choice; + replayqueryinput[replayquerypos+1] = 0; + ++replayquerypos; + + replayhutquery = replayqueryinput; + + preparefilemenu(false, true); + dir_on[menudepthleft] = 0; + PrepReplayList(); + } + } + + return true; +} + +static void M_HandleReplayHutList(INT32 choice) +{ + if (M_HandleReplayHutQuery(choice)) + return; + + switch (choice) + { + case 'f': + case 'F': + replayqueryopen = true; + break; + case KEY_UPARROW: if (dir_on[menudepthleft]) dir_on[menudepthleft]--; @@ -7179,6 +7252,17 @@ static void M_DrawReplayHut(void) { DrawReplayHutReplayInfo(); } + + // Draw search query + if (replayqueryopen || replayquerypos) + { + M_DrawTextBox(8, 200 - 16, MAXREPLAYQUERY, 1); + V_DrawString(16, 200 - 8, V_ALLOWLOWERCASE, replayqueryinput); + + // draw text cursor for name + if (replayqueryopen && skullAnimCounter < 4) // blink cursor + V_DrawCharacter(16 + V_StringWidth(replayqueryinput, V_ALLOWLOWERCASE), 200 - 8, '_', false); + } } static void M_DrawReplayStartMenu(void) From 0bd2c49dae43bac5bc62bad3b430e2eece393610 Mon Sep 17 00:00:00 2001 From: Indev Date: Thu, 8 Aug 2024 18:43:03 +0300 Subject: [PATCH 02/10] Search replays using title instead of filename --- src/d_netcmd.c | 1 + src/filesrch.c | 5 -- src/filesrch.h | 1 - src/m_menu.c | 132 +++++++++++++++++++++++++++++++++++++++---------- src/m_menu.h | 1 + 5 files changed, 108 insertions(+), 32 deletions(-) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 140bc7076..2c0d9ebf3 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -999,6 +999,7 @@ void D_RegisterClientCommands(void) //CV_RegisterVar(&cv_alwaysfreelook2); //CV_RegisterVar(&cv_chasefreelook); //CV_RegisterVar(&cv_chasefreelook2); + CV_RegisterVar(&cv_replaysearchrate); CV_RegisterVar(&cv_showfocuslost); CV_RegisterVar(&cv_pauseifunfocused); diff --git a/src/filesrch.c b/src/filesrch.c index 623dbcaa2..3a3a576ce 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -326,7 +326,6 @@ consvar_t cv_addons_search_type = {"addons_search_type", "Anywhere", CV_SAVE, ad char menupath[1024]; size_t menupathindex[menudepth]; size_t menudepthleft = menudepth; -char *replayhutquery = NULL; char menusearch[MAXSTRINGLENGTH+1]; @@ -692,8 +691,6 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (replayhut) { if (strcasecmp(".lmp", dent->d_name+len-5)) continue; // Not a replay - if (replayhutquery != NULL && strcasestr(dent->d_name, replayhutquery) == NULL) - continue; } else if (!cv_addons_showall.value) { @@ -768,8 +765,6 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) if (replayhut) { if (strcasecmp(".lmp", dent->d_name+len-5)) continue; // Not a replay - if (replayhutquery != NULL && strcasestr(dent->d_name, replayhutquery) == NULL) - continue; ext = EXT_TXT; // This isn't used anywhere but better safe than sorry for messing with this... } else diff --git a/src/filesrch.h b/src/filesrch.h index 9bf4caa24..dd0fb404a 100644 --- a/src/filesrch.h +++ b/src/filesrch.h @@ -33,7 +33,6 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want extern char menupath[1024]; extern size_t menupathindex[menudepth]; extern size_t menudepthleft; -extern char *replayhutquery; extern char menusearch[MAXSTRINGLENGTH+1]; diff --git a/src/m_menu.c b/src/m_menu.c index e864ad492..d02a809ee 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -386,6 +386,7 @@ menu_t OP_DriftGaugeDef; menu_t MISC_ReplayHutDef; menu_t MISC_ReplayOptionsDef; static void M_HandleReplayHutList(INT32 choice); +static void M_HutCheckReplays(size_t maxnum); static void M_DrawReplayHut(void); static void M_DrawReplayStartMenu(void); static boolean M_QuitReplayHut(void); @@ -464,6 +465,9 @@ static void Dummystaff_OnChange(void); // CONSOLE VARIABLES AND THEIR POSSIBLE VALUES GO HERE. // ========================================================================== +// How much replays are checked per frame when using search +consvar_t cv_replaysearchrate = {"replaysearchrate", "50", CV_SAVE, CV_Natural, NULL, 0, NULL, NULL, 0, 0, NULL }; + consvar_t cv_showfocuslost = {"showfocuslost", "Yes", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL }; static CV_PossibleValue_t map_cons_t[] = { @@ -619,6 +623,7 @@ static menuitem_t MISC_ReplayOptionsMenu[] = {IT_CVAR|IT_STRING, NULL, "Record Replays", &cv_recordmultiplayerdemos, 0}, {IT_CVAR|IT_STRING, NULL, "Sync Check Interval", &cv_netdemosyncquality, 10}, {IT_CVAR|IT_STRING, NULL, "Max demo size (MiB)", &cv_maxdemosize, 20}, + {IT_CVAR|IT_STRING, NULL, "Replay Search Rate", &cv_replaysearchrate, 30}, }; static tic_t playback_last_menu_interaction_leveltime = 0; @@ -4430,6 +4435,9 @@ void M_Ticker(void) else playback_enterheld = 0; + if (currentMenu == &MISC_ReplayHutDef) + M_HutCheckReplays(cv_replaysearchrate.value); + interpTimerHackAllow = true; //added : 30-01-98 : test mode for five seconds @@ -6709,45 +6717,122 @@ static void M_HandleAddons(INT32 choice) } // ---- REPLAY HUT ----- -menudemo_t *demolist; +menudemo_t *demolist; // Replays that that have been checked to match with query +menudemo_t *demolist_all; // All replays #define MAXREPLAYQUERY 30 -char replayqueryinput[MAXREPLAYQUERY+1]; -size_t replayquerypos = 0; -boolean replayqueryopen = false; +char replayqueryinput[MAXREPLAYQUERY+1]; // The input typed +size_t replayquerypos = 0; // Position in input window +boolean replayqueryopen = false; // Is query window open? + +size_t replayqueryfound = 0; // Number of checked replay entries +size_t replayquerycheck = 0; // Index of next replay entry to check in demolist_all #define DF_ENCORE 0x40 static INT16 replayScrollTitle = 0; static SINT8 replayScrollDelay = TICRATE, replayScrollDir = 1; +static void ResetReplayQuery(void) +{ + memset(replayqueryinput, 0, MAXREPLAYQUERY); + replayquerypos = 0; + replayqueryopen = false; + + replayqueryfound = 0; + replayquerycheck = 0; +} + +static void AddCheckedReplay(void) +{ + memcpy(&demolist[replayqueryfound], &demolist_all[replayquerycheck], sizeof(menudemo_t)); + ++replayqueryfound; + ++replayquerycheck; +} + +// Check up to maxnum replays if they match with query +static void M_HutCheckReplays(size_t maxnum) +{ + // Already checked everything + if (replayquerycheck == sizedirmenu) + return; + + // If we don't have any query, just copy all demos and consider them checked + if (replayquerypos == 0) + { + // Mark as done + replayqueryfound = replayquerycheck = sizedirmenu; + + memcpy(demolist, demolist_all, sizeof(menudemo_t) * sizedirmenu); + + return; + } + + while (maxnum-- && replayquerycheck < sizedirmenu) + { + switch (demolist_all[replayquerycheck].type) + { + case MD_SUBDIR: + AddCheckedReplay(); // Just add subdirs + // Will also be decremented on next iteration. Basically, we just don't want to + // spend our number of tries on subdirs, since we don't check anything for them + ++maxnum; + break; + + case MD_NOTLOADED: + G_LoadDemoInfo(&demolist_all[replayquerycheck]); + ++maxnum; // Gonna check this one next iteration, don't decrease our number of tries yet + break; + + case MD_OUTDATED: + case MD_LOADED: + if (strcasestr(demolist_all[replayquerycheck].title, replayqueryinput) != NULL) + AddCheckedReplay(); // It matches, add it! + else + replayquerycheck++; // Doesn't match, moving on... + + break; + + // Don't show invalid replays + case MD_INVALID: + replayquerycheck++; + } + } +} + static void PrepReplayList(void) { size_t i; + replayquerycheck = replayqueryfound = 0; + if (demolist) Z_Free(demolist); + if (demolist_all) + Z_Free(demolist_all); + demolist = Z_Calloc(sizeof(menudemo_t) * sizedirmenu, PU_STATIC, NULL); + demolist_all = Z_Calloc(sizeof(menudemo_t) * sizedirmenu, PU_STATIC, NULL); for (i = 0; i < sizedirmenu; i++) { if (dirmenu[i][DIR_TYPE] == EXT_UP) { - demolist[i].type = MD_SUBDIR; - sprintf(demolist[i].title, "UP"); + demolist_all[i].type = MD_SUBDIR; + sprintf(demolist_all[i].title, "UP"); } else if (dirmenu[i][DIR_TYPE] == EXT_FOLDER) { - demolist[i].type = MD_SUBDIR; - strncpy(demolist[i].title, dirmenu[i] + DIR_STRING, 64); + demolist_all[i].type = MD_SUBDIR; + strncpy(demolist_all[i].title, dirmenu[i] + DIR_STRING, 64); } else { - demolist[i].type = MD_NOTLOADED; + demolist_all[i].type = MD_NOTLOADED; // FIXME - do something with buffer sizes. menupath is 1024 chars but filepath is only // 256. I'm not really sure what to do here but don't want to leave warnings... - snprintf(demolist[i].filepath, 255, "%.254s%s", menupath, dirmenu[i] + DIR_STRING); - sprintf(demolist[i].title, "....."); + snprintf(demolist_all[i].filepath, 255, "%.254s%s", menupath, dirmenu[i] + DIR_STRING); + sprintf(demolist_all[i].title, "....."); } } } @@ -6762,11 +6847,7 @@ void M_ReplayHut(INT32 choice) menupathindex[(menudepthleft = menudepth-1)] = strlen(menupath); } - // Reset - replayhutquery = NULL; - memset(replayqueryinput, 0, 30); - replayquerypos = 0; - replayqueryopen = false; + ResetReplayQuery(); if (!preparefilemenu(false, true)) { @@ -6812,9 +6893,6 @@ static boolean M_HandleReplayHutQuery(INT32 choice) replayquerypos--; replayqueryinput[replayquerypos] = 0; - if (!replayquerypos) - replayhutquery = NULL; - preparefilemenu(false, true); dir_on[menudepthleft] = 0; PrepReplayList(); @@ -6833,8 +6911,6 @@ static boolean M_HandleReplayHutQuery(INT32 choice) replayqueryinput[replayquerypos+1] = 0; ++replayquerypos; - replayhutquery = replayqueryinput; - preparefilemenu(false, true); dir_on[menudepthleft] = 0; PrepReplayList(); @@ -6868,7 +6944,7 @@ static void M_HandleReplayHutList(INT32 choice) break; case KEY_DOWNARROW: - if (dir_on[menudepthleft] < sizedirmenu-1) + if (dir_on[menudepthleft] < replayqueryfound-1) dir_on[menudepthleft]++; else return; @@ -6908,6 +6984,7 @@ static void M_HandleReplayHutList(INT32 choice) { S_StartSound(NULL, sfx_menu1); dir_on[menudepthleft] = 1; + ResetReplayQuery(); PrepReplayList(); } } @@ -7127,7 +7204,7 @@ static void M_DrawReplayHut(void) if (itemOn > replaylistitem) { itemOn = replaylistitem; - dir_on[menudepthleft] = sizedirmenu-1; + dir_on[menudepthleft] = replayqueryfound-1; replayScrollTitle = 0; replayScrollDelay = TICRATE; replayScrollDir = 1; } else if (itemOn < replaylistitem) @@ -7141,7 +7218,7 @@ static void M_DrawReplayHut(void) INT32 maxy; // Scroll menu items if needed cursory = y + currentMenu->menuitems[replaylistitem].alphaKey + dir_on[menudepthleft]*10; - maxy = y + currentMenu->menuitems[replaylistitem].alphaKey + sizedirmenu*10; + maxy = y + currentMenu->menuitems[replaylistitem].alphaKey + replayqueryfound*10; if (cursory > maxy - 20) cursory = maxy - 20; @@ -7175,7 +7252,7 @@ static void M_DrawReplayHut(void) y += currentMenu->menuitems[replaylistitem].alphaKey; - for (i = 0; i < (INT16)sizedirmenu; i++) + for (i = 0; i < (INT16)replayqueryfound; i++) { INT32 localy = y+i*10; INT32 localx = x; @@ -7233,7 +7310,7 @@ static void M_DrawReplayHut(void) } // Draw scrollbar - y = sizedirmenu*10 + currentMenu->menuitems[replaylistitem].alphaKey + 30; + y = replayqueryfound*10 + currentMenu->menuitems[replaylistitem].alphaKey + 30; if (y > SCALEDVIEWHEIGHT-80) { V_DrawFill(BASEVIDWIDTH-4, 75, 4, SCALEDVIEWHEIGHT-80, V_SNAPTOTOP|V_SNAPTORIGHT|239); @@ -7262,6 +7339,9 @@ static void M_DrawReplayHut(void) // draw text cursor for name if (replayqueryopen && skullAnimCounter < 4) // blink cursor V_DrawCharacter(16 + V_StringWidth(replayqueryinput, V_ALLOWLOWERCASE), 200 - 8, '_', false); + + if (replayquerycheck < sizedirmenu) + V_DrawString(16, 200 - 24, V_ALLOWLOWERCASE, va("Searching %u/%u...", (unsigned)replayquerycheck, (unsigned)sizedirmenu)); } } diff --git a/src/m_menu.h b/src/m_menu.h index a01c2ddef..3d29963f2 100644 --- a/src/m_menu.h +++ b/src/m_menu.h @@ -255,6 +255,7 @@ extern INT32 mapwads[NUMMAPS]; extern description_t description[MAXSKINS]; +extern consvar_t cv_replaysearchrate; extern consvar_t cv_showfocuslost; extern consvar_t cv_newgametype, cv_nextmap, cv_chooseskin, cv_serversort; extern consvar_t cv_showallmaps; From bb0c777cac7e67aac136c3c1a890e1f07a6e66ff Mon Sep 17 00:00:00 2001 From: Indev Date: Thu, 8 Aug 2024 21:31:09 +0300 Subject: [PATCH 03/10] Fix M_HutCheckReplays still being called in menu (hopefully) --- src/m_menu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/m_menu.c b/src/m_menu.c index d02a809ee..8b07ef019 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -4435,7 +4435,7 @@ void M_Ticker(void) else playback_enterheld = 0; - if (currentMenu == &MISC_ReplayHutDef) + if (demo.inreplayhut) M_HutCheckReplays(cv_replaysearchrate.value); interpTimerHackAllow = true; From 0758cf39c2f4e9b87d13fa48246a22b526af6186 Mon Sep 17 00:00:00 2001 From: Indev Date: Thu, 8 Aug 2024 22:01:19 +0300 Subject: [PATCH 04/10] Greatly improve replay search perfomance by only reading replay name, and not entire file. Increase default search rate up to 1000 --- src/g_game.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/g_game.h | 1 + src/m_menu.c | 8 +++----- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/g_game.c b/src/g_game.c index e05e7fcc0..117205b44 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -7211,6 +7211,58 @@ void G_LoadDemoInfo(menudemo_t *pdemo) Z_Free(infobuffer); } +void G_LoadDemoTitle(menudemo_t *pdemo) +{ + UINT8 infobuffer[96], *info_p; + UINT16 pdemoversion; + size_t count; + + FILE *handle = fopen(pdemo->filepath, "rb"); + + if (!handle) + { + CONS_Alert(CONS_ERROR, M_GetText("Failed to read file '%s'.\n"), pdemo->filepath); + sprintf(pdemo->title, "INVALID REPLAY"); + return; + } + + count = fread(infobuffer, 1, 96, handle); + fclose(handle); + info_p = infobuffer; + + // First check isn't too accurate technically, but well, valid replays should be larger than that anyway + if (count < 96 || memcmp(info_p, DEMOHEADER, 12)) + { + CONS_Alert(CONS_ERROR, M_GetText("%s is not a SRB2Kart replay file.\n"), pdemo->filepath); + sprintf(pdemo->title, "INVALID REPLAY"); + Z_Free(infobuffer); + return; + } + + info_p += 12; // DEMOHEADER + + READUINT8(info_p); + READUINT8(info_p); + pdemoversion = READUINT16(info_p); + + switch(pdemoversion) + { + case DEMOVERSION: // latest always supported + // demo title + M_Memcpy(pdemo->title, info_p, 64); + break; +#ifdef DEMO_COMPAT_100 + case 0x0001: + sprintf(pdemo->title, "Legacy Replay"); + break; +#endif + // too old, cannot support. + default: + CONS_Alert(CONS_ERROR, M_GetText("%s is an incompatible replay format and cannot be played.\n"), pdemo->filepath); + sprintf(pdemo->title, "INVALID REPLAY"); + } +} + // // G_PlayDemo // diff --git a/src/g_game.h b/src/g_game.h index bfc158583..977cc4dc3 100644 --- a/src/g_game.h +++ b/src/g_game.h @@ -265,6 +265,7 @@ void G_DeferedInitNew(boolean pencoremode, const char *mapname, INT32 pickedchar void G_DoLoadLevel(boolean resetplayer); void G_LoadDemoInfo(menudemo_t *pdemo); +void G_LoadDemoTitle(menudemo_t *pdemo); // For use in replay search feature void G_DeferedPlayDemo(const char *demo); // Can be called by the startup code or M_Responder, calls P_SetupLevel. diff --git a/src/m_menu.c b/src/m_menu.c index 8b07ef019..c269b0b0a 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -466,7 +466,7 @@ static void Dummystaff_OnChange(void); // ========================================================================== // How much replays are checked per frame when using search -consvar_t cv_replaysearchrate = {"replaysearchrate", "50", CV_SAVE, CV_Natural, NULL, 0, NULL, NULL, 0, 0, NULL }; +consvar_t cv_replaysearchrate = {"replaysearchrate", "1000", CV_SAVE, CV_Natural, NULL, 0, NULL, NULL, 0, 0, NULL }; consvar_t cv_showfocuslost = {"showfocuslost", "Yes", CV_SAVE, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL }; @@ -6779,10 +6779,8 @@ static void M_HutCheckReplays(size_t maxnum) break; case MD_NOTLOADED: - G_LoadDemoInfo(&demolist_all[replayquerycheck]); - ++maxnum; // Gonna check this one next iteration, don't decrease our number of tries yet - break; - + G_LoadDemoTitle(&demolist_all[replayquerycheck]); + /* FALLTHRU */ case MD_OUTDATED: case MD_LOADED: if (strcasestr(demolist_all[replayquerycheck].title, replayqueryinput) != NULL) From ee4869993d1cf29fe4f9325d09dad5647537cf57 Mon Sep 17 00:00:00 2001 From: Indev Date: Thu, 8 Aug 2024 22:03:00 +0300 Subject: [PATCH 05/10] Oops shouldn't z_free that --- src/g_game.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/g_game.c b/src/g_game.c index 117205b44..bc2937838 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -7235,7 +7235,6 @@ void G_LoadDemoTitle(menudemo_t *pdemo) { CONS_Alert(CONS_ERROR, M_GetText("%s is not a SRB2Kart replay file.\n"), pdemo->filepath); sprintf(pdemo->title, "INVALID REPLAY"); - Z_Free(infobuffer); return; } From c9aa764444a75eaa052070f76ab678abc5c47984 Mon Sep 17 00:00:00 2001 From: Indev Date: Thu, 8 Aug 2024 22:09:30 +0300 Subject: [PATCH 06/10] Add alternative button ('/') to open replay search --- src/m_menu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/m_menu.c b/src/m_menu.c index c269b0b0a..b667081e5 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6925,6 +6925,7 @@ static void M_HandleReplayHutList(INT32 choice) switch (choice) { + case '/': case 'f': case 'F': replayqueryopen = true; From 8db99291ac4e9f1603137c11b560f7c664b23640 Mon Sep 17 00:00:00 2001 From: Indev Date: Thu, 8 Aug 2024 22:40:22 +0300 Subject: [PATCH 07/10] Visual improvements for replay search --- src/m_menu.c | 31 ++++++++++++++++++++----------- src/m_menu.h | 3 +++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index b667081e5..e7688f238 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -4744,6 +4744,12 @@ void M_DrawTextBox(INT32 x, INT32 y, INT32 width, INT32 boxlines) V_DrawFill(x+5, y+5, width*8+6, boxlines*8+6, 239); } +void M_DrawTextBoxFlags(INT32 x, INT32 y, INT32 width, INT32 boxlines, INT32 flags) +{ + // Solid color textbox. + V_DrawFill(x+5, y+5, width*8+6, boxlines*8+6, 239|flags); +} + // horizontally centered text static void M_CentreText(INT32 y, const char *string) { @@ -6720,7 +6726,7 @@ static void M_HandleAddons(INT32 choice) menudemo_t *demolist; // Replays that that have been checked to match with query menudemo_t *demolist_all; // All replays -#define MAXREPLAYQUERY 30 +#define MAXREPLAYQUERY 40 char replayqueryinput[MAXREPLAYQUERY+1]; // The input typed size_t replayquerypos = 0; // Position in input window boolean replayqueryopen = false; // Is query window open? @@ -7258,7 +7264,7 @@ static void M_DrawReplayHut(void) if (localy < 65) continue; - if (localy >= SCALEDVIEWHEIGHT) + if (localy >= SCALEDVIEWHEIGHT - 24) break; if (demolist[i].type == MD_NOTLOADED && !processed_one_this_frame) @@ -7329,19 +7335,22 @@ static void M_DrawReplayHut(void) DrawReplayHutReplayInfo(); } + x -= 40; + // Draw search query + M_DrawTextBoxFlags(x, 200 - 22, MAXREPLAYQUERY, 1, V_SNAPTOBOTTOM); + if (replayqueryopen || replayquerypos) - { - M_DrawTextBox(8, 200 - 16, MAXREPLAYQUERY, 1); - V_DrawString(16, 200 - 8, V_ALLOWLOWERCASE, replayqueryinput); + V_DrawString(x + 8, 200 - 14, V_ALLOWLOWERCASE|V_SNAPTOBOTTOM, replayqueryinput); + else + V_DrawString(x + 8, 200 - 14, V_ALLOWLOWERCASE|V_SNAPTOBOTTOM, "\x86Press '/' or 'f' to search replays"); - // draw text cursor for name - if (replayqueryopen && skullAnimCounter < 4) // blink cursor - V_DrawCharacter(16 + V_StringWidth(replayqueryinput, V_ALLOWLOWERCASE), 200 - 8, '_', false); + // draw text cursor for name + if (replayqueryopen && skullAnimCounter < 4) // blink cursor + V_DrawCharacter(x + 8 + V_StringWidth(replayqueryinput, V_ALLOWLOWERCASE), 200 - 14, '_'|V_SNAPTOBOTTOM, false); - if (replayquerycheck < sizedirmenu) - V_DrawString(16, 200 - 24, V_ALLOWLOWERCASE, va("Searching %u/%u...", (unsigned)replayquerycheck, (unsigned)sizedirmenu)); - } + if (replayquerycheck < sizedirmenu) + V_DrawString(x + 8, 200 - 30, V_ALLOWLOWERCASE|V_SNAPTOBOTTOM, va("Searching %u/%u...", (unsigned)replayquerycheck, (unsigned)sizedirmenu)); } static void M_DrawReplayStartMenu(void) diff --git a/src/m_menu.h b/src/m_menu.h index 3d29963f2..e74fd491f 100644 --- a/src/m_menu.h +++ b/src/m_menu.h @@ -59,6 +59,9 @@ void M_SortServerList(void); // Draws a box with a texture inside as background for messages void M_DrawTextBox(INT32 x, INT32 y, INT32 width, INT32 boxlines); +// Same but with extra flags applied +void M_DrawTextBoxFlags(INT32 x, INT32 y, INT32 width, INT32 boxlines, INT32 flags); + // Used in d_netcmd to restart time attack void M_ModeAttackRetry(INT32 choice); From c2cfc96d879475edf1ecc5da09be8d8412b853c7 Mon Sep 17 00:00:00 2001 From: Indev Date: Fri, 9 Aug 2024 16:05:48 +0300 Subject: [PATCH 08/10] Load replay titles in separate thread --- src/m_menu.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 102 insertions(+), 11 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index e7688f238..55646f921 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6724,7 +6724,25 @@ static void M_HandleAddons(INT32 choice) // ---- REPLAY HUT ----- menudemo_t *demolist; // Replays that that have been checked to match with query + +// Locked behind Lock_search_state menudemo_t *demolist_all; // All replays +boolean replaynamesloaded = false; + +#ifdef HAVE_THREADS +I_mutex replayquerymutex; + +// g_in_exiting_signal_handler is an evil hack +// to avoid infinite SIGABRT recursion in the signal handler +// due to poisoned locks or mach-o kernel not supporting locks in signals +// or something like that. idk +# define Lock_search_state() if (!g_in_exiting_signal_handler) { I_lock_mutex(&replayquerymutex); } +# define Unlock_search_state() if (!g_in_exiting_signal_handler) { I_unlock_mutex(replayquerymutex); } +#else/*HAVE_THREADS*/ +# define Lock_search_state() +# define Unlock_search_state() +#endif/*HAVE_THREADS*/ + #define MAXREPLAYQUERY 40 char replayqueryinput[MAXREPLAYQUERY+1]; // The input typed @@ -6738,6 +6756,53 @@ size_t replayquerycheck = 0; // Index of next replay entry to check in demolist_ static INT16 replayScrollTitle = 0; static SINT8 replayScrollDelay = TICRATE, replayScrollDir = 1; +static void ReplayNamesLoadThread(void* userdata) +{ + + Lock_search_state(); + + size_t demolist_all_size = sizedirmenu; + menudemo_t *demolist_all_local = (menudemo_t*)malloc(sizeof(menudemo_t)*sizedirmenu); + memcpy(demolist_all_local, demolist_all, sizeof(menudemo_t)*sizedirmenu); + char *replaydirpath = (char*)userdata; + + Unlock_search_state(); + + for (size_t i = 0; i < demolist_all_size; ++i) + { + if (demolist_all_local[i].type != MD_SUBDIR) + G_LoadDemoTitle(&demolist_all_local[i]); + } + + Lock_search_state(); + + if (strcmp(menupath, replaydirpath) == 0) + { + memcpy(demolist_all, demolist_all_local, sizeof(menudemo_t)*sizedirmenu); + replaynamesloaded = true; + } + + Unlock_search_state(); + + // Was allocated before thread start, we need to free it + free(replaydirpath); +} + +static void LoadReplayNames(void) +{ + char *replaydirpath = strdup(menupath); + + Lock_search_state(); + replaynamesloaded = false; + Unlock_search_state(); + +#ifdef HAVE_THREADS + I_spawn_thread("replay-names-load", ReplayNamesLoadThread, replaydirpath); +#else + ReplayNamesLoadThread(replaydirpath); +#endif +} + static void ResetReplayQuery(void) { memset(replayqueryinput, 0, MAXREPLAYQUERY); @@ -6758,6 +6823,9 @@ static void AddCheckedReplay(void) // Check up to maxnum replays if they match with query static void M_HutCheckReplays(size_t maxnum) { + if (!replaynamesloaded) + return; + // Already checked everything if (replayquerycheck == sizedirmenu) return; @@ -6785,11 +6853,9 @@ static void M_HutCheckReplays(size_t maxnum) break; case MD_NOTLOADED: - G_LoadDemoTitle(&demolist_all[replayquerycheck]); - /* FALLTHRU */ case MD_OUTDATED: case MD_LOADED: - if (strcasestr(demolist_all[replayquerycheck].title, replayqueryinput) != NULL) + if (demolist_all[replayquerycheck].title[0] && strcasestr(demolist_all[replayquerycheck].title, replayqueryinput) != NULL) AddCheckedReplay(); // It matches, add it! else replayquerycheck++; // Doesn't match, moving on... @@ -6803,7 +6869,7 @@ static void M_HutCheckReplays(size_t maxnum) } } -static void PrepReplayList(void) +static void PrepReplayList(boolean reset) { size_t i; @@ -6812,10 +6878,16 @@ static void PrepReplayList(void) if (demolist) Z_Free(demolist); + demolist = Z_Calloc(sizeof(menudemo_t) * sizedirmenu, PU_STATIC, NULL); + + // If directory didn't change, keep demolist_all + if (!reset) return; + + Lock_search_state(); + if (demolist_all) Z_Free(demolist_all); - demolist = Z_Calloc(sizeof(menudemo_t) * sizedirmenu, PU_STATIC, NULL); demolist_all = Z_Calloc(sizeof(menudemo_t) * sizedirmenu, PU_STATIC, NULL); for (i = 0; i < sizedirmenu; i++) @@ -6839,6 +6911,10 @@ static void PrepReplayList(void) sprintf(demolist_all[i].title, "....."); } } + + Unlock_search_state(); + + LoadReplayNames(); } void M_ReplayHut(INT32 choice) @@ -6864,7 +6940,7 @@ void M_ReplayHut(INT32 choice) replayScrollTitle = 0; replayScrollDelay = TICRATE; replayScrollDir = 1; - PrepReplayList(); + PrepReplayList(true); menuactive = true; M_SetupNextMenu(&MISC_ReplayHutDef); @@ -6899,7 +6975,7 @@ static boolean M_HandleReplayHutQuery(INT32 choice) preparefilemenu(false, true); dir_on[menudepthleft] = 0; - PrepReplayList(); + PrepReplayList(false); break; @@ -6917,7 +6993,7 @@ static boolean M_HandleReplayHutQuery(INT32 choice) preparefilemenu(false, true); dir_on[menudepthleft] = 0; - PrepReplayList(); + PrepReplayList(false); } } @@ -6938,6 +7014,9 @@ static void M_HandleReplayHutList(INT32 choice) break; case KEY_UPARROW: + if (!replaynamesloaded) + return; + if (dir_on[menudepthleft]) dir_on[menudepthleft]--; else @@ -6949,6 +7028,9 @@ static void M_HandleReplayHutList(INT32 choice) break; case KEY_DOWNARROW: + if (!replaynamesloaded) + return; + if (dir_on[menudepthleft] < replayqueryfound-1) dir_on[menudepthleft]++; else @@ -6964,6 +7046,9 @@ static void M_HandleReplayHutList(INT32 choice) break; case KEY_ENTER: + if (!replaynamesloaded) + return; + switch (dirmenu[dir_on[menudepthleft]][DIR_TYPE]) { case EXT_FOLDER: @@ -6990,7 +7075,7 @@ static void M_HandleReplayHutList(INT32 choice) S_StartSound(NULL, sfx_menu1); dir_on[menudepthleft] = 1; ResetReplayQuery(); - PrepReplayList(); + PrepReplayList(true); } } else @@ -7008,7 +7093,7 @@ static void M_HandleReplayHutList(INT32 choice) M_QuitReplayHut(); return; } - PrepReplayList(); + PrepReplayList(true); break; default: // We can't just use M_SetupNextMenu because that'll run ReplayDef's quitroutine and boot us back to the title screen! @@ -7257,6 +7342,12 @@ static void M_DrawReplayHut(void) y += currentMenu->menuitems[replaylistitem].alphaKey; + if (!replaynamesloaded) + { + const char *msg = "Loading replays, please wait..."; + V_DrawCenteredString(160, 100, V_ALLOWLOWERCASE, msg); + } + for (i = 0; i < (INT16)replayqueryfound; i++) { INT32 localy = y+i*10; @@ -7349,7 +7440,7 @@ static void M_DrawReplayHut(void) if (replayqueryopen && skullAnimCounter < 4) // blink cursor V_DrawCharacter(x + 8 + V_StringWidth(replayqueryinput, V_ALLOWLOWERCASE), 200 - 14, '_'|V_SNAPTOBOTTOM, false); - if (replayquerycheck < sizedirmenu) + if (replaynamesloaded && replayquerycheck < sizedirmenu) V_DrawString(x + 8, 200 - 30, V_ALLOWLOWERCASE|V_SNAPTOBOTTOM, va("Searching %u/%u...", (unsigned)replayquerycheck, (unsigned)sizedirmenu)); } From b4612d6bd430aebffce88612309f6cce583192af Mon Sep 17 00:00:00 2001 From: Indev Date: Fri, 9 Aug 2024 19:45:16 +0300 Subject: [PATCH 09/10] Fix smol memory leak in replay search thread oops --- src/m_menu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/m_menu.c b/src/m_menu.c index 55646f921..d72a803ef 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -6758,7 +6758,6 @@ static SINT8 replayScrollDelay = TICRATE, replayScrollDir = 1; static void ReplayNamesLoadThread(void* userdata) { - Lock_search_state(); size_t demolist_all_size = sizedirmenu; @@ -6786,6 +6785,7 @@ static void ReplayNamesLoadThread(void* userdata) // Was allocated before thread start, we need to free it free(replaydirpath); + free(demolist_all_local); } static void LoadReplayNames(void) From a56ce0992378f1cb66a567b1fd79beace96a4242 Mon Sep 17 00:00:00 2001 From: Indev Date: Sun, 11 Aug 2024 11:57:54 +0300 Subject: [PATCH 10/10] Fix LUA_EvalMath warnings being broken sometimes --- src/lua_script.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lua_script.c b/src/lua_script.c index 85aefbfbd..f4bf8a07a 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -399,8 +399,14 @@ fixed_t LUA_EvalMath(const char *word) if (luaL_dostring(mL, buf)) { p = lua_tostring(mL, -1); - while (*p++ != ':' && *p) ; - p += 3; // "1: " + + // If there is [string "..."]:1: text, skip it + if (strstr(p, ":") != NULL) + { + while (*p++ != ':' && *p); + + p += 3; // "1: " + } CONS_Alert(CONS_WARNING, "%s\n", p); } else