1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 12:17:26 +08:00
osu-lazer/osu.Game.Tournament/IPC/FileBasedIPC.cs

169 lines
5.6 KiB
C#
Raw Normal View History

2018-11-08 00:23:00 +08:00
// 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;
2018-11-10 23:45:48 +08:00
using osu.Framework.Logging;
2018-11-08 00:23:00 +08:00
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
{
2018-11-10 23:45:48 +08:00
public enum TourneyState
{
Initialising,
Idle,
WaitingForClients,
Playing,
Ranking
}
2018-11-08 00:23:00 +08:00
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>();
2018-11-10 23:45:48 +08:00
public readonly Bindable<TourneyState> State = new Bindable<TourneyState>();
private int lastBeatmapId;
2018-11-10 23:45:48 +08:00
public int Score1;
public int Score2;
2018-11-08 00:23:00 +08:00
[BackgroundDependencyLoader]
private void load()
{
var stable = new StableStorage();
const string file_ipc_filename = "ipc.txt";
2018-11-10 23:45:48 +08:00
const string file_ipc_state_filename = "ipc-state.txt";
const string file_ipc_scores_filename = "ipc-scores.txt";
2018-11-08 00:23:00 +08:00
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)
2018-11-08 00:23:00 +08:00
{
lastBeatmapId = beatmapId;
2018-11-08 00:23:00 +08:00
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.
}
2018-11-10 23:45:48 +08:00
try
{
using (var stream = stable.GetStream(file_ipc_state_filename))
using (var sr = new StreamReader(stream))
{
State.Value = (TourneyState)Enum.Parse(typeof(TourneyState), sr.ReadLine());
}
}
catch (Exception e)
{
Logger.Log(e.ToString(), LoggingTarget.Runtime);
// file might be in use.
}
try
{
using (var stream = stable.GetStream(file_ipc_scores_filename))
using (var sr = new StreamReader(stream))
{
Score1 = int.Parse(sr.ReadLine());
Score2 = int.Parse(sr.ReadLine());
}
}
catch (Exception e)
{
Logger.Log(e.ToString(), LoggingTarget.Runtime);
// file might be in use.
}
2018-11-08 00:23:00 +08:00
}, 250, true);
}
/// <summary>
/// A method of accessing an osu-stable install in a controlled fashion.
/// </summary>
private class StableStorage : WindowsStorage
{
protected override string LocateBasePath()
{
2018-11-10 23:45:48 +08:00
2018-11-08 00:23:00 +08:00
bool checkExists(string p)
{
2018-11-10 23:45:48 +08:00
return File.Exists(Path.Combine(p, "ipc.txt"));
2018-11-08 00:23:00 +08:00
}
2018-11-10 23:45:48 +08:00
string stableInstallPath = string.Empty;
2018-11-08 00:23:00 +08:00
try
{
2018-11-10 23:45:48 +08:00
try
{
stableInstallPath = "E:\\osu!tourney";
if (checkExists(stableInstallPath))
return stableInstallPath;
stableInstallPath = "E:\\osu!mappool";
if (checkExists(stableInstallPath))
return stableInstallPath;
}
catch
{
}
stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"osu!");
if (checkExists(stableInstallPath))
return stableInstallPath;
2018-11-08 00:23:00 +08:00
2018-11-10 23:45:48 +08:00
stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".osu");
2018-11-08 00:23:00 +08:00
if (checkExists(stableInstallPath))
return stableInstallPath;
2018-11-10 23:45:48 +08:00
return null;
2018-11-08 00:23:00 +08:00
}
2018-11-10 23:45:48 +08:00
finally
2018-11-08 00:23:00 +08:00
{
2018-11-10 23:45:48 +08:00
Logger.Log($"Stable path for tourney usage: {stableInstallPath}");
2018-11-08 00:23:00 +08:00
}
}
public StableStorage()
: base(string.Empty, null)
{
}
}
}
}