From e7936e70459cb1925da68c01584ad1b6fc2b6fb2 Mon Sep 17 00:00:00 2001
From: John Cote <jwcotejr@gmail.com>
Date: Tue, 29 Jun 2021 15:08:37 -0400
Subject: [PATCH] multiplayer: Fix crashes on Astron clients

---
 otp/distributed/TelemetryLimited.py |  3 +++
 otp/distributed/TelemetryLimiter.py | 35 ++++++++++++++++++++++-------
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/otp/distributed/TelemetryLimited.py b/otp/distributed/TelemetryLimited.py
index af65ef0..6416eca 100644
--- a/otp/distributed/TelemetryLimited.py
+++ b/otp/distributed/TelemetryLimited.py
@@ -18,5 +18,8 @@ class TelemetryLimited:
             self._limits.remove(limit)
 
     def enforceTelemetryLimits(self):
+        if __astron__ and self.isEmpty():
+            return
+
         for limit in self._limits:
             limit(self)
diff --git a/otp/distributed/TelemetryLimiter.py b/otp/distributed/TelemetryLimiter.py
index f08e4db..6a104bc 100644
--- a/otp/distributed/TelemetryLimiter.py
+++ b/otp/distributed/TelemetryLimiter.py
@@ -22,6 +22,11 @@ class TelemetryLimiter(DirectObject):
         self._objs[id] = obj
         self.accept(self._getDummyEventName(obj), self._dummyEventHandler)
 
+    if __astron__:
+        def hasObj(self, obj):
+            id = obj.getTelemetryLimiterId()
+            return id in self._objs
+
     def _getDummyEventName(self, obj):
         return '%s-%s-%s-%s' % (self.LeakDetectEventName,
          obj.getTelemetryLimiterId(),
@@ -80,7 +85,10 @@ class TLGatherAllAvs(DirectObject):
             self._handlePlayerArrive(av)
 
         self.accept(DistributedPlayer.GetPlayerGenerateEvent(), self._handlePlayerArrive)
-        self.accept(DistributedPlayer.GetPlayerNetworkDeleteEvent(), self._handlePlayerLeave)
+        if __astron__:
+            self.accept(DistributedPlayer.GetPlayerDeleteEvent(), self._handlePlayerLeave)
+        else:
+            self.accept(DistributedPlayer.GetPlayerNetworkDeleteEvent(), self._handlePlayerLeave)
 
     def _handlePlayerArrive(self, av):
         if av is not localAvatar:
@@ -94,14 +102,25 @@ class TLGatherAllAvs(DirectObject):
             self._avId2limits[av.doId] = limitList
             base.cr.telemetryLimiter.addObj(av)
 
-    def _handlePlayerLeave(self, av):
-        if av is not localAvatar:
-            base.cr.telemetryLimiter.removeObj(av)
-            for limit in self._avId2limits[av.doId]:
-                av.removeTelemetryLimit(limit)
+    if __astron__:
+        def _handlePlayerLeave(self, av):
+            if av is not localAvatar and base.cr.telemetryLimiter.hasObj(av) and av.doId in self._avId2limits:
+                base.cr.telemetryLimiter.removeObj(av)
+                for limit in self._avId2limits[av.doId]:
+                    av.removeTelemetryLimit(limit)
 
-            del self._avId2limits[av.doId]
-            del self._avs[av.doId]
+                del self._avId2limits[av.doId]
+                if av.doId in self._avs:
+                    del self._avs[av.doId]
+    else:
+        def _handlePlayerLeave(self, av):
+            if av is not localAvatar:
+                base.cr.telemetryLimiter.removeObj(av)
+                for limit in self._avId2limits[av.doId]:
+                    av.removeTelemetryLimit(limit)
+
+                del self._avId2limits[av.doId]
+                del self._avs[av.doId]
 
     def destroy(self):
         self.ignoreAll()