1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-15 17:47:18 +08:00

Support Discord game invites in multiplayer lobbies

This commit is contained in:
jvyden 2024-02-29 19:57:32 -05:00
parent dd3e4893b3
commit 060e17e989
No known key found for this signature in database
GPG Key ID: 18BCF2BE0262B278

View File

@ -9,10 +9,13 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Logging;
using osu.Game;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
using osu.Game.Rulesets;
using osu.Game.Users;
using LogLevel = osu.Framework.Logging.LogLevel;
@ -22,6 +25,7 @@ namespace osu.Desktop
internal partial class DiscordRichPresence : Component
{
private const string client_id = "367827983903490050";
public const string DISCORD_PROTOCOL = $"discord-{client_id}://";
private DiscordRpcClient client = null!;
@ -33,6 +37,12 @@ namespace osu.Desktop
[Resolved]
private IAPIProvider api { get; set; } = null!;
[Resolved]
private OsuGame game { get; set; } = null!;
[Resolved]
private MultiplayerClient multiplayerClient { get; set; } = null!;
private readonly IBindable<UserStatus?> status = new Bindable<UserStatus?>();
private readonly IBindable<UserActivity> activity = new Bindable<UserActivity>();
@ -40,7 +50,12 @@ namespace osu.Desktop
private readonly RichPresence presence = new RichPresence
{
Assets = new Assets { LargeImageKey = "osu_logo_lazer", }
Assets = new Assets { LargeImageKey = "osu_logo_lazer" },
Secrets = new Secrets
{
JoinSecret = null,
SpectateSecret = null,
},
};
[BackgroundDependencyLoader]
@ -52,8 +67,14 @@ namespace osu.Desktop
};
client.OnReady += onReady;
client.OnError += (_, e) => Logger.Log($"An error occurred with Discord RPC Client: {e.Code} {e.Message}", LoggingTarget.Network, LogLevel.Error);
client.OnError += (_, e) => Logger.Log($"An error occurred with Discord RPC Client: {e.Code} {e.Message}", LoggingTarget.Network);
// set up stuff for spectate/join
// first, we register a uri scheme for when osu! isn't running and a user clicks join/spectate
// the rpc library we use also happens to _require_ that we do this
client.RegisterUriScheme();
client.Subscribe(EventType.Join); // we have to explicitly tell discord to send us join events.
client.OnJoin += onJoin;
config.BindWith(OsuSetting.DiscordRichPresence, privacyMode);
@ -114,6 +135,28 @@ namespace osu.Desktop
{
presence.Buttons = null;
}
if (!hideIdentifiableInformation && multiplayerClient.Room != null)
{
MultiplayerRoom room = multiplayerClient.Room;
presence.Party = new Party
{
Privacy = string.IsNullOrEmpty(room.Settings.Password) ? Party.PrivacySetting.Public : Party.PrivacySetting.Private,
ID = room.RoomID.ToString(),
// technically lobbies can have infinite users, but Discord needs this to be set to something.
// 1024 just happens to look nice.
// https://discord.com/channels/188630481301012481/188630652340404224/1212967974793642034
Max = 1024,
Size = room.Users.Count,
};
presence.Secrets.JoinSecret = $"{room.RoomID}:{room.Settings.Password}";
}
else
{
presence.Party = null;
presence.Secrets.JoinSecret = null;
}
}
else
{
@ -139,6 +182,22 @@ namespace osu.Desktop
client.SetPresence(presence);
}
private void onJoin(object sender, JoinMessage args)
{
game.Window?.Raise(); // users will expect to be brought back to osu! when joining a lobby from discord
if (!tryParseRoomSecret(args.Secret, out long roomId, out string? password))
Logger.Log("Failed to parse the room secret Discord gave us", LoggingTarget.Network, LogLevel.Error);
var request = new GetRoomRequest(roomId);
request.Success += room => Schedule(() =>
{
game.PresentMultiplayerMatch(room, password);
});
request.Failure += _ => Logger.Log("Couldn't find the room Discord gave us", LoggingTarget.Network, LogLevel.Error);
api.Queue(request);
}
private static readonly int ellipsis_length = Encoding.UTF8.GetByteCount(new[] { '…' });
private string truncate(string str)
@ -160,6 +219,26 @@ namespace osu.Desktop
});
}
private static bool tryParseRoomSecret(ReadOnlySpan<char> secret, out long roomId, out string? password)
{
roomId = 0;
password = null;
int roomSecretSplitIndex = secret.IndexOf(':');
if (roomSecretSplitIndex == -1)
return false;
if (!long.TryParse(secret[..roomSecretSplitIndex], out roomId))
return false;
// just convert to string here, we're going to have to alloc it later anyways
password = secret[(roomSecretSplitIndex + 1)..].ToString();
if (password.Length == 0) password = null;
return true;
}
private int? getBeatmapID(UserActivity activity)
{
switch (activity)