Refactor room secret handling to use JSON

Also log room secrets for debugging purposes
This commit is contained in:
jvyden 2024-03-05 18:15:53 -05:00
parent cceb616a18
commit b53777c2a4
No known key found for this signature in database
GPG Key ID: 18BCF2BE0262B278

View File

@ -5,6 +5,7 @@ using System;
using System.Text; using System.Text;
using DiscordRPC; using DiscordRPC;
using DiscordRPC.Message; using DiscordRPC.Message;
using Newtonsoft.Json;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -147,7 +148,13 @@ namespace osu.Desktop
Size = room.Users.Count, Size = room.Users.Count,
}; };
presence.Secrets.JoinSecret = $"{room.RoomID}:{room.Settings.Password}"; RoomSecret roomSecret = new RoomSecret
{
RoomID = room.RoomID,
Password = room.Settings.Password,
};
presence.Secrets.JoinSecret = JsonConvert.SerializeObject(roomSecret);
} }
else else
{ {
@ -182,9 +189,13 @@ namespace osu.Desktop
private void onJoin(object sender, JoinMessage args) private void onJoin(object sender, JoinMessage args)
{ {
game.Window?.Raise(); game.Window?.Raise();
Logger.Log($"Received room secret from Discord RPC Client: {args.Secret}", LoggingTarget.Network, LogLevel.Debug);
if (!tryParseRoomSecret(args.Secret, out long roomId, out string? password)) if (!tryParseRoomSecret(args.Secret, out long roomId, out string? password))
{
Logger.Log("Could not parse room from Discord RPC Client", LoggingTarget.Network, LogLevel.Important); Logger.Log("Could not parse room from Discord RPC Client", LoggingTarget.Network, LogLevel.Important);
return;
}
var request = new GetRoomRequest(roomId); var request = new GetRoomRequest(roomId);
request.Success += room => Schedule(() => request.Success += room => Schedule(() =>
@ -216,21 +227,26 @@ namespace osu.Desktop
}); });
} }
private static bool tryParseRoomSecret(ReadOnlySpan<char> secret, out long roomId, out string? password) private static bool tryParseRoomSecret(string secretJson, out long roomId, out string? password)
{ {
roomId = 0; roomId = 0;
password = null; password = null;
int roomSecretSplitIndex = secret.IndexOf(':'); RoomSecret? roomSecret;
if (roomSecretSplitIndex == -1) try
{
roomSecret = JsonConvert.DeserializeObject<RoomSecret>(secretJson);
}
catch
{
return false; return false;
}
if (!long.TryParse(secret[..roomSecretSplitIndex], out roomId)) if (roomSecret == null) return false;
return false;
password = secret[(roomSecretSplitIndex + 1)..].ToString(); roomId = roomSecret.RoomID;
if (password.Length == 0) password = null; password = roomSecret.Password;
return true; return true;
} }
@ -254,5 +270,14 @@ namespace osu.Desktop
client.Dispose(); client.Dispose();
base.Dispose(isDisposing); base.Dispose(isDisposing);
} }
private class RoomSecret
{
[JsonProperty(@"roomId", Required = Required.Always)]
public long RoomID { get; set; }
[JsonProperty(@"password", Required = Required.AllowNull)]
public string? Password { get; set; }
}
} }
} }