From f1017ef362831db87b5b4b1cf7f202063e119ec5 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 31 Oct 2023 13:05:56 +0100 Subject: [PATCH] fixed DEHSUPP multiple load prevention. The code in place did not work reliably, it now just checks if the StateMap could be created and if so, skips any subsequent load while only unloading everything in the finalization step. --- src/gamedata/d_dehacked.cpp | 67 ++++++++++++++----------------------- 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/src/gamedata/d_dehacked.cpp b/src/gamedata/d_dehacked.cpp index 78efd6c839..79d663505c 100644 --- a/src/gamedata/d_dehacked.cpp +++ b/src/gamedata/d_dehacked.cpp @@ -3258,7 +3258,7 @@ static bool DoDehPatch() dversion = 3; } - if (!LoadDehSupp ()) + if (StateMap.Size() == 0 && !LoadDehSupp ()) // only load this once. { Printf ("Could not load DEH support data\n"); UnloadDehSupp (); @@ -3280,7 +3280,6 @@ static bool DoDehPatch() } } while (cont); - UnloadDehSupp (); PatchName = ""; delete[] PatchFile; if (!batchrun) Printf ("Patch installed\n"); @@ -3292,43 +3291,34 @@ static inline bool CompareLabel (const char *want, const uint8_t *have) return *(uint32_t *)want == *(uint32_t *)have; } -static int DehUseCount; - static void UnloadDehSupp () { - if (--DehUseCount <= 0) - { - VMDisassemblyDumper disasmdump(VMDisassemblyDumper::Append); + VMDisassemblyDumper disasmdump(VMDisassemblyDumper::Append); - // Handle MBF params here, before the required arrays are cleared - for (unsigned int i=0; i < MBFParamStates.Size(); i++) - { - SetDehParams(MBFParamStates[i].state, MBFParamStates[i].pointer, disasmdump, &MBFParamStates[i]); - } - stateargs.Clear(); - MBFParamStates.Clear(); - MBFParamStates.ShrinkToFit(); - MBFCodePointers.Clear(); - MBFCodePointers.ShrinkToFit(); - // OrgSprNames and StateMap are not freed here, because if you load a second - // dehacked patch through some means other than including it - // in the first patch, it won't see the state/sprite information - // that was altered by the first. So we need to keep the - // StateMap around until all patches have been applied. - DehUseCount = 0; - Actions.Reset(); - OrgHeights.Reset(); - CodePConv.Reset(); - SoundMap.Reset(); - InfoNames.Reset(); - BitNames.Reset(); - StyleNames.Reset(); - AmmoNames.Reset(); - UnchangedSpriteNames.Reset(); + // Handle MBF params here, before the required arrays are cleared + for (unsigned int i=0; i < MBFParamStates.Size(); i++) + { + SetDehParams(MBFParamStates[i].state, MBFParamStates[i].pointer, disasmdump, &MBFParamStates[i]); } + stateargs.Clear(); + MBFParamStates.Clear(); + MBFParamStates.ShrinkToFit(); + MBFCodePointers.Clear(); + MBFCodePointers.ShrinkToFit(); + OrgSprNames.Reset(); + StateMap.Reset(); + Actions.Reset(); + OrgHeights.Reset(); + CodePConv.Reset(); + SoundMap.Reset(); + InfoNames.Reset(); + BitNames.Reset(); + StyleNames.Reset(); + AmmoNames.Reset(); + UnchangedSpriteNames.Reset(); } -static bool LoadDehSupp () +bool LoadDehSupp () { try { @@ -3348,12 +3338,6 @@ static bool LoadDehSupp () } bool gotnames = false; - - if (++DehUseCount > 1) - { - return true; - } - if (EnglishStrings.CountUsed() == 0) EnglishStrings = GStrings.GetDefaultStrings(); @@ -3506,6 +3490,7 @@ static bool LoadDehSupp () // This mapping is mainly for P_SetSafeFlash. for (int i = 0; i < s.StateSpan; i++) { + assert(FState::StaticFindStateOwner(s.State + i)); dehExtStates.Insert(dehcount, s.State + i); s.State[i].DehIndex = dehcount; dehcount++; @@ -3739,9 +3724,7 @@ void FinishDehPatch () } } } - // Now that all Dehacked patches have been processed, it's okay to free StateMap. - StateMap.Reset(); - OrgSprNames.Reset(); + UnloadDehSupp(); TouchedActors.Reset(); EnglishStrings.Clear(); GStrings.SetOverrideStrings(DehStrings);