From e2d7431456025f38a1f2c9941d81e27ff61da830 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 24 Jul 2018 16:54:24 +0200 Subject: [PATCH 1/2] move agent scripting interface to its own class to get rid of deprecated API --- assignment-client/src/Agent.cpp | 3 +- assignment-client/src/Agent.h | 32 +------- .../src/AgentScriptingInterface.cpp | 16 ++++ .../src/AgentScriptingInterface.h | 80 +++++++++++++++++++ libraries/networking/src/ThreadedAssignment.h | 22 ----- scripts/developer/tests/agentAPITest.js | 55 +++++++++++++ 6 files changed, 154 insertions(+), 54 deletions(-) create mode 100644 assignment-client/src/AgentScriptingInterface.cpp create mode 100644 assignment-client/src/AgentScriptingInterface.h create mode 100644 scripts/developer/tests/agentAPITest.js diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index 73444d1198..9b1d5d27e6 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -50,6 +50,7 @@ #include "entities/AssignmentParentFinder.h" #include "RecordingScriptingInterface.h" #include "AbstractAudioInterface.h" +#include "AgentScriptingInterface.h" static const int RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES = 10; @@ -452,7 +453,7 @@ void Agent::executeScript() { packetReceiver.registerListener(PacketType::AvatarIdentity, avatarHashMap.data(), "processAvatarIdentityPacket"); // register ourselves to the script engine - _scriptEngine->registerGlobalObject("Agent", this); + _scriptEngine->registerGlobalObject("Agent", new AgentScriptingInterface(this)); _scriptEngine->registerGlobalObject("SoundCache", DependencyManager::get().data()); _scriptEngine->registerGlobalObject("AnimationCache", DependencyManager::get().data()); diff --git a/assignment-client/src/Agent.h b/assignment-client/src/Agent.h index 0fc3fbe1f9..b55dea89be 100644 --- a/assignment-client/src/Agent.h +++ b/assignment-client/src/Agent.h @@ -33,19 +33,6 @@ #include "entities/EntityTreeHeadlessViewer.h" #include "avatars/ScriptableAvatar.h" -/**jsdoc - * @namespace Agent - * - * @hifi-assignment-client - * - * @property {boolean} isAvatar - * @property {boolean} isPlayingAvatarSound Read-only. - * @property {boolean} isListeningToAudioStream - * @property {boolean} isNoiseGateEnabled - * @property {number} lastReceivedAudioLoudness Read-only. - * @property {Uuid} sessionUUID Read-only. - */ - class Agent : public ThreadedAssignment { Q_OBJECT @@ -73,28 +60,11 @@ public: virtual void aboutToFinish() override; public slots: - /**jsdoc - * @function Agent.run - * @deprecated This function is being removed from the API. - */ void run() override; - /**jsdoc - * @function Agent.playAvatarSound - * @param {object} avatarSound - */ void playAvatarSound(SharedSoundPointer avatarSound); - - /**jsdoc - * @function Agent.setIsAvatar - * @param {boolean} isAvatar - */ - void setIsAvatar(bool isAvatar); - /**jsdoc - * @function Agent.isAvatar - * @returns {boolean} - */ + void setIsAvatar(bool isAvatar); bool isAvatar() const { return _isAvatar; } private slots: diff --git a/assignment-client/src/AgentScriptingInterface.cpp b/assignment-client/src/AgentScriptingInterface.cpp new file mode 100644 index 0000000000..9fe1897b94 --- /dev/null +++ b/assignment-client/src/AgentScriptingInterface.cpp @@ -0,0 +1,16 @@ +// +// AgentScriptingInterface.cpp +// assignment-client/src +// +// Created by Thijs Wenker on 7/23/18. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "AgentScriptingInterface.h" + +AgentScriptingInterface::AgentScriptingInterface(Agent* agent) : + _agent(agent) +{ } diff --git a/assignment-client/src/AgentScriptingInterface.h b/assignment-client/src/AgentScriptingInterface.h new file mode 100644 index 0000000000..e35d85d130 --- /dev/null +++ b/assignment-client/src/AgentScriptingInterface.h @@ -0,0 +1,80 @@ +// +// AgentScriptingInterface.h +// assignment-client/src +// +// Created by Thijs Wenker on 7/23/18. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#pragma once + +#ifndef hifi_AgentScriptingInterface_h +#define hifi_AgentScriptingInterface_h + +#include + +#include "Agent.h" + +/**jsdoc +* @namespace Agent +* +* @hifi-assignment-client +* +* @property {boolean} isAvatar +* @property {boolean} isPlayingAvatarSound Read-only. +* @property {boolean} isListeningToAudioStream +* @property {boolean} isNoiseGateEnabled +* @property {number} lastReceivedAudioLoudness Read-only. +* @property {Uuid} sessionUUID Read-only. +*/ +class AgentScriptingInterface : public QObject { + Q_OBJECT + Q_PROPERTY(bool isAvatar READ isAvatar WRITE setIsAvatar) + Q_PROPERTY(bool isPlayingAvatarSound READ isPlayingAvatarSound) + Q_PROPERTY(bool isListeningToAudioStream READ isListeningToAudioStream WRITE setIsListeningToAudioStream) + Q_PROPERTY(bool isNoiseGateEnabled READ isNoiseGateEnabled WRITE setIsNoiseGateEnabled) + Q_PROPERTY(float lastReceivedAudioLoudness READ getLastReceivedAudioLoudness) + Q_PROPERTY(QUuid sessionUUID READ getSessionUUID) + +public: + AgentScriptingInterface(Agent* agent); + + bool isPlayingAvatarSound() const { return _agent->isPlayingAvatarSound(); } + + bool isListeningToAudioStream() const { return _agent->isListeningToAudioStream(); } + void setIsListeningToAudioStream(bool isListeningToAudioStream) const { _agent->setIsListeningToAudioStream(isListeningToAudioStream); } + + bool isNoiseGateEnabled() const { return _agent->isNoiseGateEnabled(); } + void setIsNoiseGateEnabled(bool isNoiseGateEnabled) const { _agent->setIsNoiseGateEnabled(isNoiseGateEnabled); } + + float getLastReceivedAudioLoudness() const { return _agent->getLastReceivedAudioLoudness(); } + QUuid getSessionUUID() const { return _agent->getSessionUUID(); } + +public slots: + /**jsdoc + * @function Agent.setIsAvatar + * @param {boolean} isAvatar + */ + void setIsAvatar(bool isAvatar) const { _agent->setIsAvatar(isAvatar); } + + /**jsdoc + * @function Agent.isAvatar + * @returns {boolean} + */ + bool isAvatar() const { return _agent->isAvatar(); } + + /**jsdoc + * @function Agent.playAvatarSound + * @param {object} avatarSound + */ + void playAvatarSound(SharedSoundPointer avatarSound) const { _agent->playAvatarSound(avatarSound); } + +private: + Agent* _agent; + +}; + + +#endif // hifi_AgentScriptingInterface_h diff --git a/libraries/networking/src/ThreadedAssignment.h b/libraries/networking/src/ThreadedAssignment.h index 9372cfa667..a1737641c1 100644 --- a/libraries/networking/src/ThreadedAssignment.h +++ b/libraries/networking/src/ThreadedAssignment.h @@ -29,34 +29,16 @@ public: void addPacketStatsAndSendStatsPacket(QJsonObject statsObject); public slots: - // JSDoc: Overridden in Agent.h. /// threaded run of assignment virtual void run() = 0; - /**jsdoc - * @function Agent.stop - * @deprecated This function is being removed from the API. - */ Q_INVOKABLE virtual void stop() { setFinished(true); } - /**jsdoc - * @function Agent.sendStatsPacket - * @deprecated This function is being removed from the API. - */ virtual void sendStatsPacket(); - /**jsdoc - * @function Agent.clearQueuedCheckIns - * @deprecated This function is being removed from the API. - */ void clearQueuedCheckIns() { _numQueuedCheckIns = 0; } signals: - /**jsdoc - * @function Agent.finished - * @returns {Signal} - * @deprecated This function is being removed from the API. - */ void finished(); protected: @@ -68,10 +50,6 @@ protected: int _numQueuedCheckIns { 0 }; protected slots: - /**jsdoc - * @function Agent.domainSettingsRequestFailed - * @deprecated This function is being removed from the API. - */ void domainSettingsRequestFailed(); private slots: diff --git a/scripts/developer/tests/agentAPITest.js b/scripts/developer/tests/agentAPITest.js new file mode 100644 index 0000000000..b7d21efbdf --- /dev/null +++ b/scripts/developer/tests/agentAPITest.js @@ -0,0 +1,55 @@ +// agentAPITest.js +// scripts/developer/tests +// +// Created by Thijs Wenker on 7/23/18. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html + +var SOUND_DATA = { url: "http://hifi-content.s3.amazonaws.com/howard/sounds/piano1.wav" }; + +// getSound function from crowd-agent.js +function getSound(data, callback) { // callback(sound) when downloaded (which may be immediate). + var sound = SoundCache.getSound(data.url); + if (sound.downloaded) { + return callback(sound); + } + function onDownloaded() { + sound.ready.disconnect(onDownloaded); + callback(sound); + } + sound.ready.connect(onDownloaded); +} + + +function agentAPITest() { + console.warn('Agent.isAvatar =', Agent.isAvatar); + + Agent.isAvatar = true; + console.warn('Agent.isAvatar =', Agent.isAvatar); + + console.warn('Agent.isListeningToAudioStream =', Agent.isListeningToAudioStream); + + Agent.isListeningToAudioStream = true; + console.warn('Agent.isListeningToAudioStream =', Agent.isListeningToAudioStream); + + console.warn('Agent.isNoiseGateEnabled =', Agent.isNoiseGateEnabled); + + Agent.isNoiseGateEnabled = true; + console.warn('Agent.isNoiseGateEnabled =', Agent.isNoiseGateEnabled); + console.warn('Agent.lastReceivedAudioLoudness =', Agent.lastReceivedAudioLoudness); + console.warn('Agent.sessionUUID =', Agent.sessionUUID); + + getSound(SOUND_DATA, function (sound) { + console.warn('Agent.isPlayingAvatarSound =', Agent.isPlayingAvatarSound); + Agent.playAvatarSound(sound); + console.warn('Agent.isPlayingAvatarSound =', Agent.isPlayingAvatarSound); + }); +} + +if (Script.context === "agent") { + agentAPITest(); +} else { + console.error('This script should be run as agent script. EXITING.'); +} From 92efc1a3b52c26d6e87584da4010c2c0743f089a Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Mon, 30 Jul 2018 18:51:19 +0200 Subject: [PATCH 2/2] style and memory leak fixes --- .../src/AgentScriptingInterface.cpp | 1 + .../src/AgentScriptingInterface.h | 22 +++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/assignment-client/src/AgentScriptingInterface.cpp b/assignment-client/src/AgentScriptingInterface.cpp index 9fe1897b94..3000c2b96f 100644 --- a/assignment-client/src/AgentScriptingInterface.cpp +++ b/assignment-client/src/AgentScriptingInterface.cpp @@ -12,5 +12,6 @@ #include "AgentScriptingInterface.h" AgentScriptingInterface::AgentScriptingInterface(Agent* agent) : + QObject(agent), _agent(agent) { } diff --git a/assignment-client/src/AgentScriptingInterface.h b/assignment-client/src/AgentScriptingInterface.h index e35d85d130..9fa7688778 100644 --- a/assignment-client/src/AgentScriptingInterface.h +++ b/assignment-client/src/AgentScriptingInterface.h @@ -18,17 +18,17 @@ #include "Agent.h" /**jsdoc -* @namespace Agent -* -* @hifi-assignment-client -* -* @property {boolean} isAvatar -* @property {boolean} isPlayingAvatarSound Read-only. -* @property {boolean} isListeningToAudioStream -* @property {boolean} isNoiseGateEnabled -* @property {number} lastReceivedAudioLoudness Read-only. -* @property {Uuid} sessionUUID Read-only. -*/ + * @namespace Agent + * + * @hifi-assignment-client + * + * @property {boolean} isAvatar + * @property {boolean} isPlayingAvatarSound Read-only. + * @property {boolean} isListeningToAudioStream + * @property {boolean} isNoiseGateEnabled + * @property {number} lastReceivedAudioLoudness Read-only. + * @property {Uuid} sessionUUID Read-only. + */ class AgentScriptingInterface : public QObject { Q_OBJECT Q_PROPERTY(bool isAvatar READ isAvatar WRITE setIsAvatar)