1
- using Newtonsoft . Json ;
1
+ using Newtonsoft . Json ;
2
2
using System ;
3
3
using DiscordRPC . Helper ;
4
4
using System . Text ;
@@ -15,15 +15,15 @@ public class BaseRichPresence
15
15
{
16
16
/// <summary>
17
17
/// The user's current <see cref="Party"/> status. For example, "Playing Solo" or "With Friends".
18
- /// <para>Max 128 bytes </para>
18
+ /// <para>Max 128 characters </para>
19
19
/// </summary>
20
20
[ JsonProperty ( "state" , NullValueHandling = NullValueHandling . Ignore ) ]
21
21
public string State
22
22
{
23
23
get { return _state ; }
24
24
set
25
25
{
26
- if ( ! ValidateString ( value , out _state , 128 , Encoding . UTF8 ) )
26
+ if ( ! ValidateString ( value , out _state , false , 128 ) )
27
27
throw new StringOutOfRangeException ( "State" , 0 , 128 ) ;
28
28
}
29
29
}
@@ -33,15 +33,15 @@ public string State
33
33
34
34
/// <summary>
35
35
/// What the user is currently doing. For example, "Competitive - Total Mayhem".
36
- /// <para>Max 128 bytes </para>
36
+ /// <para>Max 128 characters </para>
37
37
/// </summary>
38
38
[ JsonProperty ( "details" , NullValueHandling = NullValueHandling . Ignore ) ]
39
39
public string Details
40
40
{
41
41
get { return _details ; }
42
42
set
43
43
{
44
- if ( ! ValidateString ( value , out _details , 128 , Encoding . UTF8 ) )
44
+ if ( ! ValidateString ( value , out _details , false , 128 ) )
45
45
throw new StringOutOfRangeException ( 128 ) ;
46
46
}
47
47
}
@@ -142,10 +142,11 @@ public bool HasSecrets()
142
142
/// </summary>
143
143
/// <param name="str">The string to check</param>
144
144
/// <param name="result">The formatted string result</param>
145
- /// <param name="bytes">The maximum number of bytes the string can take up</param>
146
- /// <param name="encoding">The encoding to count the bytes with</param>
145
+ /// <param name="useBytes">True if you need to validate the string by byte length</param>
146
+ /// <param name="length">The maximum number of bytes/characters the string can take up</param>
147
+ /// <param name="encoding">The encoding to count the bytes with, optional</param>
147
148
/// <returns>True if the string fits within the number of bytes</returns>
148
- internal static bool ValidateString ( string str , out string result , int bytes , Encoding encoding )
149
+ internal static bool ValidateString ( string str , out string result , bool useBytes , int length , Encoding encoding = null )
149
150
{
150
151
result = str ;
151
152
if ( str == null )
@@ -155,7 +156,7 @@ internal static bool ValidateString(string str, out string result, int bytes, En
155
156
var s = str . Trim ( ) ;
156
157
157
158
//Make sure it fits
158
- if ( ! s . WithinLength ( bytes , encoding ) )
159
+ if ( useBytes && ! s . WithinLength ( length , encoding ) || s . Length > length )
159
160
return false ;
160
161
161
162
//Make sure its not empty
@@ -166,10 +167,10 @@ internal static bool ValidateString(string str, out string result, int bytes, En
166
167
/// <summary>
167
168
/// Operator that converts a presence into a boolean for null checks.
168
169
/// </summary>
169
- /// <param name="presesnce "></param>
170
- public static implicit operator bool ( BaseRichPresence presesnce )
170
+ /// <param name="presence "></param>
171
+ public static implicit operator bool ( BaseRichPresence presence )
171
172
{
172
- return presesnce != null ;
173
+ return presence != null ;
173
174
}
174
175
175
176
/// <summary>
@@ -297,7 +298,7 @@ public class Secrets
297
298
/// <summary>
298
299
/// The unique match code to distinguish different games/lobbies. Use <see cref="Secrets.CreateSecret(Random)"/> to get an appropriately sized secret.
299
300
/// <para>This cannot be null and must be supplied for the Join / Spectate feature to work.</para>
300
- /// <para>Max Length of 128 Bytes </para>
301
+ /// <para>Max Length of 128 characters </para>
301
302
/// </summary>
302
303
[ 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." ) ]
303
304
[ JsonProperty ( "match" , NullValueHandling = NullValueHandling . Ignore ) ]
@@ -306,7 +307,7 @@ public string MatchSecret
306
307
get { return _matchSecret ; }
307
308
set
308
309
{
309
- if ( ! BaseRichPresence . ValidateString ( value , out _matchSecret , 128 , Encoding . UTF8 ) )
310
+ if ( ! BaseRichPresence . ValidateString ( value , out _matchSecret , false , 128 ) )
310
311
throw new StringOutOfRangeException ( 128 ) ;
311
312
}
312
313
}
@@ -317,15 +318,15 @@ public string MatchSecret
317
318
/// <para>It is recommended to encrypt this information so its hard for people to replicate it.
318
319
/// Do <b>NOT</b> just use the IP address in this. That is a bad practice and can leave your players vulnerable!
319
320
/// </para>
320
- /// <para>Max Length of 128 Bytes </para>
321
+ /// <para>Max Length of 128 characters </para>
321
322
/// </summary>
322
323
[ JsonProperty ( "join" , NullValueHandling = NullValueHandling . Ignore ) ]
323
324
public string JoinSecret
324
325
{
325
326
get { return _joinSecret ; }
326
327
set
327
328
{
328
- if ( ! BaseRichPresence . ValidateString ( value , out _joinSecret , 128 , Encoding . UTF8 ) )
329
+ if ( ! BaseRichPresence . ValidateString ( value , out _joinSecret , false , 128 ) )
329
330
throw new StringOutOfRangeException ( 128 ) ;
330
331
}
331
332
}
@@ -336,15 +337,15 @@ public string JoinSecret
336
337
/// <para>It is recommended to encrypt this information so its hard for people to replicate it.
337
338
/// Do <b>NOT</b> just use the IP address in this. That is a bad practice and can leave your players vulnerable!
338
339
/// </para>
339
- /// <para>Max Length of 128 Bytes </para>
340
+ /// <para>Max Length of 128 characters </para>
340
341
/// </summary>
341
342
[ JsonProperty ( "spectate" , NullValueHandling = NullValueHandling . Ignore ) ]
342
343
public string SpectateSecret
343
344
{
344
345
get { return _spectateSecret ; }
345
346
set
346
347
{
347
- if ( ! BaseRichPresence . ValidateString ( value , out _spectateSecret , 128 , Encoding . UTF8 ) )
348
+ if ( ! BaseRichPresence . ValidateString ( value , out _spectateSecret , false , 128 ) )
348
349
throw new StringOutOfRangeException ( 128 ) ;
349
350
}
350
351
}
@@ -406,7 +407,7 @@ public class Assets
406
407
{
407
408
/// <summary>
408
409
/// Name of the uploaded image for the large profile artwork.
409
- /// <para>Max 256 Bytes .</para>
410
+ /// <para>Max 256 characters .</para>
410
411
/// </summary>
411
412
/// <remarks>Allows URL to directly link to images.</remarks>
412
413
[ JsonProperty ( "large_image" , NullValueHandling = NullValueHandling . Ignore ) ]
@@ -415,7 +416,7 @@ public string LargeImageKey
415
416
get { return _largeimagekey ; }
416
417
set
417
418
{
418
- if ( ! BaseRichPresence . ValidateString ( value , out _largeimagekey , 256 , Encoding . UTF8 ) )
419
+ if ( ! BaseRichPresence . ValidateString ( value , out _largeimagekey , false , 256 ) )
419
420
throw new StringOutOfRangeException ( 256 ) ;
420
421
421
422
//Get if this is a external link
@@ -439,15 +440,15 @@ public bool IsLargeImageKeyExternal
439
440
440
441
/// <summary>
441
442
/// The tooltip for the large square image. For example, "Summoners Rift" or "Horizon Lunar Colony".
442
- /// <para>Max 128 Bytes .</para>
443
+ /// <para>Max 128 characters .</para>
443
444
/// </summary>
444
445
[ JsonProperty ( "large_text" , NullValueHandling = NullValueHandling . Ignore ) ]
445
446
public string LargeImageText
446
447
{
447
448
get { return _largeimagetext ; }
448
449
set
449
450
{
450
- if ( ! BaseRichPresence . ValidateString ( value , out _largeimagetext , 128 , Encoding . UTF8 ) )
451
+ if ( ! BaseRichPresence . ValidateString ( value , out _largeimagetext , false , 128 ) )
451
452
throw new StringOutOfRangeException ( 128 ) ;
452
453
}
453
454
}
@@ -456,7 +457,7 @@ public string LargeImageText
456
457
457
458
/// <summary>
458
459
/// Name of the uploaded image for the small profile artwork.
459
- /// <para>Max 256 Bytes .</para>
460
+ /// <para>Max 256 characters .</para>
460
461
/// </summary>
461
462
/// <remarks>Allows URL to directly link to images.</remarks>
462
463
[ JsonProperty ( "small_image" , NullValueHandling = NullValueHandling . Ignore ) ]
@@ -465,7 +466,7 @@ public string SmallImageKey
465
466
get { return _smallimagekey ; }
466
467
set
467
468
{
468
- if ( ! BaseRichPresence . ValidateString ( value , out _smallimagekey , 256 , Encoding . UTF8 ) )
469
+ if ( ! BaseRichPresence . ValidateString ( value , out _smallimagekey , false , 256 ) )
469
470
throw new StringOutOfRangeException ( 256 ) ;
470
471
471
472
//Get if this is a external link
@@ -486,18 +487,18 @@ public bool IsSmallImageKeyExternal
486
487
get { return _issmallimagekeyexternal ; }
487
488
}
488
489
private bool _issmallimagekeyexternal ;
489
-
490
+
490
491
/// <summary>
491
492
/// The tooltip for the small circle image. For example, "LvL 6" or "Ultimate 85%".
492
- /// <para>Max 128 Bytes .</para>
493
+ /// <para>Max 128 characters .</para>
493
494
/// </summary>
494
495
[ JsonProperty ( "small_text" , NullValueHandling = NullValueHandling . Ignore ) ]
495
496
public string SmallImageText
496
497
{
497
498
get { return _smallimagetext ; }
498
499
set
499
500
{
500
- if ( ! BaseRichPresence . ValidateString ( value , out _smallimagetext , 128 , Encoding . UTF8 ) )
501
+ if ( ! BaseRichPresence . ValidateString ( value , out _smallimagetext , false , 128 ) )
501
502
throw new StringOutOfRangeException ( 128 ) ;
502
503
}
503
504
}
@@ -768,31 +769,31 @@ public class Button
768
769
{
769
770
/// <summary>
770
771
/// Text shown on the button
771
- /// <para>Max 32 bytes.</para>
772
+ /// <para>Max 31 bytes.</para>
772
773
/// </summary>
773
774
[ JsonProperty ( "label" ) ]
774
775
public string Label
775
776
{
776
777
get { return _label ; }
777
778
set
778
779
{
779
- if ( ! BaseRichPresence . ValidateString ( value , out _label , 32 , Encoding . UTF8 ) )
780
- throw new StringOutOfRangeException ( 32 ) ;
780
+ if ( ! BaseRichPresence . ValidateString ( value , out _label , true , 31 , Encoding . UTF8 ) )
781
+ throw new StringOutOfRangeException ( 31 ) ;
781
782
}
782
783
}
783
784
private string _label ;
784
785
785
786
/// <summary>
786
787
/// The URL opened when clicking the button.
787
- /// <para>Max 512 bytes .</para>
788
+ /// <para>Max 512 characters .</para>
788
789
/// </summary>
789
790
[ JsonProperty ( "url" ) ]
790
791
public string Url
791
792
{
792
793
get { return _url ; }
793
794
set
794
795
{
795
- if ( ! BaseRichPresence . ValidateString ( value , out _url , 512 , Encoding . UTF8 ) )
796
+ if ( ! BaseRichPresence . ValidateString ( value , out _url , false , 512 ) )
796
797
throw new StringOutOfRangeException ( 512 ) ;
797
798
798
799
if ( ! Uri . TryCreate ( _url , UriKind . Absolute , out var uriResult ) ) // || !(uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
0 commit comments