Advertisement
Guest User

MultiplayerMenu.cs

a guest
Aug 22nd, 2019
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.98 KB | None | 0 0
  1. using BeardedManStudios.Forge.Networking;
  2. using BeardedManStudios.Forge.Networking.Unity;
  3. using BeardedManStudios.Forge.Networking.Lobby;
  4. using BeardedManStudios.SimpleJSON;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.UI;
  9. using Steamworks;
  10.  
  11. public class MultiplayerMenu : MonoBehaviour
  12. {
  13.     public InputField ipAddress = null;
  14.     public InputField portNumber = null;
  15.     public bool DontChangeSceneOnConnect = false;
  16.     public bool connectUsingMatchmaking = false;
  17.  
  18.     public GameObject networkManager = null;
  19.     public GameObject[] ToggledButtons;
  20.     public GameObject mainMenu;
  21.     public GameObject connectMenu;
  22.     private NetworkManager mgr = null;
  23.     private NetWorker server;
  24.  
  25.     private List<Button> _uiButtons = new List<Button>();
  26.     private bool _matchmaking = false;
  27.     public bool useMainThreadManagerForRPCs = true;
  28.     public bool useInlineChat = false;
  29.  
  30.     public bool getLocalNetworkConnections = false;
  31.  
  32.     public bool useTCP = false;
  33.  
  34.     private CallResult<LobbyMatchList_t> OnLobbyMatchListCallResult;
  35.  
  36.     private void Start()
  37.     {
  38.         SteamAPI.Init();
  39.  
  40.         OnLobbyMatchListCallResult = CallResult<LobbyMatchList_t>.Create(OnLobbyMatchList);
  41.        
  42.         for (int i = 0; i < ToggledButtons.Length; ++i)
  43.         {
  44.             Button btn = ToggledButtons[i].GetComponent<Button>();
  45.             if (btn != null)
  46.                 _uiButtons.Add(btn);
  47.         }
  48.        
  49.     }
  50.  
  51.     void OnLobbyMatchList(LobbyMatchList_t pCallback, bool bIOFailure)
  52.     {
  53.         BMSLogger.DebugLog("[" + LobbyMatchList_t.k_iCallback + " - LobbyMatchList] - " + pCallback.m_nLobbiesMatching);
  54.  
  55.         if (pCallback.m_nLobbiesMatching == 0)
  56.         {
  57.             return;
  58.         }
  59.  
  60.         int lobbyToJoin = Random.Range(0, (int)pCallback.m_nLobbiesMatching);
  61.  
  62.         BMSLogger.DebugLog("Joining lobby id: " + lobbyToJoin);
  63.  
  64.         CSteamID lobby = SteamMatchmaking.GetLobbyByIndex(lobbyToJoin);
  65.  
  66.         BMSLogger.DebugLog("Joining Lobby SteamID: " + lobby.m_SteamID);
  67.  
  68.         SteamP2PClient client = new SteamP2PClient();
  69.         ((SteamP2PClient)client).Connect(lobby);
  70.  
  71.         Connected(client);
  72.     }
  73.  
  74.     public void ToggleConnectMenu()
  75.     {
  76.         mainMenu.SetActive(false);
  77.         connectMenu.SetActive(true);
  78.     }
  79.  
  80.     public void ToggleMainMenu()
  81.     {
  82.         mainMenu.SetActive(true);
  83.         connectMenu.SetActive(false);
  84.     }
  85.    
  86.     // Request a list of lobbies and connect to a random one.
  87.     public void ConnectToMatchmaking()
  88.     {
  89.         if (!SteamManager.Initialized)
  90.         {
  91.             BMSLogger.DebugLog("Steam manager not initialized");
  92.             return;
  93.         }
  94.  
  95.         BMSLogger.DebugLog("Requesting lobby list");
  96.  
  97.         SteamAPICall_t handle = SteamMatchmaking.RequestLobbyList();
  98.         OnLobbyMatchListCallResult.Set(handle);
  99.        
  100.     }
  101.  
  102.     public void Host()
  103.     {
  104.         // Currently there is a bug in the SteamP2PServer code where the lobby max member count is hard coded to be 5.
  105.         // Until a fix is in place please change line 186 of the SteamP2PServer to read
  106.         //    `m_CreateLobbyResult = SteamMatchmaking.CreateLobby(lobbyType, MaxConnections);`
  107.         server = new SteamP2PServer(5);
  108.  
  109.         // Don't yet have a way to invite players to lobby. Until then all hosts are set to be public
  110.         //((SteamP2PServer)server).Host(SteamUser.GetSteamID(), isPrivateLobby ? ELobbyType.k_ELobbyTypeFriendsOnly : ELobbyType.k_ELobbyTypePublic, OnLobbyReady);
  111.         ((SteamP2PServer)server).Host(SteamUser.GetSteamID(), ELobbyType.k_ELobbyTypePublic, OnLobbyReady);
  112.  
  113.         BMSLogger.DebugLog("Lobby will be created by user id: " + SteamUser.GetSteamID());
  114.  
  115.         server.playerTimeout += (player, sender) => { BMSLogger.DebugLog("Player " + player.NetworkId + " timed out"); };
  116.  
  117.         Connected(server);
  118.  
  119.     }
  120.  
  121.     private void OnLobbyReady()
  122.     {
  123.         // If the host has not set a server name then let's use his/her name instead to name the lobby
  124.         var personalName = SteamFriends.GetPersonaName();
  125.  
  126.         var lobbyId = ((SteamP2PServer)server).LobbyID;
  127.  
  128.         BMSLogger.DebugLog("Created a lobby with id: " + lobbyId);
  129.  
  130.         //Set lobby metadata here
  131.  
  132.         //// Set the name of the lobby
  133.         //SteamMatchmaking.SetLobbyData(lobbyId, "name", gameName);
  134.  
  135.         //// Set the unique id of our game so the server list only gets the games with this id
  136.         //SteamMatchmaking.SetLobbyData(lobbyId, "fnr_gameId", gameId);
  137.  
  138.         //// Set all other game information
  139.         //SteamMatchmaking.SetLobbyData(lobbyId, "fnr_gameType", type);
  140.         //SteamMatchmaking.SetLobbyData(lobbyId, "fnr_gameMode", mode);
  141.         //SteamMatchmaking.SetLobbyData(lobbyId, "fnr_gameDesc", comment);
  142.     }
  143.    
  144.     public void Connected(NetWorker networker)
  145.     {
  146.         if (!networker.IsBound)
  147.         {
  148.             BMSLogger.DebugLog("NetWorker failed to bind");
  149.             return;
  150.         }
  151.  
  152.         if (mgr == null && networkManager == null)
  153.         {
  154.             Debug.LogWarning("A network manager was not provided, generating a new one instead");
  155.             networkManager = new GameObject("Network Manager");
  156.             mgr = networkManager.AddComponent<NetworkManager>();
  157.         }
  158.         else if (mgr == null)
  159.             mgr = Instantiate(networkManager).GetComponent<NetworkManager>();
  160.  
  161.  
  162.         mgr.Initialize(networker);
  163.  
  164.         if (networker is IServer)
  165.         {
  166.             if (!DontChangeSceneOnConnect)
  167.                 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
  168.             else
  169.                 NetworkObject.Flush(networker); //Called because we are already in the correct scene!
  170.         }
  171.     }
  172.    
  173.     private void SetToggledButtons(bool value)
  174.     {
  175.         for (int i = 0; i < _uiButtons.Count; ++i)
  176.             _uiButtons[i].interactable = value;
  177.     }
  178.  
  179.     private void OnApplicationQuit()
  180.     {
  181.         if (getLocalNetworkConnections)
  182.             NetWorker.EndSession();
  183.  
  184.         if (server != null) server.Disconnect(true);
  185.  
  186.         SteamAPI.Shutdown();
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement