2020-08-22 18:08:25 +01:00
|
|
|
|
using OWML.Common;
|
2020-12-20 10:56:15 +00:00
|
|
|
|
using OWML.Utils;
|
2021-04-26 14:30:21 +01:00
|
|
|
|
using QSB.Animation.Player;
|
2021-05-07 14:58:37 +01:00
|
|
|
|
using QSB.Animation.Player.Thrusters;
|
2020-08-13 21:46:16 +02:00
|
|
|
|
using QSB.DeathSync;
|
2020-12-14 16:24:52 +00:00
|
|
|
|
using QSB.Events;
|
2020-11-20 19:50:31 +00:00
|
|
|
|
using QSB.Instruments;
|
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;
|
2021-03-28 17:53:05 +01:00
|
|
|
|
using QSB.PoolSync;
|
2021-04-11 17:05:02 +01:00
|
|
|
|
using QSB.ProbeSync.TransformSync;
|
|
|
|
|
using QSB.ShipSync.TransformSync;
|
2020-02-24 19:55:16 +01:00
|
|
|
|
using QSB.TimeSync;
|
2020-07-30 22:27:14 +02:00
|
|
|
|
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;
|
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; }
|
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
|
|
|
|
|
2020-12-14 21:20:53 +00:00
|
|
|
|
private QSBNetworkLobby _lobby;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private AssetBundle _assetBundle;
|
|
|
|
|
private GameObject _probePrefab;
|
2021-02-19 21:25:17 +00:00
|
|
|
|
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
|
|
|
|
{
|
2020-12-16 09:08:38 +00:00
|
|
|
|
base.Awake();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
Instance = this;
|
|
|
|
|
|
|
|
|
|
_lobby = gameObject.AddComponent<QSBNetworkLobby>();
|
2020-12-14 16:24:52 +00:00
|
|
|
|
_assetBundle = QSBCore.NetworkAssetBundle;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2021-07-07 19:45:14 +01:00
|
|
|
|
playerPrefab = _assetBundle.LoadAsset<GameObject>("assets/NETWORK_Player_Body.prefab");
|
2020-12-14 21:20:53 +00:00
|
|
|
|
SetupNetworkId(playerPrefab);
|
|
|
|
|
SetupNetworkTransform(playerPrefab);
|
|
|
|
|
playerPrefab.AddComponent<PlayerTransformSync>();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
playerPrefab.AddComponent<AnimationSync>();
|
2021-05-03 20:13:03 +01:00
|
|
|
|
playerPrefab.AddComponent<CrouchSync>();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
playerPrefab.AddComponent<WakeUpSync>();
|
2021-05-07 14:58:37 +01:00
|
|
|
|
playerPrefab.AddComponent<JetpackAccelerationSync>();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
playerPrefab.AddComponent<InstrumentsManager>();
|
|
|
|
|
|
2021-04-13 21:31:59 +01:00
|
|
|
|
ShipPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkship.prefab");
|
|
|
|
|
SetupNetworkId(ShipPrefab);
|
2021-07-06 22:29:47 +01:00
|
|
|
|
SetupNetworkTransform(ShipPrefab);
|
2021-04-13 21:31:59 +01:00
|
|
|
|
ShipPrefab.AddComponent<ShipTransformSync>();
|
|
|
|
|
spawnPrefabs.Add(ShipPrefab);
|
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
_probePrefab = _assetBundle.LoadAsset<GameObject>("assets/networkprobe.prefab");
|
2020-12-14 21:20:53 +00:00
|
|
|
|
SetupNetworkId(_probePrefab);
|
|
|
|
|
SetupNetworkTransform(_probePrefab);
|
|
|
|
|
_probePrefab.AddComponent<PlayerProbeSync>();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
spawnPrefabs.Add(_probePrefab);
|
|
|
|
|
|
|
|
|
|
OrbPrefab = _assetBundle.LoadAsset<GameObject>("assets/networkorb.prefab");
|
2020-12-14 21:20:53 +00:00
|
|
|
|
SetupNetworkId(OrbPrefab);
|
|
|
|
|
SetupNetworkTransform(OrbPrefab);
|
|
|
|
|
OrbPrefab.AddComponent<NomaiOrbTransformSync>();
|
2020-12-02 21:29:53 +00:00
|
|
|
|
spawnPrefabs.Add(OrbPrefab);
|
|
|
|
|
|
|
|
|
|
ConfigureNetworkManager();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 21:20:53 +00:00
|
|
|
|
private void SetupNetworkId(GameObject go)
|
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
var ident = go.AddComponent<QNetworkIdentity>();
|
2020-12-14 21:20:53 +00:00
|
|
|
|
ident.LocalPlayerAuthority = true;
|
2020-12-16 17:34:54 +00:00
|
|
|
|
ident.SetValue("m_AssetId", go.GetComponent<NetworkIdentity>().assetId);
|
|
|
|
|
ident.SetValue("m_SceneId", go.GetComponent<NetworkIdentity>().sceneId);
|
2020-12-14 21:20:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetupNetworkTransform(GameObject go)
|
|
|
|
|
{
|
2021-03-31 10:30:51 +01:00
|
|
|
|
foreach (var item in go.GetComponents<NetworkTransformChild>())
|
|
|
|
|
{
|
|
|
|
|
var child = go.AddComponent<QNetworkTransformChild>();
|
|
|
|
|
child.Target = item.target;
|
|
|
|
|
Destroy(item);
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-01-01 11:01:52 +00:00
|
|
|
|
Destroy(go.GetComponent<NetworkTransform>());
|
2020-12-19 10:56:25 +00:00
|
|
|
|
Destroy(go.GetComponent<NetworkIdentity>());
|
2020-12-14 21:20:53 +00: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
|
|
|
|
{
|
2020-12-11 13:14:58 +00:00
|
|
|
|
QSBWorldSync.OldDialogueTrees = Resources.FindObjectsOfTypeAll<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
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"Setting defaultServerIP to {networkAddress}");
|
|
|
|
|
var config = QSBCore.Helper.Config;
|
|
|
|
|
config.SetSettingsValue("defaultServerIP", networkAddress);
|
|
|
|
|
QSBCore.Helper.Storage.Save(config, Constants.ModConfigFileName);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
2020-12-12 18:14:04 +00:00
|
|
|
|
QSBEventManager.Init();
|
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
gameObject.AddComponent<RespawnOnDeath>();
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
_lobby.CanEditName = false;
|
|
|
|
|
|
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-02-13 10:11:02 +00:00
|
|
|
|
() => QSBEventManager.FireEvent(EventNames.QSBPlayerJoin, _lobby.PlayerName));
|
2020-12-02 21:29:53 +00:00
|
|
|
|
|
2020-12-14 16:24:52 +00:00
|
|
|
|
if (!QSBCore.IsServer)
|
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-02-10 19:34:41 +00:00
|
|
|
|
() => QSBEventManager.FireEvent(EventNames.QSBPlayerStatesRequest));
|
2020-12-11 22:42:21 +00:00
|
|
|
|
}
|
2021-02-19 21:25:17 +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>());
|
|
|
|
|
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-02-19 21:25:17 +00:00
|
|
|
|
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
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
_lobby.CanEditName = true;
|
2021-02-09 17:18:01 +00:00
|
|
|
|
|
|
|
|
|
IsReady = false;
|
2021-02-19 21:25:17 +00:00
|
|
|
|
_everConnected = false;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
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>();
|
2021-03-28 14:19:26 +01:00
|
|
|
|
foreach (var platform in Resources.FindObjectsOfTypeAll<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
|
|
|
|
|
2021-03-23 13:18:29 +00:00
|
|
|
|
foreach (var camera in Resources.FindObjectsOfTypeAll<CustomNomaiRemoteCamera>())
|
|
|
|
|
{
|
|
|
|
|
Destroy(camera);
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-03-28 14:19:26 +01:00
|
|
|
|
foreach (var streaming in Resources.FindObjectsOfTypeAll<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
|
|
|
|
}
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-22 21:08:10 +00:00
|
|
|
|
}
|