Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 337 files). Show all
Unified · Split
| @@ -27,7 +27,6 @@ · 26 unmodified lines | ||
| 27 | 27 | #include <NetworkAccessManager.h> |
| 28 | 28 | #include <NodeList.h> |
| 29 | 29 | #include <PacketHeaders.h> |
| 30 | −#include <Sound.h> | |
| 31 | 30 | #include <UUID.h> |
| 32 | 31 | #include <VoxelConstants.h> |
| 33 | 32 | #include <VoxelDetail.h> |
| @@ -47,14 +46,6 @@ · 13 unmodified lines | ||
| 47 | 46 | VoxelsScriptingInterface ScriptEngine::_voxelsScriptingInterface; |
| 48 | 47 | EntityScriptingInterface ScriptEngine::_entityScriptingInterface; |
| 49 | 48 | |
| 50 | −static QScriptValue soundConstructor(QScriptContext* context, QScriptEngine* engine) { | |
| 51 | − QUrl soundURL = QUrl(context->argument(0).toString()); | |
| 52 | − bool isStereo = context->argument(1).toBool(); | |
| 53 | − QScriptValue soundScriptValue = engine->newQObject(new Sound(soundURL, isStereo), QScriptEngine::ScriptOwnership); | |
| 54 | − | |
| 55 | − return soundScriptValue; | |
| 56 | −} | |
| 57 | − | |
| 58 | 49 | static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine){ |
| 59 | 50 | qDebug() << "script:print()<<" << context->argument(0).toString(); |
| 60 | 51 | QString message = context->argument(0).toString() |
| @@ -74,14 +65,6 @@ void avatarDataFromScriptValue(const QScriptValue &object, AvatarData* &out) { · 13 unmodified lines | ||
| 74 | 65 | out = qobject_cast<AvatarData*>(object.toQObject()); |
| 75 | 66 | } |
| 76 | 67 | |
| 77 | −QScriptValue injectorToScriptValue(QScriptEngine* engine, AudioInjector* const &in) { | |
| 78 | − return engine->newQObject(in); | |
| 79 | −} | |
| 80 | − | |
| 81 | −void injectorFromScriptValue(const QScriptValue &object, AudioInjector* &out) { | |
| 82 | − out = qobject_cast<AudioInjector*>(object.toQObject()); | |
| 83 | −} | |
| 84 | − | |
| 85 | 68 | QScriptValue inputControllerToScriptValue(QScriptEngine *engine, AbstractInputController* const &in) { |
| 86 | 69 | return engine->newQObject(in); |
| 87 | 70 | } |
| @@ -117,71 +100,6 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 29 unmodified lines | ||
| 117 | 100 | { |
| 118 | 101 | } |
| 119 | 102 | |
| 120 | −ScriptEngine::ScriptEngine(const QUrl& scriptURL, | |
| 121 | − AbstractControllerScriptingInterface* controllerScriptingInterface) : | |
| 122 | − _scriptContents(), | |
| 123 | − _isFinished(false), | |
| 124 | − _isRunning(false), | |
| 125 | − _isInitialized(false), | |
| 126 | − _isAvatar(false), | |
| 127 | − _avatarIdentityTimer(NULL), | |
| 128 | − _avatarBillboardTimer(NULL), | |
| 129 | − _timerFunctionMap(), | |
| 130 | − _isListeningToAudioStream(false), | |
| 131 | − _avatarSound(NULL), | |
| 132 | − _numAvatarSoundSentBytes(0), | |
| 133 | − _controllerScriptingInterface(controllerScriptingInterface), | |
| 134 | − _avatarData(NULL), | |
| 135 | − _scriptName(), | |
| 136 | − _fileNameString(), | |
| 137 | − _quatLibrary(), | |
| 138 | − _vec3Library(), | |
| 139 | − _uuidLibrary(), | |
| 140 | − _animationCache(this), | |
| 141 | − _isUserLoaded(false), | |
| 142 | − _arrayBufferClass(new ArrayBufferClass(this)) | |
| 143 | −{ | |
| 144 | − QString scriptURLString = scriptURL.toString(); | |
| 145 | − _fileNameString = scriptURLString; | |
| 146 | − | |
| 147 | − QUrl url(scriptURL); | |
| 148 | − | |
| 149 | − // if the scheme length is one or lower, maybe they typed in a file, let's try | |
| 150 | − const int WINDOWS_DRIVE_LETTER_SIZE = 1; | |
| 151 | − if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { | |
| 152 | − url = QUrl::fromLocalFile(scriptURLString); | |
| 153 | − } | |
| 154 | − | |
| 155 | − // ok, let's see if it's valid... and if so, load it | |
| 156 | − if (url.isValid()) { | |
| 157 | − if (url.scheme() == "file") { | |
| 158 | − QString fileName = url.toLocalFile(); | |
| 159 | − QFile scriptFile(fileName); | |
| 160 | − if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { | |
| 161 | − qDebug() << "Loading file:" << fileName; | |
| 162 | − QTextStream in(&scriptFile); | |
| 163 | − _scriptContents = in.readAll(); | |
| 164 | − } else { | |
| 165 | − qDebug() << "ERROR Loading file:" << fileName; | |
| 166 | − emit errorMessage("ERROR Loading file:" + fileName); | |
| 167 | − } | |
| 168 | − } else { | |
| 169 | − QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 170 | − QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); | |
| 171 | − qDebug() << "Downloading script at" << url; | |
| 172 | − QEventLoop loop; | |
| 173 | − QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | |
| 174 | − loop.exec(); | |
| 175 | − if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) { | |
| 176 | − _scriptContents = reply->readAll(); | |
| 177 | − } else { | |
| 178 | − qDebug() << "ERROR Loading file:" << url.toString(); | |
| 179 | − emit errorMessage("ERROR Loading file:" + url.toString()); | |
| 180 | − } | |
| 181 | − } | |
| 182 | − } | |
| 183 | −} | |
| 184 | − | |
| 185 | 103 | void ScriptEngine::setIsAvatar(bool isAvatar) { |
| 186 | 104 | _isAvatar = isAvatar; |
| 187 | 105 | |
| @@ -234,7 +152,55 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents, const QStrin · 46 unmodified lines | ||
| 234 | 152 | return true; |
| 235 | 153 | } |
| 236 | 154 | |
| 237 | −Q_SCRIPT_DECLARE_QMETAOBJECT(AudioInjectorOptions, QObject*) | |
| 155 | +void ScriptEngine::loadURL(const QUrl& scriptURL) { | |
| 156 | + if (_isRunning) { | |
| 157 | + return; | |
| 158 | + } | |
| 159 | + | |
| 160 | + _fileNameString = scriptURL.toString(); | |
| 161 | + | |
| 162 | + QUrl url(scriptURL); | |
| 163 | + | |
| 164 | + // if the scheme length is one or lower, maybe they typed in a file, let's try | |
| 165 | + const int WINDOWS_DRIVE_LETTER_SIZE = 1; | |
| 166 | + if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { | |
| 167 | + url = QUrl::fromLocalFile(_fileNameString); | |
| 168 | + } | |
| 169 | + | |
| 170 | + // ok, let's see if it's valid... and if so, load it | |
| 171 | + if (url.isValid()) { | |
| 172 | + if (url.scheme() == "file") { | |
| 173 | + _fileNameString = url.toLocalFile(); | |
| 174 | + QFile scriptFile(_fileNameString); | |
| 175 | + if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { | |
| 176 | + qDebug() << "ScriptEngine loading file:" << _fileNameString; | |
| 177 | + QTextStream in(&scriptFile); | |
| 178 | + _scriptContents = in.readAll(); | |
| 179 | + emit scriptLoaded(_fileNameString); | |
| 180 | + } else { | |
| 181 | + qDebug() << "ERROR Loading file:" << _fileNameString; | |
| 182 | + emit errorLoadingScript(_fileNameString); | |
| 183 | + } | |
| 184 | + } else { | |
| 185 | + QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 186 | + QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); | |
| 187 | + connect(reply, &QNetworkReply::finished, this, &ScriptEngine::handleScriptDownload); | |
| 188 | + } | |
| 189 | + } | |
| 190 | +} | |
| 191 | + | |
| 192 | +void ScriptEngine::handleScriptDownload() { | |
| 193 | + QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); | |
| 194 | + | |
| 195 | + if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) { | |
| 196 | + _scriptContents = reply->readAll(); | |
| 197 | + emit scriptLoaded(_fileNameString); | |
| 198 | + } else { | |
| 199 | + qDebug() << "ERROR Loading file:" << reply->url().toString(); | |
| 200 | + emit errorLoadingScript(_fileNameString); | |
| 201 | + } | |
| 202 | +} | |
| 203 | + | |
| 238 | 204 | Q_SCRIPT_DECLARE_QMETAOBJECT(LocalVoxels, QString) |
| 239 | 205 | |
| 240 | 206 | void ScriptEngine::init() { |
| @@ -254,6 +220,7 @@ void ScriptEngine::init() { · 13 unmodified lines | ||
| 254 | 220 | registerMenuItemProperties(this); |
| 255 | 221 | registerAnimationTypes(this); |
| 256 | 222 | registerAvatarTypes(this); |
| 223 | + registerAudioMetaTypes(this); | |
| 257 | 224 | Bitstream::registerTypes(this); |
| 258 | 225 | |
| 259 | 226 | qScriptRegisterMetaType(this, EntityItemPropertiesToScriptValue, EntityItemPropertiesFromScriptValue); |
| @@ -271,13 +238,6 @@ void ScriptEngine::init() { · 11 unmodified lines | ||
| 271 | 238 | QScriptValue printConstructorValue = newFunction(debugPrint); |
| 272 | 239 | globalObject().setProperty("print", printConstructorValue); |
| 273 | 240 | |
| 274 | − QScriptValue soundConstructorValue = newFunction(soundConstructor); | |
| 275 | − QScriptValue soundMetaObject = newQMetaObject(&Sound::staticMetaObject, soundConstructorValue); | |
| 276 | − globalObject().setProperty("Sound", soundMetaObject); | |
| 277 | − | |
| 278 | − QScriptValue injectionOptionValue = scriptValueFromQMetaObject<AudioInjectorOptions>(); | |
| 279 | − globalObject().setProperty("AudioInjectionOptions", injectionOptionValue); | |
| 280 | − | |
| 281 | 241 | QScriptValue localVoxelsValue = scriptValueFromQMetaObject<LocalVoxels>(); |
| 282 | 242 | globalObject().setProperty("LocalVoxels", localVoxelsValue); |
| 283 | 243 | |
| @@ -322,6 +282,11 @@ QScriptValue ScriptEngine::registerGlobalObject(const QString& name, QObject* ob · 38 unmodified lines | ||
| 322 | 282 | return QScriptValue::NullValue; |
| 323 | 283 | } |
| 324 | 284 | |
| 285 | +void ScriptEngine::registerFunction(const QString& name, QScriptEngine::FunctionSignature fun, int numArguments) { | |
| 286 | + QScriptValue scriptFun = newFunction(fun, numArguments); | |
| 287 | + globalObject().setProperty(name, scriptFun); | |
| 288 | +} | |
| 289 | + | |
| 325 | 290 | void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::FunctionSignature getter, |
| 326 | 291 | QScriptEngine::FunctionSignature setter, QScriptValue object) { |
| 327 | 292 | QScriptValue setterFunction = newFunction(setter, 1); |
| @@ -636,7 +601,7 @@ void ScriptEngine::stopTimer(QTimer *timer) { · 308 unmodified lines | ||
| 636 | 601 | } |
| 637 | 602 | } |
| 638 | 603 | |
| 639 | −QUrl ScriptEngine::resolveInclude(const QString& include) const { | |
| 604 | +QUrl ScriptEngine::resolvePath(const QString& include) const { | |
| 640 | 605 | // first lets check to see if it's already a full URL |
| 641 | 606 | QUrl url(include); |
| 642 | 607 | if (!url.scheme().isEmpty()) { |
| @@ -662,7 +627,7 @@ void ScriptEngine::print(const QString& message) { · 19 unmodified lines | ||
| 662 | 627 | } |
| 663 | 628 | |
| 664 | 629 | void ScriptEngine::include(const QString& includeFile) { |
| 665 | − QUrl url = resolveInclude(includeFile); | |
| 630 | + QUrl url = resolvePath(includeFile); | |
| 666 | 631 | QString includeContents; |
| 667 | 632 | |
| 668 | 633 | if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp") { |
| @@ -700,7 +665,7 @@ void ScriptEngine::include(const QString& includeFile) { · 31 unmodified lines | ||
| 700 | 665 | } |
| 701 | 666 | |
| 702 | 667 | void ScriptEngine::load(const QString& loadFile) { |
| 703 | − QUrl url = resolveInclude(loadFile); | |
| 668 | + QUrl url = resolvePath(loadFile); | |
| 704 | 669 | emit loadScript(url.toString(), false); |
| 705 | 670 | } |
| 706 | 671 | |