|
| 1 | +using DiscordRPC.Exceptions; |
| 2 | +using DiscordRPC.Helper; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using System; |
| 5 | + |
| 6 | +namespace DiscordRPC |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Information about the pictures used in the Rich Presence. |
| 10 | + /// </summary> |
| 11 | + [Serializable] |
| 12 | + public class Assets |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Name of the uploaded image for the large profile artwork. |
| 16 | + /// <para>Max 256 characters.</para> |
| 17 | + /// </summary> |
| 18 | + /// <remarks>Allows URL to directly link to images.</remarks> |
| 19 | + [JsonProperty("large_image", NullValueHandling = NullValueHandling.Ignore)] |
| 20 | + public string LargeImageKey |
| 21 | + { |
| 22 | + get { return _largeimagekey; } |
| 23 | + set |
| 24 | + { |
| 25 | + if (!BaseRichPresence.ValidateString(value, out _largeimagekey, false, 256)) |
| 26 | + throw new StringOutOfRangeException(256); |
| 27 | + |
| 28 | + //Get if this is a external link |
| 29 | + _islargeimagekeyexternal = _largeimagekey?.StartsWith("mp:external/") ?? false; |
| 30 | + |
| 31 | + //Reset the large image ID |
| 32 | + _largeimageID = null; |
| 33 | + } |
| 34 | + } |
| 35 | + private string _largeimagekey; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Gets if the large square image is from an external link |
| 39 | + /// </summary> |
| 40 | + [JsonIgnore] |
| 41 | + public bool IsLargeImageKeyExternal |
| 42 | + { |
| 43 | + get { return _islargeimagekeyexternal; } |
| 44 | + } |
| 45 | + private bool _islargeimagekeyexternal; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// The tooltip for the large square image. For example, "Summoners Rift" or "Horizon Lunar Colony". |
| 49 | + /// <para>Max 128 characters.</para> |
| 50 | + /// </summary> |
| 51 | + [JsonProperty("large_text", NullValueHandling = NullValueHandling.Ignore)] |
| 52 | + public string LargeImageText |
| 53 | + { |
| 54 | + get { return _largeimagetext; } |
| 55 | + set |
| 56 | + { |
| 57 | + if (!BaseRichPresence.ValidateString(value, out _largeimagetext, false, 128)) |
| 58 | + throw new StringOutOfRangeException(128); |
| 59 | + } |
| 60 | + } |
| 61 | + private string _largeimagetext; |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// URL that is linked to when clicking on the large image in the activity card. |
| 65 | + /// <para>Max 256 characters.</para> |
| 66 | + /// </summary> |
| 67 | + [JsonProperty("large_url", NullValueHandling = NullValueHandling.Ignore)] |
| 68 | + public string LargeImageUrl |
| 69 | + { |
| 70 | + get { return _largeimageurl; } |
| 71 | + set |
| 72 | + { |
| 73 | + if (!BaseRichPresence.ValidateString(value, out _largeimageurl, false, 256)) |
| 74 | + throw new StringOutOfRangeException(256); |
| 75 | + |
| 76 | + if (!BaseRichPresence.ValidateUrl(_largeimageurl)) |
| 77 | + throw new ArgumentException("Url must be a valid URI"); |
| 78 | + } |
| 79 | + } |
| 80 | + private string _largeimageurl; |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Name of the uploaded image for the small profile artwork. |
| 84 | + /// <para>Max 256 characters.</para> |
| 85 | + /// </summary> |
| 86 | + /// <remarks>Allows URL to directly link to images.</remarks> |
| 87 | + [JsonProperty("small_image", NullValueHandling = NullValueHandling.Ignore)] |
| 88 | + public string SmallImageKey |
| 89 | + { |
| 90 | + get { return _smallimagekey; } |
| 91 | + set |
| 92 | + { |
| 93 | + if (!BaseRichPresence.ValidateString(value, out _smallimagekey, false, 256)) |
| 94 | + throw new StringOutOfRangeException(256); |
| 95 | + |
| 96 | + //Get if this is a external link |
| 97 | + _issmallimagekeyexternal = _smallimagekey?.StartsWith("mp:external/") ?? false; |
| 98 | + |
| 99 | + //Reset the small image id |
| 100 | + _smallimageID = null; |
| 101 | + } |
| 102 | + } |
| 103 | + private string _smallimagekey; |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Gets if the small profile artwork is from an external link |
| 107 | + /// </summary> |
| 108 | + [JsonIgnore] |
| 109 | + public bool IsSmallImageKeyExternal |
| 110 | + { |
| 111 | + get { return _issmallimagekeyexternal; } |
| 112 | + } |
| 113 | + private bool _issmallimagekeyexternal; |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// The tooltip for the small circle image. For example, "LvL 6" or "Ultimate 85%". |
| 117 | + /// <para>Max 128 characters.</para> |
| 118 | + /// </summary> |
| 119 | + [JsonProperty("small_text", NullValueHandling = NullValueHandling.Ignore)] |
| 120 | + public string SmallImageText |
| 121 | + { |
| 122 | + get { return _smallimagetext; } |
| 123 | + set |
| 124 | + { |
| 125 | + if (!BaseRichPresence.ValidateString(value, out _smallimagetext, false, 128)) |
| 126 | + throw new StringOutOfRangeException(128); |
| 127 | + } |
| 128 | + } |
| 129 | + private string _smallimagetext; |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// URL that is linked to when clicking on the small image in the activity card. |
| 133 | + /// <para>Max 256 characters.</para> |
| 134 | + /// </summary> |
| 135 | + [JsonProperty("small_url", NullValueHandling = NullValueHandling.Ignore)] |
| 136 | + public string SmallImageUrl |
| 137 | + { |
| 138 | + get { return _smallimageurl; } |
| 139 | + set |
| 140 | + { |
| 141 | + if (!BaseRichPresence.ValidateString(value, out _smallimageurl, false, 256)) |
| 142 | + throw new StringOutOfRangeException(256); |
| 143 | + |
| 144 | + if (!BaseRichPresence.ValidateUrl(_smallimageurl)) |
| 145 | + throw new ArgumentException("Url must be a valid URI"); |
| 146 | + } |
| 147 | + } |
| 148 | + private string _smallimageurl; |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// The ID of the large image. This is only set after Update Presence and will automatically become null when <see cref="LargeImageKey"/> is changed. |
| 152 | + /// </summary> |
| 153 | + [JsonIgnore] |
| 154 | + public ulong? LargeImageID { get { return _largeimageID; } } |
| 155 | + private ulong? _largeimageID; |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// The ID of the small image. This is only set after Update Presence and will automatically become null when <see cref="SmallImageKey"/> is changed. |
| 159 | + /// </summary> |
| 160 | + [JsonIgnore] |
| 161 | + public ulong? SmallImageID { get { return _smallimageID; } } |
| 162 | + private ulong? _smallimageID; |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Merges this asset with the other, taking into account for ID's instead of keys. |
| 166 | + /// </summary> |
| 167 | + /// <param name="other"></param> |
| 168 | + internal void Merge(Assets other) |
| 169 | + { |
| 170 | + //Copy over the names |
| 171 | + _smallimagetext = other._smallimagetext; |
| 172 | + _smallimageurl = other._smallimageurl; |
| 173 | + _largeimagetext = other._largeimagetext; |
| 174 | + _largeimageurl = other._largeimageurl; |
| 175 | + |
| 176 | + //Convert large ID |
| 177 | + ulong largeID; |
| 178 | + if (ulong.TryParse(other._largeimagekey, out largeID)) |
| 179 | + { |
| 180 | + _largeimageID = largeID; |
| 181 | + } |
| 182 | + else |
| 183 | + { |
| 184 | + _largeimagekey = other._largeimagekey; |
| 185 | + _largeimageID = null; |
| 186 | + } |
| 187 | + |
| 188 | + //Convert the small ID |
| 189 | + ulong smallID; |
| 190 | + if (ulong.TryParse(other._smallimagekey, out smallID)) |
| 191 | + { |
| 192 | + _smallimageID = smallID; |
| 193 | + } |
| 194 | + else |
| 195 | + { |
| 196 | + _smallimagekey = other._smallimagekey; |
| 197 | + _smallimageID = null; |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments