Files
K-C-Multiplayer/RiptideSteamTransport/LobbyManager.cs

190 lines
6.1 KiB
C#

using KCM;
using KCM.Enums;
using KCM.Packets.Handlers;
using Steamworks;
using System;
using UnityEngine;
namespace Riptide.Demos.Steam.PlayerHosted
{
public class LobbyManager : MonoBehaviour
{
private static LobbyManager _singleton;
internal static LobbyManager Singleton
{
get => _singleton;
private set
{
if (_singleton == null)
_singleton = value;
else if (_singleton != value)
{
Debug.Log($"{nameof(LobbyManager)} instance already exists, destroying object!");
Destroy(value);
}
}
}
protected Callback<LobbyCreated_t> lobbyCreated;
protected Callback<GameLobbyJoinRequested_t> gameLobbyJoinRequested;
protected Callback<LobbyEnter_t> lobbyEnter;
private const string HostAddressKey = "HostAddress";
private CSteamID lobbyId;
private void Awake()
{
Singleton = this;
}
private void Start()
{
if (!KCMSteamManager.Initialized)
{
Main.helper.Log("Steam is not initialized!");
return;
}
lobbyCreated = Callback<LobbyCreated_t>.Create(OnLobbyCreated);
gameLobbyJoinRequested = Callback<GameLobbyJoinRequested_t>.Create(OnGameLobbyJoinRequested);
lobbyEnter = Callback<LobbyEnter_t>.Create(OnLobbyEnter);
}
public static bool loadingSave = false;
internal void CreateLobby(bool loadingSave = false)
{
var result = SteamMatchmaking.CreateLobby(ELobbyType.k_ELobbyTypePublic, 25);
LobbyManager.loadingSave = loadingSave;
}
private void OnLobbyCreated(LobbyCreated_t callback)
{
if (callback.m_eResult != EResult.k_EResultOK)
{
Main.helper.Log("Create lobby failed");
return;
}
lobbyId = new CSteamID(callback.m_ulSteamIDLobby);
KCServer.StartServer();
Main.TransitionTo(MenuState.ServerLobby);
try
{
Main.helper.Log("About to call connect");
KCClient.Connect("127.0.0.1");
World.inst.Generate();
ServerLobbyScript.WorldSeed.text = World.inst.GetTextSeed();
LobbyHandler.ClearPlayerList();
ServerBrowser.registerServer = true;
}
catch (System.Exception ex)
{
Main.helper.Log("----------------------- Main exception -----------------------");
Main.helper.Log(ex.ToString());
Main.helper.Log("----------------------- Main message -----------------------");
Main.helper.Log(ex.Message);
Main.helper.Log("----------------------- Main stacktrace -----------------------");
Main.helper.Log(ex.StackTrace);
if (ex.InnerException != null)
{
Main.helper.Log("----------------------- Inner exception -----------------------");
Main.helper.Log(ex.InnerException.ToString());
Main.helper.Log("----------------------- Inner message -----------------------");
Main.helper.Log(ex.InnerException.Message);
Main.helper.Log("----------------------- Inner stacktrace -----------------------");
Main.helper.Log(ex.InnerException.StackTrace);
}
}
}
internal void JoinLobby(ulong lobbyId)
{
SteamMatchmaking.JoinLobby(new CSteamID(lobbyId));
}
private void OnGameLobbyJoinRequested(GameLobbyJoinRequested_t callback)
{
SteamMatchmaking.JoinLobby(callback.m_steamIDLobby);
}
private void OnLobbyEnter(LobbyEnter_t callback)
{
if (KCServer.IsRunning)
return;
lobbyId = new CSteamID(callback.m_ulSteamIDLobby);
CSteamID hostId = SteamMatchmaking.GetLobbyOwner(lobbyId);
KCClient.Connect(hostId.ToString());
}
public void LeaveLobby()
{
Main.helper.Log("LeaveLobby called - cleaning up connection state");
try
{
// Disconnect client first
if (KCClient.client != null && (KCClient.client.IsConnected || KCClient.client.IsConnecting))
{
Main.helper.Log("Disconnecting client...");
KCClient.client.Disconnect();
}
// Stop server if running
if (KCServer.IsRunning)
{
Main.helper.Log("Stopping server...");
KCServer.server.Stop();
}
// Leave Steam lobby
if (lobbyId.IsValid())
{
Main.helper.Log("Leaving Steam lobby...");
SteamMatchmaking.LeaveLobby(lobbyId);
}
// Clear player data
Main.helper.Log("Clearing player data...");
Main.kCPlayers.Clear();
Main.clientSteamIds.Clear();
// Clear UI
LobbyHandler.ClearPlayerList();
LobbyHandler.ClearChatEntries();
// Reset flags
ServerBrowser.registerServer = false;
loadingSave = false;
Main.helper.Log("Lobby cleanup completed successfully");
// Transition back to server browser
Main.TransitionTo(MenuState.ServerBrowser);
}
catch (System.Exception ex)
{
Main.helper.Log("Error during LeaveLobby:");
Main.helper.Log(ex.Message);
Main.helper.Log(ex.StackTrace);
// Still try to transition back even if cleanup failed
Main.TransitionTo(MenuState.ServerBrowser);
}
}
}
}