quantum-space-buddies/QSB/QSBNetworkManager.cs

303 lines
9.5 KiB
C#
Raw Normal View History

2021-12-07 15:56:08 +00:00
using OWML.Common;
2020-12-20 10:56:15 +00:00
using OWML.Utils;
using QSB.Anglerfish.TransformSync;
2021-12-01 17:50:56 -08:00
using QSB.AuthoritySync;
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;
using QSB.JellyfishSync.TransformSync;
using QSB.Messaging;
2021-12-23 17:07:29 -08:00
using QSB.OrbSync.Messages;
2021-04-11 17:05:02 +01:00
using QSB.OrbSync.TransformSync;
2021-12-23 17:07:29 -08:00
using QSB.OrbSync.WorldObjects;
2020-11-03 21:11:10 +00:00
using QSB.Patches;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2021-12-23 17:07:29 -08:00
using QSB.Player.Messages;
2021-04-11 17:05:02 +01:00
using QSB.Player.TransformSync;
using QSB.PoolSync;
using QSB.ShipSync.TransformSync;
using QSB.TimeSync;
using QSB.Tools.ProbeTool.TransformSync;
using QSB.TornadoSync.TransformSync;
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;
2021-12-07 15:56:08 +00:00
using System;
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-12-01 01:10:38 -08:00
public GameObject JellyfishPrefab { get; private set; }
2021-12-04 06:21:11 -08:00
public GameObject OccasionalPrefab { 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 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-02 21:29:53 +00:00
playerPrefab = QSBCore.NetworkAssetBundle.LoadAsset<GameObject>("Assets/Prefabs/NETWORK_Player_Body.prefab");
2020-12-02 21:29:53 +00:00
ShipPrefab = MakeNewNetworkObject(2, "NetworkShip", typeof(ShipTransformSync));
2021-04-13 21:31:59 +01:00
spawnPrefabs.Add(ShipPrefab);
_probePrefab = MakeNewNetworkObject(3, "NetworkProbe", typeof(PlayerProbeSync));
2020-12-02 21:29:53 +00:00
spawnPrefabs.Add(_probePrefab);
OrbPrefab = MakeNewNetworkObject(4, "NetworkOrb", typeof(NomaiOrbTransformSync));
2020-12-02 21:29:53 +00:00
spawnPrefabs.Add(OrbPrefab);
AnglerPrefab = MakeNewNetworkObject(5, "NetworkAngler", typeof(AnglerTransformSync));
2021-11-09 17:56:45 -08:00
spawnPrefabs.Add(AnglerPrefab);
JellyfishPrefab = MakeNewNetworkObject(6, "NetworkJellyfish", typeof(JellyfishTransformSync));
2021-12-01 01:10:38 -08:00
spawnPrefabs.Add(JellyfishPrefab);
OccasionalPrefab = MakeNewNetworkObject(7, "NetworkOccasional", typeof(OccasionalTransformSync));
2021-12-04 06:21:11 -08:00
spawnPrefabs.Add(OccasionalPrefab);
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
}
/// create a new network prefab from the network object prefab template.
/// this works by calling Unload(false) and then reloading the AssetBundle,
/// which makes LoadAsset give you a new resource.
/// see https://docs.unity3d.com/Manual/AssetBundles-Native.html.
private static GameObject MakeNewNetworkObject(int assetId, string name, Type transformSyncType)
{
QSBCore.NetworkAssetBundle.Unload(false);
QSBCore.NetworkAssetBundle = QSBCore.Helper.Assets.LoadBundle("AssetBundles/network");
var template = QSBCore.NetworkAssetBundle.LoadAsset<GameObject>("Assets/Prefabs/NetworkObject.prefab");
DebugLog.DebugWrite($"MakeNewNetworkObject - prefab id {template.GetInstanceID()} "
+ $"for {assetId} {name} {transformSyncType.Name}");
template.name = name;
template.GetRequiredComponent<QNetworkIdentity>().SetValue("m_AssetId", assetId);
template.AddComponent(transformSyncType);
return template;
}
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
{
2021-12-13 21:18:16 -08:00
QSBWorldSync.OldDialogueTrees.AddRange(QSBWorldSync.GetUnityObjects<CharacterDialogueTree>());
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();
QSBMessageManager.Init();
2020-12-12 18:14:04 +00:00
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-12-22 16:44:03 -08:00
() => new PlayerJoinMessage(PlayerName).Send());
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,
2021-12-22 18:20:53 -08:00
() => new RequestStateResyncMessage().Send());
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();
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);
}
public override void OnServerDisconnect(QNetworkConnection conn) // Called on the server when any client disconnects
2020-12-02 21:29:53 +00:00
{
DebugLog.DebugWrite("OnServerDisconnect", MessageType.Info);
// revert authority from ship
if (ShipTransformSync.LocalInstance)
2020-12-02 21:29:53 +00:00
{
var identity = ShipTransformSync.LocalInstance.NetIdentity;
if (identity.ClientAuthorityOwner == conn)
2020-12-02 21:29:53 +00:00
{
2021-12-01 22:09:12 -08:00
identity.SetAuthority(QSBPlayerManager.LocalPlayerId);
2020-12-02 21:29:53 +00:00
}
}
2021-12-17 21:14:30 -08:00
// stop dragging for the orbs this player was dragging
foreach (var qsbOrb in QSBWorldSync.GetWorldObjects<QSBOrb>())
{
2021-12-17 21:14:30 -08:00
if (!qsbOrb.TransformSync.enabled)
{
continue;
}
var identity = qsbOrb.TransformSync.NetIdentity;
if (identity.ClientAuthorityOwner == conn)
{
2021-12-17 21:14:30 -08:00
qsbOrb.SetDragging(false);
2021-12-23 01:50:09 -08:00
qsbOrb.SendMessage(new OrbDragMessage(false));
}
}
2021-12-01 17:50:56 -08:00
AuthorityManager.OnDisconnect(conn.GetPlayerId());
base.OnServerDisconnect(conn);
2020-12-02 21:29:53 +00:00
}
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-12-23 01:50:09 -08:00
private static void RemoveWorldObjects()
2021-02-09 21:35:31 +00:00
{
2021-12-11 22:00:21 -08:00
QSBWorldSync.RemoveWorldObjects();
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
}