| 1813 | |
| 1814 | |
| 1815 | ResultType Script::UpdateOrCreateTimer(Label *aLabel, char *aPeriod, char *aPriority, bool aEnable |
| 1816 | , bool aUpdatePriorityOnly) |
| 1817 | // Caller should specific a blank aPeriod to prevent the timer's period from being changed |
| 1818 | // (i.e. if caller just wants to turn on or off an existing timer). But if it does this |
| 1819 | // for a non-existent timer, that timer will be created with the default period as specfied in |
| 1820 | // the constructor. |
| 1821 | { |
| 1822 | ScriptTimer *timer; |
| 1823 | for (timer = mFirstTimer; timer != NULL; timer = timer->mNextTimer) |
| 1824 | if (timer->mLabel == aLabel) // Match found. |
| 1825 | break; |
| 1826 | bool timer_existed = (timer != NULL); |
| 1827 | if (!timer_existed) // Create it. |
| 1828 | { |
| 1829 | if ( !(timer = new ScriptTimer(aLabel)) ) |
| 1830 | return ScriptError(ERR_OUTOFMEM); |
| 1831 | if (!mFirstTimer) |
| 1832 | mFirstTimer = mLastTimer = timer; |
| 1833 | else |
| 1834 | { |
| 1835 | mLastTimer->mNextTimer = timer; |
| 1836 | // This must be done after the above: |
| 1837 | mLastTimer = timer; |
| 1838 | } |
| 1839 | ++mTimerCount; |
| 1840 | } |
| 1841 | // Update its members: |
| 1842 | if (aEnable && !timer->mEnabled) // Must check both or the below count will be wrong. |
| 1843 | { |
| 1844 | // The exception is if the timer already existed but the caller only wanted its priority changed: |
| 1845 | if (!(timer_existed && aUpdatePriorityOnly)) |
| 1846 | { |
| 1847 | timer->mEnabled = true; |
| 1848 | ++mTimerEnabledCount; |
| 1849 | SET_MAIN_TIMER // Ensure the timer is always running when there is at least one enabled timed subroutine. |
| 1850 | } |
| 1851 | //else do nothing, leave it disabled. |
| 1852 | } |
| 1853 | else if (!aEnable && timer->mEnabled) // Must check both or the below count will be wrong. |
| 1854 | { |
| 1855 | timer->mEnabled = false; |
| 1856 | --mTimerEnabledCount; |
| 1857 | // If there are now no enabled timed subroutines, kill the main timer since there's no other |
| 1858 | // reason for it to exist if we're here. This is because or direct or indirect caller is |
| 1859 | // currently always ExecUntil(), which doesn't need the timer while its running except to |
| 1860 | // support timed subroutines. UPDATE: The above is faulty; Must also check g_nLayersNeedingTimer |
| 1861 | // because our caller can be one that still needs a timer as proven by this script that |
| 1862 | // hangs otherwise: |
| 1863 | //SetTimer, Test, on |
| 1864 | //Sleep, 1000 |
| 1865 | //msgbox, done |
| 1866 | //return |
| 1867 | //Test: |
| 1868 | //SetTimer, Test, off |
| 1869 | //return |
| 1870 | if (!mTimerEnabledCount && !g_nLayersNeedingTimer && !Hotkey::sJoyHotkeyCount) |
| 1871 | KILL_MAIN_TIMER |
| 1872 | } |