Check Steam ownership of the paid app with BIsSubscribedApp; keep Debug fake ownership out of Release. Ref: https://partner.steamgames.com/doc/api/ISteamApps#BIsSubscribedApp
88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using Steamworks;
|
|
|
|
namespace ytLive.Services.Entitlement;
|
|
|
|
/// <summary>
|
|
/// Reads ownership of the paid app from Steam. In the free Demo, this checks the associated paid
|
|
/// app ID; no key, server, or LlamaCasty account is involved. A missing ID, Steam client, or native
|
|
/// library fails closed so the app can continue with the free output profile.
|
|
/// </summary>
|
|
public sealed class SteamEntitlement : IEntitlement
|
|
{
|
|
private readonly uint _paidAppId;
|
|
private readonly Func<bool> _initializeSteam;
|
|
private readonly Func<uint, bool> _isAppSubscribed;
|
|
private readonly Action _shutdownSteam;
|
|
|
|
private bool _initAttempted;
|
|
private bool _available;
|
|
private bool? _cached;
|
|
|
|
public SteamEntitlement(uint paidAppId)
|
|
: this(
|
|
paidAppId,
|
|
SteamAPI.Init,
|
|
appId => SteamApps.BIsSubscribedApp(new AppId_t(appId)),
|
|
SteamAPI.Shutdown)
|
|
{
|
|
}
|
|
|
|
internal SteamEntitlement(
|
|
uint paidAppId,
|
|
Func<bool> initializeSteam,
|
|
Func<uint, bool> isAppSubscribed,
|
|
Action shutdownSteam)
|
|
{
|
|
_paidAppId = paidAppId;
|
|
_initializeSteam = initializeSteam;
|
|
_isAppSubscribed = isAppSubscribed;
|
|
_shutdownSteam = shutdownSteam;
|
|
}
|
|
|
|
/// <summary>Configured with no paid app ID; ownership fails closed without touching Steam.</summary>
|
|
public static SteamEntitlement NotConfigured() => new(0);
|
|
|
|
public bool IsAppOwned
|
|
{
|
|
get
|
|
{
|
|
if (_paidAppId == 0) return false;
|
|
if (_cached is { } known) return known;
|
|
|
|
if (!_initAttempted)
|
|
{
|
|
_initAttempted = true;
|
|
try
|
|
{
|
|
_available = _initializeSteam();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
_available = false;
|
|
}
|
|
}
|
|
|
|
if (!_available) { _cached = false; return false; }
|
|
|
|
try
|
|
{
|
|
var owned = _isAppSubscribed(_paidAppId);
|
|
_cached = owned;
|
|
return owned;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Shutdown()
|
|
{
|
|
if (_available) _shutdownSteam();
|
|
_available = false;
|
|
_cached = null;
|
|
_initAttempted = false;
|
|
}
|
|
}
|