From 605d9737f0c920d97b5220bb3ae24d61ced398bd Mon Sep 17 00:00:00 2001 From: faint <46868845+ficcialfaint@users.noreply.github.com> Date: Sun, 26 Mar 2023 02:58:22 +0300 Subject: [PATCH] Improve Discord Rich Presence (#3816) --- Resources/Locale/en-US/discordRPC.ftl | 4 +++ Robust.Client/BaseClient.cs | 14 +++++++- Robust.Client/Utility/DiscordRichPresence.cs | 35 ++++++++++--------- Robust.Client/Utility/IDiscordRichPresence.cs | 4 +-- 4 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 Resources/Locale/en-US/discordRPC.ftl diff --git a/Resources/Locale/en-US/discordRPC.ftl b/Resources/Locale/en-US/discordRPC.ftl new file mode 100644 index 000000000..f25ff3251 --- /dev/null +++ b/Resources/Locale/en-US/discordRPC.ftl @@ -0,0 +1,4 @@ +discord-rpc-in-main-menu = In Main Menu +discord-rpc-character = Username: {$username} +discord-rpc-on-server = On Server: {$servername} +discord-rpc-players = Players: {$players}/{$maxplayers} \ No newline at end of file diff --git a/Robust.Client/BaseClient.cs b/Robust.Client/BaseClient.cs index c81a2a852..81793ae0a 100644 --- a/Robust.Client/BaseClient.cs +++ b/Robust.Client/BaseClient.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Net; using Robust.Client.Configuration; using Robust.Client.Debugging; @@ -63,9 +64,16 @@ namespace Robust.Client _configManager.OnValueChanged(CVars.NetTickrate, TickRateChanged, invokeImmediately: true); _playMan.Initialize(); + _playMan.PlayerListUpdated += OnPlayerListUpdated; Reset(); } + private void OnPlayerListUpdated(object? sender, EventArgs e) + { + var serverPlayers = _playMan.PlayerCount; + _discord.Update(GameInfo!.ServerName, _net.ServerChannel!.UserName, GameInfo.ServerMaxPlayers.ToString(), serverPlayers.ToString()); + } + private void SyncTimeBase(MsgSyncTimeBase message) { Logger.DebugS("client", $"Synchronized time base: {message.Tick}: {message.Time}"); @@ -167,7 +175,7 @@ namespace Robust.Client var userName = _net.ServerChannel!.UserName; var userId = _net.ServerChannel.UserId; - _discord.Update(info.ServerName, userName, info.ServerMaxPlayers.ToString()); + // start up player management _playMan.Startup(); @@ -175,6 +183,10 @@ namespace Robust.Client _playMan.LocalPlayer.Name = userName; _playMan.LocalPlayer.StatusChanged += OnLocalStatusChanged; + + var serverPlayers = _playMan.PlayerCount; + _discord.Update(info.ServerName, userName, info.ServerMaxPlayers.ToString(), serverPlayers.ToString()); + } /// diff --git a/Robust.Client/Utility/DiscordRichPresence.cs b/Robust.Client/Utility/DiscordRichPresence.cs index 354796235..e9e80143b 100644 --- a/Robust.Client/Utility/DiscordRichPresence.cs +++ b/Robust.Client/Utility/DiscordRichPresence.cs @@ -1,8 +1,9 @@ -using DiscordRPC; +using DiscordRPC; using DiscordRPC.Logging; using Robust.Shared; using Robust.Shared.Configuration; using Robust.Shared.IoC; +using Robust.Shared.Localization; using Robust.Shared.Log; using LogLevel = DiscordRPC.Logging.LogLevel; @@ -10,17 +11,7 @@ namespace Robust.Client.Utility { internal sealed class DiscordRichPresence : IDiscordRichPresence { - private static readonly RichPresence _defaultPresence = new() - { - Details = "In Main Menu", - State = "In Main Menu", - Assets = new Assets - { - LargeImageKey = "devstation", - LargeImageText = "I think coolsville SUCKS", - SmallImageKey = "logo" - } - }; + private static RichPresence _defaultPresence = new() {}; private RichPresence? _activePresence; @@ -28,11 +19,21 @@ namespace Robust.Client.Utility [Dependency] private readonly IConfigurationManager _configurationManager = default!; [Dependency] private readonly ILogManager _logManager = default!; + [Dependency] private readonly ILocalizationManager _loc = default!; private bool _initialized; public void Initialize() { + _defaultPresence = new() + { + State = _loc.GetString("discord-rpc-in-main-menu"), + Assets = new Assets + { + LargeImageKey = "logo", + LargeImageText = "I think coolsville SUCKS" + } + }; _configurationManager.OnValueChanged(CVars.DiscordEnabled, newValue => { if (!_initialized) @@ -52,7 +53,7 @@ namespace Robust.Client.Utility _stop(); } }); - + if (_configurationManager.GetCVar(CVars.DiscordEnabled)) { _start(); @@ -92,16 +93,16 @@ namespace Robust.Client.Utility _client = null; } - public void Update(string serverName, string username, string maxUser) + public void Update(string serverName, string username, string maxUsers, string users) { _activePresence = new RichPresence { - Details = $"On Server: {serverName}", - State = $"Max players: {maxUser}", + Details = _loc.GetString("discord-rpc-on-server", ("servername", serverName)), + State = _loc.GetString("discord-rpc-players", ("players", users), ("maxplayers", maxUsers)), Assets = new Assets { LargeImageKey = "devstation", - LargeImageText = $"Character: {username}", + LargeImageText = _loc.GetString("discord-rpc-character", ("username", username)), SmallImageKey = "logo" } }; diff --git a/Robust.Client/Utility/IDiscordRichPresence.cs b/Robust.Client/Utility/IDiscordRichPresence.cs index 65e68eb37..2915c2527 100644 --- a/Robust.Client/Utility/IDiscordRichPresence.cs +++ b/Robust.Client/Utility/IDiscordRichPresence.cs @@ -1,11 +1,11 @@ -using System; +using System; namespace Robust.Client.Utility { public interface IDiscordRichPresence: IDisposable { void Initialize(); - void Update(string serverName, string username, string maxUser); + void Update(string serverName, string username, string maxUsers, string users); void ClearPresence(); } }