Skip to content

Commit

Permalink
fixed DEHSUPP multiple load prevention.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
coelckers committed Oct 31, 2023
1 parent 61c2f5f commit f1017ef
Showing 1 changed file with 25 additions and 42 deletions.
67 changes: 25 additions & 42 deletions src/gamedata/d_dehacked.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand All @@ -3280,7 +3280,6 @@ static bool DoDehPatch()
}
} while (cont);

UnloadDehSupp ();
PatchName = "";
delete[] PatchFile;
if (!batchrun) Printf ("Patch installed\n");
Expand All @@ -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
{
Expand All @@ -3348,12 +3338,6 @@ static bool LoadDehSupp ()
}
bool gotnames = false;


if (++DehUseCount > 1)
{
return true;
}

if (EnglishStrings.CountUsed() == 0)
EnglishStrings = GStrings.GetDefaultStrings();

Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f1017ef

Please sign in to comment.