summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Decompile/SteamManager.cs
blob: fb2f9047df3a390ae83a8c6e17cbab4a6a53e4ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Text;
using AOT;
using Steamworks;
using UnityEngine;
using UnityEngine.Events;

[DisallowMultipleComponent]
public class SteamManager : MonoBehaviour
{
	public struct LeaderboardEntry
	{
		public string username;

		public int score;
	}

	protected static bool s_EverInitialized = false;

	protected static SteamManager s_instance;

	protected bool m_bInitialized;

	protected SteamAPIWarningMessageHook_t m_SteamAPIWarningMessageHook;

	private static CallResult<LeaderboardFindResult_t> _findLeaderboardCallback = new CallResult<LeaderboardFindResult_t>();

	public static int scoreToUpload = -1;

	private static CallResult<LeaderboardScoresDownloaded_t> _downloadedScoresCallback = new CallResult<LeaderboardScoresDownloaded_t>();

	public List<LeaderboardEntry> lastDownloadedLeaderboardEntires = new List<LeaderboardEntry>();

	public UnityEvent OnLeaderboardDownloadCallbackComplete = new UnityEvent();

	public static SteamManager Instance
	{
		get
		{
			if (s_instance == null)
			{
				return new GameObject("SteamManager").AddComponent<SteamManager>();
			}
			return s_instance;
		}
	}

	public static bool Initialized => Instance.m_bInitialized;

	[MonoPInvokeCallback(typeof(SteamAPIWarningMessageHook_t))]
	protected static void SteamAPIDebugTextHook(int nSeverity, StringBuilder pchDebugText)
	{
		Debug.LogWarning(pchDebugText);
	}

	[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
	private static void InitOnPlayMode()
	{
		s_EverInitialized = false;
		s_instance = null;
	}

	protected virtual void Awake()
	{
		if (s_instance != null)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		s_instance = this;
		scoreToUpload = -1;
		if (s_EverInitialized)
		{
			throw new Exception("Tried to Initialize the SteamAPI twice in one session!");
		}
		UnityEngine.Object.DontDestroyOnLoad(base.transform.root.gameObject);
		if (!Packsize.Test())
		{
			Debug.LogError("[Steamworks.NET] Packsize Test returned false, the wrong version of Steamworks.NET is being run in this platform.", this);
		}
		if (!DllCheck.Test())
		{
			Debug.LogError("[Steamworks.NET] DllCheck Test returned false, One or more of the Steamworks binaries seems to be the wrong version.", this);
		}
		try
		{
			if (SteamAPI.RestartAppIfNecessary(AppId_t.Invalid))
			{
				Debug.Log("[Steamworks.NET] Shutting down because RestartAppIfNecessary returned true. Steam will restart the application.");
				Application.Quit();
				return;
			}
		}
		catch (DllNotFoundException ex)
		{
			Debug.Log("[Steamworks.NET] Could not load [lib]steam_api.dll/so/dylib. It's likely not in the correct location. Refer to the README for more details.\n" + ex, this);
			Application.Quit();
			return;
		}
		m_bInitialized = SteamAPI.Init();
		if (!m_bInitialized)
		{
			Debug.Log("[Steamworks.NET] SteamAPI_Init() failed. This is likely the case because the Steam client is not running.", this);
		}
		else
		{
			s_EverInitialized = true;
		}
	}

	protected virtual void OnEnable()
	{
		if (s_instance == null)
		{
			s_instance = this;
		}
		if (m_bInitialized && m_SteamAPIWarningMessageHook == null)
		{
			m_SteamAPIWarningMessageHook = SteamAPIDebugTextHook;
			SteamClient.SetWarningMessageHook(m_SteamAPIWarningMessageHook);
		}
	}

	protected virtual void OnDestroy()
	{
		if (!(s_instance != this))
		{
			s_instance = null;
			if (m_bInitialized)
			{
				SteamAPI.Shutdown();
			}
		}
	}

	protected virtual void Update()
	{
		if (m_bInitialized)
		{
			SteamAPI.RunCallbacks();
		}
	}

	public void UploadHighscore(int _score, string _leaderboardName)
	{
		if (Initialized)
		{
			if (scoreToUpload != -1)
			{
				Debug.LogError("You can't upload multiple scores symultaneously with the current system.");
				return;
			}
			scoreToUpload = _score;
			SteamAPICall_t hAPICall = SteamUserStats.FindOrCreateLeaderboard(_leaderboardName, ELeaderboardSortMethod.k_ELeaderboardSortMethodDescending, ELeaderboardDisplayType.k_ELeaderboardDisplayTypeNumeric);
			_findLeaderboardCallback.Set(hAPICall, OnFindLeaderboardForUploadingScore);
		}
	}

	public void OnFindLeaderboardForUploadingScore(LeaderboardFindResult_t _callback, bool _failure)
	{
		if (_failure)
		{
			scoreToUpload = -1;
			return;
		}
		SteamUserStats.UploadLeaderboardScore(_callback.m_hSteamLeaderboard, ELeaderboardUploadScoreMethod.k_ELeaderboardUploadScoreMethodKeepBest, scoreToUpload, new int[0], 0);
		scoreToUpload = -1;
	}

	public void DownloadFriendsHighscores(string _leaderboardName)
	{
		lastDownloadedLeaderboardEntires.Clear();
		if (Initialized)
		{
			SteamAPICall_t hAPICall = SteamUserStats.FindLeaderboard(_leaderboardName);
			_findLeaderboardCallback.Set(hAPICall, OnFindLeaderboardFodDownloadingFriendsScore);
		}
	}

	public void OnFindLeaderboardFodDownloadingFriendsScore(LeaderboardFindResult_t _callback, bool _failure)
	{
		if (!_failure)
		{
			SteamAPICall_t hAPICall = SteamUserStats.DownloadLeaderboardEntries(_callback.m_hSteamLeaderboard, ELeaderboardDataRequest.k_ELeaderboardDataRequestFriends, 1, 100);
			_downloadedScoresCallback.Set(hAPICall, OnDownloadedScores);
		}
	}

	public void OnDownloadedScores(LeaderboardScoresDownloaded_t _callback, bool _failure)
	{
		if (_failure)
		{
			OnLeaderboardDownloadCallbackComplete.Invoke();
			return;
		}
		SteamUserStats.GetLeaderboardName(_callback.m_hSteamLeaderboard);
		int[] pDetails = new int[0];
		for (int i = 0; i < _callback.m_cEntryCount; i++)
		{
			SteamUserStats.GetDownloadedLeaderboardEntry(_callback.m_hSteamLeaderboardEntries, i, out var pLeaderboardEntry, pDetails, 0);
			LeaderboardEntry item = default(LeaderboardEntry);
			item.username = SteamFriends.GetFriendPersonaName(pLeaderboardEntry.m_steamIDUser);
			item.score = pLeaderboardEntry.m_nScore;
			lastDownloadedLeaderboardEntires.Add(item);
		}
		OnLeaderboardDownloadCallbackComplete.Invoke();
	}
}