SteamworksManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. namespace KairoEngine.SteamIntegration
  7. {
  8. [HideMonoScript]
  9. public class SteamworksManager : MonoBehaviour
  10. {
  11. [BoxGroup("Configurations"), LabelText("Steamworks App ID")] public uint appId = 0;
  12. [BoxGroup("Configurations"), LabelText("Use Stats")] public bool useStats = true;
  13. [BoxGroup("Configurations"), LabelText("Use Achievements"), ShowIf("@useStats")] public bool useAchievements = true;
  14. [BoxGroup("Configurations"), ShowInInspector] public static bool showDebug = false;
  15. [FoldoutGroup("Stats"), InlineProperty, HideLabel, ShowIf("@useStats")]
  16. public SteamworksStats stats = new SteamworksStats();
  17. [FoldoutGroup("Achievements"), InlineProperty, HideLabel, ShowIf("@useAchievements && useStats")]
  18. public SteamworksAchievements achievements = new SteamworksAchievements();
  19. [HideInInspector] public SteamworksStatLibrary steamStatsLibray;
  20. public static bool hasInitialized = false;
  21. private string playerName;
  22. private void OnDestroy() => StopSteamworks();
  23. private void OnApplicationQuit() => StopSteamworks();
  24. private void OnDisable() => StopSteamworks();
  25. private void Start()
  26. {
  27. if(appId == 0)
  28. {
  29. if(showDebug) Debug.LogWarning("Missing AppId for Steamworks manager. Please configure Steam Module in GameConfig.");
  30. return;
  31. }
  32. try
  33. {
  34. Steamworks.SteamClient.Init( appId, true );
  35. hasInitialized = true;
  36. RefreshStats();
  37. playerName = Steamworks.SteamClient.Name;
  38. var playerSteamId = Steamworks.SteamClient.SteamId;
  39. if(showDebug) Debug.Log($"{playerName} ({playerSteamId})");
  40. Timer.ExecuteRealTimeNotDisposable(1000, () => {
  41. if(useStats) stats.Start(steamStatsLibray, showDebug);
  42. if(useAchievements) achievements.Start();
  43. });
  44. //PrintFriendsList();
  45. }
  46. catch ( System.Exception e )
  47. {
  48. Debug.LogError(e.ToString());
  49. }
  50. }
  51. void Update()
  52. {
  53. Steamworks.SteamClient.RunCallbacks();
  54. }
  55. public void StopSteamworks()
  56. {
  57. stats.Stop();
  58. achievements.Stop();
  59. Steamworks.SteamClient.Shutdown();
  60. hasInitialized = false;
  61. }
  62. public static bool HasInitialized()
  63. {
  64. if(hasInitialized == false)
  65. {
  66. Debug.LogWarning("Not connected to Steam");
  67. return false;
  68. }
  69. return true;
  70. }
  71. public static void RefreshStats()
  72. {
  73. if(!HasInitialized()) return;
  74. bool received = Steamworks.SteamUserStats.RequestCurrentStats();
  75. if(showDebug)
  76. {
  77. if(received) Debug.Log("Received current stats from Steam");
  78. else Debug.LogWarning("Error requesting current Steam stats");
  79. }
  80. }
  81. // [ButtonGroup("Buttons"), Button("Print Friends List")]
  82. // public void PrintFriendsList()
  83. // {
  84. // if(!HasInitialized()) return;
  85. // List<string> friends = new List<string>();
  86. // string friendsText = "";
  87. // foreach ( var player in Steamworks.SteamFriends.GetFriends() )
  88. // {
  89. // friends.Add(player.Name);
  90. // }
  91. // for (int i = 0; i < friends.Count; i++)
  92. // {
  93. // if(i < friends.Count - 1) friendsText += $"{friends[i]}, ";
  94. // else friendsText += $"{friends[i]}";
  95. // }
  96. // Debug.Log($"Friends of {playerName}: {friendsText}");
  97. // }
  98. [Button("Reset Player Stats & Achievements"), GUIColor("@Color.red"), PropertyTooltip("The password is RESETDATA")]
  99. public void ResetSteamStats(string password, bool resetAchievements = true)
  100. {
  101. if(password != "RESETDATA")
  102. {
  103. Debug.LogWarning("Incorrect password");
  104. return;
  105. }
  106. if(!SteamworksManager.HasInitialized()) return;
  107. Steamworks.SteamUserStats.ResetAll( resetAchievements ); // true = wipe achivements too
  108. Steamworks.SteamUserStats.StoreStats();
  109. SteamworksManager.RefreshStats();
  110. Debug.Log($"Steamworks data for player {playerName} has been reset!");
  111. }
  112. public void ShowDebug(bool debug) => showDebug = debug;
  113. }
  114. }