Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 358 files). Show all
Unified · Split
| @@ -94,77 +94,11 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 93 unmodified lines | ||
| 94 | 94 | _quatLibrary(), |
| 95 | 95 | _vec3Library(), |
| 96 | 96 | _uuidLibrary(), |
| 97 | − _animationCache(this), | |
| 98 | 97 | _isUserLoaded(false), |
| 99 | 98 | _arrayBufferClass(new ArrayBufferClass(this)) |
| 100 | 99 | { |
| 101 | 100 | } |
| 102 | 101 | |
| 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 | 102 | void ScriptEngine::setIsAvatar(bool isAvatar) { |
| 169 | 103 | _isAvatar = isAvatar; |
| 170 | 104 | |
| @@ -217,6 +151,55 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents, const QStrin · 46 unmodified lines | ||
| 217 | 151 | return true; |
| 218 | 152 | } |
| 219 | 153 | |
| 154 | +void ScriptEngine::loadURL(const QUrl& scriptURL) { | |
| 155 | + if (_isRunning) { | |
| 156 | + return; | |
| 157 | + } | |
| 158 | + | |
| 159 | + _fileNameString = scriptURL.toString(); | |
| 160 | + | |
| 161 | + QUrl url(scriptURL); | |
| 162 | + | |
| 163 | + // if the scheme length is one or lower, maybe they typed in a file, let's try | |
| 164 | + const int WINDOWS_DRIVE_LETTER_SIZE = 1; | |
| 165 | + if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { | |
| 166 | + url = QUrl::fromLocalFile(_fileNameString); | |
| 167 | + } | |
| 168 | + | |
| 169 | + // ok, let's see if it's valid... and if so, load it | |
| 170 | + if (url.isValid()) { | |
| 171 | + if (url.scheme() == "file") { | |
| 172 | + _fileNameString = url.toLocalFile(); | |
| 173 | + QFile scriptFile(_fileNameString); | |
| 174 | + if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { | |
| 175 | + qDebug() << "ScriptEngine loading file:" << _fileNameString; | |
| 176 | + QTextStream in(&scriptFile); | |
| 177 | + _scriptContents = in.readAll(); | |
| 178 | + emit scriptLoaded(_fileNameString); | |
| 179 | + } else { | |
| 180 | + qDebug() << "ERROR Loading file:" << _fileNameString; | |
| 181 | + emit errorLoadingScript(_fileNameString); | |
| 182 | + } | |
| 183 | + } else { | |
| 184 | + QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 185 | + QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); | |
| 186 | + connect(reply, &QNetworkReply::finished, this, &ScriptEngine::handleScriptDownload); | |
| 187 | + } | |
| 188 | + } | |
| 189 | +} | |
| 190 | + | |
| 191 | +void ScriptEngine::handleScriptDownload() { | |
| 192 | + QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); | |
| 193 | + | |
| 194 | + if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) { | |
| 195 | + _scriptContents = reply->readAll(); | |
| 196 | + emit scriptLoaded(_fileNameString); | |
| 197 | + } else { | |
| 198 | + qDebug() << "ERROR Loading file:" << reply->url().toString(); | |
| 199 | + emit errorLoadingScript(_fileNameString); | |
| 200 | + } | |
| 201 | +} | |
| 202 | + | |
| 220 | 203 | Q_SCRIPT_DECLARE_QMETAOBJECT(LocalVoxels, QString) |
| 221 | 204 | |
| 222 | 205 | void ScriptEngine::init() { |
| @@ -272,7 +255,7 @@ void ScriptEngine::init() { · 49 unmodified lines | ||
| 272 | 255 | registerGlobalObject("Quat", &_quatLibrary); |
| 273 | 256 | registerGlobalObject("Vec3", &_vec3Library); |
| 274 | 257 | registerGlobalObject("Uuid", &_uuidLibrary); |
| 275 | − registerGlobalObject("AnimationCache", &_animationCache); | |
| 258 | + registerGlobalObject("AnimationCache", DependencyManager::get<AnimationCache>().data()); | |
| 276 | 259 | |
| 277 | 260 | registerGlobalObject("Voxels", &_voxelsScriptingInterface); |
| 278 | 261 | |
| @@ -501,17 +484,17 @@ void ScriptEngine::run() { · 222 unmodified lines | ||
| 501 | 484 | |
| 502 | 485 | // write audio packet to AudioMixer nodes |
| 503 | 486 | NodeList* nodeList = NodeList::getInstance(); |
| 504 | − foreach(const SharedNodePointer& node, nodeList->getNodeHash()) { | |
| 487 | + nodeList->eachNode([this, &nodeList, &audioPacket, &numPreSequenceNumberBytes](const SharedNodePointer& node){ | |
| 505 | 488 | // only send to nodes of type AudioMixer |
| 506 | 489 | if (node->getType() == NodeType::AudioMixer) { |
| 507 | 490 | // pack sequence number |
| 508 | 491 | quint16 sequence = _outgoingScriptAudioSequenceNumbers[node->getUUID()]++; |
| 509 | 492 | memcpy(audioPacket.data() + numPreSequenceNumberBytes, &sequence, sizeof(quint16)); |
| 510 | − | |
| 493 | + | |
| 511 | 494 | // send audio packet |
| 512 | 495 | nodeList->writeDatagram(audioPacket, node); |
| 513 | 496 | } |
| 514 | − } | |
| 497 | + }); | |
| 515 | 498 | } |
| 516 | 499 | } |
| 517 | 500 | |