Files
SVSimServer/SVSim.EmulatedEntrypoint/Services/SteamSessionService.cs
2026-05-23 14:18:01 -04:00

67 lines
2.0 KiB
C#

using System.Collections.Concurrent;
using Steamworks;
namespace SVSim.EmulatedEntrypoint.Services;
public class SteamSessionService : IDisposable
{
private readonly ConcurrentDictionary<string, ulong> _validatedSessionTickets = new();
private readonly object _initLock = new();
private bool _steamInitialized;
private const int ShadowVerseAppId = 453480;
/// <summary>
/// Validates if a given session ticket is valid, and matches up with the given steamid.
/// </summary>
/// <param name="ticket">the ticket, represented as a hexadecimal string</param>
/// <param name="steamId">the steamid that should be associated with the ticket</param>
/// <returns>whether the ticket is valid for the given steamid</returns>
public bool IsTicketValidForUser(string ticket, ulong steamId)
{
if (_validatedSessionTickets.TryGetValue(ticket, out ulong storedSteamId))
{
return storedSteamId == steamId;
}
EnsureSteamInitialized();
List<byte> ticketBytes = new List<byte>();
for (int i = 0; i < ticket.Length; i += 2)
{
ticketBytes.Add(Convert.ToByte(ticket.Substring(i, 2), 16));
}
var steamCheckResults = SteamServer.BeginAuthSession(ticketBytes.ToArray(), new SteamId { Value = steamId });
if (steamCheckResults)
{
_validatedSessionTickets.TryAdd(ticket, steamId);
}
return steamCheckResults;
}
private void EnsureSteamInitialized()
{
if (_steamInitialized) return;
lock (_initLock)
{
if (_steamInitialized) return;
SteamServer.Init(ShadowVerseAppId, new SteamServerInit
{
GamePort = default,
QueryPort = default
});
_steamInitialized = true;
}
}
public void Dispose()
{
if (_steamInitialized)
{
SteamServer.Shutdown();
}
}
}