Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 193 files). Show all
Unified · Split
| @@ -31,6 +31,7 @@ · 30 unmodified lines | ||
| 31 | 31 | |
| 32 | 32 | #include "AnimationObject.h" |
| 33 | 33 | #include "ArrayBufferViewClass.h" |
| 34 | +#include "BatchLoader.h" | |
| 34 | 35 | #include "DataViewClass.h" |
| 35 | 36 | #include "EventTypes.h" |
| 36 | 37 | #include "MenuItemProperties.h" |
| @@ -111,7 +112,7 @@ void ScriptEngine::setIsAvatar(bool isAvatar) { · 74 unmodified lines | ||
| 111 | 112 | _avatarIdentityTimer->start(AVATAR_IDENTITY_PACKET_SEND_INTERVAL_MSECS); |
| 112 | 113 | _avatarBillboardTimer->start(AVATAR_BILLBOARD_PACKET_SEND_INTERVAL_MSECS); |
| 113 | 114 | } |
| 114 | − | |
| 115 | + | |
| 115 | 116 | if (!_isAvatar) { |
| 116 | 117 | delete _avatarIdentityTimer; |
| 117 | 118 | _avatarIdentityTimer = NULL; |
| @@ -306,7 +307,7 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN · 188 unmodified lines | ||
| 306 | 307 | QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber); |
| 307 | 308 | if (hasUncaughtException()) { |
| 308 | 309 | int line = uncaughtExceptionLineNumber(); |
| 309 | − qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ": " << result.toString(); | |
| 310 | + qDebug() << "Uncaught exception at (" << _fileNameString << " : " << fileName << ") line" << line << ": " << result.toString(); | |
| 310 | 311 | } |
| 311 | 312 | emit evaluationFinished(result, hasUncaughtException()); |
| 312 | 313 | clearExceptions(); |
| @@ -597,46 +598,55 @@ void ScriptEngine::print(const QString& message) { · 284 unmodified lines | ||
| 597 | 598 | emit printedMessage(message); |
| 598 | 599 | } |
| 599 | 600 | |
| 600 | −void ScriptEngine::include(const QString& includeFile) { | |
| 601 | − QUrl url = resolvePath(includeFile); | |
| 602 | − QString includeContents; | |
| 601 | +/** | |
| 602 | + * If a callback is specified, the included files will be loaded asynchronously and the callback will be called | |
| 603 | + * when all of the files have finished loading. | |
| 604 | + * If no callback is specified, the included files will be loaded synchronously and will block execution until | |
| 605 | + * all of the files have finished loading. | |
| 606 | + */ | |
| 607 | +void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callback) { | |
| 608 | + QList<QUrl> urls; | |
| 609 | + for (QString file : includeFiles) { | |
| 610 | + urls.append(resolvePath(file)); | |
| 611 | + } | |
| 612 | + | |
| 613 | + BatchLoader* loader = new BatchLoader(urls); | |
| 614 | + | |
| 615 | + auto evaluateScripts = [=](const QMap<QUrl, QString>& data) { | |
| 616 | + for (QUrl url : urls) { | |
| 617 | + QString contents = data[url]; | |
| 618 | + if (contents.isNull()) { | |
| 619 | + qDebug() << "Error loading file: " << url; | |
| 620 | + } else { | |
| 621 | + QScriptValue result = evaluate(contents, url.toString()); | |
| 622 | + } | |
| 623 | + } | |
| 624 | + | |
| 625 | + if (callback.isFunction()) { | |
| 626 | + QScriptValue(callback).call(); | |
| 627 | + } | |
| 628 | + | |
| 629 | + loader->deleteLater(); | |
| 630 | + }; | |
| 603 | 631 | |
| 604 | − if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp") { | |
| 605 | − QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 606 | − QNetworkRequest networkRequest = QNetworkRequest(url); | |
| 607 | − networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT); | |
| 608 | − QNetworkReply* reply = networkAccessManager.get(networkRequest); | |
| 609 | − qDebug() << "Downloading included script at" << includeFile; | |
| 632 | + connect(loader, &BatchLoader::finished, this, evaluateScripts); | |
| 633 | + | |
| 634 | + // If we are destroyed before the loader completes, make sure to clean it up | |
| 635 | + connect(this, &QObject::destroyed, loader, &QObject::deleteLater); | |
| 636 | + | |
| 637 | + loader->start(); | |
| 638 | + | |
| 639 | + if (!callback.isFunction() && !loader->isFinished()) { | |
| 610 | 640 | QEventLoop loop; |
| 611 | − QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | |
| 641 | + QObject::connect(loader, &BatchLoader::finished, &loop, &QEventLoop::quit); | |
| 612 | 642 | loop.exec(); |
| 613 | − includeContents = reply->readAll(); | |
| 614 | − reply->deleteLater(); | |
| 615 | − } else { | |
| 616 | −#ifdef _WIN32 | |
| 617 | − QString fileName = url.toString(); | |
| 618 | −#else | |
| 619 | − QString fileName = url.toLocalFile(); | |
| 620 | −#endif | |
| 621 | − | |
| 622 | − QFile scriptFile(fileName); | |
| 623 | − if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { | |
| 624 | − qDebug() << "Including file:" << fileName; | |
| 625 | − QTextStream in(&scriptFile); | |
| 626 | − includeContents = in.readAll(); | |
| 627 | − } else { | |
| 628 | − qDebug() << "ERROR Including file:" << fileName; | |
| 629 | − emit errorMessage("ERROR Including file:" + fileName); | |
| 630 | − } | |
| 631 | 643 | } |
| 644 | +} | |
| 632 | 645 | |
| 633 | − QScriptValue result = evaluate(includeContents); | |
| 634 | − if (hasUncaughtException()) { | |
| 635 | − int line = uncaughtExceptionLineNumber(); | |
| 636 | − qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString(); | |
| 637 | − emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString()); | |
| 638 | − clearExceptions(); | |
| 639 | − } | |
| 646 | +void ScriptEngine::include(const QString& includeFile, QScriptValue callback) { | |
| 647 | + QStringList urls; | |
| 648 | + urls.append(includeFile); | |
| 649 | + include(urls, callback); | |
| 640 | 650 | } |
| 641 | 651 | |
| 642 | 652 | void ScriptEngine::load(const QString& loadFile) { |