Untitled

mail@pastecode.io avatar
unknown
csharp
14 days ago
4.4 kB
3
Indexable
Never
using System.Collections.Generic;
using Mirror;
using Steamworks;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using Unity.VisualScripting;
using UnityEngine.UIElements;
using UnityEngine.UI;

public class SteamLobbyManager : MonoBehaviour
{
    public static SteamLobbyManager Instance;
 
    public bool PrivacySetting;
    [SerializeField] TMP_Text InputtedServerName;
    [SerializeField] private GameObject buttons;
    public TMP_Dropdown FilterDropdown;
    private NetworkManager networkManager;
    private const string HostAddressKey = "HostAddress";

    protected Callback<LobbyCreated_t> lobbyCreated;
    protected Callback<GameLobbyJoinRequested_t> gameLobbyJoinRequested;
    protected Callback<LobbyEnter_t> gameLobbyEntered;

    protected Callback<LobbyMatchList_t> LobbyList;
    protected Callback<LobbyDataUpdate_t> LobbyDataUpdated;

    public List<CSteamID> lobbyIDs = new List<CSteamID>();
        public void JoinLobby(CSteamID lobbyID)
        {
            SteamMatchmaking.JoinLobby(lobbyID);
        }

    void Start()
    {
        if (!SteamManager.Initialized) { return; }
        if(Instance == null) { Instance=this; }

            networkManager = GetComponent<NetworkManager>();

            lobbyCreated = Callback<LobbyCreated_t>.Create(OnLobbyCreated);
            gameLobbyJoinRequested = Callback<GameLobbyJoinRequested_t>.Create(OnGameLobbyJoinRequested);
            gameLobbyEntered = Callback<LobbyEnter_t>.Create(OnLobbyEntered);

            LobbyList = Callback<LobbyMatchList_t>.Create(OnGetLobbyList);
            LobbyDataUpdated = Callback<LobbyDataUpdate_t>.Create(OnGetLobbyData);
    }

    public void HostLobby()
    {
        buttons.SetActive(false);
        
        SteamMatchmaking.CreateLobby((ELobbyType)(PrivacySetting? 1 : 2), networkManager.maxConnections);
        DebugConsole.instance.Log("Attempting to create a server...");
    }

    private void OnLobbyCreated(LobbyCreated_t callback)
    {
        if(callback.m_eResult != EResult.k_EResultOK)
        {
             DebugConsole.instance.LogError("Failed to create server");
            buttons.SetActive(true);
            return;
        }
        else
        {
            DebugConsole.instance.LogSuccess("Created Server Successfully");
        }
        networkManager.StartHost();

        DebugConsole.instance.Log("Your Server was hosted "+(ELobbyType)(PrivacySetting? 1 : 2)+" with the name "+InputtedServerName.text);
    }

        private void OnGameLobbyJoinRequested(GameLobbyJoinRequested_t callback)
        {
            SteamMatchmaking.JoinLobby(callback.m_steamIDLobby);
        }

        private void OnLobbyEntered(LobbyEnter_t callback)
        {
            DontDestroyOnLoad(networkManager);
            SceneManager.LoadScene("GameScene", LoadSceneMode.Single);
            DebugConsole.instance.Log("Lobby Joined");

            if (NetworkServer.active) {return;}
                
            string hostAddress = SteamMatchmaking.GetLobbyData(
                new CSteamID(callback.m_ulSteamIDLobby),
                HostAddressKey);
                
                networkManager.networkAddress = hostAddress;
                networkManager.StartClient();

            DebugConsole.instance.Log("HostAddress is "+ hostAddress);
                buttons.SetActive(false);
        }

        public void GetLobbiesList()
        {
            if(lobbyIDs.Count>0) {lobbyIDs.Clear();}
    
            SteamMatchmaking.AddRequestLobbyListDistanceFilter((ELobbyDistanceFilter)FilterDropdown.value);
            SteamMatchmaking.RequestLobbyList();
        }

        void OnGetLobbyList(LobbyMatchList_t result)
        {
            if(LobbyListManager.instance.LobbyList.Count>0) {LobbyListManager.instance.DestroyLobbies();}

                for(int i=0; i<result.m_nLobbiesMatching; i++)
                {
                    CSteamID lobbyID = SteamMatchmaking.GetLobbyByIndex(i);
                    lobbyIDs.Add(lobbyID);
                    SteamMatchmaking.RequestLobbyData(lobbyID);
            }
        }

        void OnGetLobbyData(LobbyDataUpdate_t result)
        {
        if(!LobbyListManager.instance){return;}
            LobbyListManager.instance.DisplayLobbies(lobbyIDs, result);
        }
}
Leave a Comment