Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 645 files). Show all
Unified · Split
| @@ -1,9 +1,12 @@ | |||
| 1 | // | 1 | // |
| 2 | // ScriptEngine.cpp | 2 | // ScriptEngine.cpp |
| 3 | // hifi | 3 | // libraries/script-engine/src |
| 4 | // | 4 | // |
| 5 | // Created by Brad Hefta-Gaub on 12/14/13. | 5 | // Created by Brad Hefta-Gaub on 12/14/13. |
| 6 | // Copyright (c) 2013 HighFidelity, Inc. All rights reserved. | 6 | // Copyright 2013 High Fidelity, Inc. |
| 7 | // | ||
| 8 | // Distributed under the Apache License, Version 2.0. | ||
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
| 7 | // | 10 | // |
| 8 | 11 | ||
| 9 | #include <QtCore/QCoreApplication> | 12 | #include <QtCore/QCoreApplication> |
| @@ -25,6 +28,7 @@ · 15 unmodified lines | |||
| 25 | 28 | ||
| 26 | #include <Sound.h> | 29 | #include <Sound.h> |
| 27 | 30 | ||
| 31 | #include "AnimationObject.h" | ||
| 28 | #include "MenuItemProperties.h" | 32 | #include "MenuItemProperties.h" |
| 29 | #include "LocalVoxels.h" | 33 | #include "LocalVoxels.h" |
| 30 | #include "ScriptEngine.h" | 34 | #include "ScriptEngine.h" |
| @@ -39,6 +43,11 @@ static QScriptValue soundConstructor(QScriptContext* context, QScriptEngine* eng · 8 unmodified lines | |||
| 39 | return soundScriptValue; | 43 | return soundScriptValue; |
| 40 | } | 44 | } |
| 41 | 45 | ||
| 46 | static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine){ | ||
| 47 | qDebug() << "script:print()<<" << context->argument(0).toString(); | ||
| 48 | engine->evaluate("Script.print('" + context->argument(0).toString() + "')"); | ||
| 49 | return QScriptValue(); | ||
| 50 | } | ||
| 42 | 51 | ||
| 43 | ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString, | 52 | ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString, |
| 44 | AbstractControllerScriptingInterface* controllerScriptingInterface) : | 53 | AbstractControllerScriptingInterface* controllerScriptingInterface) : |
| @@ -60,7 +69,9 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 15 unmodified lines | |||
| 60 | _scriptName(), | 69 | _scriptName(), |
| 61 | _fileNameString(fileNameString), | 70 | _fileNameString(fileNameString), |
| 62 | _quatLibrary(), | 71 | _quatLibrary(), |
| 63 | _vec3Library() | 72 | _vec3Library(), |
| 73 | _uuidLibrary(), | ||
| 74 | _animationCache(this) | ||
| 64 | { | 75 | { |
| 65 | } | 76 | } |
| 66 | 77 | ||
| @@ -83,15 +94,18 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, · 16 unmodified lines | |||
| 83 | _scriptName(), | 94 | _scriptName(), |
| 84 | _fileNameString(), | 95 | _fileNameString(), |
| 85 | _quatLibrary(), | 96 | _quatLibrary(), |
| 86 | _vec3Library() | 97 | _vec3Library(), |
| 98 | _uuidLibrary(), | ||
| 99 | _animationCache(this) | ||
| 87 | { | 100 | { |
| 88 | QString scriptURLString = scriptURL.toString(); | 101 | QString scriptURLString = scriptURL.toString(); |
| 89 | _fileNameString = scriptURLString; | 102 | _fileNameString = scriptURLString; |
| 90 | 103 | ||
| 91 | QUrl url(scriptURL); | 104 | QUrl url(scriptURL); |
| 92 | 105 | ||
| 93 | // if the scheme is empty, maybe they typed in a file, let's try | 106 | // if the scheme length is one or lower, maybe they typed in a file, let's try |
| 94 | if (url.scheme().isEmpty()) { | 107 | const int WINDOWS_DRIVE_LETTER_SIZE = 1; |
| 108 | if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { | ||
| 95 | url = QUrl::fromLocalFile(scriptURLString); | 109 | url = QUrl::fromLocalFile(scriptURLString); |
| 96 | } | 110 | } |
| 97 | 111 | ||
| @@ -106,6 +120,7 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, · 8 unmodified lines | |||
| 106 | _scriptContents = in.readAll(); | 120 | _scriptContents = in.readAll(); |
| 107 | } else { | 121 | } else { |
| 108 | qDebug() << "ERROR Loading file:" << fileName; | 122 | qDebug() << "ERROR Loading file:" << fileName; |
| 123 | emit errorMessage("ERROR Loading file:" + fileName); | ||
| 109 | } | 124 | } |
| 110 | } else { | 125 | } else { |
| 111 | QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); | 126 | QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); |
| @@ -147,6 +162,14 @@ void ScriptEngine::setAvatarData(AvatarData* avatarData, const QString& objectNa · 35 unmodified lines | |||
| 147 | registerGlobalObject(objectName, _avatarData); | 162 | registerGlobalObject(objectName, _avatarData); |
| 148 | } | 163 | } |
| 149 | 164 | ||
| 165 | void ScriptEngine::setAvatarHashMap(AvatarHashMap* avatarHashMap, const QString& objectName) { | ||
| 166 | // remove the old Avatar property, if it exists | ||
| 167 | _engine.globalObject().setProperty(objectName, QScriptValue()); | ||
| 168 | |||
| 169 | // give the script engine the new avatar hash map | ||
| 170 | registerGlobalObject(objectName, avatarHashMap); | ||
| 171 | } | ||
| 172 | |||
| 150 | bool ScriptEngine::setScriptContents(const QString& scriptContents, const QString& fileNameString) { | 173 | bool ScriptEngine::setScriptContents(const QString& scriptContents, const QString& fileNameString) { |
| 151 | if (_isRunning) { | 174 | if (_isRunning) { |
| 152 | return false; | 175 | return false; |
| @@ -174,13 +197,18 @@ void ScriptEngine::init() { · 21 unmodified lines | |||
| 174 | registerVoxelMetaTypes(&_engine); | 197 | registerVoxelMetaTypes(&_engine); |
| 175 | registerEventTypes(&_engine); | 198 | registerEventTypes(&_engine); |
| 176 | registerMenuItemProperties(&_engine); | 199 | registerMenuItemProperties(&_engine); |
| 200 | registerAnimationTypes(&_engine); | ||
| 177 | 201 | ||
| 178 | qScriptRegisterMetaType(&_engine, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue); | 202 | qScriptRegisterMetaType(&_engine, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue); |
| 179 | qScriptRegisterMetaType(&_engine, ParticleIDtoScriptValue, ParticleIDfromScriptValue); | 203 | qScriptRegisterMetaType(&_engine, ParticleIDtoScriptValue, ParticleIDfromScriptValue); |
| 180 | qScriptRegisterSequenceMetaType<QVector<ParticleID> >(&_engine); | 204 | qScriptRegisterSequenceMetaType<QVector<ParticleID> >(&_engine); |
| 181 | qScriptRegisterSequenceMetaType<QVector<glm::vec2> >(&_engine); | 205 | qScriptRegisterSequenceMetaType<QVector<glm::vec2> >(&_engine); |
| 206 | qScriptRegisterSequenceMetaType<QVector<glm::quat> >(&_engine); | ||
| 182 | qScriptRegisterSequenceMetaType<QVector<QString> >(&_engine); | 207 | qScriptRegisterSequenceMetaType<QVector<QString> >(&_engine); |
| 183 | 208 | ||
| 209 | QScriptValue printConstructorValue = _engine.newFunction(debugPrint); | ||
| 210 | _engine.globalObject().setProperty("print", printConstructorValue); | ||
| 211 | |||
| 184 | QScriptValue soundConstructorValue = _engine.newFunction(soundConstructor); | 212 | QScriptValue soundConstructorValue = _engine.newFunction(soundConstructor); |
| 185 | QScriptValue soundMetaObject = _engine.newQMetaObject(&Sound::staticMetaObject, soundConstructorValue); | 213 | QScriptValue soundMetaObject = _engine.newQMetaObject(&Sound::staticMetaObject, soundConstructorValue); |
| 186 | _engine.globalObject().setProperty("Sound", soundMetaObject); | 214 | _engine.globalObject().setProperty("Sound", soundMetaObject); |
| @@ -197,6 +225,8 @@ void ScriptEngine::init() { · 10 unmodified lines | |||
| 197 | registerGlobalObject("Particles", &_particlesScriptingInterface); | 225 | registerGlobalObject("Particles", &_particlesScriptingInterface); |
| 198 | registerGlobalObject("Quat", &_quatLibrary); | 226 | registerGlobalObject("Quat", &_quatLibrary); |
| 199 | registerGlobalObject("Vec3", &_vec3Library); | 227 | registerGlobalObject("Vec3", &_vec3Library); |
| 228 | registerGlobalObject("Uuid", &_uuidLibrary); | ||
| 229 | registerGlobalObject("AnimationCache", &_animationCache); | ||
| 200 | 230 | ||
| 201 | registerGlobalObject("Voxels", &_voxelsScriptingInterface); | 231 | registerGlobalObject("Voxels", &_voxelsScriptingInterface); |
| 202 | 232 | ||
| @@ -225,6 +255,7 @@ void ScriptEngine::evaluate() { · 22 unmodified lines | |||
| 225 | if (_engine.hasUncaughtException()) { | 255 | if (_engine.hasUncaughtException()) { |
| 226 | int line = _engine.uncaughtExceptionLineNumber(); | 256 | int line = _engine.uncaughtExceptionLineNumber(); |
| 227 | qDebug() << "Uncaught exception at line" << line << ":" << result.toString(); | 257 | qDebug() << "Uncaught exception at line" << line << ":" << result.toString(); |
| 258 | emit errorMessage("Uncaught exception at line" + QString::number(line) + ":" + result.toString()); | ||
| 228 | } | 259 | } |
| 229 | } | 260 | } |
| 230 | 261 | ||
| @@ -245,15 +276,18 @@ void ScriptEngine::run() { · 14 unmodified lines | |||
| 245 | init(); | 276 | init(); |
| 246 | } | 277 | } |
| 247 | _isRunning = true; | 278 | _isRunning = true; |
| 279 | emit runningStateChanged(); | ||
| 248 | 280 | ||
| 249 | QScriptValue result = _engine.evaluate(_scriptContents); | 281 | QScriptValue result = _engine.evaluate(_scriptContents); |
| 250 | if (_engine.hasUncaughtException()) { | 282 | if (_engine.hasUncaughtException()) { |
| 251 | int line = _engine.uncaughtExceptionLineNumber(); | 283 | int line = _engine.uncaughtExceptionLineNumber(); |
| 284 | |||
| 252 | qDebug() << "Uncaught exception at line" << line << ":" << result.toString(); | 285 | qDebug() << "Uncaught exception at line" << line << ":" << result.toString(); |
| 286 | emit errorMessage("Uncaught exception at line" + QString::number(line) + ":" + result.toString()); | ||
| 253 | } | 287 | } |
| 254 | 288 | ||
| 255 | timeval startTime; | 289 | QElapsedTimer startTime; |
| 256 | gettimeofday(&startTime, NULL); | 290 | startTime.start(); |
| 257 | 291 | ||
| 258 | int thisFrame = 0; | 292 | int thisFrame = 0; |
| 259 | 293 | ||
| @@ -262,7 +296,7 @@ void ScriptEngine::run() { · 2 unmodified lines | |||
| 262 | qint64 lastUpdate = usecTimestampNow(); | 296 | qint64 lastUpdate = usecTimestampNow(); |
| 263 | 297 | ||
| 264 | while (!_isFinished) { | 298 | while (!_isFinished) { |
| 265 | int usecToSleep = usecTimestamp(&startTime) + (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - usecTimestampNow(); | 299 | int usecToSleep = (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - startTime.nsecsElapsed() / 1000; // nsec to usec |
| 266 | if (usecToSleep > 0) { | 300 | if (usecToSleep > 0) { |
| 267 | usleep(usecToSleep); | 301 | usleep(usecToSleep); |
| 268 | } | 302 | } |
| @@ -380,6 +414,7 @@ void ScriptEngine::run() { · 111 unmodified lines | |||
| 380 | if (_engine.hasUncaughtException()) { | 414 | if (_engine.hasUncaughtException()) { |
| 381 | int line = _engine.uncaughtExceptionLineNumber(); | 415 | int line = _engine.uncaughtExceptionLineNumber(); |
| 382 | qDebug() << "Uncaught exception at line" << line << ":" << _engine.uncaughtException().toString(); | 416 | qDebug() << "Uncaught exception at line" << line << ":" << _engine.uncaughtException().toString(); |
| 417 | emit errorMessage("Uncaught exception at line" + QString::number(line) + ":" + _engine.uncaughtException().toString()); | ||
| 383 | } | 418 | } |
| 384 | } | 419 | } |
| 385 | emit scriptEnding(); | 420 | emit scriptEnding(); |
| @@ -415,10 +450,12 @@ void ScriptEngine::run() { · 29 unmodified lines | |||
| 415 | emit finished(_fileNameString); | 450 | emit finished(_fileNameString); |
| 416 | 451 | ||
| 417 | _isRunning = false; | 452 | _isRunning = false; |
| 453 | emit runningStateChanged(); | ||
| 418 | } | 454 | } |
| 419 | 455 | ||
| 420 | void ScriptEngine::stop() { | 456 | void ScriptEngine::stop() { |
| 421 | _isFinished = true; | 457 | _isFinished = true; |
| 458 | emit runningStateChanged(); | ||
| 422 | } | 459 | } |
| 423 | 460 | ||
| 424 | void ScriptEngine::timerFired() { | 461 | void ScriptEngine::timerFired() { |
| @@ -489,6 +526,10 @@ QUrl ScriptEngine::resolveInclude(const QString& include) const { · 64 unmodified lines | |||
| 489 | return url; | 526 | return url; |
| 490 | } | 527 | } |
| 491 | 528 | ||
| 529 | void ScriptEngine::print(const QString& message) { | ||
| 530 | emit printedMessage(message); | ||
| 531 | } | ||
| 532 | |||
| 492 | void ScriptEngine::include(const QString& includeFile) { | 533 | void ScriptEngine::include(const QString& includeFile) { |
| 493 | QUrl url = resolveInclude(includeFile); | 534 | QUrl url = resolveInclude(includeFile); |
| 494 | QString includeContents; | 535 | QString includeContents; |
| @@ -502,6 +543,7 @@ void ScriptEngine::include(const QString& includeFile) { · 7 unmodified lines | |||
| 502 | includeContents = in.readAll(); | 543 | includeContents = in.readAll(); |
| 503 | } else { | 544 | } else { |
| 504 | qDebug() << "ERROR Loading file:" << fileName; | 545 | qDebug() << "ERROR Loading file:" << fileName; |
| 546 | emit errorMessage("ERROR Loading file:" + fileName); | ||
| 505 | } | 547 | } |
| 506 | } else { | 548 | } else { |
| 507 | QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); | 549 | QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); |
| @@ -517,5 +559,6 @@ void ScriptEngine::include(const QString& includeFile) { · 9 unmodified lines | |||
| 517 | if (_engine.hasUncaughtException()) { | 559 | if (_engine.hasUncaughtException()) { |
| 518 | int line = _engine.uncaughtExceptionLineNumber(); | 560 | int line = _engine.uncaughtExceptionLineNumber(); |
| 519 | qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString(); | 561 | qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString(); |
| 562 | emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString()); | ||
| 520 | } | 563 | } |
| 521 | } | 564 | } |