diff --git a/DiscordRPC/DiscordRpcClient.cs b/DiscordRPC/DiscordRpcClient.cs index 80ac2a5d..4240f771 100644 --- a/DiscordRPC/DiscordRpcClient.cs +++ b/DiscordRPC/DiscordRpcClient.cs @@ -333,10 +333,10 @@ private void ProcessMessage(IMessage message) //Resend our presence and subscription SynchronizeState(); } - + if (OnReady != null) OnReady.Invoke(this, message as ReadyMessage); - + break; case MessageType.Close: @@ -366,8 +366,8 @@ private void ProcessMessage(IMessage message) { var sub = message as SubscribeMessage; Subscription |= sub.Event; - } - + } + if (OnSubscribe != null) OnSubscribe.Invoke(this, message as SubscribeMessage); @@ -483,169 +483,91 @@ public void SetPresence(RichPresence presence) #region Updates /// - /// Updates only the of the and updates/removes the buttons. Returns the newly edited Rich Presence. + /// The update function delegate /// - /// The buttons of the Rich Presence + public delegate void UpdateFunc(ref RichPresence presence); + + /// + /// Updates the values assigned in the delegate passed + /// + /// Delegate used to update the rich presence /// Updated Rich Presence - public RichPresence UpdateButtons(Button[] button = null) + public RichPresence Update(UpdateFunc func) { if (!IsInitialized) - { throw new UninitializedException(); - } // Clone the presence RichPresence presence; lock (_sync) { - if (CurrentPresence == null) - { - presence = new RichPresence(); - } - else - { - presence = CurrentPresence.Clone(); - } + presence = CurrentPresence == null ? new RichPresence() : CurrentPresence.Clone(); } - // Update the buttons. - presence.Buttons = button; + func(ref presence); SetPresence(presence); return presence; } + /// + /// Updates only the of the and sends the updated presence to Discord. Returns the newly edited Rich Presence. + /// + /// The type of the Rich Presence + /// Updated Rich Presence + public RichPresence UpdateType(ActivityType type) => Update((ref RichPresence p) => p.Type = type); + + /// + /// Updates only the of the and updates/removes the buttons. Returns the newly edited Rich Presence. + /// + /// The buttons of the Rich Presence + /// Updated Rich Presence + public RichPresence UpdateButtons(Button[] buttons = null) => Update((ref RichPresence p) => p.Buttons = buttons); + /// /// Updates only the of the and updates the button with the given index. Returns the newly edited Rich Presence. /// /// The buttons of the Rich Presence /// The number of the button /// Updated Rich Presence - public RichPresence SetButton(Button button, int index = 0) - { - if (!IsInitialized) - { - throw new UninitializedException(); - } - - // Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) - { - presence = new RichPresence(); - } - else - { - presence = CurrentPresence.Clone(); - } - } - - // Update the buttons - presence.Buttons[index] = button; - SetPresence(presence); - - return presence; - } + public RichPresence SetButton(Button button, int index = 0) => Update((ref RichPresence p) => p.Buttons[index] = button); /// /// Updates only the of the and sends the updated presence to Discord. Returns the newly edited Rich Presence. /// /// The details of the Rich Presence /// Updated Rich Presence - public RichPresence UpdateDetails(string details) - { - if (!IsInitialized) - throw new UninitializedException(); - - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } + public RichPresence UpdateDetails(string details) => Update((ref RichPresence p) => p.Details = details); - //Update the value - presence.Details = details; - SetPresence(presence); - return presence; - } /// /// Updates only the of the and sends the updated presence to Discord. Returns the newly edited Rich Presence. /// /// The state of the Rich Presence /// Updated Rich Presence - public RichPresence UpdateState(string state) - { - if (!IsInitialized) - throw new UninitializedException(); + public RichPresence UpdateState(string state) => Update((ref RichPresence p) => p.State = state); - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } - - //Update the value - presence.State = state; - SetPresence(presence); - return presence; - } /// /// Updates only the of the and sends the updated presence to Discord. Returns the newly edited Rich Presence. /// /// The party of the Rich Presence /// Updated Rich Presence - public RichPresence UpdateParty(Party party) - { - if (!IsInitialized) - throw new UninitializedException(); - - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } + public RichPresence UpdateParty(Party party) => Update((ref RichPresence p) => p.Party = party); - //Update the value - presence.Party = party; - SetPresence(presence); - return presence; - } /// /// Updates the of the and sends the update presence to Discord. Returns the newly edited Rich Presence. /// Will return null if no presence exists and will throw a new if the Party does not exist. /// /// The new size of the party. It cannot be greater than /// Updated Rich Presence - public RichPresence UpdatePartySize(int size) + public RichPresence UpdatePartySize(int size) => Update((ref RichPresence p) => { - if (!IsInitialized) - throw new UninitializedException(); - - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } - - //Ensure it has a party - if (presence.Party == null) + // Ensure we have a party going on + if (p.Party == null) throw new BadPresenceException("Cannot set the size of the party if the party does not exist"); - //Update the value - presence.Party.Size = size; - SetPresence(presence); - return presence; - } + // Update the size + p.Party.Size = size; + }); /// /// Updates the of the and sends the update presence to Discord. Returns the newly edited Rich Presence. @@ -654,29 +576,15 @@ public RichPresence UpdatePartySize(int size) /// The new size of the party. It cannot be greater than /// The new size of the party. It cannot be smaller than /// Updated Rich Presence - public RichPresence UpdatePartySize(int size, int max) + public RichPresence UpdatePartySize(int size, int max) => Update((ref RichPresence p) => { - if (!IsInitialized) - throw new UninitializedException(); - - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } - - //Ensure it has a party - if (presence.Party == null) + // Ensure we have a party going on + if (p.Party == null) throw new BadPresenceException("Cannot set the size of the party if the party does not exist"); - //Update the value - presence.Party.Size = size; - presence.Party.Max = max; - SetPresence(presence); - return presence; - } + p.Party.Size = size; + p.Party.Max = max; + }); /// /// Updates the large of the and sends the updated presence to Discord. Both and are optional and will be ignored it null. @@ -684,25 +592,14 @@ public RichPresence UpdatePartySize(int size, int max) /// Optional: The new key to set the asset too /// Optional: The new tooltip to display on the asset /// Updated Rich Presence - public RichPresence UpdateLargeAsset(string key = null, string tooltip = null) + public RichPresence UpdateLargeAsset(string key = null, string tooltip = null) => Update((ref RichPresence p) => { - if (!IsInitialized) - throw new UninitializedException(); - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } + if (p.Assets == null) + p.Assets = new Assets(); - //Update the value - if (presence.Assets == null) presence.Assets = new Assets(); - presence.Assets.LargeImageKey = key ?? presence.Assets.LargeImageKey; - presence.Assets.LargeImageText = tooltip ?? presence.Assets.LargeImageText; - SetPresence(presence); - return presence; - } + p.Assets.LargeImageKey = key ?? p.Assets.LargeImageKey; + p.Assets.LargeImageText = tooltip ?? p.Assets.LargeImageText; + }); /// /// Updates the small of the and sends the updated presence to Discord. Both and are optional and will be ignored it null. @@ -710,134 +607,66 @@ public RichPresence UpdateLargeAsset(string key = null, string tooltip = null) /// Optional: The new key to set the asset too /// Optional: The new tooltip to display on the asset /// Updated Rich Presence - public RichPresence UpdateSmallAsset(string key = null, string tooltip = null) + public RichPresence UpdateSmallAsset(string key = null, string tooltip = null) => Update((ref RichPresence p) => { - if (!IsInitialized) - throw new UninitializedException(); - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } + if (p.Assets == null) + p.Assets = new Assets(); - //Update the value - if (presence.Assets == null) presence.Assets = new Assets(); - presence.Assets.SmallImageKey = key ?? presence.Assets.SmallImageKey; - presence.Assets.SmallImageText = tooltip ?? presence.Assets.SmallImageText; - SetPresence(presence); - return presence; - } + p.Assets.SmallImageKey = key ?? p.Assets.SmallImageKey; + p.Assets.SmallImageText = tooltip ?? p.Assets.SmallImageText; + }); /// /// Updates the of the and sends the updated presence to Discord. Will override previous secret entirely. /// /// The new secret to send to discord. /// Updated Rich Presence - public RichPresence UpdateSecrets(Secrets secrets) - { - if (!IsInitialized) - throw new UninitializedException(); - - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } - - //Update the value - presence.Secrets = secrets; - SetPresence(presence); - return presence; - } + public RichPresence UpdateSecrets(Secrets secrets) => Update((ref RichPresence p) => p.Secrets = secrets); /// /// Sets the start time of the to now and sends the updated presence to Discord. /// /// Updated Rich Presence - public RichPresence UpdateStartTime() { return UpdateStartTime(DateTime.UtcNow); } + public RichPresence UpdateStartTime() => UpdateStartTime(DateTime.UtcNow); /// /// Sets the start time of the and sends the updated presence to Discord. /// /// The new time for the start /// Updated Rich Presence - public RichPresence UpdateStartTime(DateTime time) + public RichPresence UpdateStartTime(DateTime time) => Update((ref RichPresence p) => { - if (!IsInitialized) - throw new UninitializedException(); + if (p.Timestamps == null) + p.Timestamps = new Timestamps(); - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } - - //Update the value - if (presence.Timestamps == null) presence.Timestamps = new Timestamps(); - presence.Timestamps.Start = time; - SetPresence(presence); - return presence; - } + p.Timestamps.Start = time; + }); /// /// Sets the end time of the to now and sends the updated presence to Discord. /// /// Updated Rich Presence - public RichPresence UpdateEndTime() { return UpdateEndTime(DateTime.UtcNow); } + public RichPresence UpdateEndTime() => UpdateEndTime(DateTime.UtcNow); /// /// Sets the end time of the and sends the updated presence to Discord. /// /// The new time for the end /// Updated Rich Presence - public RichPresence UpdateEndTime(DateTime time) + public RichPresence UpdateEndTime(DateTime time) => Update((ref RichPresence p) => { - if (!IsInitialized) - throw new UninitializedException(); + if (p.Timestamps == null) + p.Timestamps = new Timestamps(); - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } - - //Update the value - if (presence.Timestamps == null) presence.Timestamps = new Timestamps(); - presence.Timestamps.End = time; - SetPresence(presence); - return presence; - } + p.Timestamps.End = time; + }); /// /// Sets the start and end time of to null and sends it to Discord. /// /// Updated Rich Presence - public RichPresence UpdateClearTime() - { - if (!IsInitialized) - throw new UninitializedException(); + public RichPresence UpdateClearTime() => Update((ref RichPresence p) => p.Timestamps = null); - //Clone the presence - RichPresence presence; - lock (_sync) - { - if (CurrentPresence == null) { presence = new RichPresence(); } - else { presence = CurrentPresence.Clone(); } - } - - //Update the value - presence.Timestamps = null; - SetPresence(presence); - return presence; - } #endregion ///