mirror of
https://github.com/overte-org/overte.git
synced 2025-04-21 06:44:06 +02:00
Update ScriptEngine to use BatchLoader
This commit is contained in:
parent
958d853b56
commit
38b38eb063
2 changed files with 43 additions and 35 deletions
|
@ -31,6 +31,7 @@
|
|||
|
||||
#include "AnimationObject.h"
|
||||
#include "ArrayBufferViewClass.h"
|
||||
#include "BatchLoader.h"
|
||||
#include "DataViewClass.h"
|
||||
#include "EventTypes.h"
|
||||
#include "MenuItemProperties.h"
|
||||
|
@ -304,7 +305,7 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN
|
|||
QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);
|
||||
if (hasUncaughtException()) {
|
||||
int line = uncaughtExceptionLineNumber();
|
||||
qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ": " << result.toString();
|
||||
qDebug() << "Uncaught exception at (" << _fileNameString << " : " << fileName << ") line" << line << ": " << result.toString();
|
||||
}
|
||||
emit evaluationFinished(result, hasUncaughtException());
|
||||
clearExceptions();
|
||||
|
@ -595,46 +596,52 @@ void ScriptEngine::print(const QString& message) {
|
|||
emit printedMessage(message);
|
||||
}
|
||||
|
||||
void ScriptEngine::include(const QString& includeFile) {
|
||||
QUrl url = resolvePath(includeFile);
|
||||
QString includeContents;
|
||||
void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callback) {
|
||||
QList<QUrl> urls;
|
||||
for (QString file : includeFiles) {
|
||||
urls.append(resolvePath(file));
|
||||
}
|
||||
|
||||
if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp") {
|
||||
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
|
||||
QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url));
|
||||
qDebug() << "Downloading included script at" << includeFile;
|
||||
QEventLoop loop;
|
||||
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
||||
loop.exec();
|
||||
includeContents = reply->readAll();
|
||||
reply->deleteLater();
|
||||
} else {
|
||||
#ifdef _WIN32
|
||||
QString fileName = url.toString();
|
||||
#else
|
||||
QString fileName = url.toLocalFile();
|
||||
#endif
|
||||
|
||||
QFile scriptFile(fileName);
|
||||
if (scriptFile.open(QFile::ReadOnly | QFile::Text)) {
|
||||
qDebug() << "Including file:" << fileName;
|
||||
QTextStream in(&scriptFile);
|
||||
includeContents = in.readAll();
|
||||
} else {
|
||||
qDebug() << "ERROR Including file:" << fileName;
|
||||
emit errorMessage("ERROR Including file:" + fileName);
|
||||
BatchLoader* loader = new BatchLoader(urls);
|
||||
|
||||
auto evaluateScripts = [=](const QMap<QUrl, QString>& data) {
|
||||
for (QUrl url : urls) {
|
||||
QString contents = data[url];
|
||||
qDebug() << "About to load: " << url;
|
||||
if (contents.isNull()) {
|
||||
qDebug() << "Error loading file: " << url;
|
||||
} else {
|
||||
QScriptValue result = evaluate(contents, url.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QScriptValue result = evaluate(includeContents);
|
||||
if (hasUncaughtException()) {
|
||||
int line = uncaughtExceptionLineNumber();
|
||||
qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString();
|
||||
emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString());
|
||||
clearExceptions();
|
||||
if (callback.isFunction()) {
|
||||
QScriptValue(callback).call();
|
||||
}
|
||||
|
||||
loader->deleteLater();
|
||||
};
|
||||
|
||||
connect(loader, &BatchLoader::finished, this, evaluateScripts);
|
||||
|
||||
// If we are destroyed before the loader completes, make sure to clean it up
|
||||
connect(this, &QObject::destroyed, loader, &QObject::deleteLater);
|
||||
|
||||
loader->start();
|
||||
|
||||
if (!callback.isFunction() && !loader->isFinished()) {
|
||||
QEventLoop loop;
|
||||
QObject::connect(loader, &BatchLoader::finished, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptEngine::include(const QString& includeFile) {
|
||||
QStringList urls;
|
||||
urls.append(includeFile);
|
||||
include(urls);
|
||||
}
|
||||
|
||||
void ScriptEngine::load(const QString& loadFile) {
|
||||
QUrl url = resolvePath(loadFile);
|
||||
emit loadScript(url.toString(), false);
|
||||
|
|
|
@ -96,6 +96,7 @@ public slots:
|
|||
QObject* setTimeout(const QScriptValue& function, int timeoutMS);
|
||||
void clearInterval(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); }
|
||||
void clearTimeout(QObject* timer) { stopTimer(reinterpret_cast<QTimer*>(timer)); }
|
||||
void include(const QStringList& includeFiles, QScriptValue callback = QScriptValue());
|
||||
void include(const QString& includeFile);
|
||||
void load(const QString& loadfile);
|
||||
void print(const QString& message);
|
||||
|
|
Loading…
Reference in a new issue