From afc36464503dd10e5dab9a09e1872416ce01b312 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 14 Mar 2018 09:48:03 +0900 Subject: [PATCH 1/6] Move API configuration hooks out of OsuGameBase Also makes username more private, and password completely private. --- osu.Game/Online/API/APIAccess.cs | 35 ++++++++++++------- osu.Game/OsuGameBase.cs | 20 ++--------- .../Sections/General/LoginSettings.cs | 2 +- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index bab53cb462..40584006cd 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -10,6 +10,7 @@ using System.Threading; using osu.Framework.Configuration; using osu.Framework.Logging; using osu.Framework.Threading; +using osu.Game.Configuration; using osu.Game.Online.API.Requests; using osu.Game.Users; @@ -17,6 +18,7 @@ namespace osu.Game.Online.API { public class APIAccess : IAPIProvider { + private readonly OsuConfigManager config; private readonly OAuth authentication; public string Endpoint = @"https://osu.ppy.sh"; @@ -27,11 +29,12 @@ namespace osu.Game.Online.API public Scheduler Scheduler = new Scheduler(); - public string Username; + /// + /// The username/email provided by the user when initiating a login. + /// + public string ProvidedUsername { get; private set; } - //private SecurePassword password; - - public string Password; + private string password; public Bindable LocalUser { get; } = new Bindable(createGuestUser()); @@ -41,18 +44,23 @@ namespace osu.Game.Online.API set { authentication.Token = string.IsNullOrEmpty(value) ? null : OAuthToken.Parse(value); } } - protected bool HasLogin => Token != null || !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password); + protected bool HasLogin => Token != null || !string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password); // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable (should dispose of this or at very least keep a reference). private readonly Thread thread; private readonly Logger log; - public APIAccess() + public APIAccess(OsuConfigManager config) { + this.config = config; + authentication = new OAuth(client_id, client_secret, Endpoint); log = Logger.GetLogger(LoggingTarget.Network); + ProvidedUsername = config.Get(OsuSetting.Username); + Token = config.Get(OsuSetting.Token); + thread = new Thread(run) { IsBackground = true }; thread.Start(); } @@ -111,12 +119,15 @@ namespace osu.Game.Online.API State = APIState.Connecting; - if (!authentication.HasValidAccessToken && !authentication.AuthenticateWithLogin(Username, Password)) + // save the username at this point, if the user requested for it to be. + config.Set(OsuSetting.Username, config.Get(OsuSetting.SaveUsername) ? ProvidedUsername : string.Empty); + + if (!authentication.HasValidAccessToken && !authentication.AuthenticateWithLogin(ProvidedUsername, password)) { //todo: this fails even on network-related issues. we should probably handle those differently. //NotificationOverlay.ShowMessage("Login failed!"); log.Add(@"Login failed!"); - Password = null; + password = null; authentication.Clear(); continue; } @@ -173,8 +184,8 @@ namespace osu.Game.Online.API { Debug.Assert(State == APIState.Offline); - Username = username; - Password = password; + ProvidedUsername = username; + this.password = password; } /// @@ -283,8 +294,8 @@ namespace osu.Game.Online.API public void Logout(bool clearUsername = true) { flushQueue(); - if (clearUsername) Username = null; - Password = null; + if (clearUsername) ProvidedUsername = null; + password = null; authentication.Clear(); LocalUser.Value = createGuestUser(); } diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index f3c46269d5..2096318a32 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -34,7 +34,7 @@ using osu.Game.Skinning; namespace osu.Game { - public class OsuGameBase : Framework.Game, IOnlineComponent, ICanAcceptFiles + public class OsuGameBase : Framework.Game, ICanAcceptFiles { protected OsuConfigManager LocalConfig; @@ -108,11 +108,7 @@ namespace osu.Game dependencies.Cache(SkinManager = new SkinManager(Host.Storage, contextFactory, Host, Audio)); - dependencies.Cache(API = new APIAccess - { - Username = LocalConfig.Get(OsuSetting.Username), - Token = LocalConfig.Get(OsuSetting.Token) - }); + dependencies.Cache(API = new APIAccess(LocalConfig)); dependencies.CacheAs(API); dependencies.Cache(RulesetStore = new RulesetStore(contextFactory)); @@ -183,8 +179,6 @@ namespace osu.Game lastBeatmap = b; }; - API.Register(this); - FileStore.Cleanup(); } @@ -211,16 +205,6 @@ namespace osu.Game private WorkingBeatmap lastBeatmap; - public void APIStateChanged(APIAccess api, APIState state) - { - switch (state) - { - case APIState.Online: - LocalConfig.Set(OsuSetting.Username, LocalConfig.Get(OsuSetting.SaveUsername) ? API.Username : string.Empty); - break; - } - } - protected override void LoadComplete() { base.LoadComplete(); diff --git a/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs b/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs index a5d068adbd..4a4fc7363e 100644 --- a/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs +++ b/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs @@ -210,7 +210,7 @@ namespace osu.Game.Overlays.Settings.Sections.General { PlaceholderText = "Email address", RelativeSizeAxes = Axes.X, - Text = api?.Username ?? string.Empty, + Text = api?.ProvidedUsername ?? string.Empty, TabbableContentContainer = this }, password = new OsuPasswordTextBox From 83cd2fd31763a773bdf78c2e2f6119b09df5c1f3 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 14 Mar 2018 10:07:16 +0900 Subject: [PATCH 2/6] Move token saving logic to APIAccess --- osu.Game/Online/API/APIAccess.cs | 21 +++++++++++++++++++-- osu.Game/OsuGameBase.cs | 8 +------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 40584006cd..91b77dcf1f 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -16,7 +16,7 @@ using osu.Game.Users; namespace osu.Game.Online.API { - public class APIAccess : IAPIProvider + public class APIAccess : IAPIProvider, IDisposable { private readonly OsuConfigManager config; private readonly OAuth authentication; @@ -27,7 +27,7 @@ namespace osu.Game.Online.API private ConcurrentQueue queue = new ConcurrentQueue(); - public Scheduler Scheduler = new Scheduler(); + public readonly Scheduler Scheduler = new Scheduler(); /// /// The username/email provided by the user when initiating a login. @@ -310,6 +310,23 @@ namespace osu.Game.Online.API { Scheduler.Update(); } + + private void dispose() + { + config.Set(OsuSetting.Token, config.Get(OsuSetting.SavePassword) ? Token : string.Empty); + config.Save(); + } + + public void Dispose() + { + dispose(); + GC.SuppressFinalize(this); + } + + ~APIAccess() + { + dispose(); + } } public enum APIState diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index 2096318a32..a3e4d34659 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -245,14 +245,8 @@ namespace osu.Game protected override void Dispose(bool isDisposing) { - //refresh token may have changed. - if (LocalConfig != null && API != null) - { - LocalConfig.Set(OsuSetting.Token, LocalConfig.Get(OsuSetting.SavePassword) ? API.Token : string.Empty); - LocalConfig.Save(); - } - base.Dispose(isDisposing); + API.Dispose(); } private readonly List fileImporters = new List(); From 07642546bbc47357a8adc6d3416824f2eaf88f39 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 14 Mar 2018 10:42:58 +0900 Subject: [PATCH 3/6] Make APIAccess a component --- osu.Game/Online/API/APIAccess.cs | 24 ++++------------------- osu.Game/Online/API/APIDownloadRequest.cs | 2 +- osu.Game/Online/API/IAPIProvider.cs | 3 +-- osu.Game/OsuGameBase.cs | 24 +++++++---------------- osu.Game/Overlays/Direct/DirectPanel.cs | 2 +- 5 files changed, 14 insertions(+), 41 deletions(-) diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 91b77dcf1f..22498d229d 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -8,15 +8,15 @@ using System.Diagnostics; using System.Net; using System.Threading; using osu.Framework.Configuration; +using osu.Framework.Graphics; using osu.Framework.Logging; -using osu.Framework.Threading; using osu.Game.Configuration; using osu.Game.Online.API.Requests; using osu.Game.Users; namespace osu.Game.Online.API { - public class APIAccess : IAPIProvider, IDisposable + public class APIAccess : Component, IAPIProvider { private readonly OsuConfigManager config; private readonly OAuth authentication; @@ -27,8 +27,6 @@ namespace osu.Game.Online.API private ConcurrentQueue queue = new ConcurrentQueue(); - public readonly Scheduler Scheduler = new Scheduler(); - /// /// The username/email provided by the user when initiating a login. /// @@ -306,27 +304,13 @@ namespace osu.Game.Online.API Id = 1, }; - public void Update() + protected override void Dispose(bool isDisposing) { - Scheduler.Update(); - } + base.Dispose(isDisposing); - private void dispose() - { config.Set(OsuSetting.Token, config.Get(OsuSetting.SavePassword) ? Token : string.Empty); config.Save(); } - - public void Dispose() - { - dispose(); - GC.SuppressFinalize(this); - } - - ~APIAccess() - { - dispose(); - } } public enum APIState diff --git a/osu.Game/Online/API/APIDownloadRequest.cs b/osu.Game/Online/API/APIDownloadRequest.cs index 2dff07a847..2c6a4e02ba 100644 --- a/osu.Game/Online/API/APIDownloadRequest.cs +++ b/osu.Game/Online/API/APIDownloadRequest.cs @@ -14,7 +14,7 @@ namespace osu.Game.Online.API return request; } - private void request_Progress(long current, long total) => API.Scheduler.Add(delegate { Progress?.Invoke(current, total); }); + private void request_Progress(long current, long total) => Progress?.Invoke(current, total); protected APIDownloadRequest() { diff --git a/osu.Game/Online/API/IAPIProvider.cs b/osu.Game/Online/API/IAPIProvider.cs index b3c8774209..4119691c85 100644 --- a/osu.Game/Online/API/IAPIProvider.cs +++ b/osu.Game/Online/API/IAPIProvider.cs @@ -1,13 +1,12 @@ // Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE -using osu.Framework; using osu.Framework.Configuration; using osu.Game.Users; namespace osu.Game.Online.API { - public interface IAPIProvider : IUpdateable + public interface IAPIProvider { /// /// The local user. diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index a3e4d34659..45fd45b4b5 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -56,8 +56,6 @@ namespace osu.Game protected override string MainResourceFile => @"osu.Game.Resources.dll"; - public APIAccess API; - private Container content; protected override Container Content => content; @@ -108,12 +106,14 @@ namespace osu.Game dependencies.Cache(SkinManager = new SkinManager(Host.Storage, contextFactory, Host, Audio)); - dependencies.Cache(API = new APIAccess(LocalConfig)); - dependencies.CacheAs(API); + var api = new APIAccess(LocalConfig); + + dependencies.Cache(api); + dependencies.CacheAs(api); dependencies.Cache(RulesetStore = new RulesetStore(contextFactory)); dependencies.Cache(FileStore = new FileStore(contextFactory, Host.Storage)); - dependencies.Cache(BeatmapManager = new BeatmapManager(Host.Storage, contextFactory, RulesetStore, API, Host)); + dependencies.Cache(BeatmapManager = new BeatmapManager(Host.Storage, contextFactory, RulesetStore, api, Host)); dependencies.Cache(ScoreStore = new ScoreStore(Host.Storage, contextFactory, Host, BeatmapManager, RulesetStore)); dependencies.Cache(KeyBindingStore = new KeyBindingStore(contextFactory, RulesetStore)); dependencies.Cache(SettingsStore = new SettingsStore(contextFactory)); @@ -180,6 +180,8 @@ namespace osu.Game }; FileStore.Cleanup(); + + AddInternal(api); } private void runMigrations() @@ -237,18 +239,6 @@ namespace osu.Game base.SetHost(host); } - protected override void Update() - { - base.Update(); - API.Update(); - } - - protected override void Dispose(bool isDisposing) - { - base.Dispose(isDisposing); - API.Dispose(); - } - private readonly List fileImporters = new List(); public void Import(params string[] paths) diff --git a/osu.Game/Overlays/Direct/DirectPanel.cs b/osu.Game/Overlays/Direct/DirectPanel.cs index e0d806c90f..cba63b4a49 100644 --- a/osu.Game/Overlays/Direct/DirectPanel.cs +++ b/osu.Game/Overlays/Direct/DirectPanel.cs @@ -186,7 +186,7 @@ namespace osu.Game.Overlays.Direct progressBar.FadeOut(500); }; - request.DownloadProgressed += progress => progressBar.Current.Value = progress; + request.DownloadProgressed += progress => Schedule(() => progressBar.Current.Value = progress); request.Success += data => { From ce2997419a8dc4e054755ad825ae6120a24543ab Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 14 Mar 2018 11:37:50 +0900 Subject: [PATCH 4/6] Expose API scheduling internally --- osu.Game/Online/API/APIAccess.cs | 2 ++ osu.Game/Online/API/APIDownloadRequest.cs | 2 +- osu.Game/Online/API/APIRequest.cs | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 22498d229d..2cb8424bcc 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -65,6 +65,8 @@ namespace osu.Game.Online.API private readonly List components = new List(); + internal void Schedule(Action action) => base.Schedule(action); + public void Register(IOnlineComponent component) { Scheduler.Add(delegate diff --git a/osu.Game/Online/API/APIDownloadRequest.cs b/osu.Game/Online/API/APIDownloadRequest.cs index 2c6a4e02ba..0a5210723d 100644 --- a/osu.Game/Online/API/APIDownloadRequest.cs +++ b/osu.Game/Online/API/APIDownloadRequest.cs @@ -14,7 +14,7 @@ namespace osu.Game.Online.API return request; } - private void request_Progress(long current, long total) => Progress?.Invoke(current, total); + private void request_Progress(long current, long total) => API.Schedule(() => Progress?.Invoke(current, total)); protected APIDownloadRequest() { diff --git a/osu.Game/Online/API/APIRequest.cs b/osu.Game/Online/API/APIRequest.cs index 35af8eefd7..4b05df661b 100644 --- a/osu.Game/Online/API/APIRequest.cs +++ b/osu.Game/Online/API/APIRequest.cs @@ -85,7 +85,7 @@ namespace osu.Game.Online.API if (checkAndProcessFailure()) return; - api.Scheduler.Add(delegate { Success?.Invoke(); }); + api.Schedule(delegate { Success?.Invoke(); }); } public void Cancel() => Fail(new OperationCanceledException(@"Request cancelled")); @@ -108,7 +108,7 @@ namespace osu.Game.Online.API { if (API == null || pendingFailure == null) return cancelled; - API.Scheduler.Add(pendingFailure); + API.Schedule(pendingFailure); pendingFailure = null; return true; } From 149ee381e9b38916838a49708939840daba8a881 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 14 Mar 2018 13:55:16 +0900 Subject: [PATCH 5/6] Update framework --- osu-framework | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu-framework b/osu-framework index d29c8365ba..727a8fb93b 160000 --- a/osu-framework +++ b/osu-framework @@ -1 +1 @@ -Subproject commit d29c8365ba3cf7924b57cf22341f4af55658764c +Subproject commit 727a8fb93b50aec18f8f83c9046243174e09de93 From 2ba2556d2af131d2d29e4f9ff5c6cdd7e5b65573 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 16 Mar 2018 15:24:47 +0900 Subject: [PATCH 6/6] Update submodules --- osu-framework | 2 +- osu-resources | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/osu-framework b/osu-framework index 727a8fb93b..41e2a0a430 160000 --- a/osu-framework +++ b/osu-framework @@ -1 +1 @@ -Subproject commit 727a8fb93b50aec18f8f83c9046243174e09de93 +Subproject commit 41e2a0a4304544fb67779c21cad1435c105982d5 diff --git a/osu-resources b/osu-resources index 92ec3d10b1..7bb0782200 160000 --- a/osu-resources +++ b/osu-resources @@ -1 +1 @@ -Subproject commit 92ec3d10b12c5e9bfc1d3b05d3db174a506efd6d +Subproject commit 7bb0782200abadf73b79ed1a3bc1d5b926c6a81e