2023-05-07 11:10:59 +00: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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using osu.Game.Database;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps
|
|
|
|
{
|
|
|
|
public class APIBeatmapMetadataSource : IOnlineBeatmapMetadataSource
|
|
|
|
{
|
|
|
|
private readonly IAPIProvider api;
|
|
|
|
|
|
|
|
public APIBeatmapMetadataSource(IAPIProvider api)
|
|
|
|
{
|
|
|
|
this.api = api;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Available => api.State.Value == APIState.Online;
|
|
|
|
|
2023-05-07 15:49:31 +00:00
|
|
|
public bool TryLookup(BeatmapInfo beatmapInfo, out OnlineBeatmapMetadata? onlineMetadata)
|
2023-05-07 11:10:59 +00:00
|
|
|
{
|
|
|
|
if (!Available)
|
2023-05-07 15:49:31 +00:00
|
|
|
{
|
|
|
|
onlineMetadata = null;
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-07 11:10:59 +00:00
|
|
|
|
|
|
|
Debug.Assert(beatmapInfo.BeatmapSet != null);
|
|
|
|
|
|
|
|
var req = new GetBeatmapRequest(beatmapInfo);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// intentionally blocking to limit web request concurrency
|
|
|
|
api.Perform(req);
|
|
|
|
|
|
|
|
if (req.CompletionState == APIRequestCompletionState.Failed)
|
|
|
|
{
|
|
|
|
logForModel(beatmapInfo.BeatmapSet, $@"Online retrieval failed for {beatmapInfo}");
|
2023-05-07 15:49:31 +00:00
|
|
|
onlineMetadata = null;
|
|
|
|
return true;
|
2023-05-07 11:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var res = req.Response;
|
|
|
|
|
|
|
|
if (res != null)
|
|
|
|
{
|
|
|
|
logForModel(beatmapInfo.BeatmapSet, $@"Online retrieval mapped {beatmapInfo} to {res.OnlineBeatmapSetID} / {res.OnlineID}.");
|
|
|
|
|
2023-05-07 15:49:31 +00:00
|
|
|
onlineMetadata = new OnlineBeatmapMetadata
|
2023-05-07 11:10:59 +00:00
|
|
|
{
|
|
|
|
BeatmapID = res.OnlineID,
|
|
|
|
BeatmapSetID = res.OnlineBeatmapSetID,
|
|
|
|
AuthorID = res.AuthorID,
|
|
|
|
BeatmapStatus = res.Status,
|
|
|
|
BeatmapSetStatus = res.BeatmapSet?.Status,
|
|
|
|
DateRanked = res.BeatmapSet?.Ranked,
|
|
|
|
DateSubmitted = res.BeatmapSet?.Submitted,
|
|
|
|
MD5Hash = res.MD5Hash,
|
|
|
|
LastUpdated = res.LastUpdated
|
|
|
|
};
|
2023-05-07 15:49:31 +00:00
|
|
|
return true;
|
2023-05-07 11:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
logForModel(beatmapInfo.BeatmapSet, $@"Online retrieval failed for {beatmapInfo} ({e.Message})");
|
2023-05-07 15:49:31 +00:00
|
|
|
onlineMetadata = null;
|
|
|
|
return false;
|
2023-05-07 11:10:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 15:49:31 +00:00
|
|
|
onlineMetadata = null;
|
|
|
|
return false;
|
2023-05-07 11:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void logForModel(BeatmapSetInfo set, string message) =>
|
|
|
|
RealmArchiveModelImporter<BeatmapSetInfo>.LogForModel(set, $@"[{nameof(APIBeatmapMetadataSource)}] {message}");
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|