Move all IPC handling to its own class

This commit is contained in:
Dean Herbert 2018-11-08 01:23:00 +09:00
parent a8b1e509e7
commit 3eabac0e3d
3 changed files with 123 additions and 93 deletions

View File

@ -0,0 +1,107 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// 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<BeatmapInfo> Beatmap = new Bindable<BeatmapInfo>();
public readonly Bindable<LegacyMods> Mods = new Bindable<LegacyMods>();
[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);
}
/// <summary>
/// A method of accessing an osu-stable install in a controlled fashion.
/// </summary>
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)
{
}
}
}
}

View File

@ -1,33 +1,18 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// 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);
}
/// <summary>
/// A method of accessing an osu-stable install in a controlled fashion.
/// </summary>
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;
}
}
}

View File

@ -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<RulesetInfo> ruleset = new Bindable<RulesetInfo>();
private Bindable<Size> 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