From b7e77e10b60d65670ebaa28ac251d2ece23e270a Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 19 Jul 2024 22:44:12 +0200 Subject: [PATCH] improve virtual resource management --- src/p_setup.c | 26 ++++++++++++++++++-------- src/p_setup.h | 2 ++ src/w_wad.c | 12 ++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/p_setup.c b/src/p_setup.c index 19107a02c..99dfc81d9 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -109,6 +109,8 @@ INT32 numstarposts; boolean levelloading; UINT8 levelfadecol; +virtres_t *curmapvirt; + // BLOCKMAP // Created from axis aligned bounding box // of the map, a rectangular array of @@ -2337,20 +2339,26 @@ static void P_MakeMapMD5(virtres_t *virt, void *dest) static void P_LoadMapFromFile(void) { - virtres_t *virt = vres_GetMap(lastloadedmaplumpnum); - - P_LoadMapData(virt); - P_LoadMapBSP(virt); - P_LoadMapLUT(virt); + P_LoadMapData(curmapvirt); + P_LoadMapBSP(curmapvirt); + P_LoadMapLUT(curmapvirt); P_LoadLineDefs2(); P_GroupLines(); - P_PrepareRawThings(vres_Find(virt, "THINGS")->data); + P_PrepareRawThings(vres_Find(curmapvirt, "THINGS")->data); - P_MakeMapMD5(virt, &mapmd5); + P_MakeMapMD5(curmapvirt, &mapmd5); - vres_Free(virt); + // We do the following silly + // construction because vres_Free + // no-sells deletions of pointers + // that are == curmapvirt. + { + virtres_t *temp = curmapvirt; + curmapvirt = NULL; + vres_Free(temp); + } } static void P_RunLevelScript(const char *scriptname) @@ -2803,6 +2811,8 @@ boolean P_SetupLevel(boolean skipprecip) if (lastloadedmaplumpnum == INT16_MAX) I_Error("Map %s not found.\n", maplumpname); + curmapvirt = vres_GetMap(lastloadedmaplumpnum); + R_ReInitColormaps(mapheaderinfo[gamemap-1]->palette, (encoremode ? W_CheckNumForName(va("%sE", maplumpname)) : LUMPERROR)); CON_SetupBackColormap(); diff --git a/src/p_setup.h b/src/p_setup.h index 9b96225ad..5df0f0b68 100644 --- a/src/p_setup.h +++ b/src/p_setup.h @@ -17,6 +17,7 @@ #include "doomdata.h" #include "doomstat.h" #include "r_defs.h" +#include "w_wad.h" extern SINT8 midgamejoin; @@ -32,6 +33,7 @@ extern boolean levelloading; extern UINT8 levelfadecol; extern lumpnum_t lastloadedmaplumpnum; // for comparative savegame +extern virtres_t *curmapvirt; // // MAP used flats lookup table // diff --git a/src/w_wad.c b/src/w_wad.c index c4aa10b1e..04015d614 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -2132,6 +2132,12 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum) virtlump_t* vlumps = NULL; size_t numlumps = 0; + if (lastloadedmaplumpnum == lumpnum && curmapvirt != NULL) + { + // Avoid duplicating all our hard work. + return curmapvirt; + } + if (W_IsLumpWad(lumpnum)) { UINT32 realentry; @@ -2208,6 +2214,12 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum) */ void vres_Free(virtres_t* vres) { + if (vres == curmapvirt) + { + // No-sell multiple references. + return; + } + while (vres->numlumps--) Z_Free(vres->vlumps[vres->numlumps].data); Z_Free(vres->vlumps);