Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Steam/SteamManager.cs
2 using Barotrauma.IO;
4 using RestSharp;
5 using System;
6 using System.Collections.Generic;
7 using System.Linq;
8 using System.Threading.Tasks;
9 using System.Xml.Linq;
10 using Color = Microsoft.Xna.Framework.Color;
11 
12 namespace Barotrauma.Steam
13 {
14  static partial class SteamManager
15  {
16  private static readonly List<Identifier> initializationErrors = new List<Identifier>();
17  public static IReadOnlyList<Identifier> InitializationErrors => initializationErrors;
18 
19  private static bool IsInitializedProjectSpecific
20  => Steamworks.SteamClient.IsValid && Steamworks.SteamClient.IsLoggedOn;
21 
22  private static void InitializeProjectSpecific()
23  {
24  if (IsInitialized) { return; }
25 
26  try
27  {
28  Steamworks.SteamClient.Init(AppID, false);
29 
30  if (IsInitialized)
31  {
32  DebugConsole.NewMessage(
33  $"Logged in as {GetUsername()} (SteamID {(GetSteamId().TryUnwrap(out var steamId) ? steamId.ToString() : "[NULL]")})");
34 
35  popularTags.Clear();
36  int i = 0;
37  foreach (KeyValuePair<string, int> commonness in tagCommonness)
38  {
39  popularTags.Insert(i, commonness.Key);
40  i++;
41  }
42  }
43 
44  Steamworks.SteamNetworkingUtils.OnDebugOutput += LogSteamworksNetworking;
45 
46  // Needed to detect invites for social overlay
47  Steamworks.SteamFriends.ListenForFriendsMessages = true;
48  }
49  catch (DllNotFoundException)
50  {
51  initializationErrors.Add("SteamDllNotFound".ToIdentifier());
52  }
53  catch (Exception e)
54  {
55  DebugConsole.ThrowError("SteamManager initialization threw an exception", e);
56  initializationErrors.Add("SteamClientInitFailed".ToIdentifier());
57  }
58 
59  if (!IsInitialized)
60  {
61  try
62  {
63  if (Steamworks.SteamClient.IsValid) { Steamworks.SteamClient.Shutdown(); }
64  }
65  catch (Exception e)
66  {
67  if (GameSettings.CurrentConfig.VerboseLogging) DebugConsole.ThrowError("Disposing Steam client failed.", e);
68  }
69  }
70  else
71  {
72  //Steamworks is completely insane so the following needs comments:
73 
74  //This callback seems to take place when the item in question has not been downloaded recently
75  Steamworks.SteamUGC.OnItemInstalled += (appId, itemId) => Workshop.OnItemDownloadComplete(itemId);
76 
77  //This callback seems to take place when the item has been downloaded recently and an update
78  //or a redownload has taken place
79  Steamworks.SteamUGC.OnDownloadItemResult += (result, id) =>
80  {
81  if (result == Steamworks.Result.OK)
82  {
83  Workshop.OnItemDownloadComplete(id);
84  }
85  };
86 
87  //Maybe I'm completely wrong! All I know is that we need to handle both!
88  }
89  }
90 
91  public static bool NetworkingDebugLog { get; private set; } = false;
92 
93  private static void LogSteamworksNetworking(Steamworks.NetDebugOutput nType, string pszMsg)
94  {
95  DebugConsole.NewMessage($"({nType}) {pszMsg}", Color.Orange);
96  }
97 
98  public static void SetSteamworksNetworkingDebugLog(bool enabled)
99  {
100  if (enabled == NetworkingDebugLog) { return; }
101  if (enabled)
102  {
103  Steamworks.SteamNetworkingUtils.DebugLevel = Steamworks.NetDebugOutput.Everything;
104  }
105  else
106  {
107  Steamworks.SteamNetworkingUtils.DebugLevel = Steamworks.NetDebugOutput.None;
108  }
109  NetworkingDebugLog = enabled;
110  }
111 
112  public static async Task InitRelayNetworkAccess()
113  {
114  if (!IsInitialized) { return; }
115 
116  await Task.Yield();
117  Steamworks.SteamNetworkingUtils.InitRelayNetworkAccess();
118 
119  //SetSteamworksNetworkingDebugLog(true);
120  var status = Steamworks.SteamNetworkingUtils.Status;
121  while (status.Avail != Steamworks.SteamNetworkingAvailability.Current)
122  {
123  if (status.Avail == Steamworks.SteamNetworkingAvailability.CannotTry ||
124  status.Avail == Steamworks.SteamNetworkingAvailability.Previously ||
125  status.Avail == Steamworks.SteamNetworkingAvailability.Failed)
126  {
127  DebugConsole.ThrowError($"Failed to initialize Steamworks network relay: " +
128  $"{Steamworks.SteamNetworkingUtils.Status.Avail}, " +
129  $"{Steamworks.SteamNetworkingUtils.Status.AvailNetConfig}, " +
130  $"{Steamworks.SteamNetworkingUtils.Status.Avail}, " +
131  $"{Steamworks.SteamNetworkingUtils.Status.Msg}");
132  break;
133  }
134  await Task.Delay(25);
135  status = Steamworks.SteamNetworkingUtils.Status;
136  }
137  //SetSteamworksNetworkingDebugLog(false);
138  }
139 
140 
141  public static bool OverlayCustomUrl(string url)
142  {
143  if (!IsInitialized || !Steamworks.SteamClient.IsValid)
144  {
145  return false;
146  }
147 
148  Steamworks.SteamFriends.OpenWebOverlay(url);
149  return true;
150  }
151  }
152 }