Skip to content

Commit

Permalink
Show account on discord rich presence (#2255)
Browse files Browse the repository at this point in the history
* add functionality for grabbing userid

* Create UserInfoResponse.cs

* add user pfp small image thingy

* add missing semicolons

* get rid of semicolons

* debugging (remove later)

* make properties nullable

* forgot to put dollar sign before interpolated string

* make properties that cant be nullable not nullable

* more debugging

* remove thing

* remove other thing

* remove thing (again) (again)

* remove thing (again) (again) (again)

* add space between username and displayname to make it more visually pleasing

* matt review changes (better code readability)

* add strings for show account on profile

* add AccountShownOnProfile setting

* add DiscordActivityJoinEnabled to integrations viewmodel

* fix accidentally swapping 2 variables

* refrence correct variables

* refrence correct variables (again)

* add showaccountonprofile strings

* add option to integrations page

* add missing < that somehow got lost

* make that its own option

* dont invert that value

* dont invert that (again)

* Update IntegrationsViewModel.cs

* fix grammatical issue in string

* move else to new line

* fix merge conflicts

* move gameJoinLoadTime check

* matt review changes

* handle if parsing userid fails

---------

Co-authored-by: pizzaboxer <[email protected]>
  • Loading branch information
axellse and pizzaboxer committed Sep 28, 2024
1 parent 0acf1ee commit 55f5ef4
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 7 deletions.
27 changes: 25 additions & 2 deletions Bloxstrap/Integrations/ActivityWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class ActivityWatcher : IDisposable
private const string GameDisconnectedEntry = "[FLog::Network] Time to disconnect replication data:";
private const string GameTeleportingEntry = "[FLog::SingleSurfaceApp] initiateTeleport";
private const string GameLeavingEntry = "[FLog::SingleSurfaceApp] leaveUGCGameInternal";
private const string GameJoinLoadTimeEntry = "[FLog::GameJoinLoadTime] Report game_join_loadtime:";

private const string GameJoinLoadTimeEntryPattern = ", userid:([0-9]+)";
private const string GameJoiningEntryPattern = @"! Joining game '([0-9a-f\-]{36})' place ([0-9]+) at ([0-9\.]+)";
private const string GameJoiningPrivateServerPattern = @"""accessCode"":""([0-9a-f\-]{36})""";
private const string GameJoiningUniversePattern = @"universeid:([0-9]+)";
Expand Down Expand Up @@ -153,7 +155,7 @@ private void ReadLogEntry(string entry)
if (!InGame && Data.PlaceId == 0)
{
// We are not in a game, nor are in the process of joining one

if (entry.Contains(GameJoiningPrivateServerEntry))
{
// we only expect to be joining a private server if we're not already in a game
Expand Down Expand Up @@ -209,6 +211,28 @@ private void ReadLogEntry(string entry)
{
// We are not confirmed to be in a game, but we are in the process of joining one

if (entry.Contains(GameJoinLoadTimeEntry))
{
Match match = Regex.Match(entry, GameJoinLoadTimeEntryPattern);

if (match.Groups.Count != 2)
{
App.Logger.WriteLine(LOG_IDENT, "Failed to assert format for game join load time entry");
App.Logger.WriteLine(LOG_IDENT, entry);
return;
}

if (!UInt64.TryParse(match.Groups[1].Value, out ulong result))
{
App.Logger.WriteLine(LOG_IDENT, "Failed to parse userid from game join load time entry");
App.Logger.WriteLine(LOG_IDENT, match.Groups[1].Value);
return;
}

Data.UserId = result;
App.Logger.WriteLine(LOG_IDENT, $"Got userid as {Data.UserId}");
}

if (entry.Contains(GameJoiningUniverseEntry))
{
var match = Regex.Match(entry, GameJoiningUniversePattern);
Expand Down Expand Up @@ -279,7 +303,6 @@ private void ReadLogEntry(string entry)
History.Insert(0, Data);

InGame = false;

Data = new();

OnGameLeave?.Invoke(this, new EventArgs());
Expand Down
38 changes: 33 additions & 5 deletions Bloxstrap/Integrations/DiscordRichPresence.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Windows;

using System.Windows;
using Bloxstrap.Models.RobloxApi;
using DiscordRPC;

namespace Bloxstrap.Integrations
Expand Down Expand Up @@ -192,6 +192,9 @@ public async Task<bool> SetCurrentGame()
}

string icon = "roblox";
string smallImageText = "Roblox";
string smallImage = "roblox";


var activity = _activityWatcher.Data;
long placeId = activity.PlaceId;
Expand Down Expand Up @@ -224,6 +227,31 @@ public async Task<bool> SetCurrentGame()

icon = universeDetails.Thumbnail.ImageUrl;

if (App.Settings.Prop.AccountShownOnProfile)
{
var userPfpResponse = await Http.GetJson<ApiArrayResponse<ThumbnailResponse>>($"https://thumbnails.roblox.com/v1/users/avatar-headshot?userIds={activity.UserId}&size=180x180&format=Png&isCircular=false"); //we can remove '-headshot' from the url if we want a full avatar picture
if (userPfpResponse is null || !userPfpResponse.Data.Any())
{
App.Logger.WriteLine(LOG_IDENT, "Could not get user thumbnail info!");
}
else
{
smallImage = userPfpResponse.Data.First().ImageUrl;
App.Logger.WriteLine(LOG_IDENT, $"Got user thumbnail as {smallImage}");
}

var userInfoResponse = await Http.GetJson<UserInfoResponse>($"https://users.roblox.com/v1/users/{activity.UserId}");
if (userInfoResponse is null)
{
App.Logger.WriteLine(LOG_IDENT, "Could not get user info!");
}
else
{
smallImageText = userInfoResponse.DisplayName + $" (@{userInfoResponse.Username})"; //example: john doe (@johndoe)
App.Logger.WriteLine(LOG_IDENT, $"Got user info as {smallImageText}");
}
}

if (!_activityWatcher.InGame || placeId != activity.PlaceId)
{
App.Logger.WriteLine(LOG_IDENT, "Aborting presence set because game activity has changed");
Expand Down Expand Up @@ -251,9 +279,9 @@ public async Task<bool> SetCurrentGame()
Assets = new Assets
{
LargeImageKey = icon,
LargeImageText = universeName,
SmallImageKey = "roblox",
SmallImageText = "Roblox"
LargeImageText = universeDetails.Data.Name,
SmallImageKey = smallImage,
SmallImageText = smallImageText
}
};

Expand Down
26 changes: 26 additions & 0 deletions Bloxstrap/Models/APIs/Roblox/UserInfoResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Bloxstrap.Models.RobloxApi
{
/// <summary>
/// Roblox.Web.Responses.Users.UserInfoResponse
/// </summary>
public class UserInfoResponse
{
[JsonPropertyName("description")]
public string ProfileDescription { get; set; } = null!;

[JsonPropertyName("created")]
public string JoinDate { get; set; } = null!;

[JsonPropertyName("isBanned")]
public bool IsBanned { get; set; }

[JsonPropertyName("hasVerifiedBadge")]
public bool HasVerifiedBadge { get; set; }

[JsonPropertyName("name")]
public string Username { get; set; } = null!;

[JsonPropertyName("displayName")]
public string DisplayName { get; set; } = null!;
}
}
2 changes: 2 additions & 0 deletions Bloxstrap/Models/Entities/ActivityData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public long UniverseId
/// This will be empty unless the server joined is a private server
/// </summary>
public string AccessCode { get; set; } = string.Empty;

public ulong UserId { get; set; } = 0;

public string MachineAddress { get; set; } = string.Empty;

Expand Down
1 change: 1 addition & 0 deletions Bloxstrap/Models/Persistable/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Settings
public bool EnableActivityTracking { get; set; } = true;
public bool UseDiscordRichPresence { get; set; } = true;
public bool HideRPCButtons { get; set; } = true;
public bool AccountShownOnProfile { get; set; } = true;
public bool ShowServerDetails { get; set; } = false;
public ObservableCollection<CustomIntegration> CustomIntegrations { get; set; } = new();

Expand Down
18 changes: 18 additions & 0 deletions Bloxstrap/Resources/Strings.Designer.cs

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

6 changes: 6 additions & 0 deletions Bloxstrap/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,12 @@ Selecting 'No' will ignore this warning and continue installation.</value>
<data name="Menu.Integrations.AllowActivityJoining.Title" xml:space="preserve">
<value>Allow activity joining</value>
</data>
<data name="Menu.Integrations.ShowAccountOnProfile.Description" xml:space="preserve">
<value>Shows which Roblox account you are using on your Discord profile.</value>
</data>
<data name="Menu.Integrations.ShowAccountOnProfile.Title" xml:space="preserve">
<value>Show account on profile</value>
</data>
<data name="Menu.Integrations.Custom.AppLocation" xml:space="preserve">
<value>Application Location</value>
</data>
Expand Down
7 changes: 7 additions & 0 deletions Bloxstrap/UI/Elements/Settings/Pages/IntegrationsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
<ui:ToggleSwitch IsChecked="{Binding DiscordActivityJoinEnabled, Mode=TwoWay}" />
</controls:OptionControl>

<controls:OptionControl
Header="{x:Static resources:Strings.Menu_Integrations_ShowAccountOnProfile_Title}"
Description="{x:Static resources:Strings.Menu_Integrations_ShowAccountOnProfile_Description}"
IsEnabled="{Binding InnerContent.IsChecked, ElementName=DiscordActivityOption, Mode=OneWay}">
<ui:ToggleSwitch IsChecked="{Binding DiscordAccountOnProfile, Mode=TwoWay}" />
</controls:OptionControl>

<TextBlock Text="{x:Static resources:Strings.Menu_Integrations_Custom_Title}" FontSize="20" FontWeight="Medium" Margin="0,16,0,0" />
<TextBlock Text="{x:Static resources:Strings.Menu_Integrations_Custom_Description}" TextWrapping="Wrap" Foreground="{DynamicResource TextFillColorSecondaryBrush}" />
<Grid Margin="0,8,0,0">
Expand Down
8 changes: 8 additions & 0 deletions Bloxstrap/UI/ViewModels/Settings/IntegrationsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public bool DiscordActivityEnabled
if (!value)
{
DiscordActivityJoinEnabled = value;
DiscordAccountOnProfile = value;
OnPropertyChanged(nameof(DiscordActivityJoinEnabled));
OnPropertyChanged(nameof(DiscordAccountOnProfile));
}
}
}
Expand All @@ -111,6 +113,12 @@ public bool DiscordActivityJoinEnabled
set => App.Settings.Prop.HideRPCButtons = !value;
}

public bool DiscordAccountOnProfile
{
get => App.Settings.Prop.AccountShownOnProfile;
set => App.Settings.Prop.AccountShownOnProfile = value;
}

public bool DisableAppPatchEnabled
{
get => App.Settings.Prop.UseDisableAppPatch;
Expand Down

0 comments on commit 55f5ef4

Please sign in to comment.