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 | #include "AnimationObject.h" | 32 | #include "AnimationObject.h" |
| 33 | #include "ArrayBufferViewClass.h" | 33 | #include "ArrayBufferViewClass.h" |
| 34 | #include "BatchLoader.h" | ||
| 34 | #include "DataViewClass.h" | 35 | #include "DataViewClass.h" |
| 35 | #include "EventTypes.h" | 36 | #include "EventTypes.h" |
| 36 | #include "MenuItemProperties.h" | 37 | #include "MenuItemProperties.h" |
| @@ -111,7 +112,7 @@ void ScriptEngine::setIsAvatar(bool isAvatar) { · 74 unmodified lines | |||
| 111 | _avatarIdentityTimer->start(AVATAR_IDENTITY_PACKET_SEND_INTERVAL_MSECS); | 112 | _avatarIdentityTimer->start(AVATAR_IDENTITY_PACKET_SEND_INTERVAL_MSECS); |
| 112 | _avatarBillboardTimer->start(AVATAR_BILLBOARD_PACKET_SEND_INTERVAL_MSECS); | 113 | _avatarBillboardTimer->start(AVATAR_BILLBOARD_PACKET_SEND_INTERVAL_MSECS); |
| 113 | } | 114 | } |
| 114 | 115 | ||
| 115 | if (!_isAvatar) { | 116 | if (!_isAvatar) { |
| 116 | delete _avatarIdentityTimer; | 117 | delete _avatarIdentityTimer; |
| 117 | _avatarIdentityTimer = NULL; | 118 | _avatarIdentityTimer = NULL; |
| @@ -306,7 +307,7 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN · 188 unmodified lines | |||
| 306 | QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber); | 307 | QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber); |
| 307 | if (hasUncaughtException()) { | 308 | if (hasUncaughtException()) { |
| 308 | int line = uncaughtExceptionLineNumber(); | 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 | emit evaluationFinished(result, hasUncaughtException()); | 312 | emit evaluationFinished(result, hasUncaughtException()); |
| 312 | clearExceptions(); | 313 | clearExceptions(); |
| @@ -597,46 +598,55 @@ void ScriptEngine::print(const QString& message) { · 284 unmodified lines | |||
| 597 | emit printedMessage(message); | 598 | emit printedMessage(message); |
| 598 | } | 599 | } |
| 599 | 600 | ||
| 600 | void ScriptEngine::include(const QString& includeFile) { | 601 | /** |
| 601 | QUrl url = resolvePath(includeFile); | 602 | * If a callback is specified, the included files will be loaded asynchronously and the callback will be called |
| 602 | QString includeContents; | 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") { | 632 | connect(loader, &BatchLoader::finished, this, evaluateScripts); |
| 605 | QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | 633 | |
| 606 | QNetworkRequest networkRequest = QNetworkRequest(url); | 634 | // If we are destroyed before the loader completes, make sure to clean it up |
| 607 | networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT); | 635 | connect(this, &QObject::destroyed, loader, &QObject::deleteLater); |
| 608 | QNetworkReply* reply = networkAccessManager.get(networkRequest); | 636 | |
| 609 | qDebug() << "Downloading included script at" << includeFile; | 637 | loader->start(); |
| 638 | |||
| 639 | if (!callback.isFunction() && !loader->isFinished()) { | ||
| 610 | QEventLoop loop; | 640 | QEventLoop loop; |
| 611 | QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | 641 | QObject::connect(loader, &BatchLoader::finished, &loop, &QEventLoop::quit); |
| 612 | loop.exec(); | 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); | 646 | void ScriptEngine::include(const QString& includeFile, QScriptValue callback) { |
| 634 | if (hasUncaughtException()) { | 647 | QStringList urls; |
| 635 | int line = uncaughtExceptionLineNumber(); | 648 | urls.append(includeFile); |
| 636 | qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString(); | 649 | include(urls, callback); |
| 637 | emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString()); | ||
| 638 | clearExceptions(); | ||
| 639 | } | ||
| 640 | } | 650 | } |
| 641 | 651 | ||
| 642 | void ScriptEngine::load(const QString& loadFile) { | 652 | void ScriptEngine::load(const QString& loadFile) { |