SanAndreasUnity/Assets/Scripts/Networking/NetCmdLineHandler.cs

63 lines
2.1 KiB
C#
Raw Normal View History

2019-07-05 19:18:29 +00:00
using System.Collections;
using System.Linq;
using SanAndreasUnity.GameModes;
2019-07-05 19:18:29 +00:00
using UnityEngine;
2019-07-05 21:03:57 +00:00
using SanAndreasUnity.Utilities;
2019-07-05 19:18:29 +00:00
2019-07-05 21:03:57 +00:00
namespace SanAndreasUnity.Net
2019-07-05 19:18:29 +00:00
{
2019-07-05 23:29:39 +00:00
public class NetCmdLineHandler : MonoBehaviour
2019-07-05 19:18:29 +00:00
{
2019-07-05 21:03:57 +00:00
public int numFramesToWait = 5;
IEnumerator Start()
{
if (!Mirror.NetworkManager.isHeadless)
yield break;
for (int i=0; i < this.numFramesToWait; i++)
yield return null;
ushort portNum = (ushort) NetManager.defaultListenPortNumber;
CmdLineUtils.GetUshortArgument("portNum", ref portNum);
string sceneName = "Main";
CmdLineUtils.GetArgument("scene", ref sceneName);
2019-07-05 23:24:22 +00:00
ushort maxNumPlayers = (ushort) NetManager.maxNumPlayers;
2019-07-05 21:03:57 +00:00
CmdLineUtils.GetUshortArgument("maxNumPlayers", ref maxNumPlayers);
string serverIp = "127.0.0.1";
CmdLineUtils.GetArgument("serverIp", ref serverIp);
string gameModeName = null;
CmdLineUtils.GetArgument("gameMode", ref gameModeName);
if (!string.IsNullOrWhiteSpace(gameModeName))
{
var gameModeInfo = GameModeManager.Instance.GameModes.FirstOrDefault(gm => gm.Name == gameModeName);
if (gameModeInfo != null)
GameModeManager.Instance.SelectGameMode(gameModeInfo);
else
Debug.LogError($"Game mode with name '{gameModeName}' not found");
}
2019-07-05 21:03:57 +00:00
if (CmdLineUtils.HasArgument("startServer"))
{
Debug.LogFormat("Starting server in headless mode, params: {0}, {1}, {2}", portNum, sceneName, maxNumPlayers);
NetManager.StartServer(portNum, sceneName, maxNumPlayers, true, false);
}
else if (CmdLineUtils.HasArgument("startClient"))
{
Debug.LogFormat("Starting client in headless mode, params: {0}, {1}", serverIp, portNum);
NetManager.StartClient(serverIp, portNum);
}
}
2019-07-05 19:18:29 +00:00
}
2019-07-05 21:03:57 +00:00
2019-07-05 19:18:29 +00:00
}