History

Fix entity script destruction

* Unload scripts that are auto-refreshing before restarting them. (#1697) * Refactors `entityScriptChanging` to account for old and new URLs, was previously always setting them both to `""` and failing. (#2187) * Cleaned up `EntityTreeRenderer::checkAndCallPreload` to be less confusing lol

Ada committed 6 months ago

Commit 1b12e111 parent 144bb539 files · +61 −62Browse files

Showing the changes to assignment-client/src/scripts/EntityScriptServer.cpp (1 of 9 files). Show all

Unified · Split

assignment-client/src/scripts/EntityScriptServer.cpp+20−16
@@ -577,7 +577,10 @@ void EntityScriptServer::shutdownScriptEngine() { · 576 unmodified lines
577}577}
578 578
579void EntityScriptServer::addingEntity(const EntityItemID& entityID) {579void EntityScriptServer::addingEntity(const EntityItemID& entityID) {
580 checkAndCallPreload(entityID);580 EntityItemPointer entity = _entityViewer.getTree()->findEntityByEntityItemID(entityID);
581 if (entity) {
582 checkAndCallPreload(entityID, "", entity->getServerScripts());
583 }
581}584}
582 585
583void EntityScriptServer::deletingEntity(const EntityItemID& entityID) {586void EntityScriptServer::deletingEntity(const EntityItemID& entityID) {
@@ -587,13 +590,13 @@ void EntityScriptServer::deletingEntity(const EntityItemID& entityID) { · 3 unmodified lines
587 }590 }
588}591}
589 592
590void EntityScriptServer::entityServerScriptChanging(const EntityItemID& entityID, bool reload) {593void EntityScriptServer::entityServerScriptChanging(const EntityItemID& entityID, const QString& oldScriptURL, const QString& newScriptURL) {
591 if (_entityViewer.getTree() && !_shuttingDown) {594 if (_entityViewer.getTree() && !_shuttingDown) {
592 checkAndCallPreload(entityID, reload);595 checkAndCallPreload(entityID, oldScriptURL, newScriptURL);
593 }596 }
594}597}
595 598
596void EntityScriptServer::checkAndCallPreload(const EntityItemID& entityID, bool forceRedownload) {599void EntityScriptServer::checkAndCallPreload(const EntityItemID& entityID, const QString& oldScriptURL, const QString& newScriptURL) {
597 if (_entityViewer.getTree() && !_shuttingDown && _entitiesScriptManager) {600 if (_entityViewer.getTree() && !_shuttingDown && _entitiesScriptManager) {
598 601
599 EntityItemPointer entity = _entityViewer.getTree()->findEntityByEntityItemID(entityID);602 EntityItemPointer entity = _entityViewer.getTree()->findEntityByEntityItemID(entityID);
@@ -601,20 +604,21 @@ void EntityScriptServer::checkAndCallPreload(const EntityItemID& entityID, bool · 1 unmodified line
601 return;604 return;
602 }605 }
603 606
604 QString serverScripts = entity->getServerScripts();
605 EntityScriptDetails details;607 EntityScriptDetails details;
606 bool isRunning = _entitiesScriptManager->getEntityScriptDetails(entityID, serverScripts, details);608 bool isRunning = _entitiesScriptManager->getEntityScriptDetails(entityID, oldScriptURL, details);
607 if (forceRedownload || !isRunning || details.scriptText != serverScripts) {609 bool reload = oldScriptURL == newScriptURL;
608 // TODO: Check if this is running on script engine thread, otherwise lambda capturing script engine pointer is needed
609 if (isRunning) {
610 _entitiesScriptManager->unloadEntityScript(entityID, serverScripts, true);
611 }
612 610
613 QString scriptUrl = entity->getServerScripts();611 // TODO: Check if this is running on script engine thread, otherwise lambda capturing script engine pointer is needed
614 if (!scriptUrl.isEmpty()) {612 if (isRunning) {
615 scriptUrl = DependencyManager::get<ResourceManager>()->normalizeURL(scriptUrl);613 _entitiesScriptManager->unloadEntityScript(entityID, oldScriptURL, true);
616 _entitiesScriptManager->loadEntityScript(entityID, scriptUrl, forceRedownload);614 }
617 }615
616 if (!newScriptURL.isEmpty()) {
617 _entitiesScriptManager->loadEntityScript(
618 entityID,
619 DependencyManager::get<ResourceManager>()->normalizeURL(newScriptURL),
620 reload
621 );
618 }622 }
619 }623 }
620}624}