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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

262 lines
9.2 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
2022-06-17 15:37:17 +08:00
#nullable disable
2018-11-08 00:23:00 +08:00
using System;
using System.IO;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.Win32;
2018-11-08 00:23:00 +08:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.ObjectExtensions;
2018-11-10 23:45:48 +08:00
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Threading;
2018-11-08 00:23:00 +08:00
using osu.Game.Beatmaps.Legacy;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
2018-11-08 00:23:00 +08:00
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
{
public Storage IPCStorage { get; private set; }
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 IRulesetStore Rulesets { get; private set; }
2018-11-08 00:23:00 +08:00
[Resolved]
private GameHost host { get; set; }
[Resolved]
private LadderInfo ladder { get; set; }
[Resolved]
private StableInfo stableInfo { get; set; }
private int lastBeatmapId;
private ScheduledDelegate scheduled;
private GetBeatmapRequest beatmapLookupRequest;
2018-11-08 00:23:00 +08:00
[BackgroundDependencyLoader]
private void load()
{
string stablePath = stableInfo.StablePath ?? findStablePath();
initialiseIPCStorage(stablePath);
}
[CanBeNull]
private Storage initialiseIPCStorage(string path)
2018-11-08 00:23:00 +08:00
{
scheduled?.Cancel();
IPCStorage = null;
try
{
if (string.IsNullOrEmpty(path))
return null;
IPCStorage = new DesktopStorage(path, 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 (IPCStorage.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 = IPCStorage.GetStream(file_ipc_filename))
using (var sr = new StreamReader(stream))
2018-11-08 00:23:00 +08:00
{
int beatmapId = int.Parse(sr.ReadLine().AsNonNull());
int mods = int.Parse(sr.ReadLine().AsNonNull());
if (lastBeatmapId != beatmapId)
{
beatmapLookupRequest?.Cancel();
lastBeatmapId = beatmapId;
var existing = ladder.CurrentMatch.Value?.Round.Value?.Beatmaps.FirstOrDefault(b => b.ID == beatmapId && b.Beatmap != null);
if (existing != null)
Beatmap.Value = existing.Beatmap;
else
{
beatmapLookupRequest = new GetBeatmapRequest(new APIBeatmap { OnlineID = beatmapId });
beatmapLookupRequest.Success += b => Beatmap.Value = new TournamentBeatmap(b);
API.Queue(beatmapLookupRequest);
}
}
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 = IPCStorage.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 = IPCStorage.GetStream(file_ipc_state_filename))
using (var sr = new StreamReader(stream))
{
State.Value = (TourneyState)Enum.Parse(typeof(TourneyState), sr.ReadLine().AsNonNull());
}
}
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 = IPCStorage.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 IPCStorage;
2018-11-08 00:23:00 +08:00
}
2020-06-13 22:20:59 +08:00
/// <summary>
/// Manually sets the path to the directory used for inter-process communication with a cutting-edge install.
/// </summary>
/// <param name="path">Path to the IPC directory</param>
/// <returns>Whether the supplied path was a valid IPC directory.</returns>
public bool SetIPCLocation(string path)
{
if (path == null || !ipcFileExistsInDirectory(path))
return false;
var newStorage = initialiseIPCStorage(stableInfo.StablePath = path);
if (newStorage == null)
return false;
stableInfo.SaveChanges();
return true;
}
2020-06-13 22:20:59 +08:00
/// <summary>
/// Tries to automatically detect the path to the directory used for inter-process communication
/// with a cutting-edge install.
/// </summary>
/// <returns>Whether an IPC directory was successfully auto-detected.</returns>
public bool AutoDetectIPCLocation() => SetIPCLocation(findStablePath());
private static bool ipcFileExistsInDirectory(string p) => p != null && File.Exists(Path.Combine(p, "ipc.txt"));
[CanBeNull]
private string findStablePath()
2018-11-08 00:23:00 +08:00
{
string stableInstallPath = findFromEnvVar() ??
findFromRegistry() ??
findFromLocalAppData() ??
findFromDotFolder();
2020-06-22 15:32:27 +08:00
Logger.Log($"Stable path for tourney usage: {stableInstallPath}");
return stableInstallPath;
}
2018-11-08 00:23:00 +08:00
private string findFromEnvVar()
{
try
{
Logger.Log("Trying to find stable with environment variables");
string stableInstallPath = Environment.GetEnvironmentVariable("OSU_STABLE_PATH");
2020-06-04 05:49:06 +08:00
if (ipcFileExistsInDirectory(stableInstallPath))
return stableInstallPath;
}
catch
{
}
return null;
}
private string findFromLocalAppData()
{
Logger.Log("Trying to find stable in %LOCALAPPDATA%");
string stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"osu!");
2020-06-04 05:49:06 +08:00
if (ipcFileExistsInDirectory(stableInstallPath))
return stableInstallPath;
return null;
}
private string findFromDotFolder()
{
Logger.Log("Trying to find stable in dotfolders");
string stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".osu");
2020-06-04 05:49:06 +08:00
if (ipcFileExistsInDirectory(stableInstallPath))
return stableInstallPath;
return null;
}
private string findFromRegistry()
{
Logger.Log("Trying to find stable in registry");
try
{
string stableInstallPath;
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey("osu"))
2020-12-02 05:39:04 +08:00
stableInstallPath = key?.OpenSubKey(@"shell\open\command")?.GetValue(string.Empty)?.ToString()?.Split('"')[1].Replace("osu!.exe", "");
if (ipcFileExistsInDirectory(stableInstallPath))
return stableInstallPath;
}
catch
{
}
return null;
2018-11-08 00:23:00 +08:00
}
}
}