Skip to content

Commit b06abb5

Browse files
authored
Merge pull request #259 from maximmax42/patch-1
Changed length validation, typo fix
2 parents c8ddd63 + 07a7cc9 commit b06abb5

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

DiscordRPC/RichPresence.cs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using System;
33
using DiscordRPC.Helper;
44
using System.Text;
@@ -15,15 +15,15 @@ public class BaseRichPresence
1515
{
1616
/// <summary>
1717
/// 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>
1919
/// </summary>
2020
[JsonProperty("state", NullValueHandling = NullValueHandling.Ignore)]
2121
public string State
2222
{
2323
get { return _state; }
2424
set
2525
{
26-
if (!ValidateString(value, out _state, 128, Encoding.UTF8))
26+
if (!ValidateString(value, out _state, false, 128))
2727
throw new StringOutOfRangeException("State", 0, 128);
2828
}
2929
}
@@ -33,15 +33,15 @@ public string State
3333

3434
/// <summary>
3535
/// What the user is currently doing. For example, "Competitive - Total Mayhem".
36-
/// <para>Max 128 bytes</para>
36+
/// <para>Max 128 characters</para>
3737
/// </summary>
3838
[JsonProperty("details", NullValueHandling = NullValueHandling.Ignore)]
3939
public string Details
4040
{
4141
get { return _details; }
4242
set
4343
{
44-
if (!ValidateString(value, out _details, 128, Encoding.UTF8))
44+
if (!ValidateString(value, out _details, false, 128))
4545
throw new StringOutOfRangeException(128);
4646
}
4747
}
@@ -142,10 +142,11 @@ public bool HasSecrets()
142142
/// </summary>
143143
/// <param name="str">The string to check</param>
144144
/// <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>
147148
/// <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)
149150
{
150151
result = str;
151152
if (str == null)
@@ -155,7 +156,7 @@ internal static bool ValidateString(string str, out string result, int bytes, En
155156
var s = str.Trim();
156157

157158
//Make sure it fits
158-
if (!s.WithinLength(bytes, encoding))
159+
if (useBytes && !s.WithinLength(length, encoding) || s.Length > length)
159160
return false;
160161

161162
//Make sure its not empty
@@ -166,10 +167,10 @@ internal static bool ValidateString(string str, out string result, int bytes, En
166167
/// <summary>
167168
/// Operator that converts a presence into a boolean for null checks.
168169
/// </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)
171172
{
172-
return presesnce != null;
173+
return presence != null;
173174
}
174175

175176
/// <summary>
@@ -297,7 +298,7 @@ public class Secrets
297298
/// <summary>
298299
/// The unique match code to distinguish different games/lobbies. Use <see cref="Secrets.CreateSecret(Random)"/> to get an appropriately sized secret.
299300
/// <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>
301302
/// </summary>
302303
[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.")]
303304
[JsonProperty("match", NullValueHandling = NullValueHandling.Ignore)]
@@ -306,7 +307,7 @@ public string MatchSecret
306307
get { return _matchSecret; }
307308
set
308309
{
309-
if (!BaseRichPresence.ValidateString(value, out _matchSecret, 128, Encoding.UTF8))
310+
if (!BaseRichPresence.ValidateString(value, out _matchSecret, false, 128))
310311
throw new StringOutOfRangeException(128);
311312
}
312313
}
@@ -317,15 +318,15 @@ public string MatchSecret
317318
/// <para>It is recommended to encrypt this information so its hard for people to replicate it.
318319
/// Do <b>NOT</b> just use the IP address in this. That is a bad practice and can leave your players vulnerable!
319320
/// </para>
320-
/// <para>Max Length of 128 Bytes</para>
321+
/// <para>Max Length of 128 characters</para>
321322
/// </summary>
322323
[JsonProperty("join", NullValueHandling = NullValueHandling.Ignore)]
323324
public string JoinSecret
324325
{
325326
get { return _joinSecret; }
326327
set
327328
{
328-
if (!BaseRichPresence.ValidateString(value, out _joinSecret, 128, Encoding.UTF8))
329+
if (!BaseRichPresence.ValidateString(value, out _joinSecret, false, 128))
329330
throw new StringOutOfRangeException(128);
330331
}
331332
}
@@ -336,15 +337,15 @@ public string JoinSecret
336337
/// <para>It is recommended to encrypt this information so its hard for people to replicate it.
337338
/// Do <b>NOT</b> just use the IP address in this. That is a bad practice and can leave your players vulnerable!
338339
/// </para>
339-
/// <para>Max Length of 128 Bytes</para>
340+
/// <para>Max Length of 128 characters</para>
340341
/// </summary>
341342
[JsonProperty("spectate", NullValueHandling = NullValueHandling.Ignore)]
342343
public string SpectateSecret
343344
{
344345
get { return _spectateSecret; }
345346
set
346347
{
347-
if (!BaseRichPresence.ValidateString(value, out _spectateSecret, 128, Encoding.UTF8))
348+
if (!BaseRichPresence.ValidateString(value, out _spectateSecret, false, 128))
348349
throw new StringOutOfRangeException(128);
349350
}
350351
}
@@ -406,7 +407,7 @@ public class Assets
406407
{
407408
/// <summary>
408409
/// Name of the uploaded image for the large profile artwork.
409-
/// <para>Max 256 Bytes.</para>
410+
/// <para>Max 256 characters.</para>
410411
/// </summary>
411412
/// <remarks>Allows URL to directly link to images.</remarks>
412413
[JsonProperty("large_image", NullValueHandling = NullValueHandling.Ignore)]
@@ -415,7 +416,7 @@ public string LargeImageKey
415416
get { return _largeimagekey; }
416417
set
417418
{
418-
if (!BaseRichPresence.ValidateString(value, out _largeimagekey, 256, Encoding.UTF8))
419+
if (!BaseRichPresence.ValidateString(value, out _largeimagekey, false, 256))
419420
throw new StringOutOfRangeException(256);
420421

421422
//Get if this is a external link
@@ -439,15 +440,15 @@ public bool IsLargeImageKeyExternal
439440

440441
/// <summary>
441442
/// 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>
443444
/// </summary>
444445
[JsonProperty("large_text", NullValueHandling = NullValueHandling.Ignore)]
445446
public string LargeImageText
446447
{
447448
get { return _largeimagetext; }
448449
set
449450
{
450-
if (!BaseRichPresence.ValidateString(value, out _largeimagetext, 128, Encoding.UTF8))
451+
if (!BaseRichPresence.ValidateString(value, out _largeimagetext, false, 128))
451452
throw new StringOutOfRangeException(128);
452453
}
453454
}
@@ -456,7 +457,7 @@ public string LargeImageText
456457

457458
/// <summary>
458459
/// Name of the uploaded image for the small profile artwork.
459-
/// <para>Max 256 Bytes.</para>
460+
/// <para>Max 256 characters.</para>
460461
/// </summary>
461462
/// <remarks>Allows URL to directly link to images.</remarks>
462463
[JsonProperty("small_image", NullValueHandling = NullValueHandling.Ignore)]
@@ -465,7 +466,7 @@ public string SmallImageKey
465466
get { return _smallimagekey; }
466467
set
467468
{
468-
if (!BaseRichPresence.ValidateString(value, out _smallimagekey, 256, Encoding.UTF8))
469+
if (!BaseRichPresence.ValidateString(value, out _smallimagekey, false, 256))
469470
throw new StringOutOfRangeException(256);
470471

471472
//Get if this is a external link
@@ -486,18 +487,18 @@ public bool IsSmallImageKeyExternal
486487
get { return _issmallimagekeyexternal; }
487488
}
488489
private bool _issmallimagekeyexternal;
489-
490+
490491
/// <summary>
491492
/// 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>
493494
/// </summary>
494495
[JsonProperty("small_text", NullValueHandling = NullValueHandling.Ignore)]
495496
public string SmallImageText
496497
{
497498
get { return _smallimagetext; }
498499
set
499500
{
500-
if (!BaseRichPresence.ValidateString(value, out _smallimagetext, 128, Encoding.UTF8))
501+
if (!BaseRichPresence.ValidateString(value, out _smallimagetext, false, 128))
501502
throw new StringOutOfRangeException(128);
502503
}
503504
}
@@ -768,31 +769,31 @@ public class Button
768769
{
769770
/// <summary>
770771
/// Text shown on the button
771-
/// <para>Max 32 bytes.</para>
772+
/// <para>Max 31 bytes.</para>
772773
/// </summary>
773774
[JsonProperty("label")]
774775
public string Label
775776
{
776777
get { return _label; }
777778
set
778779
{
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);
781782
}
782783
}
783784
private string _label;
784785

785786
/// <summary>
786787
/// The URL opened when clicking the button.
787-
/// <para>Max 512 bytes.</para>
788+
/// <para>Max 512 characters.</para>
788789
/// </summary>
789790
[JsonProperty("url")]
790791
public string Url
791792
{
792793
get { return _url; }
793794
set
794795
{
795-
if (!BaseRichPresence.ValidateString(value, out _url, 512, Encoding.UTF8))
796+
if (!BaseRichPresence.ValidateString(value, out _url, false, 512))
796797
throw new StringOutOfRangeException(512);
797798

798799
if (!Uri.TryCreate(_url, UriKind.Absolute, out var uriResult)) // || !(uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))

0 commit comments

Comments
 (0)