change authority before client disconnects and wipes stuff out

(fixes #380)
This commit is contained in:
JohnCorby 2021-11-30 23:52:00 -08:00
parent 99d0eab098
commit cb84fcd847
3 changed files with 45 additions and 30 deletions

View File

@ -1,4 +1,6 @@
using OWML.Common; using System;
using System.Linq;
using OWML.Common;
using OWML.Utils; using OWML.Utils;
using QSB.ClientServerStateSync; using QSB.ClientServerStateSync;
using QSB.DeathSync; using QSB.DeathSync;
@ -8,13 +10,13 @@ using QSB.Patches;
using QSB.Player; using QSB.Player;
using QSB.Player.TransformSync; using QSB.Player.TransformSync;
using QSB.PoolSync; using QSB.PoolSync;
using QSB.ShipSync.TransformSync;
using QSB.SuspendableSync;
using QSB.TimeSync; using QSB.TimeSync;
using QSB.Utility; using QSB.Utility;
using QSB.WorldSync; using QSB.WorldSync;
using QuantumUNET; using QuantumUNET;
using QuantumUNET.Components; using QuantumUNET.Components;
using System;
using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
@ -157,7 +159,7 @@ namespace QSB
if (!QSBCore.IsHost) if (!QSBCore.IsHost)
{ {
QSBCore.UnityEvents.RunWhen(() => QSBEventManager.Ready && PlayerTransformSync.LocalInstance != null, QSBCore.UnityEvents.RunWhen(() => QSBEventManager.Ready && PlayerTransformSync.LocalInstance != null,
() => QSBEventManager.FireEvent(EventNames.QSBRequestStateResync)); () => QSBEventManager.FireEvent(EventNames.QSBRequestStateResync));
} }
_everConnected = true; _everConnected = true;
@ -199,24 +201,38 @@ namespace QSB
OnClientDisconnected?.SafeInvoke(conn.LastError); OnClientDisconnected?.SafeInvoke(conn.LastError);
} }
public override void OnServerDisconnect(QNetworkConnection connection) // Called on the server when any client disconnects public override void OnServerDisconnect(QNetworkConnection conn) // Called on the server when any client disconnects
{ {
base.OnServerDisconnect(connection);
DebugLog.DebugWrite("OnServerDisconnect", MessageType.Info); DebugLog.DebugWrite("OnServerDisconnect", MessageType.Info);
// remove authority for orbs
foreach (var item in NomaiOrbTransformSync.OrbTransformSyncs) foreach (var item in NomaiOrbTransformSync.OrbTransformSyncs)
{ {
if (item is null) if (!item)
{ {
continue; continue;
} }
var identity = item.GetComponent<QNetworkIdentity>(); var identity = item.NetIdentity;
if (identity.ClientAuthorityOwner == connection) if (identity.ClientAuthorityOwner == conn)
{ {
identity.RemoveClientAuthority(connection); identity.RemoveClientAuthority(conn);
} }
} }
// remove authority from ship
if (!ShipTransformSync.LocalInstance)
{
var identity = ShipTransformSync.LocalInstance.NetIdentity;
if (identity.ClientAuthorityOwner == conn)
{
identity.RemoveClientAuthority(conn);
}
}
SuspendableManager.OnDisconnect(conn);
base.OnServerDisconnect(conn);
} }
public override void OnStopServer() public override void OnStopServer()

View File

@ -31,7 +31,7 @@ namespace QSB.SuspendableSync
return; return;
} }
SuspendableManager.SetSuspended(message.FromId, message.Identity, message.Suspended); SuspendableManager.UpdateSuspended(message.FromId, message.Identity, message.Suspended);
} }
} }
} }

View File

@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using QSB.Player; using HarmonyLib;
using QSB.Utility; using QSB.Utility;
using QuantumUNET; using QuantumUNET;
using QuantumUNET.Components; using QuantumUNET.Components;
@ -12,25 +12,10 @@ namespace QSB.SuspendableSync
{ {
private static readonly Dictionary<QNetworkIdentity, List<uint>> _unsuspendedFor = new(); private static readonly Dictionary<QNetworkIdentity, List<uint>> _unsuspendedFor = new();
static SuspendableManager() => QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
private static void OnPlayerLeave(uint id)
{
if (!QSBCore.IsHost)
{
return;
}
foreach (var (identity, _) in _unsuspendedFor)
{
SetSuspended(id, identity, false);
}
}
public static void Register(QNetworkIdentity identity) => _unsuspendedFor.Add(identity, new List<uint>()); public static void Register(QNetworkIdentity identity) => _unsuspendedFor.Add(identity, new List<uint>());
public static void Unregister(QNetworkIdentity identity) => _unsuspendedFor.Remove(identity); public static void Unregister(QNetworkIdentity identity) => _unsuspendedFor.Remove(identity);
public static void SetSuspended(uint id, QNetworkIdentity identity, bool suspended) public static void UpdateSuspended(uint id, QNetworkIdentity identity, bool suspended)
{ {
var unsuspendedFor = _unsuspendedFor[identity]; var unsuspendedFor = _unsuspendedFor[identity];
@ -51,7 +36,6 @@ namespace QSB.SuspendableSync
var newOwner = unsuspendedFor.Count != 0 ? unsuspendedFor[0] : uint.MaxValue; var newOwner = unsuspendedFor.Count != 0 ? unsuspendedFor[0] : uint.MaxValue;
SetAuthority(identity, newOwner); SetAuthority(identity, newOwner);
} }
private static void SetAuthority(QNetworkIdentity identity, uint id) private static void SetAuthority(QNetworkIdentity identity, uint id)
@ -76,7 +60,22 @@ namespace QSB.SuspendableSync
identity.AssignClientAuthority(newConn); identity.AssignClientAuthority(newConn);
} }
DebugLog.DebugWrite($"{QSBPlayerManager.LocalPlayerId}.{identity.NetId}:{identity.gameObject.name} - set authority to {id}"); DebugLog.DebugWrite($"{identity.NetId}:{identity.gameObject.name} - set authority to {id}");
}
/// transfer authority to a different client
public static void OnDisconnect(QNetworkConnection conn)
{
var id = conn.GetPlayerId();
foreach (var (identity, unsuspendedFor) in _unsuspendedFor)
{
if (unsuspendedFor.Remove(id))
{
var newOwner = unsuspendedFor.Count != 0 ? unsuspendedFor[0] : uint.MaxValue;
SetAuthority(identity, newOwner);
}
}
} }
} }
} }