quantum-space-buddies/QSB/QSBNetworkManager.cs

312 lines
14 KiB
C#
Raw Normal View History

2020-08-22 18:08:25 +01:00
using OWML.Common;
2020-09-06 09:07:31 +01:00
using OWML.ModHelper.Events;
2020-08-22 18:08:25 +01:00
using QSB.Animation;
2020-08-13 21:46:16 +02:00
using QSB.DeathSync;
2020-09-17 20:14:55 +01:00
using QSB.ElevatorSync;
2020-11-03 21:42:14 +00:00
using QSB.EventsCore;
2020-08-13 19:25:12 +02:00
using QSB.GeyserSync;
2020-11-20 19:50:31 +00:00
using QSB.Instruments;
using QSB.OrbSync;
2020-11-03 21:11:10 +00:00
using QSB.Patches;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2020-12-02 21:21:43 +00:00
using QSB.QuantumUNET;
2020-11-03 22:29:23 +00:00
using QSB.SectorSync;
using QSB.TimeSync;
2020-02-21 23:36:07 +01:00
using QSB.TransformSync;
using QSB.Utility;
2020-09-04 20:57:35 +01:00
using QSB.WorldSync;
2020-08-21 14:04:13 +01:00
using System;
using System.Linq;
2020-02-21 23:36:07 +01:00
using UnityEngine;
2020-02-13 20:23:26 +01:00
using UnityEngine.Networking;
2020-02-15 20:48:02 +01:00
namespace QSB
{
2020-12-02 18:40:38 +00:00
public class QSBNetworkManager : QSBNetworkManagerUNET
2020-02-15 20:48:02 +01:00
{
private const int MaxConnections = 128;
2020-09-06 09:07:31 +01:00
private const int MaxBufferedPackets = 64;
2020-08-18 22:37:27 +02:00
public static QSBNetworkManager Instance { get; private set; }
public event Action OnNetworkManagerReady;
public bool IsReady { get; private set; }
private QSBNetworkLobby _lobby;
2020-02-21 23:36:07 +01:00
private AssetBundle _assetBundle;
private GameObject _shipPrefab;
private GameObject _cameraPrefab;
private GameObject _probePrefab;
2020-09-29 21:34:46 +01:00
public GameObject OrbPrefab;
2020-02-21 23:36:07 +01:00
2020-02-15 20:48:02 +01:00
private void Awake()
{
2020-12-02 18:40:38 +00:00
DebugLog.DebugWrite("AWAKE");
2020-08-18 22:37:27 +02:00
Instance = this;
_lobby = gameObject.AddComponent<QSBNetworkLobby>();
2020-08-17 16:51:56 +01:00
_assetBundle = QSB.NetworkAssetBundle;
2020-12-02 18:40:38 +00:00
DebugLog.DebugWrite("player");
playerPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkplayer.prefab");
2020-12-02 18:40:38 +00:00
var ident = playerPrefab.AddComponent<QSBNetworkIdentity>();
ident.LocalPlayerAuthority = true;
2020-12-02 20:43:16 +00:00
ident.SetValue("m_AssetId", playerPrefab.GetComponent<NetworkIdentity>().assetId);
ident.SetValue("m_SceneId", playerPrefab.GetComponent<NetworkIdentity>().sceneId);
2020-02-21 23:36:07 +01:00
playerPrefab.AddComponent<PlayerTransformSync>();
playerPrefab.AddComponent<AnimationSync>();
playerPrefab.AddComponent<WakeUpSync>();
2020-11-20 19:50:31 +00:00
playerPrefab.AddComponent<InstrumentsManager>();
2020-12-02 18:40:38 +00:00
DebugLog.DebugWrite("ship");
_shipPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkship.prefab");
2020-12-02 18:40:38 +00:00
ident = _shipPrefab.AddComponent<QSBNetworkIdentity>();
ident.LocalPlayerAuthority = true;
2020-12-02 20:43:16 +00:00
ident.SetValue("m_AssetId", _shipPrefab.GetComponent<NetworkIdentity>().assetId);
ident.SetValue("m_SceneId", _shipPrefab.GetComponent<NetworkIdentity>().sceneId);
_shipPrefab.AddComponent<ShipTransformSync>();
spawnPrefabs.Add(_shipPrefab);
2020-12-02 18:40:38 +00:00
DebugLog.DebugWrite("camera");
_cameraPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkcameraroot.prefab");
2020-12-02 18:40:38 +00:00
ident = _cameraPrefab.AddComponent<QSBNetworkIdentity>();
ident.LocalPlayerAuthority = true;
2020-12-02 20:43:16 +00:00
ident.SetValue("m_AssetId", _cameraPrefab.GetComponent<NetworkIdentity>().assetId);
ident.SetValue("m_SceneId", _cameraPrefab.GetComponent<NetworkIdentity>().sceneId);
_cameraPrefab.AddComponent<PlayerCameraSync>();
spawnPrefabs.Add(_cameraPrefab);
2020-12-02 18:40:38 +00:00
DebugLog.DebugWrite("probe");
_probePrefab = _assetBundle.LoadAsset<GameObject>("assets/networkprobe.prefab");
2020-12-02 18:40:38 +00:00
ident = _probePrefab.AddComponent<QSBNetworkIdentity>();
ident.LocalPlayerAuthority = true;
2020-12-02 21:23:01 +00:00
ident.SetValue("m_AssetId", _probePrefab.GetComponent<NetworkIdentity>().assetId);
2020-12-02 20:43:16 +00:00
ident.SetValue("m_SceneId", _probePrefab.GetComponent<NetworkIdentity>().sceneId);
_probePrefab.AddComponent<PlayerProbeSync>();
spawnPrefabs.Add(_probePrefab);
2020-12-02 18:40:38 +00:00
DebugLog.DebugWrite("orb");
2020-09-29 21:34:46 +01:00
OrbPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkorb.prefab");
2020-12-02 18:40:38 +00:00
ident = OrbPrefab.AddComponent<QSBNetworkIdentity>();
ident.LocalPlayerAuthority = true;
2020-12-02 20:43:16 +00:00
ident.SetValue("m_AssetId", OrbPrefab.GetComponent<NetworkIdentity>().assetId);
ident.SetValue("m_SceneId", OrbPrefab.GetComponent<NetworkIdentity>().sceneId);
2020-09-29 21:34:46 +01:00
OrbPrefab.AddComponent<NomaiOrbTransformSync>();
spawnPrefabs.Add(OrbPrefab);
ConfigureNetworkManager();
2020-11-25 09:54:52 +00:00
QSBSceneManager.OnUniverseSceneLoaded += OnSceneLoaded;
}
2020-11-28 21:03:14 +00:00
private void OnDestroy()
2020-11-28 19:25:49 +00:00
=> QSBSceneManager.OnUniverseSceneLoaded -= OnSceneLoaded;
2020-11-25 09:54:52 +00:00
private void OnSceneLoaded(OWScene scene)
{
2020-11-28 11:26:59 +00:00
DebugLog.DebugWrite("scene loaded");
2020-11-25 09:54:52 +00:00
OrbManager.Instance.BuildOrbs();
2020-11-28 11:26:59 +00:00
OrbManager.Instance.QueueBuildSlots();
2020-11-25 09:54:52 +00:00
WorldRegistry.OldDialogueTrees.Clear();
WorldRegistry.OldDialogueTrees = Resources.FindObjectsOfTypeAll<CharacterDialogueTree>().ToList();
}
private void ConfigureNetworkManager()
{
networkAddress = QSB.DefaultServerIP;
2020-08-23 12:42:48 +02:00
networkPort = QSB.Port;
maxConnections = MaxConnections;
customConfig = true;
connectionConfig.AddChannel(QosType.Reliable);
connectionConfig.AddChannel(QosType.Unreliable);
2020-09-07 20:27:56 +02:00
this.SetValue("m_MaxBufferedPackets", MaxBufferedPackets);
channels.Add(QosType.Reliable);
channels.Add(QosType.Unreliable);
2020-11-04 09:34:01 +00:00
DebugLog.DebugWrite("Network Manager ready.", MessageType.Success);
}
2020-10-23 10:36:13 +01:00
public override void OnStartServer()
{
2020-12-02 09:51:53 +00:00
DebugLog.DebugWrite("OnStartServer", MessageType.Info);
2020-10-23 10:36:13 +01:00
if (WorldRegistry.OrbSyncList.Count == 0 && QSBSceneManager.IsInUniverse)
{
2020-10-23 10:38:51 +01:00
OrbManager.Instance.QueueBuildOrbs();
2020-10-23 10:36:13 +01:00
}
2020-10-24 15:31:20 +01:00
if (WorldRegistry.OldDialogueTrees.Count == 0 && QSBSceneManager.IsInUniverse)
{
WorldRegistry.OldDialogueTrees = Resources.FindObjectsOfTypeAll<CharacterDialogueTree>().ToList();
}
2020-11-08 14:41:16 +00:00
2020-12-02 12:42:26 +00:00
QSBNetworkServer.UnregisterHandler(40);
QSBNetworkServer.UnregisterHandler(41);
QSBNetworkServer.UnregisterHandler(42);
QSBNetworkServer.RegisterHandler(40, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationServerMessage));
QSBNetworkServer.RegisterHandler(41, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationParametersServerMessage));
QSBNetworkServer.RegisterHandler(42, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationTriggerServerMessage));
2020-10-23 10:36:13 +01:00
}
2020-12-02 12:42:26 +00:00
public override void OnServerAddPlayer(QSBNetworkConnection connection, short playerControllerId) // Called on the server when a client joins
{
2020-12-02 09:51:53 +00:00
DebugLog.DebugWrite("OnServerAddPlayer", MessageType.Info);
base.OnServerAddPlayer(connection, playerControllerId);
2020-12-02 12:42:26 +00:00
QSBNetworkServer.SpawnWithClientAuthority(Instantiate(_shipPrefab), connection);
QSBNetworkServer.SpawnWithClientAuthority(Instantiate(_cameraPrefab), connection);
QSBNetworkServer.SpawnWithClientAuthority(Instantiate(_probePrefab), connection);
2020-02-13 20:23:26 +01:00
}
2020-12-02 18:40:38 +00:00
public override void OnClientConnect(QSBNetworkConnection connection) // Called on the client when connecting to a server
2020-02-15 20:48:02 +01:00
{
2020-12-02 09:51:53 +00:00
DebugLog.DebugWrite("OnClientConnect", MessageType.Info);
base.OnClientConnect(connection);
2020-02-13 20:23:26 +01:00
2020-11-03 22:29:23 +00:00
gameObject.AddComponent<SectorSync.SectorSync>();
gameObject.AddComponent<RespawnOnDeath>();
gameObject.AddComponent<PreventShipDestruction>();
2020-11-25 09:54:52 +00:00
if (QSBSceneManager.IsInUniverse)
{
QSBSectorManager.Instance.RebuildSectors();
2020-11-29 15:23:42 +00:00
OrbManager.Instance.QueueBuildSlots();
2020-11-25 09:54:52 +00:00
}
2020-09-29 21:34:46 +01:00
2020-12-02 18:40:38 +00:00
if (!QSBNetworkServer.localClientActive)
{
2020-11-03 21:11:10 +00:00
QSBPatchManager.DoPatchType(QSBPatchTypes.OnNonServerClientConnect);
2020-11-09 21:05:50 +00:00
singleton.client.UnregisterHandler(40);
singleton.client.UnregisterHandler(41);
2020-12-02 12:42:26 +00:00
singleton.client.RegisterHandlerSafe(40, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationClientMessage));
singleton.client.RegisterHandlerSafe(41, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationParametersClientMessage));
}
2020-11-08 14:41:16 +00:00
singleton.client.UnregisterHandler(42);
2020-12-02 12:42:26 +00:00
singleton.client.RegisterHandlerSafe(42, new QSBNetworkMessageDelegate(QSBNetworkAnimator.OnAnimationTriggerClientMessage));
2020-11-08 14:41:16 +00:00
2020-11-03 21:11:10 +00:00
QSBPatchManager.DoPatchType(QSBPatchTypes.OnClientConnect);
2020-08-18 22:37:27 +02:00
_lobby.CanEditName = false;
OnNetworkManagerReady?.Invoke();
IsReady = true;
2020-08-08 12:23:23 +01:00
2020-11-03 21:11:10 +00:00
QSB.Helper.Events.Unity.RunWhen(() => PlayerTransformSync.LocalInstance != null, QSBEventManager.Init);
2020-08-09 14:26:33 +01:00
2020-11-03 21:11:10 +00:00
QSB.Helper.Events.Unity.RunWhen(() => QSBEventManager.Ready,
2020-08-18 22:37:27 +02:00
() => GlobalMessenger<string>.FireEvent(EventNames.QSBPlayerJoin, _lobby.PlayerName));
2020-09-02 11:17:04 +01:00
2020-11-03 21:11:10 +00:00
QSB.Helper.Events.Unity.RunWhen(() => QSBEventManager.Ready,
2020-09-02 11:17:04 +01:00
() => GlobalMessenger.FireEvent(EventNames.QSBPlayerStatesRequest));
}
public override void OnStopClient() // Called on the client when closing connection
{
2020-12-02 09:51:53 +00:00
DebugLog.DebugWrite("OnStopClient", MessageType.Info);
2020-08-22 18:08:25 +01:00
DebugLog.ToConsole("Disconnecting from server...", MessageType.Info);
2020-11-03 22:29:23 +00:00
Destroy(GetComponent<SectorSync.SectorSync>());
Destroy(GetComponent<RespawnOnDeath>());
Destroy(GetComponent<PreventShipDestruction>());
2020-11-03 21:11:10 +00:00
QSBEventManager.Reset();
2020-11-03 21:18:40 +00:00
QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove());
2020-08-20 19:31:10 +01:00
2020-11-03 21:18:40 +00:00
foreach (var player in QSBPlayerManager.PlayerList)
2020-08-20 17:29:47 +01:00
{
2020-11-03 21:18:40 +00:00
QSBPlayerManager.GetPlayerNetIds(player).ForEach(CleanupNetworkBehaviour);
2020-08-20 17:29:47 +01:00
}
2020-11-03 21:18:40 +00:00
QSBPlayerManager.RemoveAllPlayers();
2020-08-20 17:29:47 +01:00
2020-09-17 20:14:55 +01:00
WorldRegistry.RemoveObjects<QSBOrbSlot>();
WorldRegistry.RemoveObjects<QSBElevator>();
WorldRegistry.RemoveObjects<QSBGeyser>();
WorldRegistry.RemoveObjects<QSBSector>();
2020-09-29 21:34:46 +01:00
WorldRegistry.OrbSyncList.Clear();
WorldRegistry.OldDialogueTrees.Clear();
2020-09-17 20:14:55 +01:00
2020-08-20 18:24:31 +01:00
_lobby.CanEditName = true;
2020-03-04 21:46:16 +01:00
}
2020-12-02 12:42:26 +00:00
public override void OnServerDisconnect(QSBNetworkConnection connection) // Called on the server when any client disconnects
2020-03-06 19:03:35 +01:00
{
2020-12-02 09:51:53 +00:00
DebugLog.DebugWrite("OnServerDisconnect", MessageType.Info);
2020-12-02 08:29:32 +00:00
var player = connection.GetPlayer();
2020-12-02 12:42:26 +00:00
var netIds = connection.ClientOwnedObjects.Select(x => x.Value).ToArray();
2020-12-02 08:29:32 +00:00
GlobalMessenger<uint, uint[]>.FireEvent(EventNames.QSBPlayerLeave, player.PlayerId, netIds);
2020-09-29 21:34:46 +01:00
foreach (var item in WorldRegistry.OrbSyncList)
{
2020-12-02 09:51:53 +00:00
var identity = item.GetComponent<QSBNetworkIdentity>();
if (identity.ClientAuthorityOwner == connection)
2020-09-29 21:34:46 +01:00
{
identity.RemoveClientAuthority(connection);
}
}
2020-12-02 08:29:32 +00:00
player.HudMarker?.Remove();
2020-08-19 21:28:04 +01:00
CleanupConnection(connection);
2020-03-06 19:03:35 +01:00
}
2020-08-15 20:32:58 +01:00
public override void OnStopServer()
{
2020-12-02 09:51:53 +00:00
DebugLog.DebugWrite("OnStopServer", MessageType.Info);
2020-11-03 22:29:23 +00:00
Destroy(GetComponent<SectorSync.SectorSync>());
2020-08-20 17:29:47 +01:00
Destroy(GetComponent<RespawnOnDeath>());
Destroy(GetComponent<PreventShipDestruction>());
2020-11-03 21:11:10 +00:00
QSBEventManager.Reset();
2020-08-24 15:51:12 +01:00
DebugLog.ToConsole("[S] Server stopped!", MessageType.Info);
2020-11-03 21:18:40 +00:00
QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove());
2020-12-02 12:42:26 +00:00
QSBNetworkServer.connections.ToList().ForEach(CleanupConnection);
2020-09-17 20:14:55 +01:00
WorldRegistry.RemoveObjects<QSBOrbSlot>();
WorldRegistry.RemoveObjects<QSBElevator>();
WorldRegistry.RemoveObjects<QSBGeyser>();
WorldRegistry.RemoveObjects<QSBSector>();
2020-08-17 16:51:56 +01:00
base.OnStopServer();
}
2020-12-02 12:42:26 +00:00
private void CleanupConnection(QSBNetworkConnection connection)
2020-08-17 16:51:56 +01:00
{
2020-12-02 08:29:32 +00:00
var player = connection.GetPlayer();
DebugLog.ToConsole($"{player.Name} disconnected.", MessageType.Info);
QSBPlayerManager.RemovePlayer(player.PlayerId);
2020-08-18 22:37:27 +02:00
2020-12-02 12:42:26 +00:00
var netIds = connection.ClientOwnedObjects?.Select(x => x.Value).ToList();
2020-10-22 21:21:41 +01:00
netIds.ForEach(CleanupNetworkBehaviour);
2020-08-17 16:51:56 +01:00
}
2020-09-04 20:09:25 +01:00
public void CleanupNetworkBehaviour(uint netId)
2020-08-17 16:51:56 +01:00
{
2020-09-04 18:54:53 +01:00
DebugLog.DebugWrite($"Cleaning up netId {netId}");
2020-08-20 14:18:11 +01:00
// Multiple networkbehaviours can use the same networkidentity (same netId), so get all of them
2020-12-02 09:51:53 +00:00
var networkBehaviours = FindObjectsOfType<QSBNetworkBehaviour>()
.Where(x => x != null && x.NetId.Value == netId);
2020-08-18 22:37:27 +02:00
foreach (var networkBehaviour in networkBehaviours)
2020-08-17 16:51:56 +01:00
{
2020-08-31 09:36:29 +01:00
var transformSync = networkBehaviour.GetComponent<TransformSync.TransformSync>();
2020-08-17 16:51:56 +01:00
2020-08-17 17:59:13 +01:00
if (transformSync != null)
2020-08-17 16:51:56 +01:00
{
2020-09-29 21:34:46 +01:00
DebugLog.DebugWrite($" * Removing TransformSync from syncobjects");
2020-11-03 21:18:40 +00:00
QSBPlayerManager.PlayerSyncObjects.Remove(transformSync);
2020-12-02 09:51:53 +00:00
if (transformSync.SyncedTransform != null && netId != QSBPlayerManager.LocalPlayerId && !networkBehaviour.HasAuthority)
2020-08-17 17:59:13 +01:00
{
2020-09-29 21:34:46 +01:00
DebugLog.DebugWrite($" * Destroying {transformSync.SyncedTransform.gameObject.name}");
2020-08-31 09:36:29 +01:00
Destroy(transformSync.SyncedTransform.gameObject);
2020-08-17 17:59:13 +01:00
}
2020-08-17 16:51:56 +01:00
}
2020-09-04 20:10:01 +01:00
2020-09-29 21:34:46 +01:00
var animationSync = networkBehaviour.GetComponent<AnimationSync>();
if (animationSync != null)
{
DebugLog.DebugWrite($" * Removing AnimationSync from syncobjects");
2020-11-03 21:18:40 +00:00
QSBPlayerManager.PlayerSyncObjects.Remove(animationSync);
2020-09-29 21:34:46 +01:00
}
2020-12-02 09:51:53 +00:00
if (!networkBehaviour.HasAuthority)
2020-09-04 18:54:53 +01:00
{
2020-09-29 21:34:46 +01:00
DebugLog.DebugWrite($" * Destroying {networkBehaviour.gameObject.name}");
2020-09-04 18:54:53 +01:00
Destroy(networkBehaviour.gameObject);
}
}
2020-02-13 20:23:26 +01:00
}
}
2020-11-22 21:08:10 +00:00
}