LANCommander/LANCommander.SDK/Clients/LobbyClient.cs

78 lines
2.6 KiB
C#
Raw Permalink Normal View History

using LANCommander.SDK.Models;
using Microsoft.Extensions.Logging;
using Steamworks;
using System;
using System.Collections.Generic;
using System.IO;
namespace LANCommander.SDK.Services
{
public class LobbyClient(ILogger<LobbyClient> logger)
{
/// <summary>
/// Get all Steam lobbies for a specified game. Game install directory must contain a file called steam_appid.txt.
/// Remember to call ReleaseSteam() when the game is done playing or lobby join is canceled!
/// </summary>
/// <param name="installDirectory">Directory that contains the game install and steam_appid.txt</param>
/// <param name="gameId">Game GUID</param>
/// <returns>Lobby information</returns>
public IEnumerable<Lobby> GetSteamLobbies(string installDirectory, Guid gameId)
{
uint appId = 0;
var appIdDefinitions = Directory.EnumerateFiles(installDirectory, "steam_appid.txt", SearchOption.AllDirectories);
foreach (var appIdDefinition in appIdDefinitions)
{
if (uint.TryParse(File.ReadAllText(appIdDefinition), out appId))
break;
}
var lobbies = new List<Lobby>();
try
{
Implement SDK using dependency injection - API requests are now made through the ApiRequestBuilder with DI supplied via the ApiRequestFactory singleton - Reliance on RestSharp and WebClient has been removed in favor of HttpClient - Auth token is now being tracked by the ITokenProvider - Network information (MAC address, broadcast addresses, IP address) is now supplied with the INetworkInformationProvider singleton - Connection state is now being maintained by the ConnectionService, with a reliance on RPC (SignalR websocket) reporting actual connection state without relying on pings - All download streams are now provided as a TrackableStream - Singleton Client class has been removed. All usage of the SDK should happen via Dependency Injection This is just a base, non-functional refactor of the SDK to use DI. The following changes to the rest of the codebase need to be made: - Usage of the SDK client in the launcher needs to be replaced in favor of injecting SDK services - PowerShell cmdlets need to be able to have services injected. Most likely a separate scope will have to be opened up per PS runtime? - Usage of the SDK client in the server needs to be replaced in favor of injecting SDK services. This should be minimal and should actually provide benefit when it comes to executing client-like features (scripts mostly) without needing a fully configured client that maintains connection state. - Configuration of the client needs to be implemented using ILANCommanderConfiguration
2025-09-22 00:29:51 -05:00
logger?.LogTrace("Initializing Steamworks with app ID {AppId}", appId);
2024-09-17 00:19:23 -05:00
SteamClient.Init(appId, true);
foreach (var friend in SteamFriends.GetFriends())
{
2025-08-18 03:02:49 -05:00
if (friend is { IsPlayingThisGame: true, GameInfo.Lobby: not null })
{
var lobby = new Lobby
{
GameId = gameId,
ExternalGameId = appId.ToString(),
Id = friend.GameInfo.Value.Lobby.Value.Id.Value.ToString(),
ExternalUserId = friend.Id.Value.ToString(),
ExternalUsername = friend.Name,
};
lobbies.Add(lobby);
2024-09-17 00:19:23 -05:00
Implement SDK using dependency injection - API requests are now made through the ApiRequestBuilder with DI supplied via the ApiRequestFactory singleton - Reliance on RestSharp and WebClient has been removed in favor of HttpClient - Auth token is now being tracked by the ITokenProvider - Network information (MAC address, broadcast addresses, IP address) is now supplied with the INetworkInformationProvider singleton - Connection state is now being maintained by the ConnectionService, with a reliance on RPC (SignalR websocket) reporting actual connection state without relying on pings - All download streams are now provided as a TrackableStream - Singleton Client class has been removed. All usage of the SDK should happen via Dependency Injection This is just a base, non-functional refactor of the SDK to use DI. The following changes to the rest of the codebase need to be made: - Usage of the SDK client in the launcher needs to be replaced in favor of injecting SDK services - PowerShell cmdlets need to be able to have services injected. Most likely a separate scope will have to be opened up per PS runtime? - Usage of the SDK client in the server needs to be replaced in favor of injecting SDK services. This should be minimal and should actually provide benefit when it comes to executing client-like features (scripts mostly) without needing a fully configured client that maintains connection state. - Configuration of the client needs to be implemented using ILANCommanderConfiguration
2025-09-22 00:29:51 -05:00
logger?.LogTrace("Found lobby | {FriendName} ({FriendId}): {LobbyId}", lobby.ExternalUsername, lobby.ExternalUserId, lobby.Id);
}
}
}
catch (Exception ex)
{
Implement SDK using dependency injection - API requests are now made through the ApiRequestBuilder with DI supplied via the ApiRequestFactory singleton - Reliance on RestSharp and WebClient has been removed in favor of HttpClient - Auth token is now being tracked by the ITokenProvider - Network information (MAC address, broadcast addresses, IP address) is now supplied with the INetworkInformationProvider singleton - Connection state is now being maintained by the ConnectionService, with a reliance on RPC (SignalR websocket) reporting actual connection state without relying on pings - All download streams are now provided as a TrackableStream - Singleton Client class has been removed. All usage of the SDK should happen via Dependency Injection This is just a base, non-functional refactor of the SDK to use DI. The following changes to the rest of the codebase need to be made: - Usage of the SDK client in the launcher needs to be replaced in favor of injecting SDK services - PowerShell cmdlets need to be able to have services injected. Most likely a separate scope will have to be opened up per PS runtime? - Usage of the SDK client in the server needs to be replaced in favor of injecting SDK services. This should be minimal and should actually provide benefit when it comes to executing client-like features (scripts mostly) without needing a fully configured client that maintains connection state. - Configuration of the client needs to be implemented using ILANCommanderConfiguration
2025-09-22 00:29:51 -05:00
logger?.LogError(ex, "Couldn't initialize Steamworks");
}
return lobbies;
}
public void ReleaseSteam()
{
try
{
SteamClient.Shutdown();
}
catch (Exception ex)
{
}
}
}
}