Skip to content

Commit d5ff964

Browse files
committed
Fixed problem with unlabelled timers firing out of sequence
1 parent c0c8f99 commit d5ff964

17 files changed

+221
-183
lines changed

OtherTypes.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,6 @@ class CTimer : public CObject
633633
bOmitFromOutput = false;
634634
bOmitFromLog = false;
635635
bExecutingScript = false;
636-
637636
};
638637

639638
bool operator== (const CTimer & rhs) const;
@@ -686,6 +685,8 @@ class CTimer : public CObject
686685
long nInvocationCount; // how many times procedure called
687686
long nMatched; // how many times the timer fired
688687

688+
unsigned long nCreateSequence; // for keeping timers in sequence of creation
689+
689690
// calculated field - when timer is next to go off (fire)
690691

691692
CmcDateTime tFireTime; // when to fire it
@@ -695,9 +696,16 @@ class CTimer : public CObject
695696
bool bSelected; // if true, selected for use in a plugin
696697
bool bExecutingScript; // if true, executing a script and cannot be deleted
697698

699+
static unsigned long GetNextTimerSequence () { return nNextCreateSequence++; }
700+
701+
private:
702+
static unsigned long nNextCreateSequence;
703+
698704
};
699705

700-
typedef CTypedPtrMap <CMapStringToPtr, CString, CTimer*> CTimerMap;
706+
typedef map <string, CTimer*> CTimerMap;
707+
typedef CTimerMap::iterator CTimerMapIterator;
708+
701709
// map for lookup name from pointer
702710
typedef map <CTimer*, string> CTimerRevMap;
703711

dialogs/plugins/PluginWizard.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,12 @@ BOOL CPluginWizardPage5::OnInitDialog()
586586
m_ctlList.InsertColumn(3, TranslateHeading ("Group"), LVCFMT_LEFT, 50);
587587

588588
int iItem = 0;
589-
POSITION pos;
590-
CString strName;
591589

592-
for (pos = m_doc->m_TimerMap.GetStartPosition(); pos; )
590+
for (CTimerMapIterator timerIt = m_doc->m_TimerMap.begin ();
591+
timerIt != m_doc->m_TimerMap.end ();
592+
timerIt++)
593593
{
594-
CTimer * t;
595-
m_doc->m_TimerMap.GetNextAssoc (pos, strName, t);
594+
CTimer * t = timerIt->second;
596595

597596
if (t->bTemporary)
598597
continue; // ignore temporary ones
@@ -643,14 +642,13 @@ void CPluginWizardPage5::OnSelectNone()
643642

644643
void CPluginWizardPage5::OnOK()
645644
{
646-
POSITION pos;
647-
CString strName;
648645

649646
// first deselect everything
650-
for (pos = m_doc->m_TimerMap.GetStartPosition(); pos; )
647+
for (CTimerMapIterator timerIt = m_doc->m_TimerMap.begin ();
648+
timerIt != m_doc->m_TimerMap.end ();
649+
timerIt++)
651650
{
652-
CTimer * t;
653-
m_doc->m_TimerMap.GetNextAssoc (pos, strName, t);
651+
CTimer * t = timerIt->second;
654652
t->bSelected = false;
655653
}
656654

@@ -659,15 +657,16 @@ void CPluginWizardPage5::OnOK()
659657
{
660658
CTimer * t = (CTimer *) m_ctlList.GetItemData (nItem);
661659
// find in Timer array (in case it doesn't exist any more) and select it
662-
for (pos = m_doc->m_TimerMap.GetStartPosition(); pos; )
660+
for (CTimerMapIterator timerIt = m_doc->m_TimerMap.begin ();
661+
timerIt != m_doc->m_TimerMap.end ();
662+
timerIt++)
663663
{
664-
CTimer * t2;
665-
m_doc->m_TimerMap.GetNextAssoc (pos, strName, t2);
666-
if (t == t2)
667-
{
668-
t->bSelected = true;
669-
break;
670-
}
664+
CTimer * t2 = timerIt->second;
665+
if (t == t2)
666+
{
667+
t->bSelected = true;
668+
break;
669+
}
671670
}
672671
} // end of each list item
673672

dialogs/world_prefs/TimerDlg.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ void CTimerDlg::DoDataExchange(CDataExchange* pDX)
142142
} // end of doing a periodical timer
143143

144144

145-
CString strTimerName;
146145
CTimer * timer_item;
147-
POSITION pos;
148146

149-
for (pos = m_pTimerMap->GetStartPosition (); pos; )
147+
for (CTimerMapIterator timerIt = m_pTimerMap->begin ();
148+
timerIt != m_pTimerMap->end ();
149+
timerIt++)
150150
{
151-
m_pTimerMap->GetNextAssoc (pos, strTimerName, timer_item);
151+
timer_item = timerIt->second;
152152

153153
// don't compare against itself
154154

dialogs/world_prefs/configuration.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ void CMUSHclientDoc:: LoadPrefsP15 (CPrefsP15 &page15)
441441

442442
CTrigger * pTrigger;
443443
CAlias * pAlias;
444-
CTimer * pTimer;
445444
CString strName;
446445
POSITION pos;
447446
LONGLONG iTimeTaken = 0;
@@ -466,10 +465,11 @@ void CMUSHclientDoc:: LoadPrefsP15 (CPrefsP15 &page15)
466465
}
467466

468467
// count number of timers fired
469-
for (pos = m_TimerMap.GetStartPosition(); pos; )
468+
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
469+
timerIt != m_TimerMap.end ();
470+
timerIt++)
470471
{
471-
m_TimerMap.GetNextAssoc (pos, strName, pTimer);
472-
nTotalTimers += pTimer->nMatched;
472+
nTotalTimers += timerIt->second->nMatched;
473473
}
474474

475475
page15.m_strBufferLines.Format ("%i / %ld",
@@ -500,7 +500,7 @@ void CMUSHclientDoc:: LoadPrefsP15 (CPrefsP15 &page15)
500500
m_AliasMap.GetCount (),
501501
nTotalAliases);
502502
page15.m_strTimers.Format ("%i (%I64d fired)",
503-
m_TimerMap.GetCount (),
503+
m_TimerMap.size (),
504504
nTotalTimers);
505505

506506
if (m_sockAddr.sin_addr.s_addr == INADDR_NONE)

dialogs/world_prefs/prefspropertypages.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6203,10 +6203,10 @@ void CPrefsP16::DoDataExchange(CDataExchange* pDX)
62036203
if (pDX->m_bSaveAndValidate)
62046204
if (m_ctlUseDefaultTimers.GetCheck ())
62056205
if (m_ctlUseDefaultTimers.GetCheck () != m_bUseDefaultTimers)
6206-
if (m_doc->m_TimerMap.GetCount () > 0)
6206+
if (m_doc->m_TimerMap.size () > 0)
62076207
if (::UMessageBox (TFormat ("By checking the option \"Override with default timers\" "
62086208
" your existing %i timer%s will be PERMANENTLY discarded next time you open this world.\n\n"
6209-
"Are you SURE you want to do this?", PLURAL (m_doc->m_TimerMap.GetCount ())),
6209+
"Are you SURE you want to do this?", PLURAL (m_doc->m_TimerMap.size ())),
62106210
MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2 ) != IDYES)
62116211
pDX->Fail ();
62126212

doc_construct.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ int i;
3939
m_VariableMap.InitHashTable (997); // allow for 1000 variables (at least)
4040
m_AliasMap.InitHashTable (293); // probably won't have many more than 300 aliases
4141
m_TriggerMap.InitHashTable (293); // probably won't have many more than 300 triggers
42-
m_TimerMap.InitHashTable (293); // probably won't have many more than 300 timers
4342

4443
SetDefaults (false); // set up numeric/boolean defaults
4544
SetAlphaDefaults (false); // set up alpha defaults
@@ -561,8 +560,14 @@ int i;
561560

562561
// delete timer map
563562

564-
DELETE_MAP (m_TimerMap, CTimer);
565-
563+
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
564+
timerIt != m_TimerMap.end ();
565+
timerIt++)
566+
delete timerIt->second;
567+
m_TimerMap.clear ();
568+
m_TimerRevMap.clear ();
569+
570+
566571
// delete variables map
567572

568573
DELETE_MAP (m_VariableMap, CVariable);

evaluate.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ if (strFileName.IsEmpty ())
613613
replace = FALSE;
614614
}
615615
else
616-
if (set_type == TIMER && !m_TimerMap.IsEmpty ())
616+
if (set_type == TIMER && !m_TimerMap.empty ())
617617
{
618618
if (::TMessageBox ("Replace existing timers?\n"
619619
"If you reply \"No\", then timers from the file"
@@ -661,7 +661,14 @@ CArchive * ar = NULL;
661661

662662
case TIMER:
663663
if (replace)
664-
DELETE_MAP (m_TimerMap, CTimer);
664+
{
665+
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
666+
timerIt != m_TimerMap.end ();
667+
timerIt++)
668+
delete timerIt->second;
669+
m_TimerMap.clear ();
670+
m_TimerRevMap.clear ();
671+
}
665672
Load_World_XML (*ar, XML_TIMERS | XML_NO_PLUGINS | XML_IMPORT_MAIN_FILE_ONLY);
666673
break;
667674

plugins.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ CPlugin::CPlugin (CMUSHclientDoc * pDoc)
159159
m_VariableMap.InitHashTable (293); // allow for 300 variables in this plugin
160160
m_AliasMap.InitHashTable (293); // ditto for aliases
161161
m_TriggerMap.InitHashTable (293); // ditto for triggers
162-
m_TimerMap.InitHashTable (293); // ditto for timers
163162
m_tDateInstalled = CTime::GetCurrentTime(); // when plugin loaded
164163

165164
m_dRequiredVersion = 0.0;
@@ -186,7 +185,12 @@ CPlugin::~CPlugin ()
186185
SaveState ();
187186
DELETE_MAP (m_TriggerMap, CTrigger);
188187
DELETE_MAP (m_AliasMap, CAlias);
189-
DELETE_MAP (m_TimerMap, CTimer);
188+
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
189+
timerIt != m_TimerMap.end ();
190+
timerIt++)
191+
delete timerIt->second;
192+
m_TimerMap.clear ();
193+
m_TimerRevMap.clear ();
190194
DELETE_MAP (m_VariableMap, CVariable);
191195
delete m_ScriptEngine;
192196

@@ -907,11 +911,11 @@ void CMUSHclientDoc::OnFilePluginwizard()
907911
a->bSelected = true;
908912
}
909913
// timers
910-
for (pos = m_TimerMap.GetStartPosition(); pos; )
914+
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
915+
timerIt != m_TimerMap.end ();
916+
timerIt++)
911917
{
912-
CTimer * t;
913-
m_TimerMap.GetNextAssoc (pos, strName, t);
914-
t->bSelected = true;
918+
timerIt->second->bSelected = true;
915919
}
916920
// variables
917921
for (pos = m_VariableMap.GetStartPosition(); pos; iCount++)
@@ -1122,24 +1126,24 @@ void CMUSHclientDoc::OnFilePluginwizard()
11221126
// ---------- timers ----------
11231127

11241128
iCount = 0;
1125-
for (pos = m_TimerMap.GetStartPosition(); pos; )
1129+
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
1130+
timerIt != m_TimerMap.end ();
1131+
timerIt++)
11261132
{
1127-
CTimer * t;
1128-
m_TimerMap.GetNextAssoc (pos, strName, t);
1129-
if (t->bSelected)
1133+
if (timerIt->second->bSelected)
11301134
iCount++;
11311135
}
11321136

11331137
if (iCount)
11341138
{
11351139
ar.WriteString (NL "<!-- Timers -->" NL NL);
11361140
Save_Header_XML (ar, "timers", false);
1137-
for (pos = m_TimerMap.GetStartPosition(); pos; )
1141+
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
1142+
timerIt != m_TimerMap.end ();
1143+
timerIt++)
11381144
{
1139-
CTimer * t;
1140-
m_TimerMap.GetNextAssoc (pos, strName, t);
1141-
if (t->bSelected)
1142-
Save_One_Timer_XML (ar, t);
1145+
if (timerIt->second->bSelected)
1146+
Save_One_Timer_XML (ar, timerIt->second);
11431147
}
11441148
Save_Footer_XML (ar, "timers");
11451149
} // end of having some
@@ -1334,17 +1338,19 @@ void CMUSHclientDoc::OnFilePluginwizard()
13341338
// ---------- timers ----------
13351339

13361340
iCount = 0;
1337-
for (pos = m_TimerMap.GetStartPosition(); pos; )
1341+
for (CTimerMapIterator timerIt = m_TimerMap.begin ();
1342+
timerIt != m_TimerMap.end ();
1343+
)
13381344
{
1339-
CTimer * t;
1340-
m_TimerMap.GetNextAssoc (pos, strName, t);
1345+
CTimer * t = timerIt->second;
13411346
if (t->bSelected)
13421347
{
1348+
CTimerMapIterator it = timerIt++; // make copy before deleting
13431349
iCount++;
13441350
// delete its pointer
13451351
delete t;
13461352
// now delete its entry
1347-
m_TimerMap.RemoveKey (strName);
1353+
m_TimerMap.erase (it);
13481354
} // end of selected Timer
13491355

13501356
// show document modified

scripting/methods/methods_info.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ VARIANT CMUSHclientDoc::GetInfo(long InfoType)
602602
case 217: SetUpVariantLong (vaResult, (long) m_nBytesOut); break;
603603
case 218: SetUpVariantLong (vaResult, GetVariableMap ().GetCount ()); break;
604604
case 219: SetUpVariantLong (vaResult, GetTriggerMap ().GetCount ()); break;
605-
case 220: SetUpVariantLong (vaResult, GetTimerMap ().GetCount ()); break;
605+
case 220: SetUpVariantLong (vaResult, GetTimerMap ().size ()); break;
606606
case 221: SetUpVariantLong (vaResult, GetAliasMap ().GetCount ()); break;
607607
case 222: SetUpVariantLong (vaResult, m_QueuedCommandsList.GetCount ()); break;
608608
case 223: SetUpVariantLong (vaResult, m_strMapList.GetCount ()); break;

scripting/methods/methods_plugins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ VARIANT CMUSHclientDoc::GetPluginInfo(LPCTSTR PluginID, short InfoType)
115115
case 8: SetUpVariantString (vaResult, pPlugin->m_strPurpose); break;
116116
case 9: SetUpVariantLong (vaResult, pPlugin->m_TriggerMap.GetCount ()); break;
117117
case 10: SetUpVariantLong (vaResult, pPlugin->m_AliasMap.GetCount ()); break;
118-
case 11: SetUpVariantLong (vaResult, pPlugin->m_TimerMap.GetCount ()); break;
118+
case 11: SetUpVariantLong (vaResult, pPlugin->m_TimerMap.size ()); break;
119119
case 12: SetUpVariantLong (vaResult, pPlugin->m_VariableMap.GetCount ()); break;
120120
case 13:
121121
if (pPlugin->m_tDateWritten.GetTime ()) // only if non-zero, otherwise return empty

0 commit comments

Comments
 (0)