623 lines
16 KiB
C#
Raw Normal View History

2020-12-23 13:48:31 +00:00
using QuantumUNET.Logging;
2020-12-07 21:19:16 +00:00
using QuantumUNET.Messages;
2020-12-02 18:40:38 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
2020-12-07 21:19:16 +00:00
namespace QuantumUNET.Components
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
public class QNetworkManager : MonoBehaviour
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
public static QNetworkManager singleton;
2020-12-02 18:40:38 +00:00
2020-12-07 20:47:07 +00:00
public int networkPort { get; set; } = 7777;
public bool serverBindToIP { get; set; }
public bool dontDestroyOnLoad { get; set; } = true;
public bool runInBackground { get; set; } = true;
public bool scriptCRCCheck { get; set; } = true;
public bool autoCreatePlayer { get; set; } = true;
public bool isNetworkActive;
public bool clientLoadedScene { get; set; }
public string serverBindAddress { get; set; } = "";
public string networkAddress { get; set; } = "localhost";
public float packetLossPercentage { get; set; }
public float maxDelay { get; set; } = 0.01f;
public GameObject playerPrefab { get; set; }
public List<GameObject> spawnPrefabs { get; } = new List<GameObject>();
2020-12-23 12:58:45 +00:00
public QNetworkClient client;
2020-12-07 20:47:07 +00:00
public int maxConnections { get; set; } = 4;
public List<QosType> channels { get; } = new List<QosType>();
2020-12-02 18:40:38 +00:00
2020-12-07 20:47:07 +00:00
private ConnectionConfig m_ConnectionConfig;
private GlobalConfig m_GlobalConfig;
2020-12-18 20:32:16 +00:00
private readonly int m_MaxBufferedPackets = 16;
private readonly bool m_AllowFragmentation = true;
2020-12-23 12:58:45 +00:00
private static readonly QAddPlayerMessage s_AddPlayerMessage = new QAddPlayerMessage();
private static readonly QRemovePlayerMessage s_RemovePlayerMessage = new QRemovePlayerMessage();
private static readonly QErrorMessage s_ErrorMessage = new QErrorMessage();
2020-12-07 20:47:07 +00:00
private static AsyncOperation s_LoadingSceneAsync;
2020-12-23 12:58:45 +00:00
private static QNetworkConnection s_ClientReadyConnection;
2020-12-07 20:47:07 +00:00
private static string s_Address;
2020-12-02 18:40:38 +00:00
2020-12-07 20:47:07 +00:00
public bool customConfig { get; set; }
2020-12-02 18:40:38 +00:00
public ConnectionConfig connectionConfig
{
get
{
if (m_ConnectionConfig == null)
{
m_ConnectionConfig = new ConnectionConfig();
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
return m_ConnectionConfig;
}
}
public GlobalConfig globalConfig
{
get
{
if (m_GlobalConfig == null)
{
m_GlobalConfig = new GlobalConfig();
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
return m_GlobalConfig;
}
}
public int numPlayers
{
get
{
2020-12-07 20:47:07 +00:00
var num = 0;
2020-12-23 12:58:45 +00:00
foreach (var networkConnection in QNetworkServer.connections)
2020-12-02 18:40:38 +00:00
{
if (networkConnection != null)
{
foreach (var controller in networkConnection.PlayerControllers)
2020-12-02 18:40:38 +00:00
{
if (controller.IsValid)
2020-12-02 18:40:38 +00:00
{
num++;
}
}
}
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
return num;
}
}
public void Awake() => InitializeSingleton();
2020-12-02 18:40:38 +00:00
private void InitializeSingleton()
{
if (!(singleton != null) || !(singleton == this))
{
2020-12-07 20:47:07 +00:00
if (dontDestroyOnLoad)
2020-12-02 18:40:38 +00:00
{
if (singleton != null)
{
QLog.Warning("Multiple NetworkManagers detected in the scene. Only one NetworkManager can exist at a time. The duplicate NetworkManager will not be used.");
Destroy(gameObject);
2020-12-02 18:40:38 +00:00
return;
}
2021-06-18 22:39:21 +01:00
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager created singleton (DontDestroyOnLoad)");
2020-12-02 18:40:38 +00:00
singleton = this;
if (Application.isPlaying)
{
DontDestroyOnLoad(gameObject);
2020-12-02 18:40:38 +00:00
}
}
else
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager created singleton (ForScene)");
2020-12-02 18:40:38 +00:00
singleton = this;
}
2021-06-18 22:39:21 +01:00
2020-12-07 20:47:07 +00:00
if (networkAddress != "")
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
s_Address = networkAddress;
2020-12-02 18:40:38 +00:00
}
else if (s_Address != "")
{
2020-12-07 20:47:07 +00:00
networkAddress = s_Address;
2020-12-02 18:40:38 +00:00
}
}
}
internal void RegisterServerMessages()
{
2020-12-23 12:58:45 +00:00
QNetworkServer.RegisterHandler(QMsgType.Connect, OnServerConnectInternal);
QNetworkServer.RegisterHandler(QMsgType.Disconnect, OnServerDisconnectInternal);
QNetworkServer.RegisterHandler(QMsgType.Ready, OnServerReadyMessageInternal);
QNetworkServer.RegisterHandler(QMsgType.AddPlayer, OnServerAddPlayerMessageInternal);
QNetworkServer.RegisterHandler(QMsgType.RemovePlayer, OnServerRemovePlayerMessageInternal);
QNetworkServer.RegisterHandler(QMsgType.Error, OnServerErrorInternal);
2020-12-02 18:40:38 +00:00
}
public bool StartServer() => StartServer(null, -1);
2020-12-02 18:40:38 +00:00
2020-12-07 20:52:10 +00:00
private bool StartServer(ConnectionConfig config, int maxConnections)
2020-12-02 18:40:38 +00:00
{
InitializeSingleton();
OnStartServer();
2020-12-07 20:47:07 +00:00
if (runInBackground)
2020-12-02 18:40:38 +00:00
{
Application.runInBackground = true;
}
2021-06-18 22:39:21 +01:00
2020-12-23 12:58:45 +00:00
QNetworkCRC.scriptCRCCheck = scriptCRCCheck;
2020-12-02 18:40:38 +00:00
if (m_GlobalConfig != null)
{
NetworkTransport.Init(m_GlobalConfig);
}
2021-06-18 22:39:21 +01:00
2020-12-07 20:47:07 +00:00
if (customConfig && m_ConnectionConfig != null && config == null)
2020-12-02 18:40:38 +00:00
{
m_ConnectionConfig.Channels.Clear();
foreach (var channel in channels)
2020-12-02 18:40:38 +00:00
{
m_ConnectionConfig.AddChannel(channel);
2020-12-02 18:40:38 +00:00
}
2021-06-18 22:39:21 +01:00
2020-12-23 12:58:45 +00:00
QNetworkServer.Configure(m_ConnectionConfig, this.maxConnections);
2020-12-02 18:40:38 +00:00
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
if (config != null)
{
2020-12-23 12:58:45 +00:00
QNetworkServer.Configure(config, maxConnections);
2020-12-02 18:40:38 +00:00
}
2021-06-18 22:39:21 +01:00
2020-12-07 20:52:10 +00:00
if (serverBindToIP && !string.IsNullOrEmpty(serverBindAddress))
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
if (!QNetworkServer.Listen(serverBindAddress, networkPort))
2020-12-02 18:40:38 +00:00
{
QLog.FatalError($"StartServer listen on {serverBindAddress} failed.");
2020-12-02 18:40:38 +00:00
return false;
}
}
2020-12-23 12:58:45 +00:00
else if (!QNetworkServer.Listen(networkPort))
2020-12-02 18:40:38 +00:00
{
QLog.FatalError("StartServer listen failed.");
2020-12-02 18:40:38 +00:00
return false;
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
RegisterServerMessages();
2020-12-23 13:48:31 +00:00
QLog.Log($"NetworkManager StartServer port:{networkPort}");
2020-12-02 18:40:38 +00:00
isNetworkActive = true;
QNetworkServer.SpawnObjects();
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
return true;
}
2020-12-23 12:58:45 +00:00
internal void RegisterClientMessages(QNetworkClient client)
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
client.RegisterHandler(QMsgType.Connect, OnClientConnectInternal);
client.RegisterHandler(QMsgType.Disconnect, OnClientDisconnectInternal);
client.RegisterHandler(QMsgType.NotReady, OnClientNotReadyMessageInternal);
client.RegisterHandler(QMsgType.Error, OnClientErrorInternal);
2020-12-07 20:47:07 +00:00
if (playerPrefab != null)
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
QClientScene.RegisterPrefab(playerPrefab);
2020-12-02 18:40:38 +00:00
}
2021-06-18 22:39:21 +01:00
foreach (var gameObject in spawnPrefabs)
2020-12-02 18:40:38 +00:00
{
if (gameObject != null)
{
2020-12-23 12:58:45 +00:00
QClientScene.RegisterPrefab(gameObject);
2020-12-02 18:40:38 +00:00
}
}
}
2020-12-23 12:58:45 +00:00
public QNetworkClient StartClient(ConnectionConfig config, int hostPort)
2020-12-02 18:40:38 +00:00
{
InitializeSingleton();
2020-12-07 20:47:07 +00:00
if (runInBackground)
2020-12-02 18:40:38 +00:00
{
Application.runInBackground = true;
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
isNetworkActive = true;
if (m_GlobalConfig != null)
{
NetworkTransport.Init(m_GlobalConfig);
}
2021-06-18 22:39:21 +01:00
2020-12-23 12:58:45 +00:00
client = new QNetworkClient
2020-12-07 20:47:07 +00:00
{
hostPort = hostPort
2020-12-07 20:47:07 +00:00
};
2020-12-02 18:40:38 +00:00
if (config != null)
{
if (config.UsePlatformSpecificProtocols && Application.platform != RuntimePlatform.PS4 && Application.platform != RuntimePlatform.PSP2)
{
throw new ArgumentOutOfRangeException("Platform specific protocols are not supported on this platform");
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
client.Configure(config, 1);
}
2020-12-07 20:47:07 +00:00
else if (customConfig && m_ConnectionConfig != null)
2020-12-02 18:40:38 +00:00
{
m_ConnectionConfig.Channels.Clear();
foreach (var channel in channels)
2020-12-02 18:40:38 +00:00
{
m_ConnectionConfig.AddChannel(channel);
2020-12-02 18:40:38 +00:00
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
if (m_ConnectionConfig.UsePlatformSpecificProtocols && Application.platform != RuntimePlatform.PS4 && Application.platform != RuntimePlatform.PSP2)
{
throw new ArgumentOutOfRangeException("Platform specific protocols are not supported on this platform");
}
2021-06-18 22:39:21 +01:00
2020-12-07 20:47:07 +00:00
client.Configure(m_ConnectionConfig, maxConnections);
2020-12-02 18:40:38 +00:00
}
2021-06-18 22:39:21 +01:00
2020-12-07 20:47:07 +00:00
RegisterClientMessages(client);
if (string.IsNullOrEmpty(networkAddress))
2020-12-02 18:40:38 +00:00
{
QLog.Error("Must set the Network Address field in the manager");
2020-12-07 20:47:07 +00:00
return null;
2020-12-02 18:40:38 +00:00
}
2021-06-18 22:39:21 +01:00
2021-04-26 14:30:21 +01:00
client.Connect(networkAddress, networkPort);
2020-12-07 20:47:07 +00:00
OnStartClient(client);
s_Address = networkAddress;
2020-12-02 18:40:38 +00:00
return client;
}
2020-12-23 12:58:45 +00:00
public QNetworkClient StartClient() => StartClient(null);
2020-12-02 18:40:38 +00:00
2020-12-23 12:58:45 +00:00
public QNetworkClient StartClient(ConnectionConfig config) => StartClient(config, 0);
2020-12-02 18:40:38 +00:00
2020-12-23 12:58:45 +00:00
public virtual QNetworkClient StartHost(ConnectionConfig config, int maxConnections)
2020-12-02 18:40:38 +00:00
{
OnStartHost();
2020-12-23 12:58:45 +00:00
QNetworkClient result;
2020-12-02 18:40:38 +00:00
if (StartServer(config, maxConnections))
{
2020-12-07 20:47:07 +00:00
var networkClient = ConnectLocalClient();
2021-04-27 20:56:04 +01:00
OnServerConnect(networkClient.connection);
2020-12-02 18:40:38 +00:00
OnStartClient(networkClient);
result = networkClient;
}
else
{
result = null;
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
return result;
}
2020-12-23 12:58:45 +00:00
public virtual QNetworkClient StartHost()
2020-12-02 18:40:38 +00:00
{
OnStartHost();
2020-12-23 12:58:45 +00:00
QNetworkClient result;
2020-12-02 18:40:38 +00:00
if (StartServer())
{
2020-12-07 20:47:07 +00:00
var networkClient = ConnectLocalClient();
2020-12-02 18:40:38 +00:00
OnStartClient(networkClient);
result = networkClient;
}
else
{
result = null;
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
return result;
}
2020-12-23 12:58:45 +00:00
private QNetworkClient ConnectLocalClient()
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log($"NetworkManager StartHost port:{networkPort}");
2020-12-07 20:47:07 +00:00
networkAddress = "localhost";
2020-12-23 12:58:45 +00:00
client = QClientScene.ConnectLocalServer();
2020-12-07 20:47:07 +00:00
RegisterClientMessages(client);
2020-12-02 18:40:38 +00:00
return client;
}
public void StopHost()
{
OnStopHost();
StopClient();
2021-02-09 17:18:01 +00:00
StopServer();
2020-12-02 18:40:38 +00:00
}
public void StopServer()
{
2020-12-23 12:58:45 +00:00
if (QNetworkServer.active)
2020-12-02 18:40:38 +00:00
{
OnStopServer();
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager StopServer");
2020-12-02 18:40:38 +00:00
isNetworkActive = false;
2020-12-23 12:58:45 +00:00
QNetworkServer.Shutdown();
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
CleanupNetworkIdentities();
}
}
public void StopClient()
{
OnStopClient();
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager StopClient");
2020-12-02 18:40:38 +00:00
isNetworkActive = false;
if (client != null)
{
client.Disconnect();
client.Shutdown();
client = null;
}
2021-06-18 22:39:21 +01:00
2020-12-23 12:58:45 +00:00
QClientScene.DestroyAllClientObjects();
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
CleanupNetworkIdentities();
}
private void CleanupNetworkIdentities()
{
2020-12-23 12:58:45 +00:00
foreach (var networkIdentity in Resources.FindObjectsOfTypeAll<QNetworkIdentity>())
2020-12-02 18:40:38 +00:00
{
networkIdentity.MarkForReset();
}
}
public bool IsClientConnected() => client != null && client.isConnected;
2020-12-02 18:40:38 +00:00
public static void Shutdown()
{
if (!(singleton == null))
{
s_ClientReadyConnection = null;
singleton.StopHost();
singleton = null;
}
}
2020-12-23 12:58:45 +00:00
internal void OnServerConnectInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnServerConnectInternal");
2020-12-07 20:47:07 +00:00
netMsg.Connection.SetMaxDelay(maxDelay);
2020-12-02 18:40:38 +00:00
if (m_MaxBufferedPackets != 512)
{
2020-12-23 12:58:45 +00:00
for (var i = 0; i < QNetworkServer.numChannels; i++)
2020-12-02 18:40:38 +00:00
{
2020-12-03 11:56:32 +00:00
netMsg.Connection.SetChannelOption(i, ChannelOption.MaxPendingBuffers, m_MaxBufferedPackets);
2020-12-02 18:40:38 +00:00
}
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
if (!m_AllowFragmentation)
{
2020-12-23 12:58:45 +00:00
for (var j = 0; j < QNetworkServer.numChannels; j++)
2020-12-02 18:40:38 +00:00
{
2020-12-03 11:56:32 +00:00
netMsg.Connection.SetChannelOption(j, ChannelOption.AllowFragmentation, 0);
2020-12-02 18:40:38 +00:00
}
}
2021-06-18 22:39:21 +01:00
2020-12-03 11:56:32 +00:00
OnServerConnect(netMsg.Connection);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
internal void OnServerDisconnectInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnServerDisconnectInternal");
2020-12-03 11:56:32 +00:00
OnServerDisconnect(netMsg.Connection);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
internal void OnServerReadyMessageInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnServerReadyMessageInternal");
2020-12-03 11:56:32 +00:00
OnServerReady(netMsg.Connection);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
internal void OnServerAddPlayerMessageInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnServerAddPlayerMessageInternal");
netMsg.ReadMessage(s_AddPlayerMessage);
2020-12-02 18:40:38 +00:00
if (s_AddPlayerMessage.msgSize != 0)
{
2020-12-07 20:47:07 +00:00
var extraMessageReader = new NetworkReader(s_AddPlayerMessage.msgData);
OnServerAddPlayer(netMsg.Connection, s_AddPlayerMessage.playerControllerId, extraMessageReader);
2020-12-02 18:40:38 +00:00
}
else
{
2020-12-07 20:47:07 +00:00
OnServerAddPlayer(netMsg.Connection, s_AddPlayerMessage.playerControllerId);
2020-12-02 18:40:38 +00:00
}
}
2020-12-23 12:58:45 +00:00
internal void OnServerRemovePlayerMessageInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnServerRemovePlayerMessageInternal");
netMsg.ReadMessage(s_RemovePlayerMessage);
2020-12-07 20:47:07 +00:00
netMsg.Connection.GetPlayerController(s_RemovePlayerMessage.PlayerControllerId, out var player);
2020-12-03 11:56:32 +00:00
OnServerRemovePlayer(netMsg.Connection, player);
2020-12-04 09:23:27 +00:00
netMsg.Connection.RemovePlayerController(s_RemovePlayerMessage.PlayerControllerId);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
internal void OnServerErrorInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnServerErrorInternal");
netMsg.ReadMessage(s_ErrorMessage);
2020-12-07 20:47:07 +00:00
OnServerError(netMsg.Connection, s_ErrorMessage.errorCode);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
internal void OnClientConnectInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnClientConnectInternal");
2020-12-07 20:47:07 +00:00
netMsg.Connection.SetMaxDelay(maxDelay);
var name = SceneManager.GetSceneAt(0).name;
clientLoadedScene = false;
OnClientConnect(netMsg.Connection);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
internal void OnClientDisconnectInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnClientDisconnectInternal");
2021-06-18 22:39:21 +01:00
2020-12-03 11:56:32 +00:00
OnClientDisconnect(netMsg.Connection);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
internal void OnClientNotReadyMessageInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnClientNotReadyMessageInternal");
2020-12-23 12:58:45 +00:00
QClientScene.SetNotReady();
2020-12-03 11:56:32 +00:00
OnClientNotReady(netMsg.Connection);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
internal void OnClientErrorInternal(QNetworkMessage netMsg)
2020-12-02 18:40:38 +00:00
{
2020-12-23 13:48:31 +00:00
QLog.Log("NetworkManager:OnClientErrorInternal");
netMsg.ReadMessage(s_ErrorMessage);
2020-12-07 20:47:07 +00:00
OnClientError(netMsg.Connection, s_ErrorMessage.errorCode);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
public virtual void OnServerConnect(QNetworkConnection conn)
2020-12-02 18:40:38 +00:00
{
}
2020-12-23 12:58:45 +00:00
public virtual void OnServerDisconnect(QNetworkConnection conn)
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
QNetworkServer.DestroyPlayersForConnection(conn);
2020-12-02 18:40:38 +00:00
if (conn.LastError != NetworkError.Ok)
{
QLog.Error($"ServerDisconnected due to error: {conn.LastError}");
2020-12-02 18:40:38 +00:00
}
}
2020-12-23 12:58:45 +00:00
public virtual void OnServerReady(QNetworkConnection conn)
2020-12-02 18:40:38 +00:00
{
if (conn.PlayerControllers.Count == 0)
{
QLog.Warning("Ready with no player object");
2020-12-02 18:40:38 +00:00
}
2021-06-18 22:39:21 +01:00
2020-12-23 12:58:45 +00:00
QNetworkServer.SetClientReady(conn);
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
public virtual void OnServerAddPlayer(QNetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader) => OnServerAddPlayerInternal(conn, playerControllerId);
2020-12-02 18:40:38 +00:00
2020-12-23 12:58:45 +00:00
public virtual void OnServerAddPlayer(QNetworkConnection conn, short playerControllerId) => OnServerAddPlayerInternal(conn, playerControllerId);
2020-12-02 18:40:38 +00:00
2020-12-23 12:58:45 +00:00
private void OnServerAddPlayerInternal(QNetworkConnection conn, short playerControllerId)
2020-12-02 18:40:38 +00:00
{
2020-12-07 20:47:07 +00:00
if (playerPrefab == null)
2020-12-02 18:40:38 +00:00
{
QLog.FatalError("The PlayerPrefab is empty on the QSBNetworkManager. Please setup a PlayerPrefab object.");
2020-12-02 18:40:38 +00:00
}
2020-12-23 12:58:45 +00:00
else if (playerPrefab.GetComponent<QNetworkIdentity>() == null)
2020-12-02 18:40:38 +00:00
{
QLog.FatalError("The PlayerPrefab does not have a QSBNetworkIdentity. Please add a QSBNetworkIdentity to the player prefab.");
2020-12-02 18:40:38 +00:00
}
2020-12-07 20:47:07 +00:00
else if (playerControllerId < conn.PlayerControllers.Count && conn.PlayerControllers[playerControllerId].IsValid && conn.PlayerControllers[playerControllerId].Gameobject != null)
2020-12-02 18:40:38 +00:00
{
QLog.Warning("There is already a player at that playerControllerId for this connections.");
2020-12-02 18:40:38 +00:00
}
else
{
var player = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
2020-12-23 12:58:45 +00:00
QNetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
2020-12-02 18:40:38 +00:00
}
}
2020-12-23 12:58:45 +00:00
public virtual void OnServerRemovePlayer(QNetworkConnection conn, QPlayerController player)
2020-12-02 18:40:38 +00:00
{
2020-12-03 11:56:32 +00:00
if (player.Gameobject != null)
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
QNetworkServer.Destroy(player.Gameobject);
2020-12-02 18:40:38 +00:00
}
}
2020-12-23 12:58:45 +00:00
public virtual void OnServerError(QNetworkConnection conn, int errorCode)
2020-12-02 18:40:38 +00:00
{
}
public virtual void OnServerSceneChanged(string sceneName)
{
}
2020-12-23 12:58:45 +00:00
public virtual void OnClientConnect(QNetworkConnection conn)
2020-12-02 18:40:38 +00:00
{
if (!clientLoadedScene)
{
2020-12-23 12:58:45 +00:00
QClientScene.Ready(conn);
2020-12-07 20:47:07 +00:00
if (autoCreatePlayer)
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
QClientScene.AddPlayer(0);
2020-12-02 18:40:38 +00:00
}
}
}
2020-12-23 12:58:45 +00:00
public virtual void OnClientDisconnect(QNetworkConnection conn)
2020-12-02 18:40:38 +00:00
{
StopClient();
if (conn.LastError != NetworkError.Ok)
{
QLog.Error($"ClientDisconnected due to error: {conn.LastError}");
2020-12-02 18:40:38 +00:00
}
}
2020-12-23 12:58:45 +00:00
public virtual void OnClientError(QNetworkConnection conn, int errorCode)
2020-12-02 18:40:38 +00:00
{
}
2020-12-23 12:58:45 +00:00
public virtual void OnClientNotReady(QNetworkConnection conn)
2020-12-02 18:40:38 +00:00
{
}
2020-12-23 12:58:45 +00:00
public virtual void OnClientSceneChanged(QNetworkConnection conn)
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
QClientScene.Ready(conn);
2020-12-07 20:47:07 +00:00
if (autoCreatePlayer)
2020-12-02 18:40:38 +00:00
{
2020-12-23 12:58:45 +00:00
var flag = QClientScene.localPlayers.Count == 0;
2020-12-07 20:47:07 +00:00
var flag2 = false;
2020-12-23 12:58:45 +00:00
foreach (var player in QClientScene.localPlayers)
2020-12-02 18:40:38 +00:00
{
if (player.Gameobject != null)
2020-12-02 18:40:38 +00:00
{
flag2 = true;
break;
}
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
if (!flag2)
{
flag = true;
}
2021-06-18 22:39:21 +01:00
2020-12-02 18:40:38 +00:00
if (flag)
{
2020-12-23 12:58:45 +00:00
QClientScene.AddPlayer(0);
2020-12-02 18:40:38 +00:00
}
}
}
public virtual void OnStartHost()
{
}
public virtual void OnStartServer()
{
}
2020-12-23 12:58:45 +00:00
public virtual void OnStartClient(QNetworkClient client)
2020-12-02 18:40:38 +00:00
{
}
public virtual void OnStopServer()
{
}
public virtual void OnStopClient()
{
}
public virtual void OnStopHost()
{
}
}
2020-12-03 08:28:05 +00:00
}