2022-07-04 16:18:33 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-09-16 13:00:05 +08:00
|
|
|
using System;
|
2022-07-04 16:49:08 +08:00
|
|
|
using System.Diagnostics;
|
2023-12-07 01:24:31 +08:00
|
|
|
using System.Threading;
|
2022-07-04 16:18:33 +08:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
|
|
using osu.Framework.Allocation;
|
2022-07-04 16:49:08 +08:00
|
|
|
using osu.Framework.Bindables;
|
2022-07-04 16:18:33 +08:00
|
|
|
using osu.Framework.Logging;
|
2022-07-05 20:42:35 +08:00
|
|
|
using osu.Game.Configuration;
|
2022-07-04 16:18:33 +08:00
|
|
|
using osu.Game.Online.API;
|
2023-12-07 01:24:31 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
|
|
|
using osu.Game.Users;
|
2022-07-04 16:18:33 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Online.Metadata
|
|
|
|
{
|
|
|
|
public partial class OnlineMetadataClient : MetadataClient
|
|
|
|
{
|
2023-12-07 01:24:31 +08:00
|
|
|
public override IBindable<bool> IsConnected { get; } = new Bindable<bool>();
|
|
|
|
|
|
|
|
public override IBindable<bool> IsWatchingUserPresence => isWatchingUserPresence;
|
|
|
|
private readonly BindableBool isWatchingUserPresence = new BindableBool();
|
|
|
|
|
|
|
|
public override IBindableDictionary<int, UserPresence> UserStates => userStates;
|
|
|
|
private readonly BindableDictionary<int, UserPresence> userStates = new BindableDictionary<int, UserPresence>();
|
|
|
|
|
2022-07-04 16:18:33 +08:00
|
|
|
private readonly string endpoint;
|
|
|
|
|
|
|
|
private IHubClientConnector? connector;
|
|
|
|
|
2022-07-05 20:42:35 +08:00
|
|
|
private Bindable<int> lastQueueId = null!;
|
2022-07-04 16:49:08 +08:00
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
private IBindable<APIUser> localUser = null!;
|
|
|
|
private IBindable<UserActivity?> userActivity = null!;
|
|
|
|
private IBindable<UserStatus?>? userStatus;
|
|
|
|
|
2022-07-04 16:18:33 +08:00
|
|
|
private HubConnection? connection => connector?.CurrentConnection;
|
|
|
|
|
2022-07-14 14:18:12 +08:00
|
|
|
public OnlineMetadataClient(EndpointConfiguration endpoints)
|
2022-07-04 16:18:33 +08:00
|
|
|
{
|
|
|
|
endpoint = endpoints.MetadataEndpointUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-07-05 20:42:35 +08:00
|
|
|
private void load(IAPIProvider api, OsuConfigManager config)
|
2022-07-04 16:18:33 +08:00
|
|
|
{
|
|
|
|
// Importantly, we are intentionally not using MessagePack here to correctly support derived class serialization.
|
|
|
|
// More information on the limitations / reasoning can be found in osu-server-spectator's initialisation code.
|
2023-12-07 01:24:31 +08:00
|
|
|
connector = api.GetHubConnector(nameof(OnlineMetadataClient), endpoint, false);
|
2022-07-04 16:18:33 +08:00
|
|
|
|
|
|
|
if (connector != null)
|
|
|
|
{
|
|
|
|
connector.ConfigureConnection = connection =>
|
|
|
|
{
|
|
|
|
// this is kind of SILLY
|
|
|
|
// https://github.com/dotnet/aspnetcore/issues/15198
|
|
|
|
connection.On<BeatmapUpdates>(nameof(IMetadataClient.BeatmapSetsUpdated), ((IMetadataClient)this).BeatmapSetsUpdated);
|
2023-12-07 01:24:31 +08:00
|
|
|
connection.On<int, UserPresence?>(nameof(IMetadataClient.UserPresenceUpdated), ((IMetadataClient)this).UserPresenceUpdated);
|
2024-02-16 19:58:13 +08:00
|
|
|
connection.On(nameof(IStatefulUserHubClient.DisconnectRequested), ((IMetadataClient)this).DisconnectRequested);
|
2022-07-04 16:18:33 +08:00
|
|
|
};
|
2022-07-04 16:49:08 +08:00
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
IsConnected.BindTo(connector.IsConnected);
|
|
|
|
IsConnected.BindValueChanged(isConnectedChanged, true);
|
2022-07-04 16:49:08 +08:00
|
|
|
}
|
2022-07-05 20:42:35 +08:00
|
|
|
|
|
|
|
lastQueueId = config.GetBindable<int>(OsuSetting.LastProcessedMetadataId);
|
2023-12-07 01:24:31 +08:00
|
|
|
|
|
|
|
localUser = api.LocalUser.GetBoundCopy();
|
|
|
|
userActivity = api.Activity.GetBoundCopy()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
localUser.BindValueChanged(_ =>
|
|
|
|
{
|
|
|
|
if (localUser.Value is not GuestUser)
|
|
|
|
{
|
|
|
|
userStatus = localUser.Value.Status.GetBoundCopy();
|
|
|
|
userStatus.BindValueChanged(status => UpdateStatus(status.NewValue), true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
userStatus = null;
|
|
|
|
}, true);
|
|
|
|
userActivity.BindValueChanged(activity =>
|
|
|
|
{
|
|
|
|
if (localUser.Value is not GuestUser)
|
|
|
|
UpdateActivity(activity.NewValue);
|
|
|
|
}, true);
|
2022-07-04 16:49:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool catchingUp;
|
|
|
|
|
|
|
|
private void isConnectedChanged(ValueChangedEvent<bool> connected)
|
|
|
|
{
|
|
|
|
if (!connected.NewValue)
|
2023-12-07 01:24:31 +08:00
|
|
|
{
|
2023-12-20 13:08:12 +08:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
isWatchingUserPresence.Value = false;
|
|
|
|
userStates.Clear();
|
|
|
|
});
|
2022-07-04 16:49:08 +08:00
|
|
|
return;
|
2023-12-07 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (localUser.Value is not GuestUser)
|
|
|
|
{
|
|
|
|
UpdateActivity(userActivity.Value);
|
|
|
|
UpdateStatus(userStatus?.Value);
|
|
|
|
}
|
2022-07-04 16:49:08 +08:00
|
|
|
|
2022-07-05 20:42:35 +08:00
|
|
|
if (lastQueueId.Value >= 0)
|
2022-07-04 16:49:08 +08:00
|
|
|
{
|
|
|
|
catchingUp = true;
|
|
|
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
Logger.Log($"Requesting catch-up from {lastQueueId.Value}");
|
2022-12-16 17:16:26 +08:00
|
|
|
var catchUpChanges = await GetChangesSince(lastQueueId.Value).ConfigureAwait(true);
|
2022-07-04 16:49:08 +08:00
|
|
|
|
2022-07-05 20:42:35 +08:00
|
|
|
lastQueueId.Value = catchUpChanges.LastProcessedQueueID;
|
2022-07-04 16:49:08 +08:00
|
|
|
|
|
|
|
if (catchUpChanges.BeatmapSetIDs.Length == 0)
|
|
|
|
{
|
|
|
|
Logger.Log($"Catch-up complete at {lastQueueId.Value}");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-16 17:16:26 +08:00
|
|
|
await ProcessChanges(catchUpChanges.BeatmapSetIDs).ConfigureAwait(true);
|
2022-07-04 16:49:08 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-16 13:00:05 +08:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Log($"Error while processing catch-up of metadata ({e.Message})");
|
|
|
|
}
|
2022-07-04 16:49:08 +08:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
catchingUp = false;
|
|
|
|
}
|
|
|
|
});
|
2022-07-04 16:18:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 16:49:08 +08:00
|
|
|
public override async Task BeatmapSetsUpdated(BeatmapUpdates updates)
|
2022-07-04 16:18:33 +08:00
|
|
|
{
|
|
|
|
Logger.Log($"Received beatmap updates {updates.BeatmapSetIDs.Length} updates with last id {updates.LastProcessedQueueID}");
|
2022-07-04 16:49:08 +08:00
|
|
|
|
|
|
|
// If we're still catching up, avoid updating the last ID as it will interfere with catch-up efforts.
|
|
|
|
if (!catchingUp)
|
2022-07-05 20:42:35 +08:00
|
|
|
lastQueueId.Value = updates.LastProcessedQueueID;
|
2022-07-04 16:49:08 +08:00
|
|
|
|
2022-12-16 17:16:26 +08:00
|
|
|
await ProcessChanges(updates.BeatmapSetIDs).ConfigureAwait(false);
|
2022-07-04 16:49:08 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 20:42:35 +08:00
|
|
|
public override Task<BeatmapUpdates> GetChangesSince(int queueId)
|
2022-07-04 16:18:33 +08:00
|
|
|
{
|
2022-07-04 16:49:08 +08:00
|
|
|
if (connector?.IsConnected.Value != true)
|
|
|
|
return Task.FromCanceled<BeatmapUpdates>(default);
|
|
|
|
|
|
|
|
Logger.Log($"Requesting any changes since last known queue id {queueId}");
|
|
|
|
|
|
|
|
Debug.Assert(connection != null);
|
|
|
|
|
|
|
|
return connection.InvokeAsync<BeatmapUpdates>(nameof(IMetadataServer.GetChangesSince), queueId);
|
2022-07-04 16:18:33 +08:00
|
|
|
}
|
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
public override Task UpdateActivity(UserActivity? activity)
|
|
|
|
{
|
|
|
|
if (connector?.IsConnected.Value != true)
|
|
|
|
return Task.FromCanceled(new CancellationToken(true));
|
|
|
|
|
|
|
|
Debug.Assert(connection != null);
|
|
|
|
return connection.InvokeAsync(nameof(IMetadataServer.UpdateActivity), activity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task UpdateStatus(UserStatus? status)
|
|
|
|
{
|
|
|
|
if (connector?.IsConnected.Value != true)
|
|
|
|
return Task.FromCanceled(new CancellationToken(true));
|
|
|
|
|
|
|
|
Debug.Assert(connection != null);
|
|
|
|
return connection.InvokeAsync(nameof(IMetadataServer.UpdateStatus), status);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task UserPresenceUpdated(int userId, UserPresence? presence)
|
|
|
|
{
|
2023-12-20 13:08:12 +08:00
|
|
|
Schedule(() =>
|
2023-12-07 01:24:31 +08:00
|
|
|
{
|
2024-01-02 21:07:59 +08:00
|
|
|
if (presence?.Status != null)
|
2023-12-07 01:24:31 +08:00
|
|
|
userStates[userId] = presence.Value;
|
|
|
|
else
|
|
|
|
userStates.Remove(userId);
|
2023-12-20 13:08:12 +08:00
|
|
|
});
|
2023-12-07 01:24:31 +08:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task BeginWatchingUserPresence()
|
|
|
|
{
|
|
|
|
if (connector?.IsConnected.Value != true)
|
|
|
|
throw new OperationCanceledException();
|
|
|
|
|
|
|
|
Debug.Assert(connection != null);
|
|
|
|
await connection.InvokeAsync(nameof(IMetadataServer.BeginWatchingUserPresence)).ConfigureAwait(false);
|
2023-12-20 18:42:05 +08:00
|
|
|
Schedule(() => isWatchingUserPresence.Value = true);
|
2023-12-07 01:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task EndWatchingUserPresence()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (connector?.IsConnected.Value != true)
|
|
|
|
throw new OperationCanceledException();
|
|
|
|
|
2023-12-20 18:42:05 +08:00
|
|
|
// must be scheduled before any remote calls to avoid mis-ordering.
|
2023-12-20 13:08:12 +08:00
|
|
|
Schedule(() => userStates.Clear());
|
2023-12-07 01:24:31 +08:00
|
|
|
Debug.Assert(connection != null);
|
|
|
|
await connection.InvokeAsync(nameof(IMetadataServer.EndWatchingUserPresence)).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2023-12-20 18:42:05 +08:00
|
|
|
Schedule(() => isWatchingUserPresence.Value = false);
|
2023-12-07 01:24:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task DisconnectRequested()
|
|
|
|
{
|
|
|
|
await base.DisconnectRequested().ConfigureAwait(false);
|
|
|
|
await EndWatchingUserPresence().ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2022-07-04 16:18:33 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
connector?.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|