quantum-space-buddies/QSB/QSBNetworkManager.cs

256 lines
7.7 KiB
C#
Raw Normal View History

2020-08-22 18:08:25 +01:00
using OWML.Common;
2020-12-20 10:56:15 +00:00
using OWML.Utils;
2021-08-09 11:49:58 +01:00
using QSB.ClientServerStateSync;
2020-08-13 21:46:16 +02:00
using QSB.DeathSync;
2020-12-14 16:24:52 +00:00
using QSB.Events;
2021-04-11 17:05:02 +01:00
using QSB.OrbSync.TransformSync;
2020-11-03 21:11:10 +00:00
using QSB.Patches;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2021-04-11 17:05:02 +01:00
using QSB.Player.TransformSync;
using QSB.PoolSync;
using QSB.TimeSync;
using QSB.Utility;
2020-09-04 20:57:35 +01:00
using QSB.WorldSync;
2020-12-04 22:15:41 +00:00
using QuantumUNET;
2020-12-07 21:19:16 +00:00
using QuantumUNET.Components;
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-23 12:58:45 +00:00
public class QSBNetworkManager : QNetworkManager
2020-12-02 21:29:53 +00:00
{
2020-12-14 21:20:53 +00:00
public static QSBNetworkManager Instance { get; private set; }
2020-12-02 21:29:53 +00:00
2020-12-14 21:20:53 +00:00
public event Action OnNetworkManagerReady;
2021-08-26 08:51:33 +01:00
public event Action OnClientConnected;
public event Action<NetworkError> OnClientDisconnected;
public event Action<NetworkError> OnClientErrorThrown;
2020-12-02 21:29:53 +00:00
2020-12-14 21:20:53 +00:00
public bool IsReady { get; private set; }
2021-04-13 21:31:59 +01:00
public GameObject OrbPrefab { get; private set; }
public GameObject ShipPrefab { get; private set; }
2021-11-09 17:56:45 -08:00
public GameObject AnglerPrefab { get; private set; }
2021-08-24 21:05:31 +01:00
public string PlayerName { get; private set; }
2020-12-03 08:28:05 +00:00
2020-12-14 21:20:53 +00:00
private const int MaxConnections = 128;
private const int MaxBufferedPackets = 64;
2020-12-02 21:29:53 +00:00
private AssetBundle _assetBundle;
private GameObject _probePrefab;
private bool _everConnected;
2020-12-02 21:29:53 +00:00
2021-03-06 13:00:28 +00:00
public new void Awake()
2020-12-02 21:29:53 +00:00
{
base.Awake();
2020-12-02 21:29:53 +00:00
Instance = this;
2021-08-24 21:05:31 +01:00
PlayerName = GetPlayerName();
2020-12-14 16:24:52 +00:00
_assetBundle = QSBCore.NetworkAssetBundle;
2020-12-02 21:29:53 +00:00
2021-11-24 22:16:56 +00:00
playerPrefab = _assetBundle.LoadAsset<GameObject>("Assets/Prefabs/NETWORK_Player_Body.prefab");
2020-12-02 21:29:53 +00:00
2021-11-24 22:16:56 +00:00
ShipPrefab = _assetBundle.LoadAsset<GameObject>("assets/Prefabs/networkship.prefab");
2021-04-13 21:31:59 +01:00
spawnPrefabs.Add(ShipPrefab);
2021-11-24 22:16:56 +00:00
_probePrefab = _assetBundle.LoadAsset<GameObject>("assets/Prefabs/networkprobe.prefab");
2020-12-02 21:29:53 +00:00
spawnPrefabs.Add(_probePrefab);
2021-11-24 22:16:56 +00:00
OrbPrefab = _assetBundle.LoadAsset<GameObject>("assets/Prefabs/networkorb.prefab");
2020-12-02 21:29:53 +00:00
spawnPrefabs.Add(OrbPrefab);
2021-11-24 22:16:56 +00:00
AnglerPrefab = _assetBundle.LoadAsset<GameObject>("assets/Prefabs/networkangler.prefab");
2021-11-09 17:56:45 -08:00
spawnPrefabs.Add(AnglerPrefab);
2020-12-02 21:29:53 +00:00
ConfigureNetworkManager();
}
2021-08-24 21:05:31 +01:00
private string GetPlayerName()
{
2021-10-25 16:35:16 +01:00
try
{
var profileManager = StandaloneProfileManager.SharedInstance;
profileManager.Initialize();
var profile = profileManager._currentProfile;
var profileName = profile.profileName;
return profileName;
}
catch (Exception ex)
{
DebugLog.ToConsole($"Error - Exception when getting player name : {ex}", MessageType.Error);
return "Player";
}
2021-08-24 21:05:31 +01:00
}
2020-12-02 21:29:53 +00:00
private void ConfigureNetworkManager()
{
2020-12-14 16:24:52 +00:00
networkAddress = QSBCore.DefaultServerIP;
networkPort = QSBCore.Port;
2020-12-02 21:29:53 +00:00
maxConnections = MaxConnections;
customConfig = true;
connectionConfig.AddChannel(QosType.Reliable);
connectionConfig.AddChannel(QosType.Unreliable);
this.SetValue("m_MaxBufferedPackets", MaxBufferedPackets);
channels.Add(QosType.Reliable);
channels.Add(QosType.Unreliable);
DebugLog.DebugWrite("Network Manager ready.", MessageType.Success);
}
public override void OnStartServer()
{
DebugLog.DebugWrite("OnStartServer", MessageType.Info);
2020-12-11 13:14:58 +00:00
if (QSBWorldSync.OldDialogueTrees.Count == 0 && QSBSceneManager.IsInUniverse)
2020-12-02 21:29:53 +00:00
{
QSBWorldSync.OldDialogueTrees = QSBWorldSync.GetUnityObjects<CharacterDialogueTree>().ToList();
2020-12-02 21:29:53 +00:00
}
}
2020-12-23 12:58:45 +00:00
public override void OnServerAddPlayer(QNetworkConnection connection, short playerControllerId) // Called on the server when a client joins
2020-12-02 21:29:53 +00:00
{
2020-12-12 18:14:04 +00:00
DebugLog.DebugWrite($"OnServerAddPlayer {playerControllerId}", MessageType.Info);
2020-12-02 21:29:53 +00:00
base.OnServerAddPlayer(connection, playerControllerId);
2020-12-23 12:58:45 +00:00
QNetworkServer.SpawnWithClientAuthority(Instantiate(_probePrefab), connection);
2020-12-02 21:29:53 +00:00
}
2020-12-23 12:58:45 +00:00
public override void OnStartClient(QNetworkClient _)
2020-12-19 21:40:54 +01:00
{
var config = QSBCore.Helper.Config;
config.SetSettingsValue("defaultServerIP", networkAddress);
QSBCore.Helper.Storage.Save(config, Constants.ModConfigFileName);
}
public override void OnClientError(QNetworkConnection conn, int errorCode)
=> OnClientErrorThrown?.SafeInvoke((NetworkError)errorCode);
2020-12-23 12:58:45 +00:00
public override void OnClientConnect(QNetworkConnection connection) // Called on the client when connecting to a server
2020-12-02 21:29:53 +00:00
{
DebugLog.DebugWrite("OnClientConnect", MessageType.Info);
base.OnClientConnect(connection);
2021-08-26 08:51:33 +01:00
OnClientConnected?.SafeInvoke();
2020-12-12 18:14:04 +00:00
QSBEventManager.Init();
2020-12-02 21:29:53 +00:00
gameObject.AddComponent<RespawnOnDeath>();
2021-08-08 19:59:40 +01:00
gameObject.AddComponent<ServerStateManager>();
2021-08-08 19:57:58 +01:00
gameObject.AddComponent<ClientStateManager>();
2020-12-02 21:29:53 +00:00
if (QSBSceneManager.IsInUniverse)
{
2021-03-23 13:18:29 +00:00
WorldObjectManager.Rebuild(QSBSceneManager.CurrentScene);
2020-12-02 21:29:53 +00:00
}
2020-12-23 12:58:45 +00:00
var specificType = QNetworkServer.active ? QSBPatchTypes.OnServerClientConnect : QSBPatchTypes.OnNonServerClientConnect;
2020-12-21 19:41:53 +00:00
QSBPatchManager.DoPatchType(specificType);
2020-12-02 21:29:53 +00:00
QSBPatchManager.DoPatchType(QSBPatchTypes.OnClientConnect);
2021-02-06 22:03:10 +00:00
OnNetworkManagerReady?.SafeInvoke();
2020-12-02 21:29:53 +00:00
IsReady = true;
2021-03-13 10:17:52 +00:00
QSBCore.UnityEvents.RunWhen(() => QSBEventManager.Ready && PlayerTransformSync.LocalInstance != null,
2021-08-24 21:05:31 +01:00
() => QSBEventManager.FireEvent(EventNames.QSBPlayerJoin, PlayerName));
2020-12-02 21:29:53 +00:00
2021-08-08 19:53:55 +01:00
if (!QSBCore.IsHost)
2020-12-11 22:42:21 +00:00
{
2021-03-13 10:17:52 +00:00
QSBCore.UnityEvents.RunWhen(() => QSBEventManager.Ready && PlayerTransformSync.LocalInstance != null,
() => QSBEventManager.FireEvent(EventNames.QSBRequestStateResync));
2020-12-11 22:42:21 +00:00
}
_everConnected = true;
2020-12-02 21:29:53 +00:00
}
public override void OnStopClient() // Called on the client when closing connection
{
DebugLog.DebugWrite("OnStopClient", MessageType.Info);
DebugLog.ToConsole("Disconnecting from server...", MessageType.Info);
Destroy(GetComponent<RespawnOnDeath>());
2021-08-08 19:53:55 +01:00
Destroy(GetComponent<ServerStateManager>());
Destroy(GetComponent<ClientStateManager>());
2020-12-02 21:29:53 +00:00
QSBEventManager.Reset();
QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove());
2021-02-09 21:35:31 +00:00
RemoveWorldObjects();
2021-04-28 16:32:01 +01:00
NomaiOrbTransformSync.OrbTransformSyncs.Clear();
2020-12-11 13:14:58 +00:00
QSBWorldSync.OldDialogueTrees.Clear();
2020-12-02 21:29:53 +00:00
2021-11-11 19:34:25 +00:00
if (WakeUpSync.LocalInstance != null)
{
WakeUpSync.LocalInstance.OnDisconnect();
}
if (_everConnected)
{
var specificType = QNetworkServer.active ? QSBPatchTypes.OnServerClientConnect : QSBPatchTypes.OnNonServerClientConnect;
QSBPatchManager.DoUnpatchType(specificType);
QSBPatchManager.DoUnpatchType(QSBPatchTypes.OnClientConnect);
}
2021-02-09 17:18:01 +00:00
IsReady = false;
_everConnected = false;
2020-12-02 21:29:53 +00:00
}
2021-08-26 08:51:33 +01:00
public override void OnClientDisconnect(QNetworkConnection conn)
{
base.OnClientDisconnect(conn);
OnClientDisconnected?.SafeInvoke(conn.LastError);
}
2020-12-23 12:58:45 +00:00
public override void OnServerDisconnect(QNetworkConnection connection) // Called on the server when any client disconnects
2020-12-02 21:29:53 +00:00
{
2020-12-12 11:27:28 +00:00
base.OnServerDisconnect(connection);
2020-12-02 21:29:53 +00:00
DebugLog.DebugWrite("OnServerDisconnect", MessageType.Info);
2021-04-28 16:32:01 +01:00
foreach (var item in NomaiOrbTransformSync.OrbTransformSyncs)
2020-12-02 21:29:53 +00:00
{
2021-10-25 11:07:09 +01:00
if (item is null)
{
continue;
}
2020-12-23 12:58:45 +00:00
var identity = item.GetComponent<QNetworkIdentity>();
2020-12-02 21:29:53 +00:00
if (identity.ClientAuthorityOwner == connection)
{
identity.RemoveClientAuthority(connection);
}
}
}
public override void OnStopServer()
{
DebugLog.DebugWrite("OnStopServer", MessageType.Info);
Destroy(GetComponent<RespawnOnDeath>());
QSBEventManager.Reset();
2020-12-13 22:25:23 +00:00
DebugLog.ToConsole("Server stopped!", MessageType.Info);
2020-12-02 21:29:53 +00:00
QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove());
2021-02-09 21:35:31 +00:00
base.OnStopServer();
}
2021-02-14 09:43:36 +00:00
2021-02-09 21:35:31 +00:00
private void RemoveWorldObjects()
{
2021-03-23 13:18:29 +00:00
QSBWorldSync.RemoveWorldObjects<IWorldObjectTypeSubset>();
QSBWorldSync.RemoveWorldObjects<IWorldObject>();
foreach (var platform in QSBWorldSync.GetUnityObjects<CustomNomaiRemoteCameraPlatform>())
2021-03-23 13:18:29 +00:00
{
2021-03-28 14:19:26 +01:00
Destroy(platform);
2021-03-23 13:18:29 +00:00
}
2021-06-18 22:38:32 +01:00
foreach (var camera in QSBWorldSync.GetUnityObjects<CustomNomaiRemoteCamera>())
2021-03-23 13:18:29 +00:00
{
Destroy(camera);
}
2021-06-18 22:38:32 +01:00
foreach (var streaming in QSBWorldSync.GetUnityObjects<CustomNomaiRemoteCameraStreaming>())
2021-03-23 13:18:29 +00:00
{
2021-03-28 14:19:26 +01:00
Destroy(streaming);
2021-03-23 13:18:29 +00:00
}
2021-07-17 09:52:46 +01:00
WorldObjectManager.SetNotReady();
2020-12-02 21:29:53 +00:00
}
}
2021-11-09 17:56:45 -08:00
}