diff --git a/osu.Game.Tournament/IPC/FileBasedIPC.cs b/osu.Game.Tournament/IPC/FileBasedIPC.cs new file mode 100644 index 0000000000..c42ccc11c2 --- /dev/null +++ b/osu.Game.Tournament/IPC/FileBasedIPC.cs @@ -0,0 +1,107 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using System.IO; +using osu.Framework.Allocation; +using osu.Framework.Configuration; +using osu.Framework.Graphics; +using osu.Framework.Platform.Windows; +using osu.Game.Beatmaps; +using osu.Game.Beatmaps.Legacy; +using osu.Game.Online.API; +using osu.Game.Online.API.Requests; +using osu.Game.Rulesets; + +namespace osu.Game.Tournament.IPC +{ + public class FileBasedIPC : Component + { + [Resolved] + protected APIAccess API { get; private set; } + + [Resolved] + protected RulesetStore Rulesets { get; private set; } + + public readonly Bindable Beatmap = new Bindable(); + + public readonly Bindable Mods = new Bindable(); + + [BackgroundDependencyLoader] + private void load() + { + var stable = new StableStorage(); + + const string file_ipc_filename = "ipc.txt"; + + if (stable.Exists(file_ipc_filename)) + Scheduler.AddDelayed(delegate + { + try + { + using (var stream = stable.GetStream(file_ipc_filename)) + using (var sr = new StreamReader(stream)) + { + var beatmapId = int.Parse(sr.ReadLine()); + var mods = int.Parse(sr.ReadLine()); + + if (Beatmap.Value?.OnlineBeatmapID != beatmapId) + { + var req = new GetBeatmapRequest(new BeatmapInfo { OnlineBeatmapID = beatmapId }); + req.Success += b => Beatmap.Value = b.ToBeatmap(Rulesets); + API.Queue(req); + } + + Mods.Value = (LegacyMods)mods; + } + } + catch + { + // file might be in use. + } + }, 250, true); + } + + /// + /// A method of accessing an osu-stable install in a controlled fashion. + /// + private class StableStorage : WindowsStorage + { + protected override string LocateBasePath() + { + bool checkExists(string p) + { + return Directory.Exists(Path.Combine(p, "Songs")); + } + + string stableInstallPath; + + try + { + stableInstallPath = "E:\\osu!mappool"; + + if (checkExists(stableInstallPath)) + return stableInstallPath; + } + catch + { + } + + stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"osu!"); + if (checkExists(stableInstallPath)) + return stableInstallPath; + + stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".osu"); + if (checkExists(stableInstallPath)) + return stableInstallPath; + + return null; + } + + public StableStorage() + : base(string.Empty, null) + { + } + } + } +} diff --git a/osu.Game.Tournament/Screens/BeatmapInfoScreen.cs b/osu.Game.Tournament/Screens/BeatmapInfoScreen.cs index 18ba947582..11cbb56b49 100644 --- a/osu.Game.Tournament/Screens/BeatmapInfoScreen.cs +++ b/osu.Game.Tournament/Screens/BeatmapInfoScreen.cs @@ -1,33 +1,18 @@ // Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE -using System; -using System.IO; using osu.Framework.Allocation; using osu.Framework.Graphics; -using osu.Framework.Platform.Windows; using osu.Game.Beatmaps; using osu.Game.Beatmaps.Legacy; -using osu.Game.Online.API; -using osu.Game.Online.API.Requests; -using osu.Game.Online.API.Requests.Responses; -using osu.Game.Rulesets; using osu.Game.Screens; using osu.Game.Tournament.Components; +using osu.Game.Tournament.IPC; namespace osu.Game.Tournament.Screens { public abstract class BeatmapInfoScreen : OsuScreen { - [Resolved] - protected APIAccess API { get; private set; } - - [Resolved] - protected RulesetStore Rulesets { get; private set; } - - private int lastBeatmapId; - private int lastMods; - protected readonly SongBar SongBar; protected BeatmapInfoScreen() @@ -40,88 +25,21 @@ protected BeatmapInfoScreen() } [BackgroundDependencyLoader] - private void load() + private void load(FileBasedIPC ipc) { - var stable = new StableStorage(); - - const string file_ipc_filename = "ipc.txt"; - - if (stable.Exists(file_ipc_filename)) - Scheduler.AddDelayed(delegate - { - try - { - using (var stream = stable.GetStream(file_ipc_filename)) - using (var sr = new StreamReader(stream)) - { - var beatmapId = int.Parse(sr.ReadLine()); - var mods = int.Parse(sr.ReadLine()); - - if (lastBeatmapId == beatmapId) - return; - - lastMods = mods; - lastBeatmapId = beatmapId; - - var req = new GetBeatmapRequest(new BeatmapInfo { OnlineBeatmapID = beatmapId }); - req.Success += success; - API.Queue(req); - } - } - catch - { - // file might be in use. - } - }, 250, true); + ipc.Beatmap.BindValueChanged(beatmapChanged, true); + ipc.Mods.BindValueChanged(modsChanged, true); } - private void success(APIBeatmap apiBeatmap) + private void modsChanged(LegacyMods mods) + { + SongBar.Mods = mods; + } + + private void beatmapChanged(BeatmapInfo beatmap) { SongBar.FadeInFromZero(300, Easing.OutQuint); - SongBar.Mods = (LegacyMods)lastMods; - SongBar.Beatmap = apiBeatmap.ToBeatmap(Rulesets); - } - - /// - /// A method of accessing an osu-stable install in a controlled fashion. - /// - private class StableStorage : WindowsStorage - { - protected override string LocateBasePath() - { - bool checkExists(string p) - { - return Directory.Exists(Path.Combine(p, "Songs")); - } - - string stableInstallPath; - - try - { - stableInstallPath = "E:\\osu!mappool"; - - if (checkExists(stableInstallPath)) - return stableInstallPath; - } - catch - { - } - - stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"osu!"); - if (checkExists(stableInstallPath)) - return stableInstallPath; - - stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".osu"); - if (checkExists(stableInstallPath)) - return stableInstallPath; - - return null; - } - - public StableStorage() - : base(string.Empty, null) - { - } + SongBar.Beatmap = beatmap; } } } diff --git a/osu.Game.Tournament/TournamentGameBase.cs b/osu.Game.Tournament/TournamentGameBase.cs index f398dd0a3d..562b141095 100644 --- a/osu.Game.Tournament/TournamentGameBase.cs +++ b/osu.Game.Tournament/TournamentGameBase.cs @@ -17,6 +17,7 @@ using osu.Game.Online.API.Requests; using osu.Game.Rulesets; using osu.Game.Tournament.Components; +using osu.Game.Tournament.IPC; namespace osu.Game.Tournament { @@ -33,6 +34,7 @@ public abstract class TournamentGameBase : OsuGameBase private readonly Bindable ruleset = new Bindable(); private Bindable windowSize; + private FileBasedIPC ipc; protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) { @@ -63,6 +65,9 @@ private void load(Storage storage, FrameworkConfigManager frameworkConfig) dependencies.Cache(Ladder); + dependencies.Cache(ipc = new FileBasedIPC()); + Add(ipc); + bool addedInfo = false; // assign teams