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

215 lines
7.5 KiB
C#
Raw Normal View History

2019-03-04 12:24:19 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-11-08 00:23:00 +08:00
using System;
using System.IO;
using System.Linq;
using Microsoft.Win32;
2018-11-08 00:23:00 +08:00
using osu.Framework.Allocation;
2018-11-10 23:45:48 +08:00
using osu.Framework.Logging;
using osu.Framework.Platform;
2018-11-08 00:23:00 +08:00
using osu.Framework.Platform.Windows;
using osu.Framework.Threading;
2018-11-08 00:23:00 +08:00
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Legacy;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Rulesets;
2019-06-18 13:51:48 +08:00
using osu.Game.Tournament.Models;
2018-11-08 00:23:00 +08:00
namespace osu.Game.Tournament.IPC
{
2018-11-15 20:28:42 +08:00
public class FileBasedIPC : MatchIPCInfo
2018-11-08 00:23:00 +08:00
{
[Resolved]
protected IAPIProvider API { get; private set; }
2018-11-08 00:23:00 +08:00
[Resolved]
protected RulesetStore Rulesets { get; private set; }
[Resolved]
private GameHost host { get; set; }
[Resolved]
private LadderInfo ladder { get; set; }
private int lastBeatmapId;
private ScheduledDelegate scheduled;
public Storage Storage { get; private set; }
2018-11-08 00:23:00 +08:00
[BackgroundDependencyLoader]
private void load()
{
LocateStableStorage();
}
public Storage LocateStableStorage()
2018-11-08 00:23:00 +08:00
{
scheduled?.Cancel();
Storage = null;
try
{
Storage = new StableStorage(host as DesktopGameHost);
2018-11-08 00:23:00 +08:00
const string file_ipc_filename = "ipc.txt";
const string file_ipc_state_filename = "ipc-state.txt";
const string file_ipc_scores_filename = "ipc-scores.txt";
const string file_ipc_channel_filename = "ipc-channel.txt";
2018-11-08 00:23:00 +08:00
if (Storage.Exists(file_ipc_filename))
2019-11-11 19:53:22 +08:00
{
scheduled = Scheduler.AddDelayed(delegate
2018-11-08 00:23:00 +08:00
{
try
2018-11-08 00:23:00 +08:00
{
using (var stream = Storage.GetStream(file_ipc_filename))
using (var sr = new StreamReader(stream))
2018-11-08 00:23:00 +08:00
{
var beatmapId = int.Parse(sr.ReadLine());
var mods = int.Parse(sr.ReadLine());
if (lastBeatmapId != beatmapId)
{
lastBeatmapId = beatmapId;
var existing = ladder.CurrentMatch.Value?.Round.Value?.Beatmaps.FirstOrDefault(b => b.ID == beatmapId && b.BeatmapInfo != null);
if (existing != null)
Beatmap.Value = existing.BeatmapInfo;
else
{
var req = new GetBeatmapRequest(new BeatmapInfo { OnlineBeatmapID = beatmapId });
req.Success += b => Beatmap.Value = b.ToBeatmap(Rulesets);
API.Queue(req);
}
}
2018-11-08 00:23:00 +08:00
Mods.Value = (LegacyMods)mods;
}
}
catch
{
// file might be in use.
2018-11-08 00:23:00 +08:00
}
2018-11-10 23:45:48 +08:00
try
2018-11-17 20:26:56 +08:00
{
using (var stream = Storage.GetStream(file_ipc_channel_filename))
using (var sr = new StreamReader(stream))
{
ChatChannel.Value = sr.ReadLine();
}
}
catch (Exception)
{
// file might be in use.
2018-11-17 20:26:56 +08:00
}
try
2018-11-10 23:45:48 +08:00
{
using (var stream = Storage.GetStream(file_ipc_state_filename))
using (var sr = new StreamReader(stream))
{
State.Value = (TourneyState)Enum.Parse(typeof(TourneyState), sr.ReadLine());
}
}
catch (Exception)
{
// file might be in use.
2018-11-10 23:45:48 +08:00
}
try
2018-11-10 23:45:48 +08:00
{
using (var stream = Storage.GetStream(file_ipc_scores_filename))
using (var sr = new StreamReader(stream))
{
Score1.Value = int.Parse(sr.ReadLine());
Score2.Value = int.Parse(sr.ReadLine());
}
2018-11-10 23:45:48 +08:00
}
catch (Exception)
{
// file might be in use.
}
}, 250, true);
2019-11-11 19:53:22 +08:00
}
}
catch (Exception e)
{
Logger.Error(e, "Stable installation could not be found; disabling file based IPC");
}
return Storage;
2018-11-08 00:23:00 +08:00
}
/// <summary>
/// A method of accessing an osu-stable install in a controlled fashion.
/// </summary>
private class StableStorage : WindowsStorage
{
protected override string LocateBasePath()
{
2019-11-12 18:37:20 +08:00
static bool checkExists(string p)
2018-11-08 00:23:00 +08:00
{
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
{
2019-02-08 14:20:22 +08:00
stableInstallPath = "G:\\My Drive\\Main\\osu!tourney";
2018-11-10 23:45:48 +08:00
if (checkExists(stableInstallPath))
return stableInstallPath;
2019-02-08 14:20:22 +08:00
stableInstallPath = "G:\\My Drive\\Main\\osu!mappool";
2018-11-10 23:45:48 +08:00
if (checkExists(stableInstallPath))
return stableInstallPath;
}
catch
{
}
try
{
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey("osu"))
2019-11-12 18:33:24 +08:00
stableInstallPath = key?.OpenSubKey(@"shell\open\command")?.GetValue(string.Empty).ToString().Split('"')[1].Replace("osu!.exe", "");
if (checkExists(stableInstallPath))
2018-11-10 23:45:48 +08:00
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(DesktopGameHost host)
: base(string.Empty, host)
2018-11-08 00:23:00 +08:00
{
}
}
}
}