using UnityEngine;
using System.IO;
using System.Collections;
using System; // Required for namespace Exception used in Create Server menu
using TNet;
[RequireComponent
(typeof(TNManager
))] public class ServerSetup : MonoBehaviour
{
//static public TNAutoJoin instance;
// The window id, changeable in editor.
public int windowId = 1;
// GUI window.
private Rect windowRect
= new Rect
(10,
10,
150,
25);
// Server preset values.
private string serverName = "Castro";
private string maxPlayers = "32";
private string tcpPort = "25566";
private int udpPort;
private string serverToJoinIP = "127.0.0.1";
public string level = "City";
public int channelID = 1;
public string successFunctionName;
public string failureFunctionName;
public enum MenuState
{
serverOff,
serverOn
}
public MenuState currMenuState;
public AudioClip buttonClick;
private ServerLog sLog;
/// <summary>
/// Set the instance so this script can be easily found.
/// </summary>
//void Awake() { if (instance == null) instance = this; }
// Use this for initialization
void Start ()
{
sLog = GetComponent<ServerLog>();
udpPort = UnityEngine.Random.Range(10000, 40000);
}
// Update is called once per frame
void Update ()
{
if (TNServerInstance.isActive)
{
currMenuState = MenuState.serverOn;
}
else
{
currMenuState = MenuState.serverOff;
}
}
void OnGUI()
{
windowRect = GUILayout.Window(windowId, windowRect, windowFunc, "Start/Stop Server");
}
private void windowFunc(int id)
{
switch (currMenuState)
{
case MenuState.serverOn:
GUILayout.Label("Server Name");
GUILayout.Box(serverName);
GUILayout.Label("TCP Port");
GUILayout.Box(tcpPort);
GUILayout.Label("UDP Port");
GUILayout.Box("" + udpPort);
GUILayout.Label("Max Players");
GUILayout.Box(maxPlayers);
if (GUILayout.Button("Disconnect"))
{
TNManager.Disconnect();
TNServerInstance.Stop();
TNManager.LoadLevel("Server");
sLog.MessageBoxString("Server stopped");
}
break;
case MenuState.serverOff:
GUILayout.Label("Server Name");
serverName = GUILayout.TextField(serverName);
GUILayout.Label("TCP Port");
tcpPort = GUILayout.TextField(tcpPort);
GUILayout.Label("UDP Port");
GUILayout.Label("" + udpPort);
GUILayout.Label("Max Players");
maxPlayers = GUILayout.TextField(maxPlayers);
if (GUILayout.Button("Create Server"))
{
//audio.clip = buttonClick;
//audio.Play();
// Try to create server.
try
{
// TNet start server.
TNServerInstance.Start(int.Parse(tcpPort), udpPort);
TNManager.Connect(serverToJoinIP);
//TNManager.LoadLevel(level);
// Joins/Starts channel + 1 for excluding the server as a player and make that slot free.
//TNManager.JoinChannel(channelID, "City", false, int.Parse(maxPlayers) + 1, "");
//MessageBox("Server started!");
//MessageBox("Server joined channel: " + TNManager.isInChannel);
}
// Something went wrong with creating the server.
catch (Exception)
{
//MessageBox("Something went wrong, check your settings");
}
}
if (GUILayout.Button("Quit"))
{
Application.Quit();
}
break;
}
}
/// <summary>
/// On success -- join a channel.
/// </summary>
void OnNetworkConnect(bool result, string message)
{
if (result)
{
sLog.MessageBoxString("Connected to server, trying to create channel...");
TNManager.JoinChannel(channelID, level);
sLog.MessageBoxString("Channel created!");
}
else if (!string.IsNullOrEmpty(failureFunctionName))
{
UnityTools.Broadcast(failureFunctionName, message);
sLog.MessageBoxString("Failed to create channel: \n" + message);
}
else
{
sLog.MessageBoxString("Failed to create channel: \n" + message);
Debug.LogError(message);
}
}
/// <summary>
/// Joined a channel (or failed to).
/// </summary>
void OnNetworkJoinChannel(bool result, string message)
{
if (result)
{
sLog.MessageBoxString("Joined channel " + channelID + " with success!");
}
else
{
sLog.MessageBoxString("Failed to join channel: " + message);
sLog.MessageBoxString("Starting to close down server...");
TNManager.Disconnect();
sLog.MessageBoxString("Detached from server...");
TNServerInstance.Stop();
sLog.MessageBoxString("Server stopped...");
TNManager.LoadLevel("Server");
sLog.MessageBoxString("Server close down finnished!");
}
}
}