3
0
Fork 0
mirror of https://github.com/lubosz/overte.git synced 2025-04-27 18:15:30 +02:00

first cut at splitting out scripting engine from agent to add to client

This commit is contained in:
ZappoMan 2013-12-14 08:14:38 -08:00
parent 02d3d384ca
commit e2fbb7beb4
12 changed files with 318 additions and 31 deletions

View file

@ -24,6 +24,7 @@
Agent::Agent(const unsigned char* dataBuffer, int numBytes) :
ThreadedAssignment(dataBuffer, numBytes)
{
_particleScriptingInterface.init();
}
void Agent::processDatagram(const QByteArray& dataByteArray, const HifiSockAddr& senderSockAddr) {

View file

@ -87,6 +87,7 @@ link_hifi_library(voxels ${TARGET_NAME} ${ROOT_DIR})
link_hifi_library(particles ${TARGET_NAME} ${ROOT_DIR})
link_hifi_library(avatars ${TARGET_NAME} ${ROOT_DIR})
link_hifi_library(audio ${TARGET_NAME} ${ROOT_DIR})
link_hifi_library(scriptengine ${TARGET_NAME} ${ROOT_DIR})
# find required libraries
find_package(Faceshift)

View file

@ -139,7 +139,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) :
_recentMaxPackets(0),
_resetRecentMaxPacketsSoon(true),
_swatch(NULL),
_pasteMode(false)
_pasteMode(false),
_scriptEngine(NULL)
{
_applicationStartupTime = startup_time;
_window->setWindowTitle("Interface");
@ -4385,3 +4386,62 @@ void Application::packetSentNotification(ssize_t length) {
_bandwidthMeter.outputStream(BandwidthMeter::VOXELS).updateValue(length);
}
void Application::loadScript() {
// shut down and stop any existing script
if (_scriptEngine) {
_scriptEngine->stop();
_scriptEngine = NULL;
}
QString desktopLocation = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QString suggestedName = desktopLocation.append("/script.js");
QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Open Script"), suggestedName,
tr("JavaScript Files (*.js)"));
QByteArray fileNameAscii = fileNameString.toLocal8Bit();
const char* fileName = fileNameAscii.data();
printf("fileName:%s\n",fileName);
std::ifstream file(fileName, std::ios::in|std::ios::binary|std::ios::ate);
if(!file.is_open()) {
printf("error loading file\n");
return;
}
qDebug("loading file %s...\n", fileName);
// get file length....
unsigned long fileLength = file.tellg();
file.seekg( 0, std::ios::beg );
// read the entire file into a buffer, WHAT!? Why not.
char* entireFile = new char[fileLength];
file.read((char*)entireFile, fileLength);
file.close();
QString script(entireFile);
delete[] entireFile;
// start the script on a new thread...
_scriptEngine = new ScriptEngine(script);
QThread* workerThread = new QThread(this);
connect(workerThread, SIGNAL(started()), _scriptEngine, SLOT(run()));
connect(_scriptEngine, SIGNAL(finished()), this, SLOT(assignmentCompleted()));
connect(_scriptEngine, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(_scriptEngine, SIGNAL(finished()), _scriptEngine, SLOT(deleteLater()));
connect(_scriptEngine, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
_scriptEngine->moveToThread(workerThread);
// Starts an event loop, and emits workerThread->started()
workerThread->start();
// restore the main window's active state
_window->activateWindow();
}

View file

@ -24,6 +24,7 @@
#include <PacketHeaders.h>
#include <ParticleCollisionSystem.h>
#include <ParticleEditPacketSender.h>
#include <ScriptEngine.h>
#include <VoxelQuery.h>
#ifndef _WIN32
@ -217,6 +218,7 @@ public slots:
void doKillLocalVoxels();
void decreaseVoxelSize();
void increaseVoxelSize();
void loadScript();
private slots:
@ -495,6 +497,8 @@ private:
std::vector<VoxelFade> _voxelFades;
std::vector<Avatar*> _avatarFades;
ScriptEngine* _scriptEngine;
};
#endif /* defined(__interface__Application__) */

View file

@ -90,6 +90,9 @@ Menu::Menu() :
this,
SLOT(login())));
addDisabledActionAndSeparator(fileMenu, "Scripts");
addActionToQMenuAndActionHash(fileMenu, MenuOption::LoadScript, Qt::CTRL | Qt::Key_O, appInstance, SLOT(loadScript()));
addDisabledActionAndSeparator(fileMenu, "Voxels");
addActionToQMenuAndActionHash(fileMenu, MenuOption::ExportVoxels, Qt::CTRL | Qt::Key_E, appInstance, SLOT(exportVoxels()));
addActionToQMenuAndActionHash(fileMenu, MenuOption::ImportVoxels, Qt::CTRL | Qt::Key_I, appInstance, SLOT(importVoxels()));

View file

@ -217,6 +217,7 @@ namespace MenuOption {
const QString OldVoxelCullingMode = "Old Voxel Culling Mode";
const QString TurnWithHead = "Turn using Head";
const QString ClickToFly = "Fly to voxel on click";
const QString LoadScript = "Open and Run Script...";
const QString Oscilloscope = "Audio Oscilloscope";
const QString Pair = "Pair";
const QString PasteVoxels = "Paste";

View file

@ -415,6 +415,9 @@ void Particle::update() {
void Particle::runScript() {
if (!_updateScript.isEmpty()) {
qDebug() << "Script: " << _updateScript << "\n";
QScriptEngine engine;
// register meta-type for glm::vec3 and rgbColor conversions

View file

@ -8,19 +8,56 @@
#include "ParticleScriptingInterface.h"
ParticleScriptingInterface::ParticleScriptingInterface() :
_jurisdictionListener(NODE_TYPE_PARTICLE_SERVER),
ParticleScriptingInterface::ParticleScriptingInterface(ParticleEditPacketSender* particleSender,
JurisdictionListener* particleJurisdictionListener) :
_nextCreatorTokenID(0)
{
_jurisdictionListener.initialize(true);
_particlePacketSender.setServerJurisdictions(_jurisdictionListener.getJurisdictions());
setParticlePacketSender(particleSender);
setJurisdictionListener(particleJurisdictionListener);
}
ParticleScriptingInterface::~ParticleScriptingInterface() {
if (_managedJuridiciontListerner) {
delete _jurisdictionListener;
}
if (_managedPacketSender) {
delete _particlePacketSender;
}
}
void ParticleScriptingInterface::setParticlePacketSender(ParticleEditPacketSender* particlePacketSender) {
_particlePacketSender = particlePacketSender;
}
void ParticleScriptingInterface::setJurisdictionListener(JurisdictionListener* jurisdictionListener) {
_jurisdictionListener = jurisdictionListener;
}
void ParticleScriptingInterface::init() {
if (_jurisdictionListener) {
_managedJuridiciontListerner = false;
} else {
_managedJuridiciontListerner = true;
_jurisdictionListener = new JurisdictionListener(NODE_TYPE_PARTICLE_SERVER);
_jurisdictionListener->initialize(true);
}
if (_particlePacketSender) {
_managedPacketSender = false;
} else {
_managedPacketSender = true;
_particlePacketSender = new ParticleEditPacketSender();
_particlePacketSender->setServerJurisdictions(_jurisdictionListener->getJurisdictions());
}
}
void ParticleScriptingInterface::queueParticleAdd(PACKET_TYPE addPacketType, ParticleDetail& addParticleDetails) {
_particlePacketSender.queueParticleEditMessages(addPacketType, 1, &addParticleDetails);
_particlePacketSender->queueParticleEditMessages(addPacketType, 1, &addParticleDetails);
}
uint32_t ParticleScriptingInterface::queueParticleAdd(glm::vec3 position, float radius,
unsigned int ParticleScriptingInterface::queueParticleAdd(glm::vec3 position, float radius,
xColor color, glm::vec3 velocity, glm::vec3 gravity, float damping, QString updateScript) {
// The application will keep track of creatorTokenID

View file

@ -18,71 +18,79 @@
class ParticleScriptingInterface : public QObject {
Q_OBJECT
public:
ParticleScriptingInterface();
ParticleEditPacketSender* getParticlePacketSender() { return &_particlePacketSender; }
JurisdictionListener* getJurisdictionListener() { return &_jurisdictionListener; }
ParticleScriptingInterface(ParticleEditPacketSender* particleSender = NULL,
JurisdictionListener* particleJurisdictionListener = NULL);
~ParticleScriptingInterface();
ParticleEditPacketSender* getParticlePacketSender() const { return _particlePacketSender; }
JurisdictionListener* getJurisdictionListener() const { return _jurisdictionListener; }
void setParticlePacketSender(ParticleEditPacketSender* particlePacketSender);
void setJurisdictionListener(JurisdictionListener* jurisdictionListener);
void init();
public slots:
/// queues the creation of a Particle which will be sent by calling process on the PacketSender
/// returns the creatorTokenID for the newly created particle
uint32_t queueParticleAdd(glm::vec3 position, float radius,
unsigned int queueParticleAdd(glm::vec3 position, float radius,
xColor color, glm::vec3 velocity, glm::vec3 gravity, float damping, QString updateScript);
/// Set the desired max packet size in bytes that should be created
void setMaxPacketSize(int maxPacketSize) { return _particlePacketSender.setMaxPacketSize(maxPacketSize); }
void setMaxPacketSize(int maxPacketSize) { return _particlePacketSender->setMaxPacketSize(maxPacketSize); }
/// returns the current desired max packet size in bytes that will be created
int getMaxPacketSize() const { return _particlePacketSender.getMaxPacketSize(); }
int getMaxPacketSize() const { return _particlePacketSender->getMaxPacketSize(); }
/// set the max packets per second send rate
void setPacketsPerSecond(int packetsPerSecond) { return _particlePacketSender.setPacketsPerSecond(packetsPerSecond); }
void setPacketsPerSecond(int packetsPerSecond) { return _particlePacketSender->setPacketsPerSecond(packetsPerSecond); }
/// get the max packets per second send rate
int getPacketsPerSecond() const { return _particlePacketSender.getPacketsPerSecond(); }
int getPacketsPerSecond() const { return _particlePacketSender->getPacketsPerSecond(); }
/// does a particle server exist to send to
bool serversExist() const { return _particlePacketSender.serversExist(); }
bool serversExist() const { return _particlePacketSender->serversExist(); }
/// are there packets waiting in the send queue to be sent
bool hasPacketsToSend() const { return _particlePacketSender.hasPacketsToSend(); }
bool hasPacketsToSend() const { return _particlePacketSender->hasPacketsToSend(); }
/// how many packets are there in the send queue waiting to be sent
int packetsToSendCount() const { return _particlePacketSender.packetsToSendCount(); }
int packetsToSendCount() const { return _particlePacketSender->packetsToSendCount(); }
/// returns the packets per second send rate of this object over its lifetime
float getLifetimePPS() const { return _particlePacketSender.getLifetimePPS(); }
float getLifetimePPS() const { return _particlePacketSender->getLifetimePPS(); }
/// returns the bytes per second send rate of this object over its lifetime
float getLifetimeBPS() const { return _particlePacketSender.getLifetimeBPS(); }
float getLifetimeBPS() const { return _particlePacketSender->getLifetimeBPS(); }
/// returns the packets per second queued rate of this object over its lifetime
float getLifetimePPSQueued() const { return _particlePacketSender.getLifetimePPSQueued(); }
float getLifetimePPSQueued() const { return _particlePacketSender->getLifetimePPSQueued(); }
/// returns the bytes per second queued rate of this object over its lifetime
float getLifetimeBPSQueued() const { return _particlePacketSender.getLifetimeBPSQueued(); }
float getLifetimeBPSQueued() const { return _particlePacketSender->getLifetimeBPSQueued(); }
/// returns lifetime of this object from first packet sent to now in usecs
long long unsigned int getLifetimeInUsecs() const { return _particlePacketSender.getLifetimeInUsecs(); }
long long unsigned int getLifetimeInUsecs() const { return _particlePacketSender->getLifetimeInUsecs(); }
/// returns lifetime of this object from first packet sent to now in usecs
float getLifetimeInSeconds() const { return _particlePacketSender.getLifetimeInSeconds(); }
float getLifetimeInSeconds() const { return _particlePacketSender->getLifetimeInSeconds(); }
/// returns the total packets sent by this object over its lifetime
long long unsigned int getLifetimePacketsSent() const { return _particlePacketSender.getLifetimePacketsSent(); }
long long unsigned int getLifetimePacketsSent() const { return _particlePacketSender->getLifetimePacketsSent(); }
/// returns the total bytes sent by this object over its lifetime
long long unsigned int getLifetimeBytesSent() const { return _particlePacketSender.getLifetimeBytesSent(); }
long long unsigned int getLifetimeBytesSent() const { return _particlePacketSender->getLifetimeBytesSent(); }
/// returns the total packets queued by this object over its lifetime
long long unsigned int getLifetimePacketsQueued() const { return _particlePacketSender.getLifetimePacketsQueued(); }
long long unsigned int getLifetimePacketsQueued() const { return _particlePacketSender->getLifetimePacketsQueued(); }
/// returns the total bytes queued by this object over its lifetime
long long unsigned int getLifetimeBytesQueued() const { return _particlePacketSender.getLifetimeBytesQueued(); }
long long unsigned int getLifetimeBytesQueued() const { return _particlePacketSender->getLifetimeBytesQueued(); }
private:
/// attached ParticleEditPacketSender that handles queuing and sending of packets to VS
ParticleEditPacketSender _particlePacketSender;
JurisdictionListener _jurisdictionListener;
ParticleEditPacketSender* _particlePacketSender;
JurisdictionListener* _jurisdictionListener;
bool _managedPacketSender;
bool _managedJuridiciontListerner;
void queueParticleAdd(PACKET_TYPE addPacketType, ParticleDetail& addParticleDetails);

View file

@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 2.8)
set(ROOT_DIR ../..)
set(MACRO_DIR ${ROOT_DIR}/cmake/macros)
# setup for find modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/modules/")
set(TARGET_NAME scriptengine)
find_package(Qt5Widgets REQUIRED)
include(${MACRO_DIR}/SetupHifiLibrary.cmake)
setup_hifi_library(${TARGET_NAME})
qt5_use_modules(${TARGET_NAME} Widgets)
include(${MACRO_DIR}/IncludeGLM.cmake)
include_glm(${TARGET_NAME} ${ROOT_DIR})
include(${MACRO_DIR}/LinkHifiLibrary.cmake)
link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR})
link_hifi_library(octree ${TARGET_NAME} ${ROOT_DIR})
link_hifi_library(voxels ${TARGET_NAME} ${ROOT_DIR})
link_hifi_library(particles ${TARGET_NAME} ${ROOT_DIR})
# link ZLIB
find_package(ZLIB)
include_directories(${ZLIB_INCLUDE_DIRS})
target_link_libraries(${TARGET_NAME} ${ZLIB_LIBRARIES})

View file

@ -0,0 +1,94 @@
//
// Agent.cpp
// hifi
//
// Created by Stephen Birarda on 7/1/13.
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#include <QtCore/QCoreApplication>
#include <QtCore/QEventLoop>
#include <QtCore/QTimer>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <AvatarData.h>
#include <NodeList.h>
#include <PacketHeaders.h>
#include <UUID.h>
#include <VoxelConstants.h>
#include "ScriptEngine.h"
ScriptEngine::ScriptEngine(QString scriptContents) {
_scriptContents = scriptContents;
_isFinished = false;
}
void ScriptEngine::run() {
QScriptEngine engine;
// register meta-type for glm::vec3 conversions
registerMetaTypes(&engine);
QScriptValue agentValue = engine.newQObject(this);
engine.globalObject().setProperty("Agent", agentValue);
QScriptValue voxelScripterValue = engine.newQObject(&_voxelScriptingInterface);
engine.globalObject().setProperty("Voxels", voxelScripterValue);
QScriptValue particleScripterValue = engine.newQObject(&_particleScriptingInterface);
engine.globalObject().setProperty("Particles", particleScripterValue);
QScriptValue treeScaleValue = engine.newVariant(QVariant(TREE_SCALE));
engine.globalObject().setProperty("TREE_SCALE", treeScaleValue);
const unsigned int VISUAL_DATA_CALLBACK_USECS = (1.0 / 60.0) * 1000 * 1000;
// let the VoxelPacketSender know how frequently we plan to call it
_voxelScriptingInterface.getVoxelPacketSender()->setProcessCallIntervalHint(VISUAL_DATA_CALLBACK_USECS);
_particleScriptingInterface.getParticlePacketSender()->setProcessCallIntervalHint(VISUAL_DATA_CALLBACK_USECS);
QScriptValue result = engine.evaluate(_scriptContents);
qDebug() << "Evaluated script.\n";
if (engine.hasUncaughtException()) {
int line = engine.uncaughtExceptionLineNumber();
qDebug() << "Uncaught exception at line" << line << ":" << result.toString() << "\n";
}
timeval startTime;
gettimeofday(&startTime, NULL);
int thisFrame = 0;
qDebug() << "before while... thisFrame:" << thisFrame << "\n";
while (!_isFinished) {
qDebug() << "while... thisFrame:" << thisFrame << "\n";
int usecToSleep = usecTimestamp(&startTime) + (thisFrame++ * VISUAL_DATA_CALLBACK_USECS) - usecTimestampNow();
if (usecToSleep > 0) {
usleep(usecToSleep);
}
QCoreApplication::processEvents();
bool willSendVisualDataCallBack = true;
if (willSendVisualDataCallBack) {
qDebug() << "willSendVisualDataCallback thisFrame:" << thisFrame << "\n";
emit willSendVisualDataCallback();
}
if (engine.hasUncaughtException()) {
int line = engine.uncaughtExceptionLineNumber();
qDebug() << "Uncaught exception at line" << line << ":" << engine.uncaughtException().toString() << "\n";
}
}
}

View file

@ -0,0 +1,45 @@
//
// ScriptEngine.h
// hifi
//
// Created by Stephen Birarda on 7/1/13.
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#ifndef __hifi__ScriptEngine__
#define __hifi__ScriptEngine__
#include <vector>
#include <QtScript/QScriptEngine>
#include <QtCore/QObject>
#include <QtCore/QUrl>
#include <VoxelScriptingInterface.h>
#include <ParticleScriptingInterface.h>
class ScriptEngine : public QObject {
Q_OBJECT
public:
ScriptEngine(QString scriptContents);
public slots:
void run();
void stop() {
_isFinished = true;
emit finished();
}
signals:
void willSendAudioDataCallback();
void willSendVisualDataCallback();
void finished();
protected:
QString _scriptContents;
bool _isFinished;
private:
VoxelScriptingInterface _voxelScriptingInterface;
ParticleScriptingInterface _particleScriptingInterface;
};
#endif /* defined(__hifi__ScriptEngine__) */