Skip to content

Commit

Permalink
Merge pull request #871 from kasenvr/revert-868-revert-659-scriptEngine
Browse files Browse the repository at this point in the history
Revert "Revert "Split Local and owned Avatar Entity scripts into their own ScriptEngine""
  • Loading branch information
two-one-five authored Nov 24, 2020
2 parents 78214b1 + b674458 commit 4e1b683
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 118 deletions.
4 changes: 3 additions & 1 deletion assignment-client/src/scripts/EntityScriptServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,9 @@ void EntityScriptServer::resetEntitiesScriptEngine() {
scriptEngines->runScriptInitializers(newEngine);
newEngine->runInThread();
auto newEngineSP = qSharedPointerCast<EntitiesScriptEngineProvider>(newEngine);
DependencyManager::get<EntityScriptingInterface>()->setEntitiesScriptEngine(newEngineSP);
// On the entity script server, these are the same
DependencyManager::get<EntityScriptingInterface>()->setPersistentEntitiesScriptEngine(newEngineSP);
DependencyManager::get<EntityScriptingInterface>()->setNonPersistentEntitiesScriptEngine(newEngineSP);

if (_entitiesScriptEngine) {
disconnect(_entitiesScriptEngine.data(), &ScriptEngine::entityScriptDetailsUpdated,
Expand Down
230 changes: 142 additions & 88 deletions libraries/entities-renderer/src/EntityTreeRenderer.cpp

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions libraries/entities-renderer/src/EntityTreeRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ public slots:
EntityRendererPointer renderableForEntity(const EntityItemPointer& entity) const { return renderableForEntityId(entity->getID()); }
render::ItemID renderableIdForEntity(const EntityItemPointer& entity) const { return renderableIdForEntityId(entity->getID()); }

void resetEntitiesScriptEngine();
void resetPersistentEntitiesScriptEngine();
void resetNonPersistentEntitiesScriptEngine();
void setupEntityScriptEngineSignals(const ScriptEnginePointer& scriptEngine);

void findBestZoneAndMaybeContainingEntities(QSet<EntityItemID>& entitiesContainingAvatar);

Expand All @@ -196,7 +198,8 @@ public slots:
QSet<EntityItemID> _currentEntitiesInside;

bool _wantScripts;
ScriptEnginePointer _entitiesScriptEngine;
ScriptEnginePointer _nonPersistentEntitiesScriptEngine; // used for domain + non-owned avatar entities, cleared on domain switch
ScriptEnginePointer _persistentEntitiesScriptEngine; // used for local + owned avatar entities, persists on domain switch, cleared on reload content

void playEntityCollisionSound(const EntityItemPointer& entity, const Collision& collision);

Expand All @@ -214,8 +217,6 @@ public slots:
std::function<RayToEntityIntersectionResult(unsigned int)> _getPrevRayPickResultOperator;
std::function<void(unsigned int, bool)> _setPrecisionPickingOperator;

bool _mouseAndPreloadSignalHandlersConnected { false };

class LayeredZone {
public:
LayeredZone(std::shared_ptr<ZoneEntityItem> zone) : zone(zone), id(zone->getID()), volume(zone->getVolumeEstimate()) {}
Expand Down
34 changes: 23 additions & 11 deletions libraries/entities/src/EntityScriptingInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,18 +1046,26 @@ QSizeF EntityScriptingInterface::textSize(const QUuid& id, const QString& text)
return EntityTree::textSize(id, text);
}

void EntityScriptingInterface::setEntitiesScriptEngine(QSharedPointer<EntitiesScriptEngineProvider> engine) {
void EntityScriptingInterface::setPersistentEntitiesScriptEngine(QSharedPointer<EntitiesScriptEngineProvider> engine) {
std::lock_guard<std::recursive_mutex> lock(_entitiesScriptEngineLock);
_entitiesScriptEngine = engine;
_persistentEntitiesScriptEngine = engine;
}

void EntityScriptingInterface::setNonPersistentEntitiesScriptEngine(QSharedPointer<EntitiesScriptEngineProvider> engine) {
std::lock_guard<std::recursive_mutex> lock(_entitiesScriptEngineLock);
_nonPersistentEntitiesScriptEngine = engine;
}

void EntityScriptingInterface::callEntityMethod(const QUuid& id, const QString& method, const QStringList& params) {
PROFILE_RANGE(script_entities, __FUNCTION__);

std::lock_guard<std::recursive_mutex> lock(_entitiesScriptEngineLock);
if (_entitiesScriptEngine) {
EntityItemID entityID{ id };
_entitiesScriptEngine->callEntityScriptMethod(entityID, method, params);

auto entity = getEntityTree()->findEntityByEntityItemID(id);
if (entity) {
std::lock_guard<std::recursive_mutex> lock(_entitiesScriptEngineLock);
auto& scriptEngine = (entity->isLocalEntity() || entity->isMyAvatarEntity()) ? _persistentEntitiesScriptEngine : _nonPersistentEntitiesScriptEngine;
if (scriptEngine) {
scriptEngine->callEntityScriptMethod(id, method, params);
}
}
}

Expand Down Expand Up @@ -1099,9 +1107,13 @@ void EntityScriptingInterface::handleEntityScriptCallMethodPacket(QSharedPointer
params << paramString;
}

std::lock_guard<std::recursive_mutex> lock(_entitiesScriptEngineLock);
if (_entitiesScriptEngine) {
_entitiesScriptEngine->callEntityScriptMethod(entityID, method, params, senderNode->getUUID());
auto entity = getEntityTree()->findEntityByEntityItemID(entityID);
if (entity) {
std::lock_guard<std::recursive_mutex> lock(_entitiesScriptEngineLock);
auto& scriptEngine = (entity->isLocalEntity() || entity->isMyAvatarEntity()) ? _persistentEntitiesScriptEngine : _nonPersistentEntitiesScriptEngine;
if (scriptEngine) {
scriptEngine->callEntityScriptMethod(entityID, method, params, senderNode->getUUID());
}
}
}
}
Expand Down Expand Up @@ -1332,7 +1344,7 @@ bool EntityPropertyMetadataRequest::script(EntityItemID entityID, QScriptValue h
if (entitiesScriptEngine) {
request->setFuture(entitiesScriptEngine->getLocalEntityScriptDetails(entityID));
}
});
}, entityID);
if (!request->isStarted()) {
request->deleteLater();
callScopedHandlerObject(handler, _engine->makeError("Entities Scripting Provider unavailable", "InternalError"), QScriptValue());
Expand Down
15 changes: 10 additions & 5 deletions libraries/entities/src/EntityScriptingInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ class EntityScriptingInterface : public OctreeScriptingInterface, public Depende

void setEntityTree(EntityTreePointer modelTree);
EntityTreePointer getEntityTree() { return _entityTree; }
void setEntitiesScriptEngine(QSharedPointer<EntitiesScriptEngineProvider> engine);
void setPersistentEntitiesScriptEngine(QSharedPointer<EntitiesScriptEngineProvider> engine);
void setNonPersistentEntitiesScriptEngine(QSharedPointer<EntitiesScriptEngineProvider> engine);

void resetActivityTracking();
ActivityTracking getActivityTracking() const { return _activityTracking; }
Expand Down Expand Up @@ -2510,9 +2511,12 @@ public slots:
void webEventReceived(const EntityItemID& entityItemID, const QVariant& message);

protected:
void withEntitiesScriptEngine(std::function<void(QSharedPointer<EntitiesScriptEngineProvider>)> function) {
std::lock_guard<std::recursive_mutex> lock(_entitiesScriptEngineLock);
function(_entitiesScriptEngine);
void withEntitiesScriptEngine(std::function<void(QSharedPointer<EntitiesScriptEngineProvider>)> function, const EntityItemID& id) {
auto entity = getEntityTree()->findEntityByEntityItemID(id);
if (entity) {
std::lock_guard<std::recursive_mutex> lock(_entitiesScriptEngineLock);
function((entity->isLocalEntity() || entity->isMyAvatarEntity()) ? _persistentEntitiesScriptEngine : _nonPersistentEntitiesScriptEngine);
}
};

private slots:
Expand Down Expand Up @@ -2542,7 +2546,8 @@ private slots:
EntityTreePointer _entityTree;

std::recursive_mutex _entitiesScriptEngineLock;
QSharedPointer<EntitiesScriptEngineProvider> _entitiesScriptEngine;
QSharedPointer<EntitiesScriptEngineProvider> _persistentEntitiesScriptEngine;
QSharedPointer<EntitiesScriptEngineProvider> _nonPersistentEntitiesScriptEngine;

bool _bidOnSimulationOwnership { false };

Expand Down
16 changes: 9 additions & 7 deletions libraries/script-engine/src/ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ void ScriptEngine::executeOnScriptThread(std::function<void()> function, const Q
function();
}

void ScriptEngine::waitTillDoneRunning() {
void ScriptEngine::waitTillDoneRunning(bool shutdown) {
// Engine should be stopped already, but be defensive
stop();

Expand Down Expand Up @@ -520,12 +520,14 @@ void ScriptEngine::waitTillDoneRunning() {
}
}

// NOTE: This will be called on the main application thread (among other threads) from stopAllScripts.
// The thread will need to continue to process events, because
// the scripts will likely need to marshall messages across to the main thread, e.g.
// if they access Settings or Menu in any of their shutdown code. So:
// Process events for this thread, allowing invokeMethod calls to pass between threads.
QCoreApplication::processEvents();
if (shutdown) {
// NOTE: This will be called on the main application thread (among other threads) from stopAllScripts.
// The thread will need to continue to process events, because
// the scripts will likely need to marshall messages across to the main thread, e.g.
// if they access Settings or Menu in any of their shutdown code. So:
// Process events for this thread, allowing invokeMethod calls to pass between threads.
QCoreApplication::processEvents();
}

// Avoid a pure busy wait
QThread::yieldCurrentThread();
Expand Down
2 changes: 1 addition & 1 deletion libraries/script-engine/src/ScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class ScriptEngine : public BaseScriptEngine, public EntitiesScriptEngineProvide
Q_INVOKABLE void stop(bool marshal = false);

// Stop any evaluating scripts and wait for the scripting thread to finish.
void waitTillDoneRunning();
void waitTillDoneRunning(bool shutdown = false);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NOTE - these are NOT intended to be public interfaces available to scripts, the are only Q_INVOKABLE so we can
Expand Down
2 changes: 1 addition & 1 deletion libraries/script-engine/src/ScriptEngines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void ScriptEngines::shutdownScripting() {
// want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing
// any application state after we leave this stopAllScripts() method
qCDebug(scriptengine) << "waiting on script:" << scriptName;
scriptEngine->waitTillDoneRunning();
scriptEngine->waitTillDoneRunning(true);
qCDebug(scriptengine) << "done waiting on script:" << scriptName;
}
// Once the script is stopped, we can remove it from our set
Expand Down

0 comments on commit 4e1b683

Please sign in to comment.