quantum-space-buddies/QSB/QSBNetworkManager.cs

244 lines
8.7 KiB
C#
Raw Normal View History

2020-03-04 21:46:16 +01:00
using System;
using System.Linq;
using OWML.ModHelper.Events;
2020-03-04 21:46:16 +01:00
using QSB.Animation;
2020-08-13 21:46:16 +02:00
using QSB.DeathSync;
2020-03-04 21:46:16 +01:00
using QSB.Events;
2020-08-13 19:25:12 +02:00
using QSB.GeyserSync;
using QSB.TimeSync;
2020-02-21 23:36:07 +01:00
using QSB.TransformSync;
using QSB.Utility;
2020-02-21 23:36:07 +01:00
using UnityEngine;
using UnityEngine.Events;
2020-02-13 20:23:26 +01:00
using UnityEngine.Networking;
2020-02-15 20:48:02 +01:00
namespace QSB
{
public class QSBNetworkManager : NetworkManager
{
public static UnityEvent OnNetworkManagerReady = new UnityEvent();
2020-07-28 15:59:24 +02:00
public static bool IsReady;
private const int MaxConnections = 128;
2020-02-21 23:36:07 +01:00
private AssetBundle _assetBundle;
private GameObject _shipPrefab;
private GameObject _cameraPrefab;
private GameObject _probePrefab;
2020-02-21 23:36:07 +01:00
2020-03-04 21:46:16 +01:00
private readonly string[] _defaultNames = {
"Arkose",
"Chert",
"Esker",
"Hal",
"Hornfels",
"Feldspar",
"Gabbro",
"Galena",
"Gneiss",
"Gossan",
"Marl",
"Mica",
"Moraine",
"Porphy",
"Riebeck",
"Rutile",
"Slate",
"Spinel",
"Tektite",
"Tephra",
"Tuff"
};
private string _playerName;
private bool _canEditName;
2020-03-04 21:46:16 +01:00
2020-02-15 20:48:02 +01:00
private void Awake()
{
2020-08-17 16:51:56 +01:00
_assetBundle = QSB.NetworkAssetBundle;
playerPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkplayer.prefab");
2020-02-21 23:36:07 +01:00
playerPrefab.AddComponent<PlayerTransformSync>();
playerPrefab.AddComponent<AnimationSync>();
playerPrefab.AddComponent<WakeUpSync>();
_shipPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkship.prefab");
_shipPrefab.AddComponent<ShipTransformSync>();
spawnPrefabs.Add(_shipPrefab);
_cameraPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkcameraroot.prefab");
_cameraPrefab.AddComponent<PlayerCameraSync>();
spawnPrefabs.Add(_cameraPrefab);
_probePrefab = _assetBundle.LoadAsset<GameObject>("assets/networkprobe.prefab");
_probePrefab.AddComponent<PlayerProbeSync>();
spawnPrefabs.Add(_probePrefab);
ConfigureNetworkManager();
2020-03-04 21:46:16 +01:00
_playerName = GetPlayerName();
_canEditName = true;
}
private string GetPlayerName()
{
var profileManager = StandaloneProfileManager.SharedInstance;
profileManager.Initialize();
var profile = profileManager.GetValue<StandaloneProfileManager.ProfileData>("_currentProfile");
var profileName = profile?.profileName;
return !string.IsNullOrEmpty(profileName)
? profileName
2020-07-28 15:59:24 +02:00
: _defaultNames.OrderBy(x => Guid.NewGuid()).First();
}
private void ConfigureNetworkManager()
{
networkAddress = QSB.DefaultServerIP;
maxConnections = MaxConnections;
customConfig = true;
connectionConfig.AddChannel(QosType.Reliable);
connectionConfig.AddChannel(QosType.Unreliable);
channels.Add(QosType.Reliable);
channels.Add(QosType.Unreliable);
}
public override void OnServerAddPlayer(NetworkConnection connection, short playerControllerId) // Called on the server when a client joins
{
2020-08-17 16:51:56 +01:00
DebugLog.ToConsole("On server add player " + playerControllerId);
base.OnServerAddPlayer(connection, playerControllerId);
// These have to be in a constant order (for now, until I get a better netId getting system...)
NetworkServer.SpawnWithClientAuthority(Instantiate(_shipPrefab), connection);
NetworkServer.SpawnWithClientAuthority(Instantiate(_cameraPrefab), connection);
NetworkServer.SpawnWithClientAuthority(Instantiate(_probePrefab), connection);
2020-08-09 22:10:47 +01:00
gameObject.AddComponent<Events.PlayerState>();
2020-02-13 20:23:26 +01:00
}
public override void OnClientConnect(NetworkConnection connection) // Called on the client when connecting to a server
2020-02-15 20:48:02 +01:00
{
base.OnClientConnect(connection);
2020-02-13 20:23:26 +01:00
2020-02-13 21:23:12 +01:00
gameObject.AddComponent<SectorSync>();
gameObject.AddComponent<RespawnOnDeath>();
gameObject.AddComponent<PreventShipDestruction>();
2020-08-13 14:32:58 +01:00
if (NetworkClient.active && !NetworkServer.active)
{
2020-08-09 21:46:51 +01:00
gameObject.AddComponent<Events.PlayerState>();
2020-08-13 19:25:12 +02:00
GeyserManager.Instance.EmptyUpdate();
2020-08-13 22:06:34 +01:00
WakeUpPatches.AddPatches();
}
_canEditName = false;
OnNetworkManagerReady.Invoke();
IsReady = true;
2020-08-08 12:23:23 +01:00
2020-08-10 18:17:54 +02:00
UnityHelper.Instance.RunWhen(() => PlayerTransformSync.LocalInstance != null, EventList.Init);
2020-08-09 14:26:33 +01:00
2020-08-18 14:00:37 +01:00
UnityHelper.Instance.RunWhen(() => EventList.Ready,
2020-08-17 16:51:56 +01:00
() => GlobalMessenger<string>.FireEvent(EventNames.QSBPlayerJoin, _playerName));
}
public override void OnStopClient() // Called on the client when closing connection
{
2020-08-18 13:56:07 +01:00
DebugLog.ToConsole("Disconnecting from server...", OWML.Common.MessageType.Info);
Destroy(GetComponent<SectorSync>());
Destroy(GetComponent<RespawnOnDeath>());
Destroy(GetComponent<PreventShipDestruction>());
2020-08-15 20:32:58 +01:00
EventList.Reset();
if (IsClientConnected())
{
2020-08-18 13:56:07 +01:00
PlayerTransformSync.LocalInstance?.gameObject.GetComponent<AnimationSync>().Reset();
}
foreach (var player in PlayerRegistry.PlayerList)
{
if (player.HudMarker != null)
{
Destroy(player.HudMarker.transform.parent.gameObject);
}
}
foreach (var connection in NetworkServer.connections)
{
CleanupConnection(connection);
}
_canEditName = true;
2020-03-04 21:46:16 +01:00
}
public override void OnServerDisconnect(NetworkConnection connection) // Called on the server when any client disconnects
2020-03-06 19:03:35 +01:00
{
var playerId = connection.playerControllers[0].gameObject.GetComponent<PlayerTransformSync>().netId.Value;
var objectIds = connection.clientOwnedObjects.Select(x => x.Value).ToArray();
2020-08-10 19:24:28 +02:00
GlobalMessenger<uint, uint[]>.FireEvent(EventNames.QSBPlayerLeave, playerId, objectIds);
2020-08-17 16:51:56 +01:00
CleanupConnection(connection);
2020-08-17 21:33:09 +01:00
var marker = PlayerRegistry.GetPlayer(playerId).HudMarker;
if (marker != null)
{
Destroy(marker.transform.parent.gameObject);
}
2020-03-06 19:03:35 +01:00
}
2020-08-15 20:32:58 +01:00
public override void OnStopServer()
{
2020-08-17 16:51:56 +01:00
DebugLog.ToConsole("Server stopped!", OWML.Common.MessageType.Info);
foreach (var connection in NetworkServer.connections)
{
CleanupConnection(connection);
}
base.OnStopServer();
}
private void CleanupConnection(NetworkConnection connection)
{
var playerId = connection.playerControllers[0].gameObject.GetComponent<PlayerTransformSync>().netId.Value;
var objectIds = connection.clientOwnedObjects.Select(x => x.Value).ToArray();
var playerName = PlayerRegistry.GetPlayer(playerId).Name;
DebugLog.ToConsole($"{playerName} disconnected.", OWML.Common.MessageType.Info);
PlayerRegistry.RemovePlayer(playerId);
foreach (var objectId in objectIds)
{
DestroyObject(objectId);
}
}
private void DestroyObject(uint objectId)
{
2020-08-17 17:59:13 +01:00
var components = FindObjectsOfType<NetworkBehaviour>()
.Where(x => x.netId.Value == objectId);
foreach (var component in components)
2020-08-17 16:51:56 +01:00
{
2020-08-17 17:59:13 +01:00
if (component == null)
{
return;
}
var transformSync = component.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-08-17 17:59:13 +01:00
PlayerRegistry.TransformSyncs.Remove(transformSync);
if (transformSync.SyncedTransform != null)
{
Destroy(transformSync.SyncedTransform.gameObject);
}
2020-08-17 16:51:56 +01:00
}
2020-08-17 17:59:13 +01:00
Destroy(component.gameObject);
2020-08-17 16:51:56 +01:00
}
2020-08-15 20:32:58 +01:00
}
2020-03-04 21:46:16 +01:00
private void OnGUI()
{
GUI.Label(new Rect(10, 10, 200f, 20f), "Name:");
if (_canEditName)
{
_playerName = GUI.TextField(new Rect(60, 10, 145, 20f), _playerName);
}
else
{
GUI.Label(new Rect(60, 10, 145, 20f), _playerName);
}
2020-02-13 20:23:26 +01:00
}
2020-02-13 20:23:26 +01:00
}
}