Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 2 files). Show all
Unified · Split
| @@ -100,71 +100,6 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 99 unmodified lines | ||
| 100 | 100 | { |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | −ScriptEngine::ScriptEngine(const QUrl& scriptURL, | |
| 104 | − AbstractControllerScriptingInterface* controllerScriptingInterface) : | |
| 105 | − _scriptContents(), | |
| 106 | − _isFinished(false), | |
| 107 | − _isRunning(false), | |
| 108 | − _isInitialized(false), | |
| 109 | − _isAvatar(false), | |
| 110 | − _avatarIdentityTimer(NULL), | |
| 111 | − _avatarBillboardTimer(NULL), | |
| 112 | − _timerFunctionMap(), | |
| 113 | − _isListeningToAudioStream(false), | |
| 114 | − _avatarSound(NULL), | |
| 115 | − _numAvatarSoundSentBytes(0), | |
| 116 | − _controllerScriptingInterface(controllerScriptingInterface), | |
| 117 | − _avatarData(NULL), | |
| 118 | − _scriptName(), | |
| 119 | − _fileNameString(), | |
| 120 | − _quatLibrary(), | |
| 121 | − _vec3Library(), | |
| 122 | − _uuidLibrary(), | |
| 123 | − _animationCache(this), | |
| 124 | − _isUserLoaded(false), | |
| 125 | − _arrayBufferClass(new ArrayBufferClass(this)) | |
| 126 | −{ | |
| 127 | − QString scriptURLString = scriptURL.toString(); | |
| 128 | − _fileNameString = scriptURLString; | |
| 129 | − | |
| 130 | − QUrl url(scriptURL); | |
| 131 | − | |
| 132 | − // if the scheme length is one or lower, maybe they typed in a file, let's try | |
| 133 | − const int WINDOWS_DRIVE_LETTER_SIZE = 1; | |
| 134 | − if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { | |
| 135 | − url = QUrl::fromLocalFile(scriptURLString); | |
| 136 | − } | |
| 137 | − | |
| 138 | − // ok, let's see if it's valid... and if so, load it | |
| 139 | − if (url.isValid()) { | |
| 140 | − if (url.scheme() == "file") { | |
| 141 | − QString fileName = url.toLocalFile(); | |
| 142 | − QFile scriptFile(fileName); | |
| 143 | − if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { | |
| 144 | − qDebug() << "Loading file:" << fileName; | |
| 145 | − QTextStream in(&scriptFile); | |
| 146 | − _scriptContents = in.readAll(); | |
| 147 | − } else { | |
| 148 | − qDebug() << "ERROR Loading file:" << fileName; | |
| 149 | − emit errorMessage("ERROR Loading file:" + fileName); | |
| 150 | − } | |
| 151 | − } else { | |
| 152 | − QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 153 | − QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); | |
| 154 | − qDebug() << "Downloading script at" << url; | |
| 155 | − QEventLoop loop; | |
| 156 | − QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | |
| 157 | − loop.exec(); | |
| 158 | − if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) { | |
| 159 | − _scriptContents = reply->readAll(); | |
| 160 | − } else { | |
| 161 | − qDebug() << "ERROR Loading file:" << url.toString(); | |
| 162 | − emit errorMessage("ERROR Loading file:" + url.toString()); | |
| 163 | − } | |
| 164 | − } | |
| 165 | − } | |
| 166 | −} | |
| 167 | − | |
| 168 | 103 | void ScriptEngine::setIsAvatar(bool isAvatar) { |
| 169 | 104 | _isAvatar = isAvatar; |
| 170 | 105 | |
| @@ -217,6 +152,55 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents, const QStrin · 46 unmodified lines | ||
| 217 | 152 | return true; |
| 218 | 153 | } |
| 219 | 154 | |
| 155 | +void ScriptEngine::loadURL(const QUrl& scriptURL) { | |
| 156 | + if (_isRunning) { | |
| 157 | + return; | |
| 158 | + } | |
| 159 | + | |
| 160 | + QString scriptURLString = scriptURL.toString(); | |
| 161 | + _fileNameString = scriptURLString; | |
| 162 | + | |
| 163 | + QUrl url(scriptURL); | |
| 164 | + | |
| 165 | + // if the scheme length is one or lower, maybe they typed in a file, let's try | |
| 166 | + const int WINDOWS_DRIVE_LETTER_SIZE = 1; | |
| 167 | + if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { | |
| 168 | + url = QUrl::fromLocalFile(scriptURLString); | |
| 169 | + } | |
| 170 | + | |
| 171 | + // ok, let's see if it's valid... and if so, load it | |
| 172 | + if (url.isValid()) { | |
| 173 | + if (url.scheme() == "file") { | |
| 174 | + QString fileName = url.toLocalFile(); | |
| 175 | + QFile scriptFile(fileName); | |
| 176 | + if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { | |
| 177 | + qDebug() << "Loading file:" << fileName; | |
| 178 | + QTextStream in(&scriptFile); | |
| 179 | + _scriptContents = in.readAll(); | |
| 180 | + emit scriptLoaded(); | |
| 181 | + } else { | |
| 182 | + qDebug() << "ERROR Loading file:" << fileName; | |
| 183 | + emit errorLoadingScript(); | |
| 184 | + } | |
| 185 | + } else { | |
| 186 | + QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 187 | + QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); | |
| 188 | + connect(reply, &QNetworkReply::finished, this, &ScriptEngine::handleScriptDownload); | |
| 189 | + } | |
| 190 | + } | |
| 191 | +} | |
| 192 | + | |
| 193 | +void ScriptEngine::handleScriptDownload() { | |
| 194 | + QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); | |
| 195 | + | |
| 196 | + if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) { | |
| 197 | + _scriptContents = reply->readAll(); | |
| 198 | + } else { | |
| 199 | + qDebug() << "ERROR Loading file:" << reply->url().toString(); | |
| 200 | + emit errorLoadingScript(); | |
| 201 | + } | |
| 202 | +} | |
| 203 | + | |
| 220 | 204 | Q_SCRIPT_DECLARE_QMETAOBJECT(LocalVoxels, QString) |
| 221 | 205 | |
| 222 | 206 | void ScriptEngine::init() { |