Skip to content

Commit

Permalink
Show Rich Presence join button w/ reserved servers
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Sep 3, 2024
1 parent 26b7cbd commit 6868037
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
84 changes: 48 additions & 36 deletions Bloxstrap/Integrations/DiscordRichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public class DiscordRichPresence : IDisposable
{
private readonly DiscordRpcClient _rpcClient = new("1005469189907173486");
private readonly ActivityWatcher _activityWatcher;

private readonly Queue<Message> _messageQueue = new();

private DiscordRPC.RichPresence? _currentPresence;
private DiscordRPC.RichPresence? _currentPresenceCopy;
private Queue<Message> _messageQueue = new();
private DiscordRPC.RichPresence? _originalPresence;

private bool _visible = true;

Expand Down Expand Up @@ -52,7 +52,7 @@ public void ProcessRPCMessage(Message message, bool implicitUpdate = true)
if (message.Command != "SetRichPresence" && message.Command != "SetLaunchData")
return;

if (_currentPresence is null || _currentPresenceCopy is null)
if (_currentPresence is null || _originalPresence is null)
{
App.Logger.WriteLine(LOG_IDENT, "Presence is not set, enqueuing message");
_messageQueue.Enqueue(message);
Expand All @@ -63,12 +63,7 @@ public void ProcessRPCMessage(Message message, bool implicitUpdate = true)

if (message.Command == "SetLaunchData")
{
var buttonQuery = _currentPresence.Buttons.Where(x => x.Label == "Join server");

if (!buttonQuery.Any())
return;

buttonQuery.First().Url = _activityWatcher.Data.GetInviteDeeplink();
_currentPresence.Buttons = GetButtons();
}
else if (message.Command == "SetRichPresence")
{
Expand All @@ -95,7 +90,7 @@ public void ProcessRPCMessage(Message message, bool implicitUpdate = true)
if (presenceData.Details.Length > 128)
App.Logger.WriteLine(LOG_IDENT, $"Details cannot be longer than 128 characters");
else if (presenceData.Details == "<reset>")
_currentPresence.Details = _currentPresenceCopy.Details;
_currentPresence.Details = _originalPresence.Details;
else
_currentPresence.Details = presenceData.Details;
}
Expand All @@ -105,7 +100,7 @@ public void ProcessRPCMessage(Message message, bool implicitUpdate = true)
if (presenceData.State.Length > 128)
App.Logger.WriteLine(LOG_IDENT, $"State cannot be longer than 128 characters");
else if (presenceData.State == "<reset>")
_currentPresence.State = _currentPresenceCopy.State;
_currentPresence.State = _originalPresence.State;
else
_currentPresence.State = presenceData.State;
}
Expand All @@ -128,8 +123,8 @@ public void ProcessRPCMessage(Message message, bool implicitUpdate = true)
}
else if (presenceData.SmallImage.Reset)
{
_currentPresence.Assets.SmallImageText = _currentPresenceCopy.Assets.SmallImageText;
_currentPresence.Assets.SmallImageKey = _currentPresenceCopy.Assets.SmallImageKey;
_currentPresence.Assets.SmallImageText = _originalPresence.Assets.SmallImageText;
_currentPresence.Assets.SmallImageKey = _originalPresence.Assets.SmallImageKey;
}
else
{
Expand All @@ -149,8 +144,8 @@ public void ProcessRPCMessage(Message message, bool implicitUpdate = true)
}
else if (presenceData.LargeImage.Reset)
{
_currentPresence.Assets.LargeImageText = _currentPresenceCopy.Assets.LargeImageText;
_currentPresence.Assets.LargeImageKey = _currentPresenceCopy.Assets.LargeImageKey;
_currentPresence.Assets.LargeImageText = _originalPresence.Assets.LargeImageText;
_currentPresence.Assets.LargeImageKey = _originalPresence.Assets.LargeImageKey;
}
else
{
Expand Down Expand Up @@ -187,7 +182,7 @@ public async Task<bool> SetCurrentGame()
{
App.Logger.WriteLine(LOG_IDENT, "Not in game, clearing presence");

_currentPresence = _currentPresenceCopy = null;
_currentPresence = _originalPresence = null;
_messageQueue.Clear();

UpdatePresence();
Expand Down Expand Up @@ -223,23 +218,6 @@ public async Task<bool> SetCurrentGame()

icon = universeDetails.Thumbnail.ImageUrl;

List<Button> buttons = new();

if (!App.Settings.Prop.HideRPCButtons && activity.ServerType == ServerType.Public)
{
buttons.Add(new Button
{
Label = "Join server",
Url = activity.GetInviteDeeplink()
});
}

buttons.Add(new Button
{
Label = "See game page",
Url = $"https://www.roblox.com/games/{placeId}"
});

if (!_activityWatcher.InGame || placeId != activity.PlaceId)
{
App.Logger.WriteLine(LOG_IDENT, "Aborting presence set because game activity has changed");
Expand All @@ -263,7 +241,7 @@ public async Task<bool> SetCurrentGame()
Details = $"Playing {universeName}",
State = status,
Timestamps = new Timestamps { Start = timeStarted.ToUniversalTime() },
Buttons = buttons.ToArray(),
Buttons = GetButtons(),
Assets = new Assets
{
LargeImageKey = icon,
Expand All @@ -274,7 +252,7 @@ public async Task<bool> SetCurrentGame()
};

// this is used for configuration from BloxstrapRPC
_currentPresenceCopy = _currentPresence.Clone();
_originalPresence = _currentPresence.Clone();

if (_messageQueue.Any())
{
Expand All @@ -287,6 +265,40 @@ public async Task<bool> SetCurrentGame()
return true;
}

public Button[] GetButtons()
{
var buttons = new List<Button>();

var data = _activityWatcher.Data;

if (!App.Settings.Prop.HideRPCButtons)
{
bool show = false;

if (data.ServerType == ServerType.Public)
show = true;
else if (data.ServerType == ServerType.Reserved && !String.IsNullOrEmpty(data.RPCLaunchData))
show = true;

if (show)
{
buttons.Add(new Button
{
Label = "Join server",
Url = data.GetInviteDeeplink()
});
}
}

buttons.Add(new Button
{
Label = "See game page",
Url = $"https://www.roblox.com/games/{data.PlaceId}"
});

return buttons.ToArray();
}

public void UpdatePresence()
{
const string LOG_IDENT = "DiscordRichPresence::UpdatePresence";
Expand Down
5 changes: 0 additions & 5 deletions Bloxstrap/Models/ActivityData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ public long UniverseId

public DateTime? TimeLeft { get; set; }

/// <summary>
/// This gets set to true if this activity is from a same-universe teleport, whether public or reserved
/// </summary>
public bool IsRetainedUniverse { get; set; } = false;

// everything below here is optional strictly for bloxstraprpc, discord rich presence, or game history

/// <summary>
Expand Down

0 comments on commit 6868037

Please sign in to comment.