2019-03-04 04:24:19 +00: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-07 16:23:00 +00:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
2018-11-17 06:18:22 +00:00
|
|
|
using System.Linq;
|
2019-06-21 06:07:04 +00:00
|
|
|
using Microsoft.Win32;
|
2018-11-07 16:23:00 +00:00
|
|
|
using osu.Framework.Allocation;
|
2021-05-14 21:53:37 +00:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2018-11-10 15:45:48 +00:00
|
|
|
using osu.Framework.Logging;
|
2019-05-26 01:25:17 +00:00
|
|
|
using osu.Framework.Platform;
|
2019-09-21 19:15:02 +00:00
|
|
|
using osu.Framework.Threading;
|
2018-11-07 16:23:00 +00:00
|
|
|
using osu.Game.Beatmaps.Legacy;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
2021-10-27 08:22:23 +00:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2018-11-07 16:23:00 +00:00
|
|
|
using osu.Game.Rulesets;
|
2019-06-18 05:51:48 +00:00
|
|
|
using osu.Game.Tournament.Models;
|
2018-11-07 16:23:00 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Tournament.IPC
|
|
|
|
{
|
2018-11-15 12:28:42 +00:00
|
|
|
public partial class FileBasedIPC : MatchIPCInfo
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2023-07-25 11:50:55 +00:00
|
|
|
public Storage? IPCStorage { get; private set; }
|
2020-06-13 13:27:46 +00:00
|
|
|
|
2018-11-07 16:23:00 +00:00
|
|
|
[Resolved]
|
2023-07-25 11:50:55 +00:00
|
|
|
protected IAPIProvider API { get; private set; } = null!;
|
2018-11-07 16:23:00 +00:00
|
|
|
|
|
|
|
[Resolved]
|
2023-07-25 11:50:55 +00:00
|
|
|
protected IRulesetStore Rulesets { get; private set; } = null!;
|
2018-11-07 16:23:00 +00:00
|
|
|
|
2019-09-21 19:15:02 +00:00
|
|
|
[Resolved]
|
2023-07-25 11:50:55 +00:00
|
|
|
private GameHost host { get; set; } = null!;
|
2019-09-21 19:15:02 +00:00
|
|
|
|
|
|
|
[Resolved]
|
2023-07-25 11:50:55 +00:00
|
|
|
private LadderInfo ladder { get; set; } = null!;
|
2019-09-21 19:15:02 +00:00
|
|
|
|
2020-06-13 13:05:52 +00:00
|
|
|
[Resolved]
|
2023-07-25 11:50:55 +00:00
|
|
|
private StableInfo stableInfo { get; set; } = null!;
|
2020-06-13 13:05:52 +00:00
|
|
|
|
2018-11-09 23:37:21 +00:00
|
|
|
private int lastBeatmapId;
|
2023-07-25 11:50:55 +00:00
|
|
|
private ScheduledDelegate? scheduled;
|
|
|
|
private GetBeatmapRequest? beatmapLookupRequest;
|
2019-09-21 19:15:02 +00:00
|
|
|
|
2018-11-07 16:23:00 +00:00
|
|
|
[BackgroundDependencyLoader]
|
2019-09-21 19:15:02 +00:00
|
|
|
private void load()
|
|
|
|
{
|
2023-07-25 11:50:55 +00:00
|
|
|
string? stablePath = stableInfo.StablePath ?? findStablePath();
|
2020-06-13 13:27:46 +00:00
|
|
|
initialiseIPCStorage(stablePath);
|
2019-09-21 19:15:02 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 11:50:55 +00:00
|
|
|
private Storage? initialiseIPCStorage(string? path)
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2019-09-21 19:15:02 +00:00
|
|
|
scheduled?.Cancel();
|
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
IPCStorage = null;
|
2018-11-17 04:36:36 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-05-08 01:38:31 +00:00
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
return null;
|
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
IPCStorage = new DesktopStorage(path, host as DesktopGameHost);
|
2018-11-07 16:23:00 +00:00
|
|
|
|
2019-09-21 19:15:02 +00: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-07 16:23:00 +00:00
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
if (IPCStorage.Exists(file_ipc_filename))
|
2019-11-11 11:53:22 +00:00
|
|
|
{
|
2019-09-21 19:15:02 +00:00
|
|
|
scheduled = Scheduler.AddDelayed(delegate
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2019-09-21 19:15:02 +00:00
|
|
|
try
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2020-05-16 00:59:48 +00:00
|
|
|
using (var stream = IPCStorage.GetStream(file_ipc_filename))
|
2019-09-21 19:15:02 +00:00
|
|
|
using (var sr = new StreamReader(stream))
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
int beatmapId = int.Parse(sr.ReadLine().AsNonNull());
|
|
|
|
int mods = int.Parse(sr.ReadLine().AsNonNull());
|
2018-11-17 06:18:22 +00:00
|
|
|
|
2019-09-21 19:15:02 +00:00
|
|
|
if (lastBeatmapId != beatmapId)
|
2018-11-17 06:18:22 +00:00
|
|
|
{
|
2020-06-04 08:20:08 +00:00
|
|
|
beatmapLookupRequest?.Cancel();
|
|
|
|
|
2019-09-21 19:15:02 +00:00
|
|
|
lastBeatmapId = beatmapId;
|
|
|
|
|
2023-07-25 11:50:55 +00:00
|
|
|
var existing = ladder.CurrentMatch.Value?.Round.Value?.Beatmaps.FirstOrDefault(b => b.ID == beatmapId);
|
2019-09-21 19:15:02 +00:00
|
|
|
|
2023-08-09 10:11:51 +00:00
|
|
|
if (existing != null)
|
2021-10-27 09:25:23 +00:00
|
|
|
Beatmap.Value = existing.Beatmap;
|
2019-09-21 19:15:02 +00:00
|
|
|
else
|
|
|
|
{
|
2021-10-27 08:22:23 +00:00
|
|
|
beatmapLookupRequest = new GetBeatmapRequest(new APIBeatmap { OnlineID = beatmapId });
|
2023-08-31 10:04:06 +00:00
|
|
|
beatmapLookupRequest.Success += b =>
|
|
|
|
{
|
|
|
|
if (lastBeatmapId == beatmapId)
|
|
|
|
Beatmap.Value = new TournamentBeatmap(b);
|
|
|
|
};
|
|
|
|
beatmapLookupRequest.Failure += _ =>
|
|
|
|
{
|
|
|
|
if (lastBeatmapId == beatmapId)
|
|
|
|
Beatmap.Value = null;
|
|
|
|
};
|
2020-06-04 08:20:08 +00:00
|
|
|
API.Queue(beatmapLookupRequest);
|
2019-09-21 19:15:02 +00:00
|
|
|
}
|
2018-11-17 06:18:22 +00:00
|
|
|
}
|
2018-11-07 16:23:00 +00:00
|
|
|
|
2019-09-21 19:15:02 +00:00
|
|
|
Mods.Value = (LegacyMods)mods;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// file might be in use.
|
2018-11-07 16:23:00 +00:00
|
|
|
}
|
2018-11-10 15:45:48 +00:00
|
|
|
|
2019-09-21 19:15:02 +00:00
|
|
|
try
|
2018-11-17 12:26:56 +00:00
|
|
|
{
|
2020-05-16 00:59:48 +00:00
|
|
|
using (var stream = IPCStorage.GetStream(file_ipc_channel_filename))
|
2019-09-21 19:15:02 +00:00
|
|
|
using (var sr = new StreamReader(stream))
|
|
|
|
{
|
2023-07-25 11:50:55 +00:00
|
|
|
ChatChannel.Value = sr.ReadLine().AsNonNull();
|
2019-09-21 19:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
// file might be in use.
|
2018-11-17 12:26:56 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 19:15:02 +00:00
|
|
|
try
|
2018-11-10 15:45:48 +00:00
|
|
|
{
|
2020-05-16 00:59:48 +00:00
|
|
|
using (var stream = IPCStorage.GetStream(file_ipc_state_filename))
|
2019-09-21 19:15:02 +00:00
|
|
|
using (var sr = new StreamReader(stream))
|
|
|
|
{
|
2022-12-26 19:36:39 +00:00
|
|
|
State.Value = Enum.Parse<TourneyState>(sr.ReadLine().AsNonNull());
|
2019-09-21 19:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
// file might be in use.
|
2018-11-10 15:45:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 19:15:02 +00:00
|
|
|
try
|
2018-11-10 15:45:48 +00:00
|
|
|
{
|
2020-05-16 00:59:48 +00:00
|
|
|
using (var stream = IPCStorage.GetStream(file_ipc_scores_filename))
|
2019-09-21 19:15:02 +00:00
|
|
|
using (var sr = new StreamReader(stream))
|
|
|
|
{
|
2023-07-25 11:50:55 +00:00
|
|
|
Score1.Value = int.Parse(sr.ReadLine().AsNonNull());
|
|
|
|
Score2.Value = int.Parse(sr.ReadLine().AsNonNull());
|
2019-09-21 19:15:02 +00:00
|
|
|
}
|
2018-11-10 15:45:48 +00:00
|
|
|
}
|
2019-09-21 19:15:02 +00:00
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
// file might be in use.
|
|
|
|
}
|
|
|
|
}, 250, true);
|
2019-11-11 11:53:22 +00:00
|
|
|
}
|
2019-09-21 19:15:02 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Error(e, "Stable installation could not be found; disabling file based IPC");
|
|
|
|
}
|
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
return IPCStorage;
|
2018-11-07 16:23:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 14:20:59 +00: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>
|
2023-07-25 11:50:55 +00:00
|
|
|
public bool SetIPCLocation(string? path)
|
2020-06-13 13:27:46 +00:00
|
|
|
{
|
2020-06-13 13:32:30 +00:00
|
|
|
if (path == null || !ipcFileExistsInDirectory(path))
|
2020-06-13 13:27:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
var newStorage = initialiseIPCStorage(stableInfo.StablePath = path);
|
|
|
|
if (newStorage == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
stableInfo.SaveChanges();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-13 14:20:59 +00: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>
|
2020-06-13 13:32:30 +00:00
|
|
|
public bool AutoDetectIPCLocation() => SetIPCLocation(findStablePath());
|
2020-06-13 13:27:46 +00:00
|
|
|
|
2023-08-09 10:11:36 +00:00
|
|
|
private static bool ipcFileExistsInDirectory(string? p) => p != null && File.Exists(Path.Combine(p, "ipc.txt"));
|
2020-05-16 00:59:48 +00:00
|
|
|
|
2023-07-25 11:50:55 +00:00
|
|
|
private string? findStablePath()
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2023-07-25 11:50:55 +00:00
|
|
|
string? stableInstallPath = findFromEnvVar() ??
|
|
|
|
findFromRegistry() ??
|
|
|
|
findFromLocalAppData() ??
|
|
|
|
findFromDotFolder();
|
2020-05-28 13:28:27 +00:00
|
|
|
|
2020-06-22 07:32:27 +00:00
|
|
|
Logger.Log($"Stable path for tourney usage: {stableInstallPath}");
|
|
|
|
return stableInstallPath;
|
2020-05-16 00:59:48 +00:00
|
|
|
}
|
2018-11-07 16:23:00 +00:00
|
|
|
|
2023-07-25 11:50:55 +00:00
|
|
|
private string? findFromEnvVar()
|
2020-05-16 00:59:48 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Logger.Log("Trying to find stable with environment variables");
|
2023-07-25 11:50:55 +00:00
|
|
|
string? stableInstallPath = Environment.GetEnvironmentVariable("OSU_STABLE_PATH");
|
2020-05-08 01:38:31 +00:00
|
|
|
|
2020-06-03 21:49:06 +00:00
|
|
|
if (ipcFileExistsInDirectory(stableInstallPath))
|
2023-08-09 10:11:36 +00:00
|
|
|
return stableInstallPath!;
|
2020-05-16 00:59:48 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-08 01:38:31 +00:00
|
|
|
|
2023-07-25 11:50:55 +00:00
|
|
|
private string? findFromLocalAppData()
|
2020-05-16 00:59:48 +00:00
|
|
|
{
|
|
|
|
Logger.Log("Trying to find stable in %LOCALAPPDATA%");
|
|
|
|
string stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"osu!");
|
|
|
|
|
2020-06-03 21:49:06 +00:00
|
|
|
if (ipcFileExistsInDirectory(stableInstallPath))
|
2020-05-16 00:59:48 +00:00
|
|
|
return stableInstallPath;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:50:55 +00:00
|
|
|
private string? findFromDotFolder()
|
2020-05-16 00:59:48 +00:00
|
|
|
{
|
|
|
|
Logger.Log("Trying to find stable in dotfolders");
|
|
|
|
string stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".osu");
|
|
|
|
|
2020-06-03 21:49:06 +00:00
|
|
|
if (ipcFileExistsInDirectory(stableInstallPath))
|
2020-05-16 00:59:48 +00:00
|
|
|
return stableInstallPath;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:50:55 +00:00
|
|
|
private string? findFromRegistry()
|
2020-05-16 00:59:48 +00:00
|
|
|
{
|
|
|
|
Logger.Log("Trying to find stable in registry");
|
|
|
|
|
2020-06-22 07:34:04 +00:00
|
|
|
try
|
|
|
|
{
|
2023-07-25 11:50:55 +00:00
|
|
|
string? stableInstallPath;
|
2020-05-16 00:59:48 +00:00
|
|
|
|
2022-12-16 09:16:26 +00:00
|
|
|
#pragma warning disable CA1416
|
2023-07-25 11:50:55 +00:00
|
|
|
using (RegistryKey? key = Registry.ClassesRoot.OpenSubKey("osu"))
|
2020-12-01 21:39:04 +00:00
|
|
|
stableInstallPath = key?.OpenSubKey(@"shell\open\command")?.GetValue(string.Empty)?.ToString()?.Split('"')[1].Replace("osu!.exe", "");
|
2022-12-16 09:16:26 +00:00
|
|
|
#pragma warning restore CA1416
|
2020-05-16 00:59:48 +00:00
|
|
|
|
2020-06-22 07:34:04 +00:00
|
|
|
if (ipcFileExistsInDirectory(stableInstallPath))
|
|
|
|
return stableInstallPath;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
2020-05-16 00:59:48 +00:00
|
|
|
|
|
|
|
return null;
|
2018-11-07 16:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|