-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynchronisedMetadata.cs
267 lines (251 loc) · 10.2 KB
/
SynchronisedMetadata.cs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using Steamworks;
using System.Globalization;
using UnityEngine;
namespace ContentWarningShop
{
/// <summary>
/// Handles synchronisation of a Steam Lobby metadata entry. When a lobby metadata key has been updated,
/// <see cref="Value"/> will automatically be updated with the new value.
/// </summary>
/// <remarks>
/// Setting lobby metadata is only allowed if the current player is the lobby's owner.
/// Use <see cref="CanSet"/> to check if assigning a new value is possible from this client.
/// </remarks>
/// <typeparam name="TValue"></typeparam>
public class SynchronisedMetadata<TValue> : IDisposable where TValue : IConvertible, IComparable
{
private bool isDisposed;
/// <summary>
/// Get if the player is the host of the current lobby. Will be <see langword="false"/> if <see cref="InLobby"/> is.
/// </summary>
public bool IsHost => SteamLobbyMetadataHandler.IsHost;
/// <summary>
/// Get if the player is in a Steam Lobby.
/// </summary>
public bool InLobby => SteamLobbyMetadataHandler.InLobby;
/// <summary>
/// The Steam Lobby Metadata key this instance is bound to.
/// </summary>
public string Key { get; protected set; } = string.Empty;
private TValue _value = default;
/// <summary>
/// The current value of this entry.
/// </summary>
/// <remarks>
/// Check <see cref="IsSynced"/> to see if the value is being actively synced with a lobby.
/// </remarks>
public TValue Value
{
get => _value;
}
/// <summary>
/// Checks if the entry is being actively synced with a lobby. Is <see langword="false"/> if the player is not currently in a lobby.
/// </summary>
/// <remarks>
/// To check if this instance will send and receive updates from the SteamAPI, see <see cref="IsConnected"/> instead.
/// </remarks>
public bool IsSynced
{
get
{
if (IsConnected == false)
{
return false;
}
return SteamLobbyMetadataHandler.InLobby;
}
}
/// <summary>
/// Whether this instance is connected to the Steamworks API and will receive events.
/// Once <see cref="Dispose()"/>was called, the instance will no longer update its value, and a new instance must be created to reconnect.
/// </summary>
public bool IsConnected => isDisposed == false;
/// <summary>
/// Event raised when <see cref="Value"/> is updated, either locally or remotely.
/// </summary>
public event Action<TValue>? ValueChanged;
/// <summary>
/// Event raised when the local player created a new Steam Lobby (is hosting a new game).
/// </summary>
/// <remarks>
/// This event can be used to update <see cref="Value"/> when the player creates a new lobby. Since instances retain their current values, if the
/// player leaves a game and hosts a new lobby, their settings may not reflect their own, but rather the last lobby host's. Use this event to apply
/// the player's own configurations for the lobby they just created.
/// </remarks>
public event Action? LobbyHosted;
/// <param name="key">The Steam Lobby Metadata key this instance will synchronise with.</param>
/// <param name="value">
/// The initial <see cref="Value"/> that should be used before the first sync.
/// If the instance is created while in a lobby, it will first attempt to fetch the current lobby value.
/// If the key doesn't exist yet, it will attempt to create it with this value (or the first time a lobby is joined).
/// </param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public SynchronisedMetadata(string key, TValue value)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
if (key.Length > Steamworks.Constants.k_nMaxLobbyKeyLength)
{
throw new ArgumentException($"Key length must not exceed {nameof(Steamworks.Constants.k_nMaxLobbyKeyLength)} ({Steamworks.Constants.k_nMaxLobbyKeyLength}); was {key.Length}", nameof(key));
}
SteamLobbyMetadataHandler.OnLobbyCreated += OnLobbyCreated;
SteamLobbyMetadataHandler.OnLobbyDataUpdate += OnLobbyUpdate;
SteamLobbyMetadataHandler.OnLobbyJoined += OnLobbyJoin;
Key = key;
_value = value;
// If we are creating the instance late, check if the key already has a registered value or not. If not, create it, if yes, fetch it.
if (SteamLobbyMetadataHandler.InLobby)
{
var valStr = SteamMatchmaking.GetLobbyData(SteamLobbyMetadataHandler.CurrentLobby, Key);
if (string.IsNullOrEmpty(valStr))
{
SetValue(_value);
}
else
{
FetchValue();
}
}
Debug.Log($"{nameof(SynchronisedMetadata<TValue>)} instance bound to lobby key: {Key}");
}
/// <param name="key">The Steam Lobby Metadata key this instance will synchronise with.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public SynchronisedMetadata(string key) : this(key, default)
{
}
/// <summary>
/// Checks if assigning a new value is possible from this client.
/// </summary>
/// <remarks>
/// If the client is not in a lobby, returns true, otherwise checks if the player is the lobby's host.
/// </remarks>
/// <returns></returns>
public bool CanSet()
{
return IsConnected == true && (InLobby == false || IsHost);
}
/// <summary>
/// Sets the entry to a new value.
/// </summary>
/// <remarks>
/// Setting a new value is only possible by the host of the lobby, or if the player in not in a lobby.
/// </remarks>
/// <param name="value"></param>
/// <returns>
/// <see langword="true"/> if assigning the new value from this client was possible, <see langword="false"/> if not.
/// </returns>
public bool SetValue(TValue value)
{
if (isDisposed)
{
throw new ObjectDisposedException();
}
if (InLobby == false)
{
_value = value;
ValueChanged?.Invoke(_value);
return true;
}
if (IsHost)
{
if (SteamMatchmaking.SetLobbyData(SteamLobbyMetadataHandler.CurrentLobby, Key, ValToString(value)) == false)
{
Debug.LogError($"Could not set {Key} to {_value}, despite being the lobby host.");
return false;
}
}
return true;
}
/// <summary>
/// Disconnects this instance from Steamworks API events, preventing it from synchronising.
/// Equivalent to <see cref="Dispose()"/>.
/// </summary>
[Obsolete($"Use Dispose instead.")]
public void Disconnect()
{
Dispose();
}
private void OnLobbyJoin()
{
if (IsHost)
{
if (SteamMatchmaking.SetLobbyData(SteamLobbyMetadataHandler.CurrentLobby, Key, ValToString(_value)))
{
Debug.Log($"Set join lobby metadata {Key} to {_value} as host.");
}
else
{
Debug.LogError($"Could not set {Key} to {_value}, despite being the lobby host.");
}
}
else
{
FetchValue();
}
}
private void OnLobbyUpdate()
{
FetchValue();
}
private void OnLobbyCreated()
{
LobbyHosted?.Invoke();
}
/// <summary>
/// Fetches the most up-to-date value from the lobby if possible.
/// </summary>
protected void FetchValue()
{
if (isDisposed)
{
throw new ObjectDisposedException();
}
if (SteamLobbyMetadataHandler.InLobby == false)
{
return;
}
var valStr = SteamMatchmaking.GetLobbyData(SteamLobbyMetadataHandler.CurrentLobby, Key);
if (string.IsNullOrEmpty(valStr))
{
return;
}
var val = StringToVal<TValue>(valStr);
if (val != null && val.Equals(_value) == false)
{
Debug.Log($"Synced from lobby metadata {Key}: {_value} -> {val}");
_value = val;
ValueChanged?.Invoke(_value);
}
}
private static string ValToString(object value)
{
if (value == null)
{
return string.Empty;
}
return (string)Convert.ChangeType(value, typeof(string), CultureInfo.InvariantCulture);
}
private static TRes StringToVal<TRes>(string value)
{
return (TRes)Convert.ChangeType(value, typeof(TRes), CultureInfo.InvariantCulture);
}
/// <summary>
/// Disconnects this instance from Steamworks API events, preventing it from synchronising and rendering this instance useless.
/// </summary>
public void Dispose()
{
if (!isDisposed)
{
GC.SuppressFinalize(this);
SteamLobbyMetadataHandler.OnLobbyCreated -= OnLobbyCreated;
SteamLobbyMetadataHandler.OnLobbyDataUpdate -= OnLobbyUpdate;
SteamLobbyMetadataHandler.OnLobbyJoined -= OnLobbyJoin;
isDisposed = true;
}
}
}
}