6 using System.Collections.Generic;
8 using System.Threading.Tasks;
10 using Color = Microsoft.Xna.Framework.Color;
14 static partial class SteamManager
16 private static readonly List<Identifier> initializationErrors =
new List<Identifier>();
17 public static IReadOnlyList<Identifier> InitializationErrors => initializationErrors;
19 private static bool IsInitializedProjectSpecific
20 => Steamworks.SteamClient.IsValid && Steamworks.SteamClient.IsLoggedOn;
22 private static void InitializeProjectSpecific()
24 if (IsInitialized) {
return; }
28 Steamworks.SteamClient.Init(AppID,
false);
32 DebugConsole.NewMessage(
33 $
"Logged in as {GetUsername()} (SteamID {(GetSteamId().TryUnwrap(out var steamId) ? steamId.ToString() : "[NULL]
")})");
37 foreach (KeyValuePair<string, int> commonness
in tagCommonness)
39 popularTags.Insert(i, commonness.Key);
44 Steamworks.SteamNetworkingUtils.OnDebugOutput += LogSteamworksNetworking;
47 Steamworks.SteamFriends.ListenForFriendsMessages =
true;
49 catch (DllNotFoundException)
51 initializationErrors.Add(
"SteamDllNotFound".ToIdentifier());
55 DebugConsole.ThrowError(
"SteamManager initialization threw an exception", e);
56 initializationErrors.Add(
"SteamClientInitFailed".ToIdentifier());
63 if (Steamworks.SteamClient.IsValid) { Steamworks.SteamClient.Shutdown(); }
67 if (GameSettings.CurrentConfig.VerboseLogging) DebugConsole.ThrowError(
"Disposing Steam client failed.", e);
75 Steamworks.SteamUGC.OnItemInstalled += (appId, itemId) => Workshop.OnItemDownloadComplete(itemId);
79 Steamworks.SteamUGC.OnDownloadItemResult += (result, id) =>
81 if (result == Steamworks.Result.OK)
83 Workshop.OnItemDownloadComplete(
id);
91 public static bool NetworkingDebugLog {
get;
private set; } =
false;
93 private static void LogSteamworksNetworking(Steamworks.NetDebugOutput nType,
string pszMsg)
95 DebugConsole.NewMessage($
"({nType}) {pszMsg}", Color.Orange);
98 public static void SetSteamworksNetworkingDebugLog(
bool enabled)
100 if (enabled == NetworkingDebugLog) {
return; }
103 Steamworks.SteamNetworkingUtils.DebugLevel = Steamworks.NetDebugOutput.Everything;
107 Steamworks.SteamNetworkingUtils.DebugLevel = Steamworks.NetDebugOutput.None;
109 NetworkingDebugLog = enabled;
112 public static async Task InitRelayNetworkAccess()
114 if (!IsInitialized) {
return; }
117 Steamworks.SteamNetworkingUtils.InitRelayNetworkAccess();
120 var status = Steamworks.SteamNetworkingUtils.Status;
121 while (status.Avail != Steamworks.SteamNetworkingAvailability.Current)
123 if (status.Avail == Steamworks.SteamNetworkingAvailability.CannotTry ||
124 status.Avail == Steamworks.SteamNetworkingAvailability.Previously ||
125 status.Avail == Steamworks.SteamNetworkingAvailability.Failed)
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}");
134 await Task.Delay(25);
135 status = Steamworks.SteamNetworkingUtils.Status;
141 public static bool OverlayCustomUrl(
string url)
143 if (!IsInitialized || !Steamworks.SteamClient.IsValid)
148 Steamworks.SteamFriends.OpenWebOverlay(url);