migrate Agent to use standard APIs of ScriptEngine

This commit is contained in:
Brad Hefta-Gaub 2015-09-11 22:12:42 -07:00
parent 54c56a92f1
commit 56118e4204
4 changed files with 25 additions and 43 deletions

View file

@ -42,7 +42,7 @@ Agent::Agent(NLPacket& packet) :
DEFAULT_WINDOW_SECONDS_FOR_DESIRED_REDUCTION, false))
{
// be the parent of the script engine so it gets moved when we do
_scriptEngine.setParent(this);
_scriptEngine->setParent(this);
DependencyManager::get<EntityScriptingInterface>()->setPacketSender(&_entityEditSender);
@ -156,8 +156,10 @@ void Agent::run() {
qDebug() << "Downloaded script:" << scriptContents;
_scriptEngine = new ScriptEngine(scriptContents, _payload);
// setup an Avatar for the script to use
ScriptableAvatar scriptedAvatar(&_scriptEngine);
ScriptableAvatar scriptedAvatar(_scriptEngine);
scriptedAvatar.setForceFaceTrackerConnected(true);
// call model URL setters with empty URLs so our avatar, if user, will have the default models
@ -168,7 +170,7 @@ void Agent::run() {
setAvatarData(&scriptedAvatar, "Avatar");
auto avatarHashMap = DependencyManager::set<AvatarHashMap>();
_scriptEngine.registerGlobalObject("AvatarList", avatarHashMap.data());
_scriptEngine->registerGlobalObject("AvatarList", avatarHashMap.data());
auto& packetReceiver = DependencyManager::get<NodeList>()->getPacketReceiver();
packetReceiver.registerListener(PacketType::BulkAvatarData, avatarHashMap.data(), "processAvatarDataPacket");
@ -177,38 +179,36 @@ void Agent::run() {
packetReceiver.registerListener(PacketType::AvatarBillboard, avatarHashMap.data(), "processAvatarBillboardPacket");
// register ourselves to the script engine
_scriptEngine.registerGlobalObject("Agent", this);
if (!_payload.isEmpty()) {
_scriptEngine.setParentURL(_payload);
}
_scriptEngine->registerGlobalObject("Agent", this);
// FIXME -we shouldn't be calling this directly, it's normally called by run(), not sure why
// viewers would need this called.
_scriptEngine.init(); // must be done before we set up the viewers
//_scriptEngine->init(); // must be done before we set up the viewers
_scriptEngine.registerGlobalObject("SoundCache", DependencyManager::get<SoundCache>().data());
_scriptEngine->registerGlobalObject("SoundCache", DependencyManager::get<SoundCache>().data());
QScriptValue webSocketServerConstructorValue = _scriptEngine.newFunction(WebSocketServerClass::constructor);
_scriptEngine.globalObject().setProperty("WebSocketServer", webSocketServerConstructorValue);
QScriptValue webSocketServerConstructorValue = _scriptEngine->newFunction(WebSocketServerClass::constructor);
_scriptEngine->globalObject().setProperty("WebSocketServer", webSocketServerConstructorValue);
auto entityScriptingInterface = DependencyManager::get<EntityScriptingInterface>();
_scriptEngine.registerGlobalObject("EntityViewer", &_entityViewer);
_scriptEngine->registerGlobalObject("EntityViewer", &_entityViewer);
_entityViewer.setJurisdictionListener(entityScriptingInterface->getJurisdictionListener());
_entityViewer.init();
entityScriptingInterface->setEntityTree(_entityViewer.getTree());
// wire up our additional agent related processing to the update signal
QObject::connect(&_scriptEngine, &ScriptEngine::update, this, &Agent::processAgentAvatarAndAudio);
QObject::connect(_scriptEngine, &ScriptEngine::update, this, &Agent::processAgentAvatarAndAudio);
_scriptEngine.setScriptContents(scriptContents);
_scriptEngine.run();
_scriptEngine->run();
setFinished(true);
// kill the avatar identity timer
delete _avatarIdentityTimer;
// delete the script engine
delete _scriptEngine;
}
void Agent::setIsAvatar(bool isAvatar) {
@ -238,7 +238,7 @@ void Agent::setIsAvatar(bool isAvatar) {
void Agent::setAvatarData(AvatarData* avatarData, const QString& objectName) {
_avatarData = avatarData;
_scriptEngine.registerGlobalObject(objectName, avatarData);
_scriptEngine->registerGlobalObject(objectName, avatarData);
}
void Agent::sendAvatarIdentityPacket() {
@ -256,7 +256,7 @@ void Agent::sendAvatarBillboardPacket() {
void Agent::processAgentAvatarAndAudio(float deltaTime) {
qDebug() << "processAgentAvatarAndAudio()";
if (!_scriptEngine.isFinished() && _isAvatar && _avatarData) {
if (!_scriptEngine->isFinished() && _isAvatar && _avatarData) {
const int SCRIPT_AUDIO_BUFFER_SAMPLES = floor(((SCRIPT_DATA_CALLBACK_USECS * AudioConstants::SAMPLE_RATE)
/ (1000 * 1000)) + 0.5);
@ -361,7 +361,7 @@ void Agent::processAgentAvatarAndAudio(float deltaTime) {
}
void Agent::aboutToFinish() {
_scriptEngine.stop();
_scriptEngine->stop();
_pingTimer->stop();
delete _pingTimer;

View file

@ -61,7 +61,7 @@ private slots:
void processAgentAvatarAndAudio(float deltaTime);
private:
ScriptEngine _scriptEngine;
ScriptEngine* _scriptEngine;
EntityEditPacketSender _entityEditSender;
EntityTreeHeadlessViewer _entityViewer;
QTimer* _pingTimer;

View file

@ -221,16 +221,6 @@ QString ScriptEngine::getFilename() const {
}
bool ScriptEngine::setScriptContents(const QString& scriptContents, const QString& fileNameString) {
if (_isRunning) {
return false;
}
_scriptContents = scriptContents;
_fileNameString = fileNameString;
return true;
}
// FIXME - remove the file/url scheme code here and let the script cache handle things
void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) {
if (_isRunning) {

View file

@ -50,11 +50,14 @@ public:
~ScriptEngine();
/// Launch the script running in a dedicated thread. This will have the side effect of evalulating
/// run the script in a dedicated thread. This will have the side effect of evalulating
/// the current script contents and calling run(). Callers will likely want to register the script with external
/// services before calling this.
void runInThread();
/// run the script in the callers thread, exit when stop() is called.
void run();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NOTE - these are NOT intended to be public interfaces available to scripts, the are only Q_INVOKABLE so we can
// properly ensure they are only called on the correct thread
@ -147,6 +150,7 @@ protected:
bool _wantSignals = true;
private:
void init();
QString getFilename() const;
void waitTillDoneRunning();
bool evaluatePending() const { return _evaluatesPending > 0; }
@ -183,18 +187,6 @@ private:
// also used in Agent, but that would be fixed if Agent was using proper apis for loading content
void setParentURL(const QString& parentURL) { _parentURL = parentURL; }
private:
//FIXME- Agent shouldn't be using these methods directly -- these methods need to be depricated from the public interfaces
friend class Agent;
/// FIXME - DEPRICATED - remove callers and fix to use standard API
/// sets the script contents, will return false if failed, will fail if script is already running
bool setScriptContents(const QString& scriptContents, const QString& fileNameString = QString(""));
/// FIXME - these should only be used internally
void init();
void run();
};
#endif // hifi_ScriptEngine_h