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 | 2 | // ScriptEngine.cpp |
| 3 | −// hifi | |
| 3 | +// libraries/script-engine/src | |
| 4 | 4 | // |
| 5 | 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 | 12 | #include <QtCore/QCoreApplication> |
| @@ -25,6 +28,7 @@ · 15 unmodified lines | ||
| 25 | 28 | |
| 26 | 29 | #include <Sound.h> |
| 27 | 30 | |
| 31 | +#include "AnimationObject.h" | |
| 28 | 32 | #include "MenuItemProperties.h" |
| 29 | 33 | #include "LocalVoxels.h" |
| 30 | 34 | #include "ScriptEngine.h" |
| @@ -39,6 +43,11 @@ static QScriptValue soundConstructor(QScriptContext* context, QScriptEngine* eng · 8 unmodified lines | ||
| 39 | 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 | 52 | ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString, |
| 44 | 53 | AbstractControllerScriptingInterface* controllerScriptingInterface) : |
| @@ -60,7 +69,9 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 15 unmodified lines | ||
| 60 | 69 | _scriptName(), |
| 61 | 70 | _fileNameString(fileNameString), |
| 62 | 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 | 94 | _scriptName(), |
| 84 | 95 | _fileNameString(), |
| 85 | 96 | _quatLibrary(), |
| 86 | − _vec3Library() | |
| 97 | + _vec3Library(), | |
| 98 | + _uuidLibrary(), | |
| 99 | + _animationCache(this) | |
| 87 | 100 | { |
| 88 | 101 | QString scriptURLString = scriptURL.toString(); |
| 89 | 102 | _fileNameString = scriptURLString; |
| 90 | 103 | |
| 91 | 104 | QUrl url(scriptURL); |
| 92 | 105 | |
| 93 | − // if the scheme is empty, maybe they typed in a file, let's try | |
| 94 | − if (url.scheme().isEmpty()) { | |
| 106 | + // if the scheme length is one or lower, maybe they typed in a file, let's try | |
| 107 | + const int WINDOWS_DRIVE_LETTER_SIZE = 1; | |
| 108 | + if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) { | |
| 95 | 109 | url = QUrl::fromLocalFile(scriptURLString); |
| 96 | 110 | } |
| 97 | 111 | |
| @@ -106,6 +120,7 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, · 8 unmodified lines | ||
| 106 | 120 | _scriptContents = in.readAll(); |
| 107 | 121 | } else { |
| 108 | 122 | qDebug() << "ERROR Loading file:" << fileName; |
| 123 | + emit errorMessage("ERROR Loading file:" + fileName); | |
| 109 | 124 | } |
| 110 | 125 | } else { |
| 111 | 126 | QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); |
| @@ -147,6 +162,14 @@ void ScriptEngine::setAvatarData(AvatarData* avatarData, const QString& objectNa · 35 unmodified lines | ||
| 147 | 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 | 173 | bool ScriptEngine::setScriptContents(const QString& scriptContents, const QString& fileNameString) { |
| 151 | 174 | if (_isRunning) { |
| 152 | 175 | return false; |
| @@ -174,13 +197,18 @@ void ScriptEngine::init() { · 21 unmodified lines | ||
| 174 | 197 | registerVoxelMetaTypes(&_engine); |
| 175 | 198 | registerEventTypes(&_engine); |
| 176 | 199 | registerMenuItemProperties(&_engine); |
| 200 | + registerAnimationTypes(&_engine); | |
| 177 | 201 | |
| 178 | 202 | qScriptRegisterMetaType(&_engine, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue); |
| 179 | 203 | qScriptRegisterMetaType(&_engine, ParticleIDtoScriptValue, ParticleIDfromScriptValue); |
| 180 | 204 | qScriptRegisterSequenceMetaType<QVector<ParticleID> >(&_engine); |
| 181 | 205 | qScriptRegisterSequenceMetaType<QVector<glm::vec2> >(&_engine); |
| 206 | + qScriptRegisterSequenceMetaType<QVector<glm::quat> >(&_engine); | |
| 182 | 207 | qScriptRegisterSequenceMetaType<QVector<QString> >(&_engine); |
| 183 | 208 | |
| 209 | + QScriptValue printConstructorValue = _engine.newFunction(debugPrint); | |
| 210 | + _engine.globalObject().setProperty("print", printConstructorValue); | |
| 211 | + | |
| 184 | 212 | QScriptValue soundConstructorValue = _engine.newFunction(soundConstructor); |
| 185 | 213 | QScriptValue soundMetaObject = _engine.newQMetaObject(&Sound::staticMetaObject, soundConstructorValue); |
| 186 | 214 | _engine.globalObject().setProperty("Sound", soundMetaObject); |
| @@ -197,6 +225,8 @@ void ScriptEngine::init() { · 10 unmodified lines | ||
| 197 | 225 | registerGlobalObject("Particles", &_particlesScriptingInterface); |
| 198 | 226 | registerGlobalObject("Quat", &_quatLibrary); |
| 199 | 227 | registerGlobalObject("Vec3", &_vec3Library); |
| 228 | + registerGlobalObject("Uuid", &_uuidLibrary); | |
| 229 | + registerGlobalObject("AnimationCache", &_animationCache); | |
| 200 | 230 | |
| 201 | 231 | registerGlobalObject("Voxels", &_voxelsScriptingInterface); |
| 202 | 232 | |
| @@ -225,6 +255,7 @@ void ScriptEngine::evaluate() { · 22 unmodified lines | ||
| 225 | 255 | if (_engine.hasUncaughtException()) { |
| 226 | 256 | int line = _engine.uncaughtExceptionLineNumber(); |
| 227 | 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 | 276 | init(); |
| 246 | 277 | } |
| 247 | 278 | _isRunning = true; |
| 279 | + emit runningStateChanged(); | |
| 248 | 280 | |
| 249 | 281 | QScriptValue result = _engine.evaluate(_scriptContents); |
| 250 | 282 | if (_engine.hasUncaughtException()) { |
| 251 | 283 | int line = _engine.uncaughtExceptionLineNumber(); |
| 284 | + | |
| 252 | 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; | |
| 256 | − gettimeofday(&startTime, NULL); | |
| 289 | + QElapsedTimer startTime; | |
| 290 | + startTime.start(); | |
| 257 | 291 | |
| 258 | 292 | int thisFrame = 0; |
| 259 | 293 | |
| @@ -262,7 +296,7 @@ void ScriptEngine::run() { · 2 unmodified lines | ||
| 262 | 296 | qint64 lastUpdate = usecTimestampNow(); |
| 263 | 297 | |
| 264 | 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 | 300 | if (usecToSleep > 0) { |
| 267 | 301 | usleep(usecToSleep); |
| 268 | 302 | } |
| @@ -380,6 +414,7 @@ void ScriptEngine::run() { · 111 unmodified lines | ||
| 380 | 414 | if (_engine.hasUncaughtException()) { |
| 381 | 415 | int line = _engine.uncaughtExceptionLineNumber(); |
| 382 | 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 | 420 | emit scriptEnding(); |
| @@ -415,10 +450,12 @@ void ScriptEngine::run() { · 29 unmodified lines | ||
| 415 | 450 | emit finished(_fileNameString); |
| 416 | 451 | |
| 417 | 452 | _isRunning = false; |
| 453 | + emit runningStateChanged(); | |
| 418 | 454 | } |
| 419 | 455 | |
| 420 | 456 | void ScriptEngine::stop() { |
| 421 | 457 | _isFinished = true; |
| 458 | + emit runningStateChanged(); | |
| 422 | 459 | } |
| 423 | 460 | |
| 424 | 461 | void ScriptEngine::timerFired() { |
| @@ -489,6 +526,10 @@ QUrl ScriptEngine::resolveInclude(const QString& include) const { · 64 unmodified lines | ||
| 489 | 526 | return url; |
| 490 | 527 | } |
| 491 | 528 | |
| 529 | +void ScriptEngine::print(const QString& message) { | |
| 530 | + emit printedMessage(message); | |
| 531 | +} | |
| 532 | + | |
| 492 | 533 | void ScriptEngine::include(const QString& includeFile) { |
| 493 | 534 | QUrl url = resolveInclude(includeFile); |
| 494 | 535 | QString includeContents; |
| @@ -502,6 +543,7 @@ void ScriptEngine::include(const QString& includeFile) { · 7 unmodified lines | ||
| 502 | 543 | includeContents = in.readAll(); |
| 503 | 544 | } else { |
| 504 | 545 | qDebug() << "ERROR Loading file:" << fileName; |
| 546 | + emit errorMessage("ERROR Loading file:" + fileName); | |
| 505 | 547 | } |
| 506 | 548 | } else { |
| 507 | 549 | QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); |
| @@ -517,5 +559,6 @@ void ScriptEngine::include(const QString& includeFile) { · 9 unmodified lines | ||
| 517 | 559 | if (_engine.hasUncaughtException()) { |
| 518 | 560 | int line = _engine.uncaughtExceptionLineNumber(); |
| 519 | 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 | } |