Skip to content

Commit

Permalink
Merge pull request Lachee#141 from fenwikk/master
Browse files Browse the repository at this point in the history
Added buttons to Unity
  • Loading branch information
Lachee authored Jun 12, 2021
2 parents 625d336 + 62a040c commit b05e7cd
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 67 deletions.
35 changes: 35 additions & 0 deletions DiscordRPC/RichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,41 @@ internal virtual bool Matches(RichPresence other)
return Instance == other.Instance;
#pragma warning restore CS0618 // Type or member is obsolete
}

/// <summary>
/// Converts this BaseRichPresence to RichPresence
/// </summary>
/// <returns></returns>
public RichPresence ToRichPresence()
{
var presence = new RichPresence();
presence.State = State;
presence.Details = Details;

presence.Party = !HasParty() ? Party : null;
presence.Secrets = !HasSecrets() ? Secrets : null;

if (HasAssets())
{
presence.Assets = new Assets()
{
SmallImageKey = Assets.SmallImageKey,
SmallImageText = Assets.SmallImageText,

LargeImageKey = Assets.LargeImageKey,
LargeImageText = Assets.LargeImageText
};
}

if (HasTimestamps())
{
presence.Timestamps = new Timestamps();
if (Timestamps.Start.HasValue) presence.Timestamps.Start = Timestamps.Start;
if (Timestamps.End.HasValue) presence.Timestamps.End = Timestamps.End;
}

return presence;
}
}

/// <summary>
Expand Down
Binary file modified Unity Example/Assets/Discord RPC/Discord Manager.prefab
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void Start()
DiscordManager.current.events.OnPresenceUpdate.AddListener((message) =>
{
Debug.Log("Received a new presence! Current App: " + message.ApplicationID + ", " + message.Name);
UpdateFields(new DiscordPresence(message.Presence));
UpdateFields(new DiscordPresence(message.Presence.ToRichPresence()));
});
}

Expand Down
Binary file not shown.
Binary file modified Unity Example/Assets/Discord RPC/Plugins/DiscordRPC.dll
Binary file not shown.
125 changes: 63 additions & 62 deletions Unity Example/Assets/Discord RPC/Scripts/DiscordManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@
/// A wrapper for the Discord Sharp Client, providing useful utilities in a Unity-Friendly form.
/// </summary>
[ExecuteInEditMode]
public class DiscordManager : MonoBehaviour {
public class DiscordManager : MonoBehaviour
{

public const string EXAMPLE_APPLICATION = "424087019149328395";

[System.Obsolete("Use current instead")]
public static DiscordManager instance { get { return _instance; } }
[System.Obsolete("Use current instead")]
public static DiscordManager instance { get { return _instance; } }

/// <summary>
/// The current instance of the Discord Manager
/// </summary>
public static DiscordManager current { get { return _instance; } }
private static DiscordManager _instance;
/// <summary>
/// The current instance of the Discord Manager
/// </summary>
public static DiscordManager current { get { return _instance; } }
private static DiscordManager _instance;

#region Properties and Configurations
[Header("Properties")]
[Header("Properties")]
[Tooltip("The ID of the Discord Application. Visit the Discord API to create a new application if nessary.")]
public string applicationID = EXAMPLE_APPLICATION;

[Tooltip("The Steam App ID. This is a optional field used to launch your game through steam instead of the executable.")]
public string steamID = "";

[Tooltip("The pipe discord is located on. Useful for testing multiple clients.")]
public DiscordPipe targetPipe = DiscordPipe.FirstAvailable;

Expand Down Expand Up @@ -58,15 +59,15 @@ public enum DiscordPipe
[SerializeField]
[Tooltip("The enabled state of the IPC connection")]
private bool active = true;

/// <summary>
/// The current Discord user. This does not get set until the first Ready event.
/// </summary>
public DiscordUser CurrentUser { get { return _currentUser; } }
[Tooltip("The current Discord user. This does not get set until the first Ready event.")]

[Header("State")]
[SerializeField] private DiscordUser _currentUser;
[Header("State")]
[SerializeField] private DiscordUser _currentUser;

/// <summary>
/// The current event subscription flag.
Expand All @@ -82,23 +83,23 @@ public enum DiscordPipe
[Tooltip("The current Rich Presence displayed on the Discord Client.")]
[SerializeField] private DiscordPresence _currentPresence;

#endregion
#endregion

[Header("Handlers and Events")]
public DiscordRPC.Unity.DiscordEvents events = new DiscordRPC.Unity.DiscordEvents();
[Header("Handlers and Events")]
public DiscordRPC.Unity.DiscordEvents events = new DiscordRPC.Unity.DiscordEvents();

/// <summary>
/// The current Discord Client.
/// </summary>
public DiscordRPC.DiscordRpcClient client { get { return _client; } }
private DiscordRPC.DiscordRpcClient _client = null;

public bool isInitialized { get { return _client != null && _client.IsInitialized; } }
public bool isInitialized { get { return _client != null && _client.IsInitialized; } }

#region Unity Events

#region Unity Events

private void OnDisable() { Deinitialize(); } //Try to dispose the client when we are disabled
private void OnDestroy() { Deinitialize(); }
private void OnDisable() { Deinitialize(); } //Try to dispose the client when we are disabled
private void OnDestroy() { Deinitialize(); }

#if (UNITY_WSA || UNITY_WSA_10_0 || UNITY_STANDALONE) && !DISABLE_DISCORD

Expand Down Expand Up @@ -152,14 +153,14 @@ private static void CreateNewManager()
}
#endif

#endregion
#endregion

/// <summary>
/// Initializes the discord client if able to. Wont initialize if <see cref="active"/> is false, we are not in playmode, we already have a instance or we already have a client.
/// <para>This function is empty unless UNITY_WSA || UNITY_WSA_10_0 || UNITY_STANDALONE) && !DISABLE_DISCORD is meet.</para>
/// </summary>
public void Initialize()
{
/// <summary>
/// Initializes the discord client if able to. Wont initialize if <see cref="active"/> is false, we are not in playmode, we already have a instance or we already have a client.
/// <para>This function is empty unless UNITY_WSA || UNITY_WSA_10_0 || UNITY_STANDALONE) && !DISABLE_DISCORD is meet.</para>
/// </summary>
public void Initialize()
{
#if (UNITY_WSA || UNITY_WSA_10_0 || UNITY_STANDALONE) && !DISABLE_DISCORD

if (!active) return; //Are we allowed to be active?
Expand Down Expand Up @@ -205,7 +206,7 @@ public void Initialize()
client.RegisterUriScheme(steamID);

//Subscribe to some initial events
#region Event Registration
#region Event Registration
client.OnError += (s, args) => Debug.LogError("[DRP] Error Occured within the Discord IPC: (" + args.Code + ") " + args.Message);
client.OnJoinRequested += (s, args) => Debug.Log("[DRP] Join Requested");

Expand Down Expand Up @@ -238,7 +239,7 @@ public void Initialize()

//Register the unity events
events.RegisterEvents(client);
#endregion
#endregion

//Set initial presence and sub. (This will enqueue it)
SetSubscription(_currentSubscription);
Expand All @@ -249,29 +250,29 @@ public void Initialize()
Debug.Log("[DRP] Discord Rich Presence intialized and connecting...");

#endif
}

/// <summary>
/// If not already disposed, it will dispose and deinitialize the discord client.
/// </summary>
public void Deinitialize()
{
//We dispose outside the scripting symbols as we always want to be able to dispose (just in case).
if (_client != null)
{
Debug.Log("[DRP] Disposing Discord IPC Client...");
_client.Dispose();
_client = null;
Debug.Log("[DRP] Finished Disconnecting");
}
}
}

/// <summary>
/// Sets the Rich Presence of the Discord Client through the pipe connection.
/// <para>This will log a error if the client is null or not yet initiated.</para>
/// </summary>
/// <param name="presence">The Rich Presence to be shown to the client</param>
public void SetPresence(DiscordPresence presence)
/// <summary>
/// If not already disposed, it will dispose and deinitialize the discord client.
/// </summary>
public void Deinitialize()
{
//We dispose outside the scripting symbols as we always want to be able to dispose (just in case).
if (_client != null)
{
Debug.Log("[DRP] Disposing Discord IPC Client...");
_client.Dispose();
_client = null;
Debug.Log("[DRP] Finished Disconnecting");
}
}

/// <summary>
/// Sets the Rich Presence of the Discord Client through the pipe connection.
/// <para>This will log a error if the client is null or not yet initiated.</para>
/// </summary>
/// <param name="presence">The Rich Presence to be shown to the client</param>
public void SetPresence(DiscordPresence presence)
{
if (client == null)
{
Expand All @@ -295,14 +296,14 @@ public void SetPresence(DiscordPresence presence)
client.SetPresence(presence != null ? presence.ToRichPresence() : null);
}

/// <summary>
/// Resends the current Rich Presence to the Discord Client via the pipe connectoin.
/// </summary>
[ContextMenu("Resend Presence")]
public void ResetPresence()
{
SetPresence(_currentPresence);
}
/// <summary>
/// Resends the current Rich Presence to the Discord Client via the pipe connectoin.
/// </summary>
[ContextMenu("Resend Presence")]
public void ResetPresence()
{
SetPresence(_currentPresence);
}

/// <summary>
/// Sets the subscription flag, unsubscribing and then subscribing to the nessary events. Used for Join / Spectate feature. If you have not registered your application, this feature is unavailable.
Expand Down Expand Up @@ -331,7 +332,7 @@ public void SetSubscription(DiscordEvent evt)
public DiscordPresence UpdateDetails(string details)
{
if (_client == null) return null;
return (DiscordPresence) _client.UpdateDetails(details);
return (DiscordPresence)_client.UpdateDetails(details);
}

public DiscordPresence UpdateState(string state)
Expand Down
26 changes: 26 additions & 0 deletions Unity Example/Assets/Discord RPC/Scripts/Presence/DiscordButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;

[System.Serializable]
public class DiscordButton
{
/// <summary>
/// The text on the button to be displayed
/// </summary>
[Tooltip("The label on the button to be displayed")]
public string label;

/// <summary>
/// The tooltip of the image.
/// </summary>]
[Tooltip("The URL on the button to be displayed")]
public string url;

/// <summary>
/// Is the asset object empty?
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
return string.IsNullOrEmpty(label) && string.IsNullOrEmpty(url);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public class DiscordPresence
public DiscordAsset smallAsset;
public DiscordAsset largeAsset;

[Header("Button Details")]

/// <summary>
/// The buttons used for the presence.
/// </summary>
[Tooltip("The buttons used for the presence")]
public DiscordButton[] buttons;

[Header("Party Details")]

/// <summary>
Expand All @@ -65,7 +73,7 @@ public DiscordPresence() { }
/// Creats a new Presence object, copying values of the Rich Presence
/// </summary>
/// <param name="presence">The rich presence, often received by discord.</param>
public DiscordPresence(DiscordRPC.BaseRichPresence presence)
public DiscordPresence(DiscordRPC.RichPresence presence)
{
if (presence != null)
{
Expand Down Expand Up @@ -98,11 +106,30 @@ public DiscordPresence(DiscordRPC.BaseRichPresence presence)
this.largeAsset = new DiscordAsset();
}

if (presence.HasButtons())
{
this.buttons = new DiscordButton[presence.Buttons.Length];

for (int i = 0; i < presence.Buttons.Length; i++)
{
this.buttons[i] = new DiscordButton()
{
label = presence.Buttons[i].Label,
url = presence.Buttons[i].Url
};
}
}
else
{
this.buttons = new DiscordButton[0];
}


if (presence.HasTimestamps())
{
//This could probably be made simpler
this.startTime = presence.Timestamps.Start.HasValue ? new DiscordTimestamp((long) presence.Timestamps.StartUnixMilliseconds.Value) : DiscordTimestamp.Invalid;
this.endTime = presence.Timestamps.End.HasValue ? new DiscordTimestamp((long) presence.Timestamps.EndUnixMilliseconds.Value) : DiscordTimestamp.Invalid;
//This could probably be made simpler
this.startTime = presence.Timestamps.Start.HasValue ? new DiscordTimestamp((long)presence.Timestamps.StartUnixMilliseconds.Value) : DiscordTimestamp.Invalid;
this.endTime = presence.Timestamps.End.HasValue ? new DiscordTimestamp((long)presence.Timestamps.EndUnixMilliseconds.Value) : DiscordTimestamp.Invalid;
}
}
else
Expand All @@ -113,6 +140,7 @@ public DiscordPresence(DiscordRPC.BaseRichPresence presence)
this.secrets = new DiscordSecrets();
this.smallAsset = new DiscordAsset();
this.largeAsset = new DiscordAsset();
this.buttons = new DiscordButton[0];
this.startTime = DiscordTimestamp.Invalid;
this.endTime = DiscordTimestamp.Invalid;
}
Expand Down Expand Up @@ -151,6 +179,20 @@ public DiscordRPC.RichPresence ToRichPresence()
if (endTime.IsValid()) presence.Timestamps.End = endTime.GetDateTime();
}

if (buttons.Length > 0)
{
presence.Buttons = new DiscordRPC.Button[buttons.Length];

for (int i = 0; i < buttons.Length; i++)
{
presence.Buttons[i] = new DiscordRPC.Button
{
Label = buttons[i].label,
Url = buttons[i].url
};
}
}

return presence;
}

Expand Down

0 comments on commit b05e7cd

Please sign in to comment.