History

fix interface crashing when reloading scripts

Dante Ruiz committed 7 years ago

Commit c4a9ca31 parent 42204581 file · +16 −23Browse files

Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 1 file). Show all

Unified · Split

libraries/script-engine/src/ScriptEngines.cpp+16−23
@@ -191,7 +191,6 @@ void ScriptEngines::shutdownScripting() { · 190 unmodified lines
191191
192192 // Gracefully stop the engine's scripting thread
193193 scriptEngine->stop();
194− removeScriptEngine(scriptEngine);
195194
196195 // We need to wait for the engine to be done running before we proceed, because we don't
197196 // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing
@@ -370,13 +369,10 @@ QStringList ScriptEngines::getRunningScripts() { · 172 unmodified lines
370369 }
371370
372371 void ScriptEngines::stopAllScripts(bool restart) {
373− QVector<QString> toReload;
374372 QReadLocker lock(&_scriptEnginesHashLock);
375373
376374 if (_isReloading) {
377375 return;
378− } else {
379− _isReloading = true;
380376 }
381377
382378 for (QHash<QUrl, ScriptEnginePointer>::const_iterator it = _scriptEnginesHash.constBegin();
@@ -389,29 +385,27 @@ void ScriptEngines::stopAllScripts(bool restart) { · 6 unmodified lines
389385
390386 // queue user scripts if restarting
391387 if (restart && scriptEngine->isUserLoaded()) {
392− toReload << it.key().toString();
388+ _isReloading = true;
389+ bool lastScript = (it == _scriptEnginesHash.constEnd() - 1);
390+ ScriptEngine::Type type = scriptEngine->getType();
391+
392+ connect(scriptEngine.data(), &ScriptEngine::finished, this, [this, type, lastScript] (QString scriptName) {
393+ reloadScript(scriptName, true)->setType(type);
394+
395+ if (lastScript) {
396+ _isReloading = false;
397+ }
398+ });
393399 }
394400
395401 // stop all scripts
396402 scriptEngine->stop();
397− removeScriptEngine(scriptEngine);
398403 }
399− // wait for engines to stop (ie: providing time for .scriptEnding cleanup handlers to run) before
400− // triggering reload of any Client scripts / Entity scripts
401− QTimer::singleShot(1000, this, [=]() {
402− for(const auto &scriptName : toReload) {
403− auto scriptEngine = getScriptEngine(scriptName);
404− if (scriptEngine && !scriptEngine->isFinished()) {
405− scriptEngine->waitTillDoneRunning();
406− }
407− reloadScript(scriptName);
408− }
409− if (restart) {
410− qCDebug(scriptengine) << "stopAllScripts -- emitting scriptsReloading";
411− emit scriptsReloading();
412− }
413− _isReloading = false;
414− });
404+
405+ if (restart) {
406+ qCDebug(scriptengine) << "stopAllScripts -- emitting scriptsReloading";
407+ emit scriptsReloading();
408+ }
415409 }
416410
417411 bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) {
@@ -439,7 +433,6 @@ bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) { · 21 unmodified lines
439433 }
440434 }
441435 scriptEngine->stop();
442− removeScriptEngine(scriptEngine);
443436 stoppedScript = true;
444437 }
445438 }