Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions SHOWOFF-NVSE/AuxTimers.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,26 @@ namespace AuxTimer
std::string varName;
};

//deferred insertion for timers created during DoCountdown iteration
//inserting into UnorderedMap mid-iteration can rehash and invalidate iterators
struct AuxTimerPendingInsertion
{
UInt32 modIndex;
UInt32 ownerID;
std::string varName;
bool isPerm;
double timeToCountdown;
UInt32 flags;
};

extern bool g_isIteratingTimers;
extern std::vector<AuxTimerPendingInsertion> g_auxTimersPendingInsertion;

extern std::vector<AuxTimerPendingRemoval> g_auxTimersToRemovePerm, g_auxTimersToRemoveTemp;
namespace Impl
{
void RemovePendingTimers(bool clearTemp);
}
void RemovePendingTimers();
void InsertPendingTimers();
}
2 changes: 1 addition & 1 deletion SHOWOFF-NVSE/ShowOffNVSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ void MessageHandler(NVSEMessagingInterface::Message* msg)
const auto isMenuMode = CdeclCall<bool>(0x702360);

AUX_TIMER_CS;
AuxTimer::RemovePendingTimers();
if (!s_auxTimerMapArraysPerm.Empty())
s_dataChangedFlags |= kChangedFlag_AuxTimerMaps; // assume a timer will change
AuxTimer::DoCountdown(globalTimeMult, vatsTimeMult, isMenuMode);
AuxTimer::RemovePendingTimers();
}

break;
Expand Down
18 changes: 18 additions & 0 deletions SHOWOFF-NVSE/functions/SO_fn_AuxTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ bool Cmd_AuxTimerStart_Execute(COMMAND_ARGS)
{
if (timeToCountdown == -1.0)
return true; //todo: dispatch xNVSE error

//can't insert into UnorderedMap mid-iteration, would invalidate iterators
if (g_isIteratingTimers)
{
g_auxTimersPendingInsertion.emplace_back(AuxTimerPendingInsertion{
varInfo.modIndex, varInfo.ownerID, varName,
varInfo.isPerm, timeToCountdown, flags
});
if (varInfo.isPerm)
{
s_dataChangedFlags |= kChangedFlag_AuxTimerMaps;
if (thisObj)
thisObj->MarkAsModified(0);
}
*result = 1;
return true;
}

value = GetTimerValue(varInfo, true);
}

Expand Down
21 changes: 21 additions & 0 deletions SHOWOFF-NVSE/internal/AuxTimers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace AuxTimer

std::vector<AuxTimerPendingRemoval> g_auxTimersToRemovePerm, g_auxTimersToRemoveTemp;

bool g_isIteratingTimers = false;
std::vector<AuxTimerPendingInsertion> g_auxTimersPendingInsertion;

AuxTimerValue* __fastcall GetTimerValue(const AuxTimerMapInfo& varInfo, bool createIfNotFound)
{
if (createIfNotFound) {
Expand Down Expand Up @@ -168,8 +171,11 @@ namespace AuxTimer

void DoCountdown(double globalTimeMult, double vatsTimeMult, bool isMenuMode)
{
g_isIteratingTimers = true;
Impl::DoCountdown(globalTimeMult, vatsTimeMult, isMenuMode, true);
Impl::DoCountdown(globalTimeMult, vatsTimeMult, isMenuMode, false);
g_isIteratingTimers = false;
InsertPendingTimers();
}

void HandleAutoRemoveTempTimers()
Expand Down Expand Up @@ -239,5 +245,20 @@ namespace AuxTimer
Impl::RemovePendingTimers(true);
Impl::RemovePendingTimers(false);
}

void InsertPendingTimers()
{
if (g_auxTimersPendingInsertion.empty())
return;

for (auto& pending : g_auxTimersPendingInsertion)
{
auto& modsMap = pending.isPerm ? s_auxTimerMapArraysPerm : s_auxTimerMapArraysTemp;
auto& entry = modsMap[pending.modIndex][pending.ownerID][const_cast<char*>(pending.varName.c_str())];
entry.SetTimeToCountdown(pending.timeToCountdown);
entry.m_flags = pending.flags;
}
g_auxTimersPendingInsertion.clear();
}
}

Loading