Client LuaCsForBarotrauma
AuthTicket.cs
1 #nullable enable
2 using System;
3 using System.Threading.Tasks;
5 
6 namespace Barotrauma.Steam
7 {
8  static partial class SteamManager
9  {
10  #region Auth ticket for Steam host
11  private static Option<Steamworks.AuthTicket> currentSteamHostAuthTicket = Option.None;
12  public static Option<Steamworks.AuthTicket> GetAuthSessionTicketForSteamHost(Endpoint remoteHostEndpoint)
13  {
14  if (!IsInitialized)
15  {
16  return Option.None;
17  }
18 
19  if (currentSteamHostAuthTicket.TryUnwrap(out var ticketToCancel))
20  {
21  ticketToCancel.Cancel();
22  }
23  currentSteamHostAuthTicket = Option.None;
24 
25  var netIdentity = remoteHostEndpoint switch
26  {
27  LidgrenEndpoint { Address: LidgrenAddress { NetAddress: var ipAddr }, Port: var ipPort }
28  => (Steamworks.Data.NetIdentity)Steamworks.Data.NetAddress.From(ipAddr, (ushort)ipPort),
29  SteamP2PEndpoint { SteamId: var steamId }
30  => (Steamworks.Data.NetIdentity)(Steamworks.SteamId)steamId.Value,
31  _
32  => throw new ArgumentOutOfRangeException(nameof(remoteHostEndpoint))
33  };
34  var newTicket = Steamworks.SteamUser.GetAuthSessionTicket(netIdentity);
35 
36  currentSteamHostAuthTicket = newTicket != null
37  ? Option.Some(newTicket)
38  : Option.None;
39 
40  return currentSteamHostAuthTicket;
41  }
42  #endregion Auth ticket for Steam host
43 
44  #region Auth ticket for EOS host
45  private const string EosHostAuthIdentity = "BarotraumaRemotePlayerAuth";
46 
47  private static Option<Steamworks.AuthTicketForWebApi> currentEosHostAuthTicket = Option.None;
48  public static async Task<Option<Steamworks.AuthTicketForWebApi>> GetAuthTicketForEosHostAuth()
49  {
50  if (!IsInitialized)
51  {
52  return Option.None;
53  }
54 
55  if (currentEosHostAuthTicket.TryUnwrap(out var ticketToCancel))
56  {
57  ticketToCancel.Cancel();
58  }
59  currentEosHostAuthTicket = Option.None;
60 
61  var newTicket = await Steamworks.SteamUser.GetAuthTicketForWebApi(identity: EosHostAuthIdentity);
62 
63  currentEosHostAuthTicket = newTicket != null
64  ? Option.Some(newTicket)
65  : Option.None;
66 
67  return currentEosHostAuthTicket;
68  }
69  #endregion Auth ticket for EOS host
70 
71  #region Auth ticket for GameAnalytics consent server
72  private const string GameAnalyticsConsentIdentity = "BarotraumaGameAnalyticsConsent";
73 
74  private static Option<Steamworks.AuthTicketForWebApi> currentGameAnalyticsConsentTicket = Option.None;
75  public static async Task<Option<Steamworks.AuthTicketForWebApi>> GetAuthTicketForGameAnalyticsConsent()
76  {
77  if (!IsInitialized)
78  {
79  return Option.None;
80  }
81 
82  if (currentGameAnalyticsConsentTicket.TryUnwrap(out var ticketToCancel))
83  {
84  ticketToCancel.Cancel();
85  }
86  currentGameAnalyticsConsentTicket = Option.None;
87 
88  var newTicket = await Steamworks.SteamUser.GetAuthTicketForWebApi(identity: GameAnalyticsConsentIdentity);
89 
90  currentGameAnalyticsConsentTicket = newTicket != null
91  ? Option.Some(newTicket)
92  : Option.None;
93 
94  return currentGameAnalyticsConsentTicket;
95  }
96  #endregion Auth ticket for GameAnalytics consent server
97  }
98 }