Showing the changes to libraries/script-engine/src/ScriptEngines.cpp (1 of 10 files). Show all
Unified · Split
| @@ -364,25 +364,43 @@ QStringList ScriptEngines::getRunningScripts() { · 363 unmodified lines | |||
| 364 | } | 364 | } |
| 365 | 365 | ||
| 366 | void ScriptEngines::stopAllScripts(bool restart) { | 366 | void ScriptEngines::stopAllScripts(bool restart) { |
| 367 | QVector<QString> toReload; | ||
| 367 | QReadLocker lock(&_scriptEnginesHashLock); | 368 | QReadLocker lock(&_scriptEnginesHashLock); |
| 368 | for (QHash<QUrl, ScriptEngine*>::const_iterator it = _scriptEnginesHash.constBegin(); | 369 | for (QHash<QUrl, ScriptEngine*>::const_iterator it = _scriptEnginesHash.constBegin(); |
| 369 | it != _scriptEnginesHash.constEnd(); it++) { | 370 | it != _scriptEnginesHash.constEnd(); it++) { |
| 371 | ScriptEngine *scriptEngine = it.value(); | ||
| 370 | // skip already stopped scripts | 372 | // skip already stopped scripts |
| 371 | if (it.value()->isFinished() || it.value()->isStopping()) { | 373 | if (scriptEngine->isFinished() || scriptEngine->isStopping()) { |
| 372 | continue; | 374 | continue; |
| 373 | } | 375 | } |
| 374 | 376 | ||
| 375 | // queue user scripts if restarting | 377 | // queue user scripts if restarting |
| 376 | if (restart && it.value()->isUserLoaded()) { | 378 | if (restart && scriptEngine->isUserLoaded()) { |
| 377 | connect(it.value(), &ScriptEngine::finished, this, [this](QString scriptName, ScriptEngine* engine) { | 379 | toReload << it.key().toString(); |
| 378 | reloadScript(scriptName); | ||
| 379 | }); | ||
| 380 | } | 380 | } |
| 381 | 381 | ||
| 382 | // stop all scripts | 382 | // stop all scripts |
| 383 | it.value()->stop(true); | ||
| 384 | qCDebug(scriptengine) << "stopping script..." << it.key(); | 383 | qCDebug(scriptengine) << "stopping script..." << it.key(); |
| 384 | scriptEngine->stop(); | ||
| 385 | } | 385 | } |
| 386 | // wait for engines to stop (ie: providing time for .scriptEnding cleanup handlers to run) before | ||
| 387 | // triggering reload of any Client scripts / Entity scripts | ||
| 388 | QTimer::singleShot(500, this, [=]() { | ||
| 389 | for(const auto &scriptName : toReload) { | ||
| 390 | auto scriptEngine = getScriptEngine(scriptName); | ||
| 391 | if (scriptEngine && !scriptEngine->isFinished()) { | ||
| 392 | qCDebug(scriptengine) << "waiting on script:" << scriptName; | ||
| 393 | scriptEngine->waitTillDoneRunning(); | ||
| 394 | qCDebug(scriptengine) << "done waiting on script:" << scriptName; | ||
| 395 | } | ||
| 396 | qCDebug(scriptengine) << "reloading script..." << scriptName; | ||
| 397 | reloadScript(scriptName); | ||
| 398 | } | ||
| 399 | if (restart) { | ||
| 400 | qCDebug(scriptengine) << "stopAllScripts -- emitting scriptsReloading"; | ||
| 401 | emit scriptsReloading(); | ||
| 402 | } | ||
| 403 | }); | ||
| 386 | } | 404 | } |
| 387 | 405 | ||
| 388 | bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) { | 406 | bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) { |
| @@ -421,9 +439,10 @@ void ScriptEngines::setScriptsLocation(const QString& scriptsLocation) { · 32 unmodified lines | |||
| 421 | } | 439 | } |
| 422 | 440 | ||
| 423 | void ScriptEngines::reloadAllScripts() { | 441 | void ScriptEngines::reloadAllScripts() { |
| 442 | qCDebug(scriptengine) << "reloadAllScripts -- clearing caches"; | ||
| 424 | DependencyManager::get<ScriptCache>()->clearCache(); | 443 | DependencyManager::get<ScriptCache>()->clearCache(); |
| 425 | DependencyManager::get<OffscreenUi>()->clearCache(); | 444 | DependencyManager::get<OffscreenUi>()->clearCache(); |
| 426 | emit scriptsReloading(); | 445 | qCDebug(scriptengine) << "reloadAllScripts -- stopping all scripts"; |
| 427 | stopAllScripts(true); | 446 | stopAllScripts(true); |
| 428 | } | 447 | } |
| 429 | 448 | ||
| @@ -456,7 +475,7 @@ ScriptEngine* ScriptEngines::loadScript(const QUrl& scriptFilename, bool isUserL · 26 unmodified lines | |||
| 456 | return scriptEngine; | 475 | return scriptEngine; |
| 457 | } | 476 | } |
| 458 | 477 | ||
| 459 | scriptEngine = new ScriptEngine(_context, NO_SCRIPT, ""); | 478 | scriptEngine = new ScriptEngine(_context, NO_SCRIPT, "about:" + scriptFilename.fileName()); |
| 460 | scriptEngine->setUserLoaded(isUserLoaded); | 479 | scriptEngine->setUserLoaded(isUserLoaded); |
| 461 | connect(scriptEngine, &ScriptEngine::doneRunning, this, [scriptEngine] { | 480 | connect(scriptEngine, &ScriptEngine::doneRunning, this, [scriptEngine] { |
| 462 | scriptEngine->deleteLater(); | 481 | scriptEngine->deleteLater(); |