2019-04-23 13:49:18 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Mirror;
|
2019-05-05 21:12:42 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
2019-04-23 13:49:18 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Net
|
|
|
|
|
{
|
|
|
|
|
|
2019-06-23 23:54:25 +00:00
|
|
|
|
public class NetManager : MonoBehaviour
|
2019-04-23 13:49:18 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static int defaultListenPortNumber { get { return 7777; } }
|
|
|
|
|
|
2019-04-23 14:08:08 +00:00
|
|
|
|
public static int listenPortNumber { get { return telepathyTransport.port; } }
|
|
|
|
|
|
2019-04-23 21:21:41 +00:00
|
|
|
|
public static bool dontListen { get { return NetworkServer.dontListen; } set { NetworkServer.dontListen = value; } }
|
|
|
|
|
|
2019-06-25 20:54:03 +00:00
|
|
|
|
public static int maxNumPlayers { get => NetworkManager.singleton.maxConnections; set { NetworkManager.singleton.maxConnections = value; } }
|
2019-06-25 20:43:00 +00:00
|
|
|
|
|
2019-04-23 14:08:08 +00:00
|
|
|
|
public static TelepathyTransport telepathyTransport { get { return ((TelepathyTransport)Transport.activeTransport); } }
|
2019-04-23 13:49:18 +00:00
|
|
|
|
|
|
|
|
|
public static string onlineScene {
|
|
|
|
|
get {
|
|
|
|
|
return NetworkManager.singleton.onlineScene;
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
NetworkManager.singleton.onlineScene = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 14:44:49 +00:00
|
|
|
|
public static NetManager Instance { get; private set; }
|
|
|
|
|
|
2019-07-09 14:14:32 +00:00
|
|
|
|
NetworkClientStatus m_lastClientStatus = NetworkClientStatus.Disconnected;
|
|
|
|
|
public event System.Action onClientStatusChanged = delegate {};
|
|
|
|
|
|
2019-04-23 13:49:18 +00:00
|
|
|
|
|
|
|
|
|
|
2019-06-24 00:01:32 +00:00
|
|
|
|
NetManager ()
|
2019-06-23 23:54:25 +00:00
|
|
|
|
{
|
|
|
|
|
// assign implementation in NetUtils
|
2019-06-24 00:01:32 +00:00
|
|
|
|
// do this in ctor, because it may be too late in Awake() - server can theoretically start before our Awake() is called
|
2019-06-23 23:54:25 +00:00
|
|
|
|
NetUtils.IsServerImpl = () => NetStatus.IsServer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-07-09 14:44:49 +00:00
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
if (null == Instance)
|
|
|
|
|
Instance = this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 14:14:32 +00:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
2019-07-09 14:44:49 +00:00
|
|
|
|
|
2019-07-09 14:14:32 +00:00
|
|
|
|
NetworkClientStatus clientStatusNow = NetStatus.clientStatus;
|
|
|
|
|
|
|
|
|
|
if (clientStatusNow != m_lastClientStatus)
|
|
|
|
|
{
|
|
|
|
|
m_lastClientStatus = clientStatusNow;
|
|
|
|
|
|
|
|
|
|
// notify
|
|
|
|
|
F.InvokeEventExceptionSafe(this.onClientStatusChanged);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-07-05 21:03:57 +00:00
|
|
|
|
public static void StartServer(ushort portNumber, string scene, ushort maxNumPlayers, bool bIsDedicated, bool bDontListen)
|
|
|
|
|
{
|
|
|
|
|
// first start a server, and then change scene
|
|
|
|
|
|
|
|
|
|
NetManager.onlineScene = scene;
|
|
|
|
|
NetManager.dontListen = bDontListen;
|
|
|
|
|
NetManager.maxNumPlayers = maxNumPlayers;
|
|
|
|
|
if (bIsDedicated)
|
|
|
|
|
NetManager.StartServer(portNumber);
|
|
|
|
|
else
|
|
|
|
|
NetManager.StartHost(portNumber);
|
|
|
|
|
|
|
|
|
|
//NetManager.ChangeScene(scene);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 13:49:18 +00:00
|
|
|
|
public static void StartServer( int portNumber ) {
|
|
|
|
|
|
|
|
|
|
CheckIfNetworkIsStarted ();
|
|
|
|
|
CheckIfPortIsValid (portNumber);
|
|
|
|
|
CheckIfOnlineSceneIsAssigned ();
|
|
|
|
|
SetupNetworkManger( "", portNumber );
|
|
|
|
|
NetworkManager.singleton.StartServer ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void StartHost( int portNumber ) {
|
|
|
|
|
|
|
|
|
|
CheckIfNetworkIsStarted ();
|
|
|
|
|
CheckIfPortIsValid (portNumber);
|
|
|
|
|
CheckIfOnlineSceneIsAssigned ();
|
|
|
|
|
SetupNetworkManger( "", portNumber );
|
|
|
|
|
NetworkManager.singleton.StartHost ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void StopServer() {
|
|
|
|
|
|
|
|
|
|
NetworkManager.singleton.StopServer ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void StopHost() {
|
|
|
|
|
|
|
|
|
|
NetworkManager.singleton.StopHost ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void StartClient( string ip, int serverPortNumber ) {
|
|
|
|
|
|
|
|
|
|
CheckIfNetworkIsStarted ();
|
|
|
|
|
CheckIfIPAddressIsValid (ip);
|
|
|
|
|
CheckIfPortIsValid (serverPortNumber);
|
|
|
|
|
SetupNetworkManger( ip, serverPortNumber );
|
|
|
|
|
NetworkManager.singleton.StartClient ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void StopClient() {
|
|
|
|
|
|
|
|
|
|
NetworkManager.singleton.StopClient ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops both server and client.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void StopNetwork() {
|
|
|
|
|
|
|
|
|
|
// NetworkManager.singleton.StopHost ();
|
|
|
|
|
NetworkManager.singleton.StopServer ();
|
|
|
|
|
NetworkManager.singleton.StopClient ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void CheckIfServerIsStarted() {
|
|
|
|
|
|
2019-04-24 14:58:13 +00:00
|
|
|
|
if (NetStatus.IsServerStarted)
|
2019-04-23 13:49:18 +00:00
|
|
|
|
throw new System.Exception ("Server already started");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CheckIfClientIsStarted() {
|
|
|
|
|
|
2019-04-23 14:54:18 +00:00
|
|
|
|
if (!NetStatus.IsClientDisconnected ())
|
2019-04-23 13:49:18 +00:00
|
|
|
|
throw new System.Exception ("Client already started");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CheckIfNetworkIsStarted() {
|
|
|
|
|
|
|
|
|
|
CheckIfServerIsStarted ();
|
|
|
|
|
CheckIfClientIsStarted ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CheckIfPortIsValid( int portNumber ) {
|
|
|
|
|
|
|
|
|
|
if (portNumber < 1 || portNumber > 65535)
|
|
|
|
|
throw new System.ArgumentOutOfRangeException ( "portNumber", "Invalid port number");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CheckIfIPAddressIsValid( string ip ) {
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty (ip))
|
|
|
|
|
throw new System.ArgumentException ("IP address empty");
|
|
|
|
|
|
|
|
|
|
// System.Net.IPAddress.Parse ();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CheckIfOnlineSceneIsAssigned() {
|
|
|
|
|
|
2019-04-23 14:08:08 +00:00
|
|
|
|
// we won't use scene management from NetworkManager
|
|
|
|
|
// if (string.IsNullOrEmpty (NetManager.onlineScene))
|
|
|
|
|
// throw new System.Exception ("Online scene is not assigned");
|
2019-04-23 13:49:18 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void SetupNetworkManger( string ip, int port ) {
|
|
|
|
|
|
|
|
|
|
NetworkManager.singleton.networkAddress = ip;
|
2019-04-23 14:08:08 +00:00
|
|
|
|
telepathyTransport.port = (ushort) port;
|
2019-04-23 13:49:18 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 14:58:13 +00:00
|
|
|
|
|
|
|
|
|
public static void AddSpawnPosition(Transform tr)
|
|
|
|
|
{
|
|
|
|
|
NetworkManager.startPositions.Add(tr);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 17:18:18 +00:00
|
|
|
|
public static Transform[] SpawnPositions { get { return NetworkManager.startPositions.ToArray(); } }
|
|
|
|
|
|
2019-04-24 14:58:13 +00:00
|
|
|
|
public static void Spawn(GameObject go)
|
|
|
|
|
{
|
|
|
|
|
NetworkServer.Spawn(go);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 21:12:42 +00:00
|
|
|
|
public static void AssignAuthority(GameObject go, Player player)
|
|
|
|
|
{
|
|
|
|
|
NetStatus.ThrowIfNotOnServer();
|
|
|
|
|
|
|
|
|
|
var netIdentity = go.GetComponentOrThrow<NetworkIdentity>();
|
|
|
|
|
|
|
|
|
|
if (netIdentity.clientAuthorityOwner == player.connectionToClient) // already has authority
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// first remove existing authority client
|
|
|
|
|
if (netIdentity.clientAuthorityOwner != null)
|
|
|
|
|
netIdentity.RemoveClientAuthority(netIdentity.clientAuthorityOwner);
|
|
|
|
|
|
|
|
|
|
// assign new authority client
|
|
|
|
|
netIdentity.AssignClientAuthority(player.connectionToClient);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 21:49:21 +00:00
|
|
|
|
public static void RemoveAuthority(GameObject go)
|
|
|
|
|
{
|
|
|
|
|
NetStatus.ThrowIfNotOnServer();
|
|
|
|
|
|
|
|
|
|
var netIdentity = go.GetComponentOrThrow<NetworkIdentity>();
|
|
|
|
|
|
|
|
|
|
if (netIdentity.clientAuthorityOwner != null)
|
|
|
|
|
netIdentity.RemoveClientAuthority(netIdentity.clientAuthorityOwner);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-24 18:46:34 +00:00
|
|
|
|
public static void ChangeScene(string newScene)
|
|
|
|
|
{
|
|
|
|
|
NetworkManager.singleton.ServerChangeScene(newScene);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 20:54:48 +00:00
|
|
|
|
public static GameObject GetNetworkObjectById(uint netId)
|
|
|
|
|
{
|
|
|
|
|
NetworkIdentity networkIdentity;
|
|
|
|
|
if (NetworkIdentity.spawned.TryGetValue(netId, out networkIdentity))
|
|
|
|
|
{
|
|
|
|
|
if (networkIdentity != null)
|
|
|
|
|
return networkIdentity.gameObject;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 13:49:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|