History

first cut at shutting down scripts ahead of other cleanup

ZappoMan committed 11 years ago

Commit 0b607fa1 parent 70400f75 files · +268 −146Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 5 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+55−3
@@ -94,8 +94,45 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 93 unmodified lines
94 _isUserLoaded(false),94 _isUserLoaded(false),
95 _arrayBufferClass(new ArrayBufferClass(this))95 _arrayBufferClass(new ArrayBufferClass(this))
96{96{
97 _allKnownScriptEngines.insert(this);
97}98}
98 99
100ScriptEngine::~ScriptEngine() {
101 qDebug() << "[" << QThread::currentThread() << "]" << "ScriptEngine::~ScriptEngine() " << getFilename();
102 _allKnownScriptEngines.remove(this);
103}
104
105QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;
106
107void ScriptEngine::gracefullyStopAllScripts() {
108 qDebug() << "[" << QThread::currentThread() << "]" << "ScriptEngine::gracefullyStopAllScripts() ----------- START ------------------";
109 foreach(ScriptEngine* scriptEngine, _allKnownScriptEngines) {
110 if (scriptEngine->isRunning()) {
111 qDebug() << "scriptEngine still alive:" << scriptEngine->getFilename() << "[" << scriptEngine << "]";
112
113 QEventLoop loop;
114 QObject::connect(scriptEngine, &ScriptEngine::doneRunning, &loop, &QEventLoop::quit);
115
116 scriptEngine->stop();
117
118 qDebug() << "waiting on script to stop... ";
119 loop.exec();
120 qDebug() << "done waiting... ";
121 }
122 }
123 qDebug() << "[" << QThread::currentThread() << "]" << "ScriptEngine::gracefullyStopAllScripts() ----------- DONE ------------------";
124}
125
126QString ScriptEngine::getFilename() const {
127 QStringList fileNameParts = _fileNameString.split("/");
128 QString lastPart;
129 if (!fileNameParts.isEmpty()) {
130 lastPart = fileNameParts.last();
131 }
132 return lastPart;
133}
134
135
99void ScriptEngine::setIsAvatar(bool isAvatar) {136void ScriptEngine::setIsAvatar(bool isAvatar) {
100 _isAvatar = isAvatar;137 _isAvatar = isAvatar;
101 138
@@ -364,16 +401,18 @@ void ScriptEngine::run() { · 262 unmodified lines
364 }401 }
365 402
366 if (_isFinished) {403 if (_isFinished) {
404 qDebug() << "ScriptEngine::run()... while() about to break " << getFilename();
367 break;405 break;
368 }406 }
369 407
370 QCoreApplication::processEvents();408 QCoreApplication::processEvents();
371 409
372 if (_isFinished) {410 if (_isFinished) {
411 qDebug() << "ScriptEngine::run()... while() about to break " << getFilename();
373 break;412 break;
374 }413 }
375 414
376 if (_entityScriptingInterface.getEntityPacketSender()->serversExist()) {415 if (!_isFinished && _entityScriptingInterface.getEntityPacketSender()->serversExist()) {
377 // release the queue of edit entity messages.416 // release the queue of edit entity messages.
378 _entityScriptingInterface.getEntityPacketSender()->releaseQueuedMessages();417 _entityScriptingInterface.getEntityPacketSender()->releaseQueuedMessages();
379 418
@@ -383,7 +422,7 @@ void ScriptEngine::run() { · 3 unmodified lines
383 }422 }
384 }423 }
385 424
386 if (_isAvatar && _avatarData) {425 if (!_isFinished && _isAvatar && _avatarData) {
387 426
388 const int SCRIPT_AUDIO_BUFFER_SAMPLES = floor(((SCRIPT_DATA_CALLBACK_USECS * AudioConstants::SAMPLE_RATE)427 const int SCRIPT_AUDIO_BUFFER_SAMPLES = floor(((SCRIPT_DATA_CALLBACK_USECS * AudioConstants::SAMPLE_RATE)
389 / (1000 * 1000)) + 0.5);428 / (1000 * 1000)) + 0.5);
@@ -493,9 +532,13 @@ void ScriptEngine::run() { · 103 unmodified lines
493 clearExceptions();532 clearExceptions();
494 }533 }
495 534
496 emit update(deltaTime);535 if (!_isFinished) {
536 //qDebug() << "ScriptEngine::run()... about to emit update() _scriptName:" << _scriptName << "_fileNameString:" << _fileNameString << "_isFinished:" << _isFinished;
537 emit update(deltaTime);
538 }
497 lastUpdate = now;539 lastUpdate = now;
498 }540 }
541 qDebug() << "ScriptEngine::run()... about to emit scriptEnding() " << getFilename();
499 emit scriptEnding();542 emit scriptEnding();
500 543
501 // kill the avatar identity timer544 // kill the avatar identity timer
@@ -513,16 +556,25 @@ void ScriptEngine::run() { · 11 unmodified lines
513 556
514 // If we were on a thread, then wait till it's done557 // If we were on a thread, then wait till it's done
515 if (thread()) {558 if (thread()) {
559 qDebug() << "ScriptEngine::run()... about to call thread()->quit() " << getFilename();
516 thread()->quit();560 thread()->quit();
517 }561 }
518 562
563 qDebug() << "ScriptEngine::run()... about to emit finished() " << getFilename();
519 emit finished(_fileNameString);564 emit finished(_fileNameString);
520 565
521 _isRunning = false;566 _isRunning = false;
567
568 qDebug() << "ScriptEngine::run()... about to emit runningStateChanged() " << getFilename();
522 emit runningStateChanged();569 emit runningStateChanged();
570
571 qDebug() << "ScriptEngine::run()... about to emit doneRunning() " << getFilename();
572 emit doneRunning();
573 qDebug() << "ScriptEngine::run()... DONE WITH run()... " << getFilename();
523}574}
524 575
525void ScriptEngine::stop() {576void ScriptEngine::stop() {
577 qDebug() << "ScriptEngine::stop()... " << getFilename();
526 _isFinished = true;578 _isFinished = true;
527 emit runningStateChanged();579 emit runningStateChanged();
528}580}