forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerUnit.cpp
402 lines (350 loc) · 10.3 KB
/
TimerUnit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "TimerUnit.h"
#include "Host.h"
#include "TLuaInterpreter.h"
#include "TTimer.h"
using namespace std;
void TimerUnit::_uninstall(TTimer* pChild, QString packageName)
{
list<TTimer*>* childrenList = pChild->mpMyChildrenList;
for (auto timer : *childrenList) {
_uninstall(timer, packageName);
uninstallList.append(timer);
}
}
void TimerUnit::uninstall(QString packageName)
{
for (auto rootTimer : mTimerRootNodeList) {
if (rootTimer->mPackageName == packageName) {
_uninstall(rootTimer, packageName);
uninstallList.append(rootTimer);
}
}
for (auto& timer : uninstallList) {
unregisterTimer(timer);
}
uninstallList.clear();
}
void TimerUnit::stopAllTriggers()
{
for (auto timer : mTimerRootNodeList) {
timer->disableTimer(timer->getID());
}
}
void TimerUnit::compileAll()
{
for (auto timer : mTimerRootNodeList) {
if (timer->isActive()) {
timer->mNeedsToBeCompiled = true;
}
}
}
void TimerUnit::reenableAllTriggers()
{
for (auto timer : mTimerRootNodeList) {
timer->enableTimer(timer->getID());
}
}
void TimerUnit::addTimerRootNode(TTimer* pT, int parentPosition, int childPosition)
{
if (!pT) {
return;
}
if (!pT->getID()) {
pT->setID(getNewID());
}
if ((parentPosition == -1) || (childPosition >= static_cast<int>(mTimerRootNodeList.size()))) {
mTimerRootNodeList.push_back(pT);
} else {
// insert item at proper position
int cnt = 0;
for (auto it = mTimerRootNodeList.begin(); it != mTimerRootNodeList.end(); it++) {
if (cnt >= childPosition) {
mTimerRootNodeList.insert(it, pT);
break;
}
cnt++;
}
}
mTimerMap.insert(pT->getID(), pT);
// kein lookup table eintrag siehe addTimer()
}
void TimerUnit::reParentTimer(int childID, int oldParentID, int newParentID, int parentPosition, int childPosition)
{
TTimer* pOldParent = getTimerPrivate(oldParentID);
TTimer* pNewParent = getTimerPrivate(newParentID);
TTimer* pChild = getTimerPrivate(childID);
if (!pChild) {
return;
}
pChild->disableTimer(childID);
if (pOldParent) {
pOldParent->popChild(pChild);
}
if (!pOldParent) {
mTimerRootNodeList.remove(pChild);
}
if (pNewParent) {
pNewParent->addChild(pChild, parentPosition, childPosition);
pChild->setParent(pNewParent);
} else {
pChild->Tree<TTimer>::setParent(nullptr);
addTimerRootNode(pChild, parentPosition, childPosition);
}
pChild->enableTimer(childID);
}
void TimerUnit::removeAllTempTimers()
{
mCleanupList.clear();
for (auto timer : mTimerRootNodeList) {
if (timer->isTemporary()) {
timer->killTimer();
timer->mOK_code = false; //important to not crash on stale Lua function args
markCleanup(timer);
}
}
}
void TimerUnit::_removeTimerRootNode(TTimer* pT)
{
if (!pT) {
return;
}
// temp timers do not need to check for names referring to multiple different
// objects as names=ID -> much faster tempTimer creation
if (!pT->isTemporary()) {
mLookupTable.remove(pT->mName, pT);
} else {
mLookupTable.remove(pT->getName());
}
mTimerMap.remove(pT->getID());
mTimerRootNodeList.remove(pT);
}
TTimer* TimerUnit::getTimer(int id)
{
if (mTimerMap.find(id) != mTimerMap.end()) {
return mTimerMap.value(id);
} else {
return nullptr;
}
}
TTimer* TimerUnit::getTimerPrivate(int id)
{
if (mTimerMap.find(id) != mTimerMap.end()) {
return mTimerMap.value(id);
} else {
return nullptr;
}
}
bool TimerUnit::registerTimer(TTimer* pT)
{
if (!pT) {
return false;
}
if (pT->getParent()) {
addTimer(pT);
pT->setIsActive(false);
return true;
} else {
addTimerRootNode(pT);
pT->setIsActive(false);
return true;
}
}
void TimerUnit::unregisterTimer(TTimer* pT)
{
if (!pT) {
return;
}
pT->deactivate();
pT->stop();
if (pT->getParent()) {
_removeTimer(pT);
return;
} else {
_removeTimerRootNode(pT);
return;
}
}
void TimerUnit::addTimer(TTimer* pT)
{
if (!pT) {
return;
}
if (!pT->getID()) {
pT->setID(getNewID());
}
mTimerMap.insert(pT->getID(), pT);
// in den lookup table wird der timer erst dann eingetragen, wenn er auch einen namen hat -> setName()
}
void TimerUnit::_removeTimer(TTimer* pT)
{
if (!pT) {
return;
}
// temp timers do not need to check for names referring to multiple different
// objects as names=ID -> much faster tempTimer creation
if (!pT->isTemporary()) {
mLookupTable.remove(pT->mName, pT);
} else {
mLookupTable.remove(pT->getName());
}
mTimerMap.remove(pT->getID());
}
bool TimerUnit::enableTimer(const QString& name)
{
bool found = false;
QMap<QString, TTimer*>::const_iterator it = mLookupTable.constFind(name);
while (it != mLookupTable.cend() && it.key() == name) {
TTimer* pT = it.value();
if (!pT->isOffsetTimer()) {
/* ret = */ pT->setIsActive(true);
} else {
pT->setShouldBeActive(true);
}
if (pT->isFolder()) {
// disable or enable all timers in the respective branch
// irrespective of the user defined state.
if (pT->shouldBeActive()) {
pT->enableTimer();
} else {
pT->disableTimer();
}
} else {
if (pT->isOffsetTimer()) {
// state of offset timers is managed by the trigger engine
if (pT->shouldBeActive()) {
pT->enableTimer();
} else {
pT->disableTimer();
}
}
}
++it;
found = true;
}
return found;
}
bool TimerUnit::disableTimer(const QString& name)
{
bool found = false;
QMap<QString, TTimer*>::const_iterator it = mLookupTable.constFind(name);
while (it != mLookupTable.cend() && it.key() == name) {
TTimer* pT = it.value();
if (pT->isOffsetTimer()) {
pT->setShouldBeActive(false);
} else {
pT->setIsActive(false);
}
pT->disableTimer();
++it;
found = true;
}
return found;
}
TTimer* TimerUnit::findTimer(const QString& name)
{
QMap<QString, TTimer*>::const_iterator it = mLookupTable.constFind(name);
while (it != mLookupTable.cend() && it.key() == name) {
TTimer* pT = it.value();
return pT;
}
return nullptr;
}
bool TimerUnit::killTimer(const QString& name)
{
for (auto timer : mTimerRootNodeList) {
if (timer->getName() == name) {
// only temporary timers can be killed
if (!timer->isTemporary()) {
return false;
}
timer->killTimer();
markCleanup(timer);
return true;
}
}
return false;
}
int TimerUnit::getNewID()
{
return ++mMaxID;
}
void TimerUnit::doCleanup()
{
for (auto timer : mCleanupList) {
delete timer;
}
mCleanupList.clear();
}
void TimerUnit::markCleanup(TTimer* pT)
{
for (auto timer : mCleanupList) {
if (timer == pT) {
return;
}
}
mCleanupList.push_back(pT);
}
void TimerUnit::_assembleReport(TTimer* pChild)
{
list<TTimer*>* childrenList = pChild->mpMyChildrenList;
for (auto timer : *childrenList) {
_assembleReport(timer);
if (timer->isActive()) {
statsActiveTriggers++;
}
if (timer->isTemporary()) {
statsTempTriggers++;
}
statsTriggerTotal++;
}
}
QString TimerUnit::assembleReport()
{
statsActiveTriggers = 0;
statsTriggerTotal = 0;
statsTempTriggers = 0;
for (auto rootTimer : mTimerRootNodeList) {
if (rootTimer->isActive()) {
statsActiveTriggers++;
}
if (rootTimer->isTemporary()) {
statsTempTriggers++;
}
statsTriggerTotal++;
list<TTimer*>* childrenList = rootTimer->mpMyChildrenList;
for (auto childTimer : *childrenList) {
_assembleReport(childTimer);
if (childTimer->isActive()) {
statsActiveTriggers++;
}
if (childTimer->isTemporary()) {
statsTempTriggers++;
}
statsTriggerTotal++;
}
}
QStringList msg;
msg << "timers current total: " << QString::number(statsTriggerTotal) << "\n"
<< "tempTimers current total: " << QString::number(statsTempTriggers) << "\n"
<< "active timers: " << QString::number(statsActiveTriggers) << "\n";
return msg.join("");
}