2 using System.Collections.Generic;
3 using System.Collections.Immutable;
5 using System.Runtime.InteropServices;
11 static partial class SteamManager
13 public const int STEAMP2P_OWNER_PORT = 30000;
15 public const uint AppID = 602960;
17 private static readonly Dictionary<string, int> tagCommonness =
new Dictionary<string, int>()
25 {
"total conversion", 5 },
27 {
"item assembly", 5 },
31 public static bool IsInitialized => IsInitializedProjectSpecific;
33 private static readonly List<string> popularTags =
new List<string>();
34 public static IEnumerable<string> PopularTags
38 if (!IsInitialized) {
return Enumerable.Empty<
string>(); }
43 public static bool SteamworksLibExists
44 => RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
45 ? File.Exists(
"steam_api64.dll")
46 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
47 ? File.Exists(
"libsteam_api64.dylib")
48 : RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
49 ? File.Exists(
"libsteam_api64.so")
52 public static void Initialize()
54 InitializeProjectSpecific();
57 public static Option<SteamId> GetSteamId()
59 if (!IsInitialized || !Steamworks.SteamClient.IsValid)
61 return Option<SteamId>.None();
64 return Option<SteamId>.Some(
new SteamId(Steamworks.SteamClient.SteamId));
67 public static Option<SteamId> GetOwnerSteamId()
69 if (!IsInitialized || !Steamworks.SteamClient.IsValid)
71 return Option<SteamId>.None();
74 return Option<SteamId>.Some(
new SteamId(Steamworks.SteamClient.SteamId));
77 public static bool IsFamilyShared()
79 if (!IsInitialized || !Steamworks.SteamClient.IsValid) {
return false; }
81 return Steamworks.SteamApps.IsSubscribedFromFamilySharing;
84 public static bool IsFreeWeekend()
86 if (!IsInitialized || !Steamworks.SteamClient.IsValid) {
return false; }
88 return Steamworks.SteamApps.IsSubscribedFromFreeWeekend;
91 public static string GetUsername()
93 if (!IsInitialized || !Steamworks.SteamClient.IsValid)
97 return Steamworks.SteamClient.Name;
100 public static uint GetNumSubscribedItems()
102 if (!IsInitialized || !Steamworks.SteamClient.IsValid)
106 return Steamworks.SteamUGC.NumSubscribedItems;
109 public static bool UnlockAchievement(
string achievementIdentifier) =>
110 UnlockAchievement(achievementIdentifier.ToIdentifier());
112 public static bool UnlockAchievement(Identifier achievementIdentifier)
114 if (!IsInitialized || !Steamworks.SteamClient.IsValid)
119 DebugConsole.Log(
"Unlocked achievement \"" + achievementIdentifier +
"\"");
121 var achievements = Steamworks.SteamUserStats.Achievements.ToList();
122 int achIndex = achievements.FindIndex(ach => ach.Identifier == achievementIdentifier);
123 bool unlocked = achIndex >= 0 ? achievements[achIndex].Trigger() :
false;
130 DebugConsole.Log($
"Failed to unlock achievement \"{achievementIdentifier}\".");
141 public static void IncrementStats(params (AchievementStat Identifier,
float Increment)[] stats)
142 => Array.ForEach(stats,
static s
143 => IncrementStat(s.Identifier, s.Increment, storeStats:
false));
145 public static bool IncrementStat(AchievementStat statName,
int increment,
bool storeStats =
true)
147 if (!IsInitialized || !Steamworks.SteamClient.IsValid) {
return false; }
148 DebugConsole.Log($
"Incremented stat \"{statName}\" by " + increment);
149 bool success = Steamworks.SteamUserStats.AddStatInt(statName.ToIdentifier().Value.ToLowerInvariant(), increment);
152 DebugConsole.Log(
"Failed to increment stat \"" + statName +
"\".");
161 public static bool IncrementStat(AchievementStat statName,
float increment,
bool storeStats =
true)
163 if (!IsInitialized || !Steamworks.SteamClient.IsValid) {
return false; }
164 DebugConsole.Log($
"Incremented stat \"{statName}\" by " + increment);
165 bool success = Steamworks.SteamUserStats.AddStatFloat(statName.ToIdentifier().Value.ToLowerInvariant(), increment);
168 DebugConsole.Log(
"Failed to increment stat \"" + statName +
"\".");
177 public static int GetStatInt(AchievementStat stat)
179 if (!IsInitialized || !Steamworks.SteamClient.IsValid) {
return 0; }
180 return Steamworks.SteamUserStats.GetStatInt(stat.ToString().ToLowerInvariant());
183 public static float GetStatFloat(AchievementStat stat)
185 if (!IsInitialized || !Steamworks.SteamClient.IsValid) {
return 0f; }
186 return Steamworks.SteamUserStats.GetStatFloat(stat.ToString().ToLowerInvariant());
189 public static ImmutableDictionary<AchievementStat, float> GetAllStats()
191 if (!IsInitialized || !Steamworks.SteamClient.IsValid) {
return ImmutableDictionary<AchievementStat, float>.Empty; }
193 var builder = ImmutableDictionary.CreateBuilder<AchievementStat,
float>();
195 foreach (AchievementStat stat
in AchievementStatExtension.SteamStats)
197 if (stat.IsFloatStat())
199 builder.Add(stat, GetStatFloat(stat));
203 builder.Add(stat, GetStatInt(stat));
207 return builder.ToImmutable();
210 public static bool StoreStats()
212 if (!IsInitialized || !Steamworks.SteamClient.IsValid) {
return false; }
213 DebugConsole.Log(
"Storing Steam stats...");
214 bool success = Steamworks.SteamUserStats.StoreStats();
217 DebugConsole.Log(
"Failed to store Steam stats.");
222 public static bool TryGetUnlockedAchievements(out List<Steamworks.Data.Achievement> achievements)
224 if (!IsInitialized || !Steamworks.SteamClient.IsValid)
229 achievements = Steamworks.SteamUserStats.Achievements.Where(a => a.State).ToList();
233 public static void Update(
float deltaTime)
237 AchievementManager.Update(deltaTime);
239 if (!IsInitialized) {
return; }
241 if (Steamworks.SteamClient.IsValid) { Steamworks.SteamClient.RunCallbacks(); }
242 if (Steamworks.SteamServer.IsValid) { Steamworks.SteamServer.RunCallbacks(); }
245 public static void ShutDown()
247 if (!IsInitialized) {
return; }
249 if (Steamworks.SteamClient.IsValid) { Steamworks.SteamClient.Shutdown(); }
250 if (Steamworks.SteamServer.IsValid) { Steamworks.SteamServer.Shutdown(); }
253 public static IEnumerable<ulong> WorkshopUrlsToIds(IEnumerable<string> urls)
255 return urls.Select((u) =>
257 if (
string.IsNullOrEmpty(u))
263 return GetWorkshopItemIDFromUrl(u);
268 public static ulong GetWorkshopItemIDFromUrl(
string url)
272 Uri uri =
new Uri(url);
273 string idStr = HttpUtility.ParseQueryString(uri.Query)[
"id".ToIdentifier()];
274 if (ulong.TryParse(idStr, out ulong
id))
281 DebugConsole.ThrowError(
"Failed to get Workshop item ID from the url \"" + url +
"\"!", e);