Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- using Steamworks;
- public class MultiplayerMenu : MonoBehaviour
- {
- public InputField ipAddress = null;
- public InputField portNumber = null;
- public bool DontChangeSceneOnConnect = false;
- public bool connectUsingMatchmaking = false;
- public GameObject networkManager = null;
- public GameObject[] ToggledButtons;
- public GameObject mainMenu;
- public GameObject connectMenu;
- private NetworkManager mgr = null;
- private NetWorker server;
- 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 = false;
- private CallResult<LobbyMatchList_t> OnLobbyMatchListCallResult;
- private void Start()
- {
- SteamAPI.Init();
- OnLobbyMatchListCallResult = CallResult<LobbyMatchList_t>.Create(OnLobbyMatchList);
- for (int i = 0; i < ToggledButtons.Length; ++i)
- {
- Button btn = ToggledButtons[i].GetComponent<Button>();
- if (btn != null)
- _uiButtons.Add(btn);
- }
- }
- void OnLobbyMatchList(LobbyMatchList_t pCallback, bool bIOFailure)
- {
- BMSLogger.DebugLog("[" + LobbyMatchList_t.k_iCallback + " - LobbyMatchList] - " + pCallback.m_nLobbiesMatching);
- if (pCallback.m_nLobbiesMatching == 0)
- {
- return;
- }
- int lobbyToJoin = Random.Range(0, (int)pCallback.m_nLobbiesMatching);
- BMSLogger.DebugLog("Joining lobby id: " + lobbyToJoin);
- CSteamID lobby = SteamMatchmaking.GetLobbyByIndex(lobbyToJoin);
- BMSLogger.DebugLog("Joining Lobby SteamID: " + lobby.m_SteamID);
- SteamP2PClient client = new SteamP2PClient();
- ((SteamP2PClient)client).Connect(lobby);
- Connected(client);
- }
- public void ToggleConnectMenu()
- {
- mainMenu.SetActive(false);
- connectMenu.SetActive(true);
- }
- public void ToggleMainMenu()
- {
- mainMenu.SetActive(true);
- connectMenu.SetActive(false);
- }
- // Request a list of lobbies and connect to a random one.
- public void ConnectToMatchmaking()
- {
- if (!SteamManager.Initialized)
- {
- BMSLogger.DebugLog("Steam manager not initialized");
- return;
- }
- BMSLogger.DebugLog("Requesting lobby list");
- SteamAPICall_t handle = SteamMatchmaking.RequestLobbyList();
- OnLobbyMatchListCallResult.Set(handle);
- }
- public void Host()
- {
- // Currently there is a bug in the SteamP2PServer code where the lobby max member count is hard coded to be 5.
- // Until a fix is in place please change line 186 of the SteamP2PServer to read
- // `m_CreateLobbyResult = SteamMatchmaking.CreateLobby(lobbyType, MaxConnections);`
- server = new SteamP2PServer(5);
- // Don't yet have a way to invite players to lobby. Until then all hosts are set to be public
- //((SteamP2PServer)server).Host(SteamUser.GetSteamID(), isPrivateLobby ? ELobbyType.k_ELobbyTypeFriendsOnly : ELobbyType.k_ELobbyTypePublic, OnLobbyReady);
- ((SteamP2PServer)server).Host(SteamUser.GetSteamID(), ELobbyType.k_ELobbyTypePublic, OnLobbyReady);
- BMSLogger.DebugLog("Lobby will be created by user id: " + SteamUser.GetSteamID());
- server.playerTimeout += (player, sender) => { BMSLogger.DebugLog("Player " + player.NetworkId + " timed out"); };
- Connected(server);
- }
- private void OnLobbyReady()
- {
- // If the host has not set a server name then let's use his/her name instead to name the lobby
- var personalName = SteamFriends.GetPersonaName();
- var lobbyId = ((SteamP2PServer)server).LobbyID;
- BMSLogger.DebugLog("Created a lobby with id: " + lobbyId);
- //Set lobby metadata here
- //// Set the name of the lobby
- //SteamMatchmaking.SetLobbyData(lobbyId, "name", gameName);
- //// Set the unique id of our game so the server list only gets the games with this id
- //SteamMatchmaking.SetLobbyData(lobbyId, "fnr_gameId", gameId);
- //// Set all other game information
- //SteamMatchmaking.SetLobbyData(lobbyId, "fnr_gameType", type);
- //SteamMatchmaking.SetLobbyData(lobbyId, "fnr_gameMode", mode);
- //SteamMatchmaking.SetLobbyData(lobbyId, "fnr_gameDesc", comment);
- }
- public void Connected(NetWorker networker)
- {
- if (!networker.IsBound)
- {
- BMSLogger.DebugLog("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>();
- mgr.Initialize(networker);
- 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 SetToggledButtons(bool value)
- {
- for (int i = 0; i < _uiButtons.Count; ++i)
- _uiButtons[i].interactable = value;
- }
- private void OnApplicationQuit()
- {
- if (getLocalNetworkConnections)
- NetWorker.EndSession();
- if (server != null) server.Disconnect(true);
- SteamAPI.Shutdown();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement