From b573b16df8e50c1125442ea46bb279ba128f2edd Mon Sep 17 00:00:00 2001
From: Zach Fox <fox@highfidelity.io>
Date: Tue, 10 Jul 2018 14:12:21 -0700
Subject: [PATCH] Fix MS16600: Prevent duplicate Spectator Camera models after
 crash

---
 .../marketplace/spectator-camera/spectatorCamera.js | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/unpublishedScripts/marketplace/spectator-camera/spectatorCamera.js b/unpublishedScripts/marketplace/spectator-camera/spectatorCamera.js
index 3e749e38a2..4c39c5fb95 100644
--- a/unpublishedScripts/marketplace/spectator-camera/spectatorCamera.js
+++ b/unpublishedScripts/marketplace/spectator-camera/spectatorCamera.js
@@ -74,6 +74,7 @@
             "collisionMask": 7,
             "dynamic": false,
             "modelURL": Script.resolvePath("spectator-camera.fbx"),
+            "name": "Spectator Camera",
             "registrationPoint": {
                 "x": 0.56,
                 "y": 0.545,
@@ -102,6 +103,18 @@
             position: cameraPosition,
             localOnly: true
         });
+
+        // Remove the existing camera model from the domain if one exists.
+        // It's easy for this to happen if the user crashes while the Spectator Camera is on.
+        // We do this down here (after the new one is rezzed) so that we don't accidentally delete
+        // the newly-rezzed model.
+        var entityIDs = Entities.findEntitiesByName("Spectator Camera", MyAvatar.position, 100, false);
+        entityIDs.forEach(function (currentEntityID) {
+            var currentEntityOwner = Entities.getEntityProperties(currentEntityID, ['owningAvatarID']).owningAvatarID;
+            if (currentEntityOwner === MyAvatar.sessionUUID && currentEntityID !== camera) {
+                Entities.deleteEntity(currentEntityID);
+            }
+        });
     }
 
     // Function Name: spectatorCameraOff()