1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-18 10:53:21 +08:00

Merge pull request #31507 from peppy/api-startup-user

Ensure API starts up with `LocalUser` in correct state
This commit is contained in:
Dan Balasescu 2025-01-14 20:36:55 +09:00 committed by GitHub
commit f2b7984e5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,6 +14,7 @@ using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using osu.Framework.Bindables;
using osu.Framework.Development;
using osu.Framework.Extensions.ExceptionExtensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
@ -111,6 +112,9 @@ namespace osu.Game.Online.API
config.BindWith(OsuSetting.UserOnlineStatus, configStatus);
// Early call to ensure the local user / "logged in" state is correct immediately.
setPlaceholderLocalUser();
localUser.BindValueChanged(u =>
{
u.OldValue?.Activity.UnbindFrom(activity);
@ -194,7 +198,7 @@ namespace osu.Game.Online.API
Debug.Assert(HasLogin);
// Ensure that we are in an online state. If not, attempt a connect.
// Ensure that we are in an online state. If not, attempt to connect.
if (state.Value != APIState.Online)
{
attemptConnect();
@ -248,17 +252,7 @@ namespace osu.Game.Online.API
/// <returns>Whether the connection attempt was successful.</returns>
private void attemptConnect()
{
if (localUser.IsDefault)
{
// Show a placeholder user if saved credentials are available.
// This is useful for storing local scores and showing a placeholder username after starting the game,
// until a valid connection has been established.
setLocalUser(new APIUser
{
Username = ProvidedUsername,
Status = { Value = configStatus.Value ?? UserStatus.Online }
});
}
Scheduler.Add(setPlaceholderLocalUser, false);
// save the username at this point, if the user requested for it to be.
config.SetValue(OsuSetting.Username, config.Get<bool>(OsuSetting.SaveUsername) ? ProvidedUsername : string.Empty);
@ -340,9 +334,11 @@ namespace osu.Game.Online.API
userReq.Success += me =>
{
Debug.Assert(ThreadSafety.IsUpdateThread);
me.Status.Value = configStatus.Value ?? UserStatus.Online;
setLocalUser(me);
localUser.Value = me;
state.Value = me.SessionVerified ? APIState.Online : APIState.RequiresSecondFactorAuth;
failureCount = 0;
@ -367,6 +363,23 @@ namespace osu.Game.Online.API
Thread.Sleep(500);
}
/// <summary>
/// Show a placeholder user if saved credentials are available.
/// This is useful for storing local scores and showing a placeholder username after starting the game,
/// until a valid connection has been established.
/// </summary>
private void setPlaceholderLocalUser()
{
if (!localUser.IsDefault)
return;
localUser.Value = new APIUser
{
Username = ProvidedUsername,
Status = { Value = configStatus.Value ?? UserStatus.Online }
};
}
public void Perform(APIRequest request)
{
try
@ -594,7 +607,7 @@ namespace osu.Game.Online.API
// Scheduled prior to state change such that the state changed event is invoked with the correct user and their friends present
Schedule(() =>
{
setLocalUser(createGuestUser());
localUser.Value = createGuestUser();
friends.Clear();
});
@ -626,8 +639,6 @@ namespace osu.Game.Online.API
private static APIUser createGuestUser() => new GuestUser();
private void setLocalUser(APIUser user) => Scheduler.Add(() => localUser.Value = user, false);
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);