From 3afcdce41331eac4f5423ff8dff31ac8047f2e50 Mon Sep 17 00:00:00 2001 From: andylippitt Date: Tue, 12 Mar 2019 09:27:48 -0400 Subject: [PATCH] cleaner world death --- Game.Engine/Core/Player.cs | 9 ++++++++- Game.Engine/Core/SystemActors/Advertisement.cs | 13 +++++++++---- Game.Engine/Core/SystemActors/SystemActorBase.cs | 7 +++++++ Game.Engine/Networking/Connection.cs | 1 + 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Game.Engine/Core/Player.cs b/Game.Engine/Core/Player.cs index 9f930e99..54d93aae 100644 --- a/Game.Engine/Core/Player.cs +++ b/Game.Engine/Core/Player.cs @@ -143,8 +143,15 @@ public void Destroy() Fleet = null; } - World.Actors.Remove(this); + if (this.Connection != null) + try + { + ((IDisposable)this.Connection).Dispose(); + } catch (Exception) { } + this.Connection = null; + World.Actors.Remove(this); + var worldPlayers = GetWorldPlayers(World); worldPlayers.Remove(this); } diff --git a/Game.Engine/Core/SystemActors/Advertisement.cs b/Game.Engine/Core/SystemActors/Advertisement.cs index 5e27dbed..10adcd85 100644 --- a/Game.Engine/Core/SystemActors/Advertisement.cs +++ b/Game.Engine/Core/SystemActors/Advertisement.cs @@ -1,6 +1,5 @@ namespace Game.Engine.Core.SystemActors { - using System; using System.Linq; public class Advertisement : SystemActorBase @@ -16,11 +15,17 @@ protected override void CycleThink() if (World.AdvertisedPlayerCount > 0) EmptySince = World.Time; + } - - if (World.Hook.AutoRemoveOnEmptyThreshold > 0 && EmptySince > 0 && (World.Time - EmptySince) > World.Hook.AutoRemoveOnEmptyThreshold) - { + protected override void CycleCreateDestroy() + { + base.CycleCreateDestroy(); + if (World.Hook.AutoRemoveOnEmptyThreshold > 0 + && EmptySince > 0 + && (World.Time - EmptySince) > World.Hook.AutoRemoveOnEmptyThreshold + ) { Worlds.Destroy(World.WorldKey); + EmptySince = 0; } } } diff --git a/Game.Engine/Core/SystemActors/SystemActorBase.cs b/Game.Engine/Core/SystemActors/SystemActorBase.cs index 09644490..010a0394 100644 --- a/Game.Engine/Core/SystemActors/SystemActorBase.cs +++ b/Game.Engine/Core/SystemActors/SystemActorBase.cs @@ -7,9 +7,13 @@ public abstract class SystemActorBase : IActor public World World = null; protected long SleepUntil = 0; protected int CycleMS = 1000; + private bool RunningThisStep = false; + public virtual void CreateDestroy() { + if (RunningThisStep) + CycleCreateDestroy(); } public virtual void Destroy() @@ -30,11 +34,14 @@ public virtual void Think() { if (World != null && World.Time > SleepUntil) { + RunningThisStep = true; CycleThink(); if (World != null) SleepUntil = World.Time + CycleMS; } + else + RunningThisStep = false; } } } \ No newline at end of file diff --git a/Game.Engine/Networking/Connection.cs b/Game.Engine/Networking/Connection.cs index 3a175678..6b788ba4 100644 --- a/Game.Engine/Networking/Connection.cs +++ b/Game.Engine/Networking/Connection.cs @@ -647,6 +647,7 @@ private async Task HandleIncomingMessage(NetQuantum quantum) if (player != null) { player.PendingDestruction = true; + player.Connection = null; } } }