2020-08-22 18:08:25 +01:00
|
|
|
|
using OWML.Common;
|
|
|
|
|
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;
|
2020-09-04 20:54:34 +01:00
|
|
|
|
using QSB.OrbSync;
|
2020-02-24 19:55:16 +01:00
|
|
|
|
using QSB.TimeSync;
|
2020-02-21 23:36:07 +01:00
|
|
|
|
using QSB.TransformSync;
|
2020-07-30 22:27:14 +02:00
|
|
|
|
using QSB.Utility;
|
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
|
|
|
|
|
{
|
|
|
|
|
public class QSBNetworkManager : NetworkManager
|
|
|
|
|
{
|
2020-03-02 17:25:37 +01:00
|
|
|
|
private const int MaxConnections = 128;
|
|
|
|
|
|
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;
|
2020-07-28 00:13:43 +01:00
|
|
|
|
private GameObject _cameraPrefab;
|
2020-08-07 20:39:07 +01:00
|
|
|
|
private GameObject _probePrefab;
|
2020-09-04 20:54:34 +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-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-07-28 00:13:43 +01:00
|
|
|
|
|
2020-02-21 21:51:58 +01:00
|
|
|
|
playerPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkplayer.prefab");
|
2020-02-21 23:36:07 +01:00
|
|
|
|
playerPrefab.AddComponent<PlayerTransformSync>();
|
2020-02-18 21:39:18 +01:00
|
|
|
|
playerPrefab.AddComponent<AnimationSync>();
|
2020-02-24 19:55:16 +01:00
|
|
|
|
playerPrefab.AddComponent<WakeUpSync>();
|
2020-08-20 21:07:40 +02:00
|
|
|
|
DebugLog.LogState("PlayerPrefab", playerPrefab);
|
2020-02-21 21:51:58 +01:00
|
|
|
|
|
|
|
|
|
_shipPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkship.prefab");
|
|
|
|
|
_shipPrefab.AddComponent<ShipTransformSync>();
|
|
|
|
|
spawnPrefabs.Add(_shipPrefab);
|
2020-08-20 21:07:40 +02:00
|
|
|
|
DebugLog.LogState("ShipPrefab", _shipPrefab);
|
2020-03-02 17:25:37 +01:00
|
|
|
|
|
2020-07-28 00:13:43 +01:00
|
|
|
|
_cameraPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkcameraroot.prefab");
|
|
|
|
|
_cameraPrefab.AddComponent<PlayerCameraSync>();
|
|
|
|
|
spawnPrefabs.Add(_cameraPrefab);
|
2020-08-20 21:07:40 +02:00
|
|
|
|
DebugLog.LogState("CameraPrefab", _cameraPrefab);
|
2020-07-28 00:13:43 +01:00
|
|
|
|
|
2020-08-07 20:39:07 +01:00
|
|
|
|
_probePrefab = _assetBundle.LoadAsset<GameObject>("assets/networkprobe.prefab");
|
|
|
|
|
_probePrefab.AddComponent<PlayerProbeSync>();
|
|
|
|
|
spawnPrefabs.Add(_probePrefab);
|
2020-08-20 21:07:40 +02:00
|
|
|
|
DebugLog.LogState("ProbePrefab", _probePrefab);
|
2020-08-07 20:39:07 +01:00
|
|
|
|
|
2020-09-04 20:54:34 +01:00
|
|
|
|
OrbPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkorb.prefab");
|
|
|
|
|
OrbPrefab.AddComponent<NomaiOrbTransformSync>();
|
|
|
|
|
spawnPrefabs.Add(OrbPrefab);
|
|
|
|
|
DebugLog.LogState("OrbPrefab", OrbPrefab);
|
|
|
|
|
|
2020-03-02 20:44:44 +01:00
|
|
|
|
ConfigureNetworkManager();
|
2020-09-04 20:54:34 +01:00
|
|
|
|
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSceneLoaded(OWScene scene, bool inUniverse)
|
|
|
|
|
{
|
|
|
|
|
WorldRegistry.OldOrbList = Resources.FindObjectsOfTypeAll<NomaiInterfaceOrb>().ToList();
|
|
|
|
|
foreach (var orb in WorldRegistry.OldOrbList)
|
|
|
|
|
{
|
|
|
|
|
if (NetworkServer.active)
|
|
|
|
|
{
|
|
|
|
|
WorldRegistry.OrbUserList.Add(orb, PlayerRegistry.LocalPlayerId);
|
|
|
|
|
NetworkServer.Spawn(Instantiate(OrbPrefab));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
WorldRegistry.OrbUserList.Add(orb, NetworkInstanceId.Invalid);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-02 20:44:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ConfigureNetworkManager()
|
|
|
|
|
{
|
2020-03-02 17:34:01 +01:00
|
|
|
|
networkAddress = QSB.DefaultServerIP;
|
2020-08-23 12:42:48 +02:00
|
|
|
|
networkPort = QSB.Port;
|
2020-03-02 17:25:37 +01:00
|
|
|
|
maxConnections = MaxConnections;
|
|
|
|
|
customConfig = true;
|
|
|
|
|
connectionConfig.AddChannel(QosType.Reliable);
|
|
|
|
|
connectionConfig.AddChannel(QosType.Unreliable);
|
|
|
|
|
channels.Add(QosType.Reliable);
|
|
|
|
|
channels.Add(QosType.Unreliable);
|
2020-02-21 21:51:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 00:13:43 +01:00
|
|
|
|
public override void OnServerAddPlayer(NetworkConnection connection, short playerControllerId) // Called on the server when a client joins
|
2020-02-21 21:51:58 +01:00
|
|
|
|
{
|
2020-08-27 12:08:49 +01:00
|
|
|
|
DebugLog.DebugWrite("[S] Add player");
|
2020-07-28 00:13:43 +01:00
|
|
|
|
base.OnServerAddPlayer(connection, playerControllerId);
|
2020-02-21 21:51:58 +01:00
|
|
|
|
|
2020-08-20 14:10:37 +01:00
|
|
|
|
// These have to be in a constant order (for now, until we get a better netId getting system...)
|
2020-07-28 00:13:43 +01:00
|
|
|
|
NetworkServer.SpawnWithClientAuthority(Instantiate(_shipPrefab), connection);
|
|
|
|
|
NetworkServer.SpawnWithClientAuthority(Instantiate(_cameraPrefab), connection);
|
2020-08-07 20:39:07 +01:00
|
|
|
|
NetworkServer.SpawnWithClientAuthority(Instantiate(_probePrefab), connection);
|
2020-05-19 19:31:54 +02:00
|
|
|
|
|
2020-08-09 22:10:47 +01:00
|
|
|
|
gameObject.AddComponent<Events.PlayerState>();
|
2020-02-13 20:23:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 00:13:43 +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
|
|
|
|
{
|
2020-07-28 00:13:43 +01:00
|
|
|
|
base.OnClientConnect(connection);
|
2020-02-13 20:23:26 +01:00
|
|
|
|
|
2020-02-13 21:23:12 +01:00
|
|
|
|
gameObject.AddComponent<SectorSync>();
|
2020-03-13 20:44:32 +01:00
|
|
|
|
gameObject.AddComponent<RespawnOnDeath>();
|
2020-03-14 21:20:55 +01:00
|
|
|
|
gameObject.AddComponent<PreventShipDestruction>();
|
2020-03-06 19:01:16 +01:00
|
|
|
|
|
2020-08-13 14:32:58 +01:00
|
|
|
|
if (NetworkClient.active && !NetworkServer.active)
|
2020-05-19 19:31:54 +02:00
|
|
|
|
{
|
2020-08-09 21:46:51 +01:00
|
|
|
|
gameObject.AddComponent<Events.PlayerState>();
|
2020-08-13 19:25:12 +02:00
|
|
|
|
GeyserManager.Instance.EmptyUpdate();
|
2020-09-04 20:54:34 +01:00
|
|
|
|
OrbSlotManager.Instance.StopChecking();
|
2020-08-13 22:06:34 +01:00
|
|
|
|
WakeUpPatches.AddPatches();
|
2020-05-19 19:31:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 20:54:34 +01:00
|
|
|
|
//QSB.Helper.HarmonyHelper.AddPostfix<NomaiInterfaceOrb>("StartDragFromPosition", typeof(OrbSlotPatches), nameof(OrbSlotPatches.StartDragCallEvent));
|
|
|
|
|
//QSB.Helper.HarmonyHelper.AddPostfix<NomaiInterfaceOrb>("CancelDrag", typeof(OrbSlotPatches), nameof(OrbSlotPatches.CancelDrag));
|
|
|
|
|
//QSB.Helper.HarmonyHelper.AddPostfix<NomaiInterfaceOrb>("SetOrbPosition", typeof(OrbSlotPatches), nameof(OrbSlotPatches.SetPosition));
|
|
|
|
|
//QSB.Helper.HarmonyHelper.AddPrefix<NomaiInterfaceOrb>("SetOrbPosition", typeof(OrbSlotPatches), nameof(OrbSlotPatches.SetOrbPosition));
|
|
|
|
|
|
2020-08-18 22:37:27 +02:00
|
|
|
|
_lobby.CanEditName = false;
|
2020-03-13 20:44:32 +01:00
|
|
|
|
|
2020-08-20 21:07:40 +02:00
|
|
|
|
OnNetworkManagerReady?.Invoke();
|
2020-03-13 20:44:32 +01:00
|
|
|
|
IsReady = true;
|
2020-08-08 12:23:23 +01:00
|
|
|
|
|
2020-09-04 20:54:34 +01:00
|
|
|
|
//NetworkServer.RegisterHandler((short)Messaging.EventType.QSBPositionMessage + MsgType.Highest + 1, new NetworkMessageDelegate(QSBTransformSync.HandleTransform));
|
|
|
|
|
|
2020-08-20 14:10:37 +01:00
|
|
|
|
QSB.Helper.Events.Unity.RunWhen(() => PlayerTransformSync.LocalInstance != null, EventList.Init);
|
2020-08-09 14:26:33 +01:00
|
|
|
|
|
2020-08-20 14:10:37 +01:00
|
|
|
|
QSB.Helper.Events.Unity.RunWhen(() => EventList.Ready,
|
2020-08-18 22:37:27 +02:00
|
|
|
|
() => GlobalMessenger<string>.FireEvent(EventNames.QSBPlayerJoin, _lobby.PlayerName));
|
2020-09-02 11:17:04 +01:00
|
|
|
|
|
|
|
|
|
QSB.Helper.Events.Unity.RunWhen(() => EventList.Ready,
|
|
|
|
|
() => GlobalMessenger.FireEvent(EventNames.QSBPlayerStatesRequest));
|
|
|
|
|
|
2020-03-06 19:01:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 00:13:43 +01:00
|
|
|
|
public override void OnStopClient() // Called on the client when closing connection
|
2020-03-06 19:01:16 +01:00
|
|
|
|
{
|
2020-08-22 18:08:25 +01:00
|
|
|
|
DebugLog.ToConsole("Disconnecting from server...", MessageType.Info);
|
2020-03-07 16:40:17 +01:00
|
|
|
|
Destroy(GetComponent<SectorSync>());
|
2020-03-13 20:44:32 +01:00
|
|
|
|
Destroy(GetComponent<RespawnOnDeath>());
|
2020-03-14 21:20:55 +01:00
|
|
|
|
Destroy(GetComponent<PreventShipDestruction>());
|
2020-08-15 20:32:58 +01:00
|
|
|
|
EventList.Reset();
|
2020-08-18 22:37:27 +02:00
|
|
|
|
PlayerRegistry.PlayerList.ForEach(player => player.HudMarker?.Remove());
|
2020-08-20 19:31:10 +01:00
|
|
|
|
|
2020-09-03 17:09:38 +01:00
|
|
|
|
foreach (var player in PlayerRegistry.PlayerList)
|
2020-08-20 17:29:47 +01:00
|
|
|
|
{
|
2020-08-20 21:07:40 +02:00
|
|
|
|
PlayerRegistry.GetPlayerNetIds(player).ForEach(CleanupNetworkBehaviour);
|
2020-08-20 17:29:47 +01:00
|
|
|
|
}
|
2020-09-04 18:54:53 +01:00
|
|
|
|
PlayerRegistry.PlayerList.ForEach(x => PlayerRegistry.PlayerList.Remove(x));
|
2020-08-20 17:29:47 +01:00
|
|
|
|
|
2020-08-20 18:24:31 +01:00
|
|
|
|
_lobby.CanEditName = true;
|
2020-03-04 21:46:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 00:13:43 +01:00
|
|
|
|
public override void OnServerDisconnect(NetworkConnection connection) // Called on the server when any client disconnects
|
2020-03-06 19:03:35 +01:00
|
|
|
|
{
|
2020-09-04 20:09:25 +01:00
|
|
|
|
var playerId = connection.playerControllers[0].gameObject.GetComponent<PlayerTransformSync>().netId.Value;
|
|
|
|
|
var netIds = connection.clientOwnedObjects.Select(x => x.Value).ToArray();
|
|
|
|
|
GlobalMessenger<uint, uint[]>.FireEvent(EventNames.QSBPlayerLeave, playerId, netIds);
|
2020-08-20 14:18:11 +01:00
|
|
|
|
PlayerRegistry.GetPlayer(playerId).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-08-20 17:29:47 +01:00
|
|
|
|
Destroy(GetComponent<SectorSync>());
|
|
|
|
|
Destroy(GetComponent<RespawnOnDeath>());
|
|
|
|
|
Destroy(GetComponent<PreventShipDestruction>());
|
|
|
|
|
EventList.Reset();
|
2020-08-24 15:51:12 +01:00
|
|
|
|
DebugLog.ToConsole("[S] Server stopped!", MessageType.Info);
|
2020-08-20 17:29:47 +01:00
|
|
|
|
PlayerRegistry.PlayerList.ForEach(player => player.HudMarker?.Remove());
|
2020-08-18 22:37:27 +02:00
|
|
|
|
NetworkServer.connections.ToList().ForEach(CleanupConnection);
|
2020-08-17 16:51:56 +01:00
|
|
|
|
base.OnStopServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CleanupConnection(NetworkConnection connection)
|
|
|
|
|
{
|
2020-09-04 20:09:25 +01:00
|
|
|
|
uint playerId;
|
2020-08-23 21:01:09 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-09-04 20:09:25 +01:00
|
|
|
|
playerId = connection.playerControllers[0].gameObject.GetComponent<PlayerTransformSync>().netId.Value;
|
2020-08-23 21:01:09 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole("Error when getting playerId in CleanupConnection: " + ex.Message, MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-20 17:29:47 +01:00
|
|
|
|
if (!PlayerRegistry.PlayerExists(playerId))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-17 16:51:56 +01:00
|
|
|
|
var playerName = PlayerRegistry.GetPlayer(playerId).Name;
|
2020-08-22 18:08:25 +01:00
|
|
|
|
DebugLog.ToConsole($"{playerName} disconnected.", MessageType.Info);
|
2020-08-17 16:51:56 +01:00
|
|
|
|
PlayerRegistry.RemovePlayer(playerId);
|
2020-08-18 22:37:27 +02:00
|
|
|
|
|
2020-08-20 14:18:11 +01:00
|
|
|
|
if (playerId != PlayerRegistry.LocalPlayerId) // We don't want to delete the local player!
|
2020-08-20 13:43:16 +01:00
|
|
|
|
{
|
2020-09-04 20:09:25 +01:00
|
|
|
|
var netIds = connection.clientOwnedObjects?.Select(x => x.Value).ToList();
|
2020-08-20 13:43:16 +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-08-18 22:37:27 +02:00
|
|
|
|
var networkBehaviours = FindObjectsOfType<NetworkBehaviour>()
|
2020-09-04 20:09:25 +01:00
|
|
|
|
.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-08-21 22:31:42 +02:00
|
|
|
|
PlayerRegistry.PlayerSyncObjects.Remove(transformSync);
|
2020-09-04 18:54:53 +01:00
|
|
|
|
if (transformSync.SyncedTransform != null && netId != PlayerRegistry.LocalPlayerId && !networkBehaviour.hasAuthority)
|
2020-08-17 17:59:13 +01:00
|
|
|
|
{
|
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-04 18:54:53 +01:00
|
|
|
|
if (!networkBehaviour.hasAuthority)
|
|
|
|
|
{
|
|
|
|
|
Destroy(networkBehaviour.gameObject);
|
|
|
|
|
}
|
2020-03-06 19:01:16 +01:00
|
|
|
|
}
|
2020-02-13 20:23:26 +01:00
|
|
|
|
}
|
2020-02-23 18:31:38 +01:00
|
|
|
|
|
2020-02-13 20:23:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|