Anonymous Anonymous - 6 months ago
834 0

No description

C#

Good old dump

using BeardedManStudios.Forge.Networking;
using BeardedManStudios.Forge.Networking.Unity;
using BeardedManStudios.Forge.Networking.Lobby;
using BeardedManStudios.SimpleJSON;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LobbyMenu : MonoBehaviour
{
    public string ipAddress = "127.0.0.1";
    public string portNumber = "15937";
    public bool DontChangeSceneOnConnect = false;
    public string masterServerHost = string.Empty;
    public ushort masterServerPort = 15940;
    public string natServerHost = string.Empty;
    public ushort natServerPort = 15941;
    public bool connectUsingMatchmaking = false;
    public bool useElo = false;
    public int myElo = 0;
    public int eloRequired = 0;

    public GameObject networkManager = null;
    public GameObject[] ToggledButtons;
    private NetworkManager mgr = null;

    private List<Button> _uiButtons = new List<Button>();
    private bool _matchmaking = false;
    public bool useMainThreadManagerForRPCs = true;
    public bool useInlineChat = false;

    public bool getLocalNetworkConnections = false;

    public bool useTCP = true;

    private void Start()
    {
        if (!useTCP)
        {
            // Do any firewall opening requests on the operating system
            NetWorker.PingForFirewall(ushort.Parse(portNumber));
        }

        if (useMainThreadManagerForRPCs)
            Rpc.MainThreadRunner = MainThreadManager.Instance;

        if (getLocalNetworkConnections)
        {
            NetWorker.localServerLocated += LocalServerLocated;
            NetWorker.RefreshLocalUdpListings(ushort.Parse(portNumber));
        }
        
    }
    
    private void LocalServerLocated(NetWorker.BroadcastEndpoints endpoint, NetWorker sender)
    {
        Debug.Log("Found endpoint: " + endpoint.Address + ":" + endpoint.Port);
    }
    
    public void Connect()
    {
        if (connectUsingMatchmaking)
        {
            ConnectToMatchmaking();
            return;
        }

        ushort port;
        if (!ushort.TryParse(portNumber, out port))
        {
            Debug.LogError("The supplied port number is not within the allowed range 0-" + ushort.MaxValue);
            return;
        }

        NetWorker client;

        if (useTCP)
        {
            client = new TCPClient();
            ((TCPClient)client).Connect(ipAddress, port);
        }
        else
        {
            client = new UDPClient();
            if (natServerHost.Trim().Length == 0)
                ((UDPClient)client).Connect(ipAddress, port);
            else
                ((UDPClient)client).Connect(ipAddress, port, natServerHost, natServerPort);
        }
        Debug.Log("Trying to connect to " + ipAddress + ":" + port);
        Connected(client);
    }

    public void ConnectToMatchmaking()
    {
        if (_matchmaking)
            return;

        SetToggledButtons(false);
        _matchmaking = true;

        if (mgr == null && networkManager == null)
            throw new System.Exception("A network manager was not provided, this is required for the tons of fancy stuff");

        mgr = Instantiate(networkManager).GetComponent<NetworkManager>();

        mgr.MatchmakingServersFromMasterServer(masterServerHost, masterServerPort, myElo, (response) =>
        {
            _matchmaking = false;
            SetToggledButtons(true);
            Debug.LogFormat("Matching Server(s) count[{0}]", response.serverResponse.Count);

            //TODO: YOUR OWN MATCHMAKING EXTRA LOGIC HERE!
            // I just make it randomly pick a server... you can do whatever you please!
            if (response != null && response.serverResponse.Count > 0)
            {
                MasterServerResponse.Server server = response.serverResponse[Random.Range(0, response.serverResponse.Count)];
                //TCPClient client = new TCPClient();
                UDPClient client = new UDPClient();
                client.Connect(server.Address, server.Port);
                Connected(client);
            }
        });
    }

    public void Host()
    {
        NetWorker server;

        if (useTCP)
        {
            server = new TCPServer(64);
            ((TCPServer)server).Connect();
        }
        else
        {
            server = new UDPServer(64);

            if (natServerHost.Trim().Length == 0)
                ((UDPServer)server).Connect(ipAddress, ushort.Parse(portNumber));
            else
                ((UDPServer)server).Connect(natHost: natServerHost, natPort: natServerPort);
        }

        server.playerTimeout += (player, sender) =>
        {
            Debug.Log("Player " + player.NetworkId + " timed out");
        };
        //LobbyService.Instance.Initialize(server);

        Connected(server);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.H))
            Host();
        else if (Input.GetKeyDown(KeyCode.C))
            Connect();
        else if (Input.GetKeyDown(KeyCode.L))
        {
            NetWorker.localServerLocated -= TestLocalServerFind;
            NetWorker.localServerLocated += TestLocalServerFind;
            NetWorker.RefreshLocalUdpListings();
        }
        if (NetworkManager.Instance != null && NetworkManager.Instance.Networker != null)
        {
            Debug.Log("NetworkObjects.Count :" + NetworkManager.Instance.Networker.Players.Count);
            if (NetworkManager.Instance.Networker.Players.Count > 1 && NetworkManager.Instance.Networker.IsServer)
            {
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
            }
        }
    }

    private void TestLocalServerFind(NetWorker.BroadcastEndpoints endpoint, NetWorker sender)
    {
        Debug.Log("Address: " + endpoint.Address + ", Port: " + endpoint.Port + ", Server? " + endpoint.IsServer);
    }

    public void Connected(NetWorker networker)
    {
        if (!networker.IsBound)
        {
            Debug.LogError("NetWorker failed to bind");
            return;
        }

        if (mgr == null && networkManager == null)
        {
            Debug.LogWarning("A network manager was not provided, generating a new one instead");
            networkManager = new GameObject("Network Manager");
            mgr = networkManager.AddComponent<NetworkManager>();
        }
        else if (mgr == null)
            mgr = Instantiate(networkManager).GetComponent<NetworkManager>();

        // If we are using the master server we need to get the registration data
        JSONNode masterServerData = null;
        if (!string.IsNullOrEmpty(masterServerHost))
        {
            string serverId = "myGame";
            string serverName = "Forge Game";
            string type = "Deathmatch";
            string mode = "Teams";
            string comment = "Demo comment...";

            masterServerData = mgr.MasterServerRegisterData(networker, serverId, serverName, type, mode, comment, useElo, eloRequired);
        }

        mgr.Initialize(networker, masterServerHost, masterServerPort, masterServerData);

        if (useInlineChat && networker.IsServer)
            SceneManager.sceneLoaded += CreateInlineChat;
        Debug.Log("Presumably connected");
        if (networker is IServer)
        {
            if (!DontChangeSceneOnConnect)
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
            else
                NetworkObject.Flush(networker); //Called because we are already in the correct scene!
        }
    }

    private void CreateInlineChat(Scene arg0, LoadSceneMode arg1)
    {
        SceneManager.sceneLoaded -= CreateInlineChat;
        var chat = NetworkManager.Instance.InstantiateChatManager();
        DontDestroyOnLoad(chat.gameObject);
    }

    private void SetToggledButtons(bool value)
    {
        for (int i = 0; i < _uiButtons.Count; ++i)
            _uiButtons[i].interactable = value;
    }

    private void OnApplicationQuit()
    {
        if (getLocalNetworkConnections)
            NetWorker.EndSession();
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download