2023-12-04 13:17:13 +00:00
|
|
|
using DiscordRPC;
|
2019-12-21 19:52:31 +00:00
|
|
|
using Ryujinx.Common;
|
2024-02-11 02:09:18 +00:00
|
|
|
using Ryujinx.UI.Common.Configuration;
|
2024-05-14 15:36:44 +01:00
|
|
|
using System.Text;
|
2019-12-21 19:52:31 +00:00
|
|
|
|
2024-02-11 02:09:18 +00:00
|
|
|
namespace Ryujinx.UI.Common
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2022-05-15 12:30:15 +01:00
|
|
|
public static class DiscordIntegrationModule
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2021-05-02 21:17:34 +01:00
|
|
|
private const string Description = "A simple, experimental Nintendo Switch emulator.";
|
2024-03-16 18:41:38 +00:00
|
|
|
private const string ApplicationId = "1216775165866807456";
|
2019-12-21 19:52:31 +00:00
|
|
|
|
2024-05-14 15:36:44 +01:00
|
|
|
private const int ApplicationByteLimit = 128;
|
|
|
|
private const string Ellipsis = "…";
|
|
|
|
|
2021-05-02 21:17:34 +01:00
|
|
|
private static DiscordRpcClient _discordClient;
|
2023-06-29 01:39:22 +01:00
|
|
|
private static RichPresence _discordPresenceMain;
|
2019-12-21 19:52:31 +00:00
|
|
|
|
|
|
|
public static void Initialize()
|
|
|
|
{
|
2021-05-02 21:17:34 +01:00
|
|
|
_discordPresenceMain = new RichPresence
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2021-01-08 08:14:13 +00:00
|
|
|
Assets = new Assets
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2023-06-29 01:39:22 +01:00
|
|
|
LargeImageKey = "ryujinx",
|
|
|
|
LargeImageText = Description,
|
2019-12-21 19:52:31 +00:00
|
|
|
},
|
2023-06-29 01:39:22 +01:00
|
|
|
Details = "Main Menu",
|
|
|
|
State = "Idling",
|
2021-05-02 21:17:34 +01:00
|
|
|
Timestamps = Timestamps.Now,
|
2024-03-16 18:41:38 +00:00
|
|
|
Buttons =
|
|
|
|
[
|
2023-06-29 01:39:22 +01:00
|
|
|
new Button
|
|
|
|
{
|
|
|
|
Label = "Website",
|
2024-03-16 18:41:38 +00:00
|
|
|
Url = "https://ryujinx.org/",
|
2023-06-29 01:39:22 +01:00
|
|
|
},
|
2024-03-16 18:41:38 +00:00
|
|
|
],
|
2019-12-21 19:52:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
|
|
|
|
}
|
|
|
|
|
2021-05-02 21:17:34 +01:00
|
|
|
private static void Update(object sender, ReactiveEventArgs<bool> evnt)
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2021-05-02 21:17:34 +01:00
|
|
|
if (evnt.OldValue != evnt.NewValue)
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
|
|
|
// If the integration was active, disable it and unload everything
|
2021-05-02 21:17:34 +01:00
|
|
|
if (evnt.OldValue)
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2020-03-29 04:25:54 +01:00
|
|
|
_discordClient?.Dispose();
|
2019-12-21 19:52:31 +00:00
|
|
|
|
2020-03-29 04:25:54 +01:00
|
|
|
_discordClient = null;
|
2019-12-21 19:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we need to activate it and the client isn't active, initialize it
|
2021-05-02 21:17:34 +01:00
|
|
|
if (evnt.NewValue && _discordClient == null)
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2024-03-16 18:41:38 +00:00
|
|
|
_discordClient = new DiscordRpcClient(ApplicationId);
|
2019-12-21 19:52:31 +00:00
|
|
|
|
2020-03-29 04:25:54 +01:00
|
|
|
_discordClient.Initialize();
|
2021-05-02 21:17:34 +01:00
|
|
|
_discordClient.SetPresence(_discordPresenceMain);
|
2019-12-21 19:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 15:36:44 +01:00
|
|
|
public static void SwitchToPlayingState(string titleId, string applicationName)
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2021-05-02 21:17:34 +01:00
|
|
|
_discordClient?.SetPresence(new RichPresence
|
2019-12-21 19:52:31 +00:00
|
|
|
{
|
2021-05-02 21:17:34 +01:00
|
|
|
Assets = new Assets
|
|
|
|
{
|
2023-06-29 01:39:22 +01:00
|
|
|
LargeImageKey = "game",
|
2024-05-14 15:36:44 +01:00
|
|
|
LargeImageText = TruncateToByteLength(applicationName, ApplicationByteLimit),
|
2023-06-29 01:39:22 +01:00
|
|
|
SmallImageKey = "ryujinx",
|
2021-05-02 21:17:34 +01:00
|
|
|
SmallImageText = Description,
|
|
|
|
},
|
2024-05-14 15:36:44 +01:00
|
|
|
Details = TruncateToByteLength($"Playing {applicationName}", ApplicationByteLimit),
|
2023-06-29 01:39:22 +01:00
|
|
|
State = (titleId == "0000000000000000") ? "Homebrew" : titleId.ToUpper(),
|
2021-05-02 21:17:34 +01:00
|
|
|
Timestamps = Timestamps.Now,
|
2024-03-16 18:41:38 +00:00
|
|
|
Buttons =
|
|
|
|
[
|
2023-06-29 01:39:22 +01:00
|
|
|
new Button
|
2021-05-02 21:17:34 +01:00
|
|
|
{
|
|
|
|
Label = "Website",
|
2023-06-29 01:39:22 +01:00
|
|
|
Url = "https://ryujinx.org/",
|
|
|
|
},
|
2024-03-16 18:41:38 +00:00
|
|
|
],
|
2021-05-02 21:17:34 +01:00
|
|
|
});
|
2019-12-21 19:52:31 +00:00
|
|
|
}
|
2020-01-21 22:23:11 +00:00
|
|
|
|
|
|
|
public static void SwitchToMainMenu()
|
|
|
|
{
|
2021-05-02 21:17:34 +01:00
|
|
|
_discordClient?.SetPresence(_discordPresenceMain);
|
2020-01-21 22:23:11 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 15:36:44 +01:00
|
|
|
private static string TruncateToByteLength(string input, int byteLimit)
|
|
|
|
{
|
|
|
|
if (Encoding.UTF8.GetByteCount(input) <= byteLimit)
|
|
|
|
{
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the length to trim the string to guarantee we have space for the trailing ellipsis.
|
|
|
|
int trimLimit = byteLimit - Encoding.UTF8.GetByteCount(Ellipsis);
|
|
|
|
|
|
|
|
// Basic trim to best case scenario of 1 byte characters.
|
|
|
|
input = input[..trimLimit];
|
|
|
|
|
|
|
|
while (Encoding.UTF8.GetByteCount(input) > trimLimit)
|
|
|
|
{
|
|
|
|
// Remove one character from the end of the string at a time.
|
|
|
|
input = input[..^1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return input.TrimEnd() + Ellipsis;
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:23:11 +00:00
|
|
|
public static void Exit()
|
|
|
|
{
|
2020-03-29 04:25:54 +01:00
|
|
|
_discordClient?.Dispose();
|
2020-01-21 22:23:11 +00:00
|
|
|
}
|
2019-12-21 19:52:31 +00:00
|
|
|
}
|
2023-06-29 01:39:22 +01:00
|
|
|
}
|