-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRichPresence.cs
More file actions
852 lines (754 loc) · 31.9 KB
/
RichPresence.cs
File metadata and controls
852 lines (754 loc) · 31.9 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
using DiscordRPC.Exceptions;
using DiscordRPC.Helper;
using Newtonsoft.Json;
using System;
using System.Text;
namespace DiscordRPC
{
/// <summary>
/// The Rich Presence structure that will be sent and received by Discord. Use this class to build your presence and update it appropriately.
/// </summary>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[Serializable]
public class RichPresence
{
/// <summary>
/// The user's current <see cref="Party"/> status. For example, "Playing Solo" or "With Friends".
/// <para>Max 128 bytes</para>
/// </summary>
[JsonProperty("state", NullValueHandling = NullValueHandling.Ignore)]
public string State
{
get { return _state; }
set
{
if (!ValidateString(value, out _state, 128, Encoding.UTF8))
throw new StringOutOfRangeException("State", 0, 128);
}
}
private string _state;
/// <summary>
/// What the user is currently doing. For example, "Competitive - Total Mayhem".
/// <para>Max 128 bytes</para>
/// </summary>
[JsonProperty("details", NullValueHandling = NullValueHandling.Ignore)]
public string Details
{
get { return _details; }
set
{
if (!ValidateString(value, out _details, 128, Encoding.UTF8))
throw new StringOutOfRangeException(128);
}
}
private string _details;
/// <summary>
/// The time elapsed / remaining time data.
/// </summary>
[JsonProperty("timestamps", NullValueHandling = NullValueHandling.Ignore)]
public Timestamps Timestamps { get; set; }
/// <summary>
/// The names of the images to use and the tooltips to give those images.
/// </summary>
[JsonProperty("assets", NullValueHandling = NullValueHandling.Ignore)]
public Assets Assets { get; set; }
/// <summary>
/// The party the player is currently in. The <see cref="Party.ID"/> must be set for this to be included in the RichPresence update.
/// </summary>
[JsonProperty("party", NullValueHandling = NullValueHandling.Ignore)]
public Party Party { get; set; }
/// <summary>
/// The secrets used for Join / Spectate. Secrets are obfuscated data of your choosing. They could be match ids, player ids, lobby ids, etc. Make this object null if you do not wish too / unable too implement the Join / Request feature.
/// <para>To keep security on the up and up, Discord requires that you properly hash/encode/encrypt/put-a-padlock-on-and-swallow-the-key-but-wait-then-how-would-you-open-it your secrets.</para>
/// <para>Visit the <see href="https://discordapp.com/developers/docs/rich-presence/how-to#secrets">Rich Presence How-To</see> for more information.</para>
/// </summary>
[JsonProperty("secrets", NullValueHandling = NullValueHandling.Ignore)]
public Secrets Secrets { get; set; }
/// <summary>
/// Marks the <see cref="Secrets.MatchSecret"/> as a game session with a specific beginning and end. It was going to be used as a form of notification, but was replaced with the join feature. It may potentially have use in the future, but it currently has no use.
/// <para>
/// "TLDR it marks the matchSecret field as an instance, that is to say a context in game that’s not like a lobby state/not in game state. It was gonna he used for notify me, but we scrapped that for ask to join. We may put it to another use in the future. For now, don’t worry about it" - Mason (Discord API Server 14 / 03 / 2018)
/// </para>
/// </summary>
[JsonProperty("instance", NullValueHandling = NullValueHandling.Ignore)]
[Obsolete("This was going to be used, but was replaced by JoinSecret instead")]
private bool Instance { get; set; }
/// <summary>
/// Clones the presence into a new instance. Used for thread safe writing and reading. This function will ignore properties if they are in a invalid state.
/// </summary>
/// <returns></returns>
public RichPresence Clone()
{
return new RichPresence
{
State = this._state != null ? _state.Clone() as string : null,
Details = this._details != null ? _details.Clone() as string : null,
Secrets = !HasSecrets() ? null : new Secrets
{
//MatchSecret = this.Secrets.MatchSecret?.Clone() as string,
JoinSecret = this.Secrets.JoinSecret != null ? this.Secrets.JoinSecret.Clone() as string : null,
SpectateSecret = this.Secrets.SpectateSecret != null ? this.Secrets.SpectateSecret.Clone() as string : null
},
Timestamps = !HasTimestamps() ? null : new Timestamps
{
Start = this.Timestamps.Start,
End = this.Timestamps.End
},
Assets = !HasAssets() ? null : new Assets
{
LargeImageKey = this.Assets.LargeImageKey != null ? this.Assets.LargeImageKey.Clone() as string : null,
LargeImageText = this.Assets.LargeImageText != null ? this.Assets.LargeImageText.Clone() as string : null,
SmallImageKey = this.Assets.SmallImageKey != null ? this.Assets.SmallImageKey.Clone() as string : null,
SmallImageText = this.Assets.SmallImageText != null ? this.Assets.SmallImageText.Clone() as string : null
},
Party = !HasParty() ? null : new Party
{
ID = this.Party.ID,
Size = this.Party.Size,
Max = this.Party.Max
}
};
}
/// <summary>
/// Merges the passed presence with this one, taking into account the image key to image id annoyance.
/// </summary>
/// <param name="presence"></param>
internal void Merge(RichPresence presence)
{
this._state = presence._state;
this._details = presence._details;
this.Party = presence.Party;
this.Timestamps = presence.Timestamps;
this.Secrets = presence.Secrets;
//If they have assets, we should merge them
if (presence.HasAssets())
{
//Make sure we actually have assets too
if (!this.HasAssets())
{
//We dont, so we will just use theirs
this.Assets = presence.Assets;
}
else
{
//We do, so we better merge them!
this.Assets.Merge(presence.Assets);
}
}
else
{
//They dont have assets, so we will just set ours to null
this.Assets = null;
}
}
/// <summary>
/// Updates this presence with any values from the previous one
/// </summary>
/// <param name="presence"></param>
[System.Obsolete("No longer used and probably can be removed.")]
internal void Update(RichPresence presence)
{
if (presence == null) return;
this._state = presence._state ?? this._state;
this._details = presence._details ?? this._details;
if (presence.Party != null)
{
if (this.Party != null)
{
this.Party.ID = presence.Party.ID ?? this.Party.ID;
this.Party.Size = presence.Party.Size;
this.Party.Max = presence.Party.Max;
}
else
{
this.Party = presence.Party;
}
}
}
#region Has Checks
/// <summary>
/// Does the Rich Presence have valid timestamps?
/// </summary>
/// <returns></returns>
public bool HasTimestamps()
{
return this.Timestamps != null && (Timestamps.Start != null || Timestamps.End != null);
}
/// <summary>
/// Does the Rich Presence have valid assets?
/// </summary>
/// <returns></returns>
public bool HasAssets()
{
return this.Assets != null;
}
/// <summary>
/// Does the Rich Presence have a valid party?
/// </summary>
/// <returns></returns>
public bool HasParty()
{
return this.Party != null && this.Party.ID != null;
}
/// <summary>
/// Does the Rich Presence have valid secrets?
/// </summary>
/// <returns></returns>
public bool HasSecrets()
{
return Secrets != null && (Secrets.JoinSecret != null || Secrets.SpectateSecret != null);
}
#endregion Has Checks
#region Builder
/// <summary>
/// Sets the state of the Rich Presence. See also <seealso cref="State"/>.
/// </summary>
/// <param name="state">The user's current <see cref="Party"/> status.</param>
/// <returns>The modified Rich Presence.</returns>
public RichPresence WithState(string state)
{
State = state;
return this;
}
/// <summary>
/// Sets the details of the Rich Presence. See also <seealso cref="Details"/>.
/// </summary>
/// <param name="details">What the user is currently doing.</param>
/// <returns>The modified Rich Presence.</returns>
public RichPresence WithDetails(string details)
{
Details = details;
return this;
}
/// <summary>
/// Sets the timestamp of the Rich Presence. See also <seealso cref="Timestamps"/>.
/// </summary>
/// <param name="timestamps">The time elapsed / remaining time data.</param>
/// <returns>The modified Rich Presence.</returns>
public RichPresence WithTimestamps(Timestamps timestamps)
{
Timestamps = timestamps;
return this;
}
/// <summary>
/// Sets the assets of the Rich Presence. See also <seealso cref="Assets"/>.
/// </summary>
/// <param name="assets">The names of the images to use and the tooltips to give those images.</param>
/// <returns>The modified Rich Presence.</returns>
public RichPresence WithAssets(Assets assets)
{
Assets = assets;
return this;
}
/// <summary>
/// Sets the Rich Presence's party. See also <seealso cref="Party"/>.
/// </summary>
/// <param name="party">The party the player is currently in.</param>
/// <returns>The modified Rich Presence.</returns>
public RichPresence WithParty(Party party)
{
Party = party;
return this;
}
/// <summary>
/// Sets the Rich Presence's secrets. See also <seealso cref="Secrets"/>.
/// </summary>
/// <param name="secrets">The secrets used for Join / Spectate.</param>
/// <returns>The modified Rich Presence.</returns>
public RichPresence WithSecrets(Secrets secrets)
{
Secrets = secrets;
return this;
}
#endregion Builder
/// <summary>
/// Attempts to call <see cref="StringTools.GetNullOrString(string)"/> on the string and return the result, if its within a valid length.
/// </summary>
/// <param name="str">The string to check</param>
/// <param name="result">The formatted string result</param>
/// <param name="bytes">The maximum number of bytes the string can take up</param>
/// <param name="encoding">The encoding to count the bytes with</param>
/// <returns>True if the string fits within the number of bytes</returns>
internal static bool ValidateString(string str, out string result, int bytes, Encoding encoding)
{
result = str;
if (str == null)
return true;
//Trim the string, for the best chance of fitting
var s = str.Trim();
//Make sure it fits
if (!s.WithinLength(bytes, encoding))
return false;
//Make sure its not empty
result = s.GetNullOrString();
return true;
}
/// <summary>
/// Operator that converts a presence into a boolean for null checks.
/// </summary>
/// <param name="presesnce"></param>
public static implicit operator bool(RichPresence presesnce)
{
return presesnce != null;
}
/// <summary>
/// Checks if the other rich presence differs from the current one
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
internal bool Matches(RichPresence other)
{
#pragma warning disable CS0618 // Type or member is obsolete
if (other == null)
return false;
if (State != other.State || Details != other.Details)
return false;
//Checks if the timestamps are different
if (Timestamps != null)
{
if (other.Timestamps == null ||
other.Timestamps.StartUnixMilliseconds != Timestamps.StartUnixMilliseconds ||
other.Timestamps.EndUnixMilliseconds != Timestamps.EndUnixMilliseconds)
return false;
}
else if (other.Timestamps != null)
{
return false;
}
//Checks if the secrets are different
if (Secrets != null)
{
if (other.Secrets == null ||
other.Secrets.JoinSecret != Secrets.JoinSecret ||
other.Secrets.MatchSecret != Secrets.MatchSecret ||
other.Secrets.SpectateSecret != Secrets.SpectateSecret)
return false;
}
else if (other.Secrets != null)
{
return false;
}
//Checks if the timestamps are different
if (Party != null)
{
if (other.Party == null ||
other.Party.ID != Party.ID ||
other.Party.Max != Party.Max ||
other.Party.Size != Party.Size)
return false;
}
else if (other.Party != null)
{
return false;
}
//Checks if the assets are different
if (Assets != null)
{
if (other.Assets == null ||
other.Assets.LargeImageKey != Assets.LargeImageKey ||
other.Assets.LargeImageText != Assets.LargeImageText ||
other.Assets.SmallImageKey != Assets.SmallImageKey ||
other.Assets.SmallImageText != Assets.SmallImageText)
return false;
}
else if (other.Assets != null)
{
return false;
}
return Instance == other.Instance;
#pragma warning restore CS0618 // Type or member is obsolete
}
}
/// <summary>
/// The secrets used for Join / Spectate. Secrets are obfuscated data of your choosing. They could be match ids, player ids, lobby ids, etc.
/// <para>To keep security on the up and up, Discord requires that you properly hash/encode/encrypt/put-a-padlock-on-and-swallow-the-key-but-wait-then-how-would-you-open-it your secrets.</para>
/// <para>You should send discord data that someone else's game client would need to join or spectate their friend. If you can't or don't want to support those actions, you don't need to send secrets.</para>
/// <para>Visit the <see href="https://discordapp.com/developers/docs/rich-presence/how-to#secrets">Rich Presence How-To</see> for more information.</para>
/// </summary>
[Serializable]
public class Secrets
{
/// <summary>
/// The unique match code to distinguish different games/lobbies. Use <see cref="Secrets.CreateSecret(Random)"/> to get an appropriately sized secret.
/// <para>This cannot be null and must be supplied for the Join / Spectate feature to work.</para>
/// <para>Max Length of 128 Bytes</para>
/// </summary>
[Obsolete("This feature has been deprecated my Mason in issue #152 on the offical library. Was originally used as a Notify Me feature, it has been replaced with Join / Spectate.")]
[JsonProperty("match", NullValueHandling = NullValueHandling.Ignore)]
public string MatchSecret
{
get { return _matchSecret; }
set
{
if (!RichPresence.ValidateString(value, out _matchSecret, 128, Encoding.UTF8))
throw new StringOutOfRangeException(128);
}
}
private string _matchSecret;
/// <summary>
/// The secret data that will tell the client how to connect to the game to play. This could be a unique identifier for a fancy match maker or player id, lobby id, etc.
/// <para>It is recommended to encrypt this information so its hard for people to replicate it.
/// Do <b>NOT</b> just use the IP address in this. That is a bad practice and can leave your players vulnerable!
/// </para>
/// <para>Max Length of 128 Bytes</para>
/// </summary>
[JsonProperty("join", NullValueHandling = NullValueHandling.Ignore)]
public string JoinSecret
{
get { return _joinSecret; }
set
{
if (!RichPresence.ValidateString(value, out _joinSecret, 128, Encoding.UTF8))
throw new StringOutOfRangeException(128);
}
}
private string _joinSecret;
/// <summary>
/// The secret data that will tell the client how to connect to the game to spectate. This could be a unique identifier for a fancy match maker or player id, lobby id, etc.
/// <para>It is recommended to encrypt this information so its hard for people to replicate it.
/// Do <b>NOT</b> just use the IP address in this. That is a bad practice and can leave your players vulnerable!
/// </para>
/// <para>Max Length of 128 Bytes</para>
/// </summary>
[JsonProperty("spectate", NullValueHandling = NullValueHandling.Ignore)]
public string SpectateSecret
{
get { return _spectateSecret; }
set
{
if (!RichPresence.ValidateString(value, out _spectateSecret, 128, Encoding.UTF8))
throw new StringOutOfRangeException(128);
}
}
private string _spectateSecret;
#region Statics
/// <summary>
/// The encoding the secret generator is using
/// </summary>
public static Encoding Encoding
{ get { return Encoding.UTF8; } }
/// <summary>
/// The length of a secret in bytes.
/// </summary>
public static int SecretLength
{ get { return 128; } }
/// <summary>
/// Creates a new secret. This is NOT a cryptographic function and should NOT be used for sensitive information. This is mainly provided as a way to generate quick IDs.
/// </summary>
/// <param name="random">The random to use</param>
/// <returns>Returns a <see cref="SecretLength"/> sized string with random characters from <see cref="Encoding"/></returns>
public static string CreateSecret(Random random)
{
//Prepare an array and fill it with random bytes
// THIS IS NOT SECURE! DO NOT USE THIS FOR PASSWORDS!
byte[] bytes = new byte[SecretLength];
random.NextBytes(bytes);
//Return the encoding. Probably should remove invalid characters but cannot be fucked.
return Encoding.GetString(bytes);
}
/// <summary>
/// Creates a secret word using more readable friendly characters. Useful for debugging purposes. This is not a cryptographic function and should NOT be used for sensitive information.
/// </summary>
/// <param name="random">The random used to generate the characters</param>
/// <returns></returns>
public static string CreateFriendlySecret(Random random)
{
string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string secret = "";
for (int i = 0; i < SecretLength; i++)
secret += charset[random.Next(charset.Length)];
return secret;
}
#endregion Statics
}
/// <summary>
/// Information about the pictures used in the Rich Presence.
/// </summary>
[Serializable]
public class Assets
{
/// <summary>
/// Name of the uploaded image for the large profile artwork.
/// <para>Max 32 Bytes.</para>
/// </summary>
[JsonProperty("large_image", NullValueHandling = NullValueHandling.Ignore)]
public string LargeImageKey
{
get { return _largeimagekey; }
set
{
if (!RichPresence.ValidateString(value, out _largeimagekey, 32, Encoding.UTF8))
throw new StringOutOfRangeException(32);
//Reset the large image ID
_largeimageID = null;
}
}
private string _largeimagekey;
/// <summary>
/// The tooltip for the large square image. For example, "Summoners Rift" or "Horizon Lunar Colony".
/// <para>Max 128 Bytes.</para>
/// </summary>
[JsonProperty("large_text", NullValueHandling = NullValueHandling.Ignore)]
public string LargeImageText
{
get { return _largeimagetext; }
set
{
if (!RichPresence.ValidateString(value, out _largeimagetext, 128, Encoding.UTF8))
throw new StringOutOfRangeException(128);
}
}
private string _largeimagetext;
/// <summary>
/// Name of the uploaded image for the small profile artwork.
/// <para>Max 32 Bytes.</para>
/// </summary>
[JsonProperty("small_image", NullValueHandling = NullValueHandling.Ignore)]
public string SmallImageKey
{
get { return _smallimagekey; }
set
{
if (!RichPresence.ValidateString(value, out _smallimagekey, 32, Encoding.UTF8))
throw new StringOutOfRangeException(32);
//Reset the small image id
_smallimageID = null;
}
}
private string _smallimagekey;
/// <summary>
/// The tooltip for the small circle image. For example, "LvL 6" or "Ultimate 85%".
/// <para>Max 128 Bytes.</para>
/// </summary>
[JsonProperty("small_text", NullValueHandling = NullValueHandling.Ignore)]
public string SmallImageText
{
get { return _smallimagetext; }
set
{
if (!RichPresence.ValidateString(value, out _smallimagetext, 128, Encoding.UTF8))
throw new StringOutOfRangeException(128);
}
}
private string _smallimagetext;
/// <summary>
/// The ID of the large image. This is only set after Update Presence and will automatically become null when <see cref="LargeImageKey"/> is changed.
/// </summary>
[JsonIgnore]
public ulong? LargeImageID
{ get { return _largeimageID; } }
private ulong? _largeimageID;
/// <summary>
/// The ID of the small image. This is only set after Update Presence and will automatically become null when <see cref="SmallImageKey"/> is changed.
/// </summary>
[JsonIgnore]
public ulong? SmallImageID
{ get { return _smallimageID; } }
private ulong? _smallimageID;
/// <summary>
/// Merges this asset with the other, taking into account for ID's instead of keys.
/// </summary>
/// <param name="other"></param>
internal void Merge(Assets other)
{
//Copy over the names
_smallimagetext = other._smallimagetext;
_largeimagetext = other._largeimagetext;
//Convert large ID
ulong largeID;
if (ulong.TryParse(other._largeimagekey, out largeID))
{
_largeimageID = largeID;
}
else
{
_largeimagekey = other._largeimagekey;
_largeimageID = null;
}
//Convert the small ID
ulong smallID;
if (ulong.TryParse(other._smallimagekey, out smallID))
{
_smallimageID = smallID;
}
else
{
_smallimagekey = other._smallimagekey;
_smallimageID = null;
}
}
}
/// <summary>
/// Structure representing the start and endtimes of a match.
/// </summary>
[Serializable]
public class Timestamps
{
/// <summary>A new timestamp that starts from the current time.</summary>
public static Timestamps Now
{ get { return new Timestamps(DateTime.UtcNow, end: null); } }
/// <summary>
/// Creates a new timestamp starting at the current time and ending in the supplied timespan
/// </summary>
/// <param name="seconds">How long the Timestamp will last for in seconds.</param>
/// <returns>Returns a new timestamp with given duration.</returns>
public static Timestamps FromTimeSpan(double seconds)
{ return FromTimeSpan(TimeSpan.FromSeconds(seconds)); }
/// <summary>
/// Creates a new timestamp starting at current time and ending in the supplied timespan
/// </summary>
/// <param name="timespan">How long the Timestamp will last for.</param>
/// <returns>Returns a new timestamp with given duration.</returns>
public static Timestamps FromTimeSpan(TimeSpan timespan)
{
return new Timestamps()
{
Start = DateTime.UtcNow,
End = DateTime.UtcNow + timespan
};
}
/// <summary>
/// The time that match started. When included (not-null), the time in the rich presence will be shown as "00:01 elapsed".
/// </summary>
[JsonIgnore]
public DateTime? Start { get; set; }
/// <summary>
/// The time the match will end. When included (not-null), the time in the rich presence will be shown as "00:01 remaining". This will override the "elapsed" to "remaining".
/// </summary>
[JsonIgnore]
public DateTime? End { get; set; }
/// <summary>
/// Creates a empty timestamp object
/// </summary>
public Timestamps()
{
Start = null;
End = null;
}
/// <summary>
/// Creates a timestamp with the set start or end time.
/// </summary>
/// <param name="start">The start time</param>
/// <param name="end">The end time</param>
public Timestamps(DateTime start, DateTime? end = null)
{
Start = start;
End = end;
}
/// <summary>
/// Converts between DateTime and Milliseconds to give the Unix Epoch Time for the <see cref="Timestamps.Start"/>.
/// </summary>
[JsonProperty("start", NullValueHandling = NullValueHandling.Ignore)]
public ulong? StartUnixMilliseconds
{
get
{
return Start.HasValue ? ToUnixMilliseconds(Start.Value) : (ulong?)null;
}
set
{
Start = value.HasValue ? FromUnixMilliseconds(value.Value) : (DateTime?)null;
}
}
/// <summary>
/// Converts between DateTime and Milliseconds to give the Unix Epoch Time for the <see cref="Timestamps.End"/>.
/// <seealso cref="End"/>
/// </summary>
[JsonProperty("end", NullValueHandling = NullValueHandling.Ignore)]
public ulong? EndUnixMilliseconds
{
get
{
return End.HasValue ? ToUnixMilliseconds(End.Value) : (ulong?)null;
}
set
{
End = value.HasValue ? FromUnixMilliseconds(value.Value) : (DateTime?)null;
}
}
/// <summary>
/// Converts a Unix Epoch time into a <see cref="DateTime"/>.
/// </summary>
/// <param name="unixTime">The time in milliseconds since 1970 / 01 / 01</param>
/// <returns></returns>
public static DateTime FromUnixMilliseconds(ulong unixTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddMilliseconds(Convert.ToDouble(unixTime));
}
/// <summary>
/// Converts a <see cref="DateTime"/> into a Unix Epoch time (in milliseconds).
/// </summary>
/// <param name="date">The datetime to convert</param>
/// <returns></returns>
public static ulong ToUnixMilliseconds(DateTime date)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToUInt64((date - epoch).TotalMilliseconds);
}
}
/// <summary>
/// Structure representing the part the player is in.
/// </summary>
[Serializable]
public class Party
{
/// <summary>
/// A unique ID for the player's current party / lobby / group. If this is not supplied, they player will not be in a party and the rest of the information will not be sent.
/// <para>Max 128 Bytes</para>
/// </summary>
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
public string ID
{ get { return _partyid; } set { _partyid = value.GetNullOrString(); } }
private string _partyid;
/// <summary>
/// The current size of the players party / lobby / group.
/// </summary>
[JsonIgnore]
public int Size { get; set; }
/// <summary>
/// The maxium size of the party / lobby / group. This is required to be larger than <see cref="Size"/>. If it is smaller than the current party size, it will automatically be set too <see cref="Size"/> when the presence is sent.
/// </summary>
[JsonIgnore]
public int Max { get; set; }
[JsonProperty("size", NullValueHandling = NullValueHandling.Ignore)]
private int[] _size
{
get
{
//see issue https://github.com/discordapp/discord-rpc/issues/111
int size = Math.Max(1, Size);
return new int[] { size, Math.Max(size, Max) };
}
set
{
if (value.Length != 2)
{
Size = 0;
Max = 0;
}
else
{
Size = value[0];
Max = value[1];
}
}
}
}
/// <summary>
/// A rich presence that has been parsed from the pipe as a response.
/// </summary>
internal class RichPresenceResponse : RichPresence
{
/// <summary>
/// ID of the client
/// </summary>
[JsonProperty("application_id")]
public string ClientID { get; private set; }
/// <summary>
/// Name of the bot
/// </summary>
[JsonProperty("name")]
public string Name { get; private set; }
}
}