History

resolve conflicts on merge with uptream/master

Stephen Birarda committed 11 years ago

Commit 821c16c2 parents 684d466 4e2cbb399 files · +2638 −5187Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 99 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+51−51
@@ -121,7 +121,7 @@ bool ScriptEngine::_doneRunningThisScript = false; · 120 unmodified lines
121121 void ScriptEngine::stopAllScripts(QObject* application) {
122122 _allScriptsMutex.lock();
123123 _stoppingAllScripts = true;
124−
124+
125125 QMutableSetIterator<ScriptEngine*> i(_allKnownScriptEngines);
126126 while (i.hasNext()) {
127127 ScriptEngine* scriptEngine = i.next();
@@ -130,14 +130,14 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 2 unmodified lines
130130
131131 // NOTE: typically all script engines are running. But there's at least one known exception to this, the
132132 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using
133− // them. We don't need to stop scripts that aren't running.
133+ // them. We don't need to stop scripts that aren't running.
134134 if (scriptEngine->isRunning()) {
135−
135+
136136 // If the script is running, but still evaluating then we need to wait for its evaluation step to
137137 // complete. After that we can handle the stop process appropriately
138138 if (scriptEngine->evaluatePending()) {
139139 while (scriptEngine->evaluatePending()) {
140−
140+
141141 // This event loop allows any started, but not yet finished evaluate() calls to complete
142142 // we need to let these complete so that we can be guaranteed that the script engine isn't
143143 // in a partially setup state, which can confuse our shutdown unwinding.
@@ -147,11 +147,11 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 3 unmodified lines
147147 }
148148 }
149149
150− // We disconnect any script engine signals from the application because we don't want to do any
150+ // We disconnect any script engine signals from the application because we don't want to do any
151151 // extra stopScript/loadScript processing that the Application normally does when scripts start
152152 // and stop. We can safely short circuit this because we know we're in the "quitting" process
153153 scriptEngine->disconnect(application);
154−
154+
155155 // Calling stop on the script engine will set it's internal _isFinished state to true, and result
156156 // in the ScriptEngine gracefully ending it's run() method.
157157 scriptEngine->stop();
@@ -160,7 +160,7 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 2 unmodified lines
160160 // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing
161161 // any application state after we leave this stopAllScripts() method
162162 scriptEngine->waitTillDoneRunning();
163−
163+
164164 // If the script is stopped, we can remove it from our set
165165 i.remove();
166166 }
@@ -177,26 +177,26 @@ void ScriptEngine::waitTillDoneRunning() { · 10 unmodified lines
177177 if (_isRunning) {
178178
179179 _doneRunningThisScript = false; // NOTE: this is static, we serialize our waiting for scripts to finish
180−
180+
181181 // NOTE: waitTillDoneRunning() will be called on the main Application thread, inside of stopAllScripts()
182− // we want the application thread to continue to process events, because the scripts will likely need to
182+ // we want the application thread to continue to process events, because the scripts will likely need to
183183 // marshall messages across to the main thread. For example if they access Settings or Meny in any of their
184184 // shutdown code.
185185 while (!_doneRunningThisScript) {
186−
186+
187187 // process events for the main application thread, allowing invokeMethod calls to pass between threads
188188 QCoreApplication::processEvents();
189189 }
190190 }
191191 }
192192
193−QString ScriptEngine::getFilename() const {
193+QString ScriptEngine::getFilename() const {
194194 QStringList fileNameParts = _fileNameString.split("/");
195195 QString lastPart;
196196 if (!fileNameParts.isEmpty()) {
197197 lastPart = fileNameParts.last();
198198 }
199− return lastPart;
199+ return lastPart;
200200 }
201201
202202
@@ -259,15 +259,15 @@ void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { · 56 unmodified lines
259259
260260 _fileNameString = scriptURL.toString();
261261 _isReloading = reload;
262−
262+
263263 QUrl url(scriptURL);
264−
264+
265265 // if the scheme length is one or lower, maybe they typed in a file, let's try
266266 const int WINDOWS_DRIVE_LETTER_SIZE = 1;
267267 if (url.scheme().size() <= WINDOWS_DRIVE_LETTER_SIZE) {
268268 url = QUrl::fromLocalFile(_fileNameString);
269269 }
270−
270+
271271 // ok, let's see if it's valid... and if so, load it
272272 if (url.isValid()) {
273273 if (url.scheme() == "file") {
@@ -304,7 +304,7 @@ void ScriptEngine::init() { · 30 unmodified lines
304304 if (_isInitialized) {
305305 return; // only initialize once
306306 }
307−
307+
308308 _isInitialized = true;
309309
310310 auto entityScriptingInterface = DependencyManager::get<EntityScriptingInterface>();
@@ -318,7 +318,7 @@ void ScriptEngine::init() { · 7 unmodified lines
318318 registerAnimationTypes(this);
319319 registerAvatarTypes(this);
320320 registerAudioMetaTypes(this);
321−
321+
322322 if (_controllerScriptingInterface) {
323323 _controllerScriptingInterface->registerControllerTypes(this);
324324 }
@@ -341,7 +341,7 @@ void ScriptEngine::init() { · 16 unmodified lines
341341
342342 QScriptValue audioEffectOptionsConstructorValue = newFunction(AudioEffectOptions::constructor);
343343 globalObject().setProperty("AudioEffectOptions", audioEffectOptionsConstructorValue);
344−
344+
345345 qScriptRegisterMetaType(this, injectorToScriptValue, injectorFromScriptValue);
346346 qScriptRegisterMetaType(this, inputControllerToScriptValue, inputControllerFromScriptValue);
347347 qScriptRegisterMetaType(this, avatarDataToScriptValue, avatarDataFromScriptValue);
@@ -436,7 +436,7 @@ void ScriptEngine::addEventHandler(const EntityItemID& entityID, const QString& · 88 unmodified lines
436436 [=](const EntityItemID& entityID) {
437437 _registeredHandlers.remove(entityID);
438438 });
439−
439+
440440 // Two common cases of event handler, differing only in argument signature.
441441 auto makeSingleEntityHandler = [=](const QString& eventName) -> std::function<void(const EntityItemID&)> {
442442 return [=](const EntityItemID& entityItemID) -> void {
@@ -454,19 +454,19 @@ void ScriptEngine::addEventHandler(const EntityItemID& entityID, const QString& · 11 unmodified lines
454454 };
455455 connect(entities.data(), &EntityScriptingInterface::enterEntity, this, makeSingleEntityHandler("enterEntity"));
456456 connect(entities.data(), &EntityScriptingInterface::leaveEntity, this, makeSingleEntityHandler("leaveEntity"));
457−
457+
458458 connect(entities.data(), &EntityScriptingInterface::mousePressOnEntity, this, makeMouseHandler("mousePressOnEntity"));
459459 connect(entities.data(), &EntityScriptingInterface::mouseMoveOnEntity, this, makeMouseHandler("mouseMoveOnEntity"));
460460 connect(entities.data(), &EntityScriptingInterface::mouseReleaseOnEntity, this, makeMouseHandler("mouseReleaseOnEntity"));
461−
461+
462462 connect(entities.data(), &EntityScriptingInterface::clickDownOnEntity, this, makeMouseHandler("clickDownOnEntity"));
463463 connect(entities.data(), &EntityScriptingInterface::holdingClickOnEntity, this, makeMouseHandler("holdingClickOnEntity"));
464464 connect(entities.data(), &EntityScriptingInterface::clickReleaseOnEntity, this, makeMouseHandler("clickReleaseOnEntity"));
465−
465+
466466 connect(entities.data(), &EntityScriptingInterface::hoverEnterEntity, this, makeMouseHandler("hoverEnterEntity"));
467467 connect(entities.data(), &EntityScriptingInterface::hoverOverEntity, this, makeMouseHandler("hoverOverEntity"));
468468 connect(entities.data(), &EntityScriptingInterface::hoverLeaveEntity, this, makeMouseHandler("hoverLeaveEntity"));
469−
469+
470470 connect(entities.data(), &EntityScriptingInterface::collisionWithEntity, this,
471471 [=](const EntityItemID& idA, const EntityItemID& idB, const Collision& collision) {
472472 generalHandler(idA, "collisionWithEntity", [=]() {
@@ -587,10 +587,12 @@ void ScriptEngine::run() { · 114 unmodified lines
587587 / (1000 * 1000)) + 0.5);
588588 const int SCRIPT_AUDIO_BUFFER_BYTES = SCRIPT_AUDIO_BUFFER_SAMPLES * sizeof(int16_t);
589589
590− QByteArray avatarPacket = nodeList->byteArrayWithPopulatedHeader(PacketTypeAvatarData);
591− avatarPacket.append(_avatarData->toByteArray());
590+ QByteArray avatarByteArray = _avatarData->toByteArray();
591+ auto avatarPacket = NLPacket::create(PacketType::AvatarData, avatarByteArray.size());
592+
593+ avatarPacket->write(avatarByteArray);
592594
593− nodeList->broadcastToNodes(avatarPacket, NodeSet() << NodeType::AvatarMixer);
595+ nodeList->broadcastToNodes(std::move(avatarPacket), NodeSet() << NodeType::AvatarMixer);
594596
595597 if (_isListeningToAudioStream || _avatarSound) {
596598 // if we have an avatar audio stream then send it out to our audio-mixer
@@ -627,16 +629,13 @@ void ScriptEngine::run() { · 30 unmodified lines
627629 _numAvatarSoundSentBytes = 0;
628630 }
629631 }
630−
631− QByteArray audioPacket = nodeList->byteArrayWithPopulatedHeader(silentFrame
632− ? PacketTypeSilentAudioFrame
633− : PacketTypeMicrophoneAudioNoEcho);
634632
635− QDataStream packetStream(&audioPacket, QIODevice::Append);
633+ auto audioPacket = NLPacket::create(silentFrame
634+ ? PacketType::SilentAudioFrame
635+ : PacketType::MicrophoneAudioNoEcho);
636636
637− // pack a placeholder value for sequence number for now, will be packed when destination node is known
638− int numPreSequenceNumberBytes = audioPacket.size();
639− packetStream << (quint16) 0;
637+ // seek past the sequence number, will be packed when destination node is known
638+ audioPacket->seek(sizeof(quint16));
640639
641640 if (silentFrame) {
642641 if (!_isListeningToAudioStream) {
@@ -645,37 +644,38 @@ void ScriptEngine::run() { · 2 unmodified lines
645644 }
646645
647646 // write the number of silent samples so the audio-mixer can uphold timing
648− packetStream.writeRawData(reinterpret_cast<const char*>(&SCRIPT_AUDIO_BUFFER_SAMPLES), sizeof(int16_t));
647+ audioPacket->writePrimitive(SCRIPT_AUDIO_BUFFER_SAMPLES);
649648
650649 // use the orientation and position of this avatar for the source of this audio
651− packetStream.writeRawData(reinterpret_cast<const char*>(&_avatarData->getPosition()), sizeof(glm::vec3));
650+ audioPacket->writePrimitive(_avatarData->getPosition());
652651 glm::quat headOrientation = _avatarData->getHeadOrientation();
653− packetStream.writeRawData(reinterpret_cast<const char*>(&headOrientation), sizeof(glm::quat));
652+ audioPacket->writePrimitive(headOrientation);
654653
655654 } else if (nextSoundOutput) {
656655 // assume scripted avatar audio is mono and set channel flag to zero
657− packetStream << (quint8)0;
656+ audioPacket->writePrimitive((quint8) 0);
658657
659658 // use the orientation and position of this avatar for the source of this audio
660− packetStream.writeRawData(reinterpret_cast<const char*>(&_avatarData->getPosition()), sizeof(glm::vec3));
659+ audioPacket->writePrimitive(_avatarData->getPosition());
661660 glm::quat headOrientation = _avatarData->getHeadOrientation();
662− packetStream.writeRawData(reinterpret_cast<const char*>(&headOrientation), sizeof(glm::quat));
661+ audioPacket->writePrimitive(headOrientation);
663662
664663 // write the raw audio data
665− packetStream.writeRawData(reinterpret_cast<const char*>(nextSoundOutput), numAvailableSamples * sizeof(int16_t));
664+ audioPacket->write(reinterpret_cast<const char*>(nextSoundOutput), numAvailableSamples * sizeof(int16_t));
666665 }
667−
666+
668667 // write audio packet to AudioMixer nodes
669668 auto nodeList = DependencyManager::get<NodeList>();
670− nodeList->eachNode([this, &nodeList, &audioPacket, &numPreSequenceNumberBytes](const SharedNodePointer& node){
669+ nodeList->eachNode([this, &nodeList, &audioPacket](const SharedNodePointer& node){
671670 // only send to nodes of type AudioMixer
672671 if (node->getType() == NodeType::AudioMixer) {
673672 // pack sequence number
674673 quint16 sequence = _outgoingScriptAudioSequenceNumbers[node->getUUID()]++;
675− memcpy(audioPacket.data() + numPreSequenceNumberBytes, &sequence, sizeof(quint16));
676−
674+ audioPacket->seek(0);
675+ audioPacket->writePrimitive(sequence);
676+
677677 // send audio packet
678− nodeList->writeDatagram(audioPacket, node);
678+ nodeList->sendUnreliablePacket(*audioPacket, node);
679679 }
680680 });
681681 }
@@ -695,7 +695,7 @@ void ScriptEngine::run() { · 13 unmodified lines
695695 emit update(deltaTime);
696696 }
697697 lastUpdate = now;
698−
698+
699699 }
700700
701701 stopAllTimers(); // make sure all our timers are stopped if the script is ending
@@ -756,13 +756,13 @@ void ScriptEngine::stop() { · 54 unmodified lines
756756 void ScriptEngine::timerFired() {
757757 QTimer* callingTimer = reinterpret_cast<QTimer*>(sender());
758758 QScriptValue timerFunction = _timerFunctionMap.value(callingTimer);
759−
759+
760760 if (!callingTimer->isActive()) {
761761 // this timer is done, we can kill it
762762 _timerFunctionMap.remove(callingTimer);
763763 delete callingTimer;
764764 }
765−
765+
766766 // call the associated JS function, if it exists
767767 if (timerFunction.isValid()) {
768768 timerFunction.call();
@@ -856,7 +856,7 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 87 unmodified lines
856856 }
857857
858858 BatchLoader* loader = new BatchLoader(urls);
859−
859+
860860 auto evaluateScripts = [=](const QMap<QUrl, QString>& data) {
861861 for (QUrl url : urls) {
862862 QString contents = data[url];
@@ -890,7 +890,7 @@ void ScriptEngine::include(const QStringList& includeFiles, QScriptValue callbac · 27 unmodified lines
890890
891891 void ScriptEngine::include(const QString& includeFile, QScriptValue callback) {
892892 if (_stoppingAllScripts) {
893− qCDebug(scriptengine) << "Script.include() while shutting down is ignored... "
893+ qCDebug(scriptengine) << "Script.include() while shutting down is ignored... "
894894 << "includeFile:" << includeFile << "parent script:" << getFilename();
895895 return; // bail early
896896 }