Skip to content

Commit

Permalink
INCOMPLETE COMMIT - WILL BE FORCE-PUSHED
Browse files Browse the repository at this point in the history
attempt to isolate calls to QtScript to an interface we control
  • Loading branch information
odysseus654 committed May 9, 2021
1 parent ba2d93c commit cbe608a
Show file tree
Hide file tree
Showing 259 changed files with 7,007 additions and 4,996 deletions.
2 changes: 1 addition & 1 deletion assignment-client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(TARGET_NAME assignment-client)

setup_hifi_project(Core Gui Network Script Quick WebSockets)
setup_hifi_project(Core Gui Network Quick WebSockets)

# Fix up the rpath so macdeployqt works
if (APPLE)
Expand Down
44 changes: 24 additions & 20 deletions assignment-client/src/Agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
#include <DebugDraw.h>
#include <EntityScriptingInterface.h>
#include <LocationScriptingInterface.h>
#include <AudioScriptingInterface.h>
#include <MessagesClient.h>
#include <NetworkAccessManager.h>
#include <NodeList.h>
#include <udt/PacketHeaders.h>
#include <ResourceCache.h>
#include <ResourceScriptingInterface.h>
#include <ScriptCache.h>
#include <ScriptEngine.h>
#include <ScriptEngines.h>
#include <ScriptManager.h>
#include <SoundCacheScriptingInterface.h>
#include <SoundCache.h>
#include <UserActivityLoggerScriptingInterface.h>
Expand Down Expand Up @@ -180,7 +183,7 @@ static const QString AGENT_LOGGING_NAME = "agent";

void Agent::run() {
// Create ScriptEngines on threaded-assignment thread then move to main thread.
DependencyManager::set<ScriptEngines>(ScriptEngine::AGENT_SCRIPT)->moveToThread(qApp->thread());
DependencyManager::set<ScriptEngines>(ScriptManager::AGENT_SCRIPT)->moveToThread(qApp->thread());

DependencyManager::set<ScriptCache>();

Expand Down Expand Up @@ -372,7 +375,7 @@ void Agent::executeScript() {
// the following block is scoped so that any shared pointers we take here
// are cleared before we call setFinished at the end of the function
{
_scriptEngine = scriptEngineFactory(ScriptEngine::AGENT_SCRIPT, _scriptContents, _payload);
_scriptManager = scriptManagerFactory(ScriptManager::AGENT_SCRIPT, _scriptContents, _payload);

// setup an Avatar for the script to use
auto scriptedAvatar = DependencyManager::get<ScriptableAvatar>();
Expand All @@ -386,10 +389,11 @@ void Agent::executeScript() {
scriptedAvatar->getHeadOrientation();

// give this AvatarData object to the script engine
_scriptEngine->registerGlobalObject("Avatar", scriptedAvatar.data());
auto scriptEngine = _scriptManager->engine();
scriptEngine->registerGlobalObject("Avatar", scriptedAvatar.data());

// give scripts access to the Users object
_scriptEngine->registerGlobalObject("Users", DependencyManager::get<UsersScriptingInterface>().data());
scriptEngine->registerGlobalObject("Users", DependencyManager::get<UsersScriptingInterface>().data());

auto player = DependencyManager::get<recording::Deck>();
connect(player.data(), &recording::Deck::playbackStateChanged, [&player, &scriptedAvatar] {
Expand Down Expand Up @@ -493,26 +497,26 @@ void Agent::executeScript() {
});

auto avatarHashMap = DependencyManager::set<AvatarHashMap>();
_scriptEngine->registerGlobalObject("AvatarList", avatarHashMap.data());
scriptEngine->registerGlobalObject("AvatarList", avatarHashMap.data());

// register ourselves to the script engine
_scriptEngine->registerGlobalObject("Agent", new AgentScriptingInterface(this));
scriptEngine->registerGlobalObject("Agent", new AgentScriptingInterface(this));

_scriptEngine->registerGlobalObject("AnimationCache", DependencyManager::get<AnimationCacheScriptingInterface>().data());
_scriptEngine->registerGlobalObject("SoundCache", DependencyManager::get<SoundCacheScriptingInterface>().data());
scriptEngine->registerGlobalObject("AnimationCache", DependencyManager::get<AnimationCacheScriptingInterface>().data());
scriptEngine->registerGlobalObject("SoundCache", DependencyManager::get<SoundCacheScriptingInterface>().data());

QScriptValue webSocketServerConstructorValue = _scriptEngine->newFunction(WebSocketServerClass::constructor);
_scriptEngine->globalObject().setProperty("WebSocketServer", webSocketServerConstructorValue);
ScriptValuePointer webSocketServerConstructorValue = scriptEngine->newFunction(WebSocketServerClass::constructor);
scriptEngine->globalObject()->setProperty("WebSocketServer", webSocketServerConstructorValue);

auto entityScriptingInterface = DependencyManager::get<EntityScriptingInterface>();

_scriptEngine->registerGlobalObject("EntityViewer", &_entityViewer);
scriptEngine->registerGlobalObject("EntityViewer", &_entityViewer);

_scriptEngine->registerGetterSetter("location", LocationScriptingInterface::locationGetter,
scriptEngine->registerGetterSetter("location", LocationScriptingInterface::locationGetter,
LocationScriptingInterface::locationSetter);

auto recordingInterface = DependencyManager::get<RecordingScriptingInterface>();
_scriptEngine->registerGlobalObject("Recording", recordingInterface.data());
scriptEngine->registerGlobalObject("Recording", recordingInterface.data());

entityScriptingInterface->init();

Expand All @@ -522,8 +526,8 @@ void Agent::executeScript() {

DependencyManager::set<AssignmentParentFinder>(_entityViewer.getTree());

DependencyManager::get<ScriptEngines>()->runScriptInitializers(_scriptEngine);
_scriptEngine->run();
DependencyManager::get<ScriptEngines>()->runScriptInitializers(_scriptManager);
_scriptManager->run();

Frame::clearFrameHandler(AUDIO_FRAME_TYPE);
Frame::clearFrameHandler(AVATAR_FRAME_TYPE);
Expand Down Expand Up @@ -602,7 +606,7 @@ void Agent::setIsAvatar(bool isAvatar) {
// start the timer
_avatarQueryTimer->start(AVATAR_VIEW_PACKET_SEND_INTERVAL_MSECS);

connect(_scriptEngine.data(), &ScriptEngine::update,
connect(_scriptManager.data(), &ScriptManager::update,
scriptableAvatar.data(), &ScriptableAvatar::update, Qt::QueuedConnection);

// tell the avatarAudioTimer to start ticking
Expand Down Expand Up @@ -638,7 +642,7 @@ void Agent::setIsAvatar(bool isAvatar) {
nodeList->sendPacket(std::move(packet), *node);
});

disconnect(_scriptEngine.data(), &ScriptEngine::update,
disconnect(_scriptManager.data(), &ScriptManager::update,
scriptableAvatar.data(), &ScriptableAvatar::update);

QMetaObject::invokeMethod(&_avatarAudioTimer, "stop");
Expand Down Expand Up @@ -875,7 +879,7 @@ void Agent::aboutToFinish() {

// drop our shared pointer to the script engine, then ask ScriptEngines to shutdown scripting
// this ensures that the ScriptEngine goes down before ScriptEngines
_scriptEngine.clear();
_scriptManager.clear();

{
DependencyManager::get<ScriptEngines>()->shutdownScripting();
Expand All @@ -895,8 +899,8 @@ void Agent::aboutToFinish() {
}

void Agent::stop() {
if (_scriptEngine) {
_scriptEngine->stop();
if (_scriptManager) {
_scriptManager->stop();
} else {
setFinished(true);
}
Expand Down
10 changes: 8 additions & 2 deletions assignment-client/src/Agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include <memory>
#include <vector>

#include <QtScript/QScriptEngine>
#include <QtCore/QObject>
#include <QtCore/QUrl>
#include <QtCore/QTimer>
#include <QUuid>
#include <QtCore/QSharedPointer>

#include <EntityEditPacketSender.h>
#include <EntityTree.h>
Expand All @@ -28,11 +28,17 @@

#include <plugins/CodecPlugin.h>

#include <Sound.h>
#include "AudioGate.h"
#include "MixedAudioStream.h"
#include "entities/EntityTreeHeadlessViewer.h"
#include "avatars/ScriptableAvatar.h"

class ScriptEngine;
class ScriptManager;
using ScriptEnginePointer = QSharedPointer<ScriptEngine>;
using ScriptManagerPointer = QSharedPointer<ScriptManager>;

class Agent : public ThreadedAssignment {
Q_OBJECT

Expand Down Expand Up @@ -89,7 +95,7 @@ private slots:
void encodeFrameOfZeros(QByteArray& encodedZeros);
void computeLoudness(const QByteArray* decodedBuffer, QSharedPointer<ScriptableAvatar>);

ScriptEnginePointer _scriptEngine;
ScriptManagerPointer _scriptManager;
EntityEditPacketSender _entityEditSender;
EntityTreeHeadlessViewer _entityViewer;

Expand Down
10 changes: 6 additions & 4 deletions assignment-client/src/avatars/ScriptableAvatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

#include <shared/QtHelpers.h>
#include <AnimUtil.h>
#include <AvatarHashMap.h>
#include <ClientTraitsHandler.h>
#include <GLMHelpers.h>
#include <ResourceRequestObserver.h>
#include <AvatarLogging.h>
#include <EntityItem.h>
#include <EntityItemProperties.h>
#include <NetworkAccessManager.h>
#include <NetworkingConstants.h>


ScriptableAvatar::ScriptableAvatar() {
ScriptableAvatar::ScriptableAvatar(): _scriptEngine(newScriptEngine()) {
_clientTraitsHandler.reset(new ClientTraitsHandler(this));
}

Expand Down Expand Up @@ -311,7 +313,7 @@ AvatarEntityMap ScriptableAvatar::getAvatarEntityDataInternal(bool allProperties
EntityItemProperties properties = entity->getProperties(desiredProperties);

QByteArray blob;
EntityItemProperties::propertiesToBlob(_scriptEngine, sessionID, properties, blob, allProperties);
EntityItemProperties::propertiesToBlob(*_scriptEngine, sessionID, properties, blob, allProperties);
data[id] = blob;
}
});
Expand All @@ -335,7 +337,7 @@ void ScriptableAvatar::setAvatarEntityData(const AvatarEntityMap& avatarEntityDa
while (dataItr != avatarEntityData.end()) {
EntityItemProperties properties;
const QByteArray& blob = dataItr.value();
if (!blob.isNull() && EntityItemProperties::blobToProperties(_scriptEngine, blob, properties)) {
if (!blob.isNull() && EntityItemProperties::blobToProperties(*_scriptEngine, blob, properties)) {
newProperties[dataItr.key()] = properties;
}
++dataItr;
Expand Down Expand Up @@ -415,7 +417,7 @@ void ScriptableAvatar::updateAvatarEntity(const QUuid& entityID, const QByteArra

EntityItemPointer entity;
EntityItemProperties properties;
if (!EntityItemProperties::blobToProperties(_scriptEngine, entityData, properties)) {
if (!EntityItemProperties::blobToProperties(*_scriptEngine, entityData, properties)) {
// entityData is corrupt
return;
}
Expand Down
4 changes: 3 additions & 1 deletion assignment-client/src/avatars/ScriptableAvatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <ScriptEngine.h>
#include <EntityItem.h>

class ScriptEngine;

/**jsdoc
* The <code>Avatar</code> API is used to manipulate scriptable avatars on the domain. This API is a subset of the
* {@link MyAvatar} API. To enable this API, set {@link Agent|Agent.isAvatar} to <code>true</code>.
Expand Down Expand Up @@ -220,7 +222,7 @@ public slots:
QHash<QString, int> _fstJointIndices; ///< 1-based, since zero is returned for missing keys
QStringList _fstJointNames; ///< in order of depth-first traversal
QUrl _skeletonFBXURL;
mutable QScriptEngine _scriptEngine;
mutable ScriptEnginePointer _scriptEngine;
std::map<QUuid, EntityItemPointer> _entities;

/// Loads the joint indices, names from the FST file (if any)
Expand Down
Loading

0 comments on commit cbe608a

Please sign in to comment.