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
577577 }
578578
579579 void EntityScriptServer::addingEntity(const EntityItemID& entityID) {
580− checkAndCallPreload(entityID);
580+ EntityItemPointer entity = _entityViewer.getTree()->findEntityByEntityItemID(entityID);
581+ if (entity) {
582+ checkAndCallPreload(entityID, "", entity->getServerScripts());
583+ }
581584 }
582585
583586 void EntityScriptServer::deletingEntity(const EntityItemID& entityID) {
@@ -587,13 +590,13 @@ void EntityScriptServer::deletingEntity(const EntityItemID& entityID) { · 3 unmodified lines
587590 }
588591 }
589592
590−void EntityScriptServer::entityServerScriptChanging(const EntityItemID& entityID, bool reload) {
593+void EntityScriptServer::entityServerScriptChanging(const EntityItemID& entityID, const QString& oldScriptURL, const QString& newScriptURL) {
591594 if (_entityViewer.getTree() && !_shuttingDown) {
592− checkAndCallPreload(entityID, reload);
595+ checkAndCallPreload(entityID, oldScriptURL, newScriptURL);
593596 }
594597 }
595598
596−void EntityScriptServer::checkAndCallPreload(const EntityItemID& entityID, bool forceRedownload) {
599+void EntityScriptServer::checkAndCallPreload(const EntityItemID& entityID, const QString& oldScriptURL, const QString& newScriptURL) {
597600 if (_entityViewer.getTree() && !_shuttingDown && _entitiesScriptManager) {
598601
599602 EntityItemPointer entity = _entityViewer.getTree()->findEntityByEntityItemID(entityID);
@@ -601,20 +604,21 @@ void EntityScriptServer::checkAndCallPreload(const EntityItemID& entityID, bool · 1 unmodified line
601604 return;
602605 }
603606
604− QString serverScripts = entity->getServerScripts();
605607 EntityScriptDetails details;
606− bool isRunning = _entitiesScriptManager->getEntityScriptDetails(entityID, serverScripts, details);
607− if (forceRedownload || !isRunning || details.scriptText != serverScripts) {
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− }
608+ bool isRunning = _entitiesScriptManager->getEntityScriptDetails(entityID, oldScriptURL, details);
609+ bool reload = oldScriptURL == newScriptURL;
612610
613− QString scriptUrl = entity->getServerScripts();
614− if (!scriptUrl.isEmpty()) {
615− scriptUrl = DependencyManager::get<ResourceManager>()->normalizeURL(scriptUrl);
616− _entitiesScriptManager->loadEntityScript(entityID, scriptUrl, forceRedownload);
617− }
611+ // TODO: Check if this is running on script engine thread, otherwise lambda capturing script engine pointer is needed
612+ if (isRunning) {
613+ _entitiesScriptManager->unloadEntityScript(entityID, oldScriptURL, true);
614+ }
615+
616+ if (!newScriptURL.isEmpty()) {
617+ _entitiesScriptManager->loadEntityScript(
618+ entityID,
619+ DependencyManager::get<ResourceManager>()->normalizeURL(newScriptURL),
620+ reload
621+ );
618622 }
619623 }
620624 }