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;
|
2020-05-16 00:59:48 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Newtonsoft.Json;
|
2019-06-21 06:07:04 +00:00
|
|
|
using Microsoft.Win32;
|
2018-11-07 16:23:00 +00:00
|
|
|
using osu.Framework.Allocation;
|
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;
|
|
|
|
using osu.Game.Beatmaps.Legacy;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
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 class FileBasedIPC : MatchIPCInfo
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
|
|
|
[Resolved]
|
2019-04-03 07:22:41 +00:00
|
|
|
protected IAPIProvider API { get; private set; }
|
2018-11-07 16:23:00 +00:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
protected RulesetStore Rulesets { get; private set; }
|
|
|
|
|
2019-09-21 19:15:02 +00:00
|
|
|
[Resolved]
|
|
|
|
private GameHost host { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private LadderInfo ladder { get; set; }
|
|
|
|
|
2018-11-09 23:37:21 +00:00
|
|
|
private int lastBeatmapId;
|
2019-09-21 19:15:02 +00:00
|
|
|
private ScheduledDelegate scheduled;
|
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
[Resolved]
|
|
|
|
private StableInfo stableInfo { get; set; }
|
|
|
|
|
|
|
|
public Storage IPCStorage { get; private set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private Storage tournamentStorage { get; set; }
|
2018-11-09 23:37:21 +00:00
|
|
|
|
2018-11-07 16:23:00 +00:00
|
|
|
[BackgroundDependencyLoader]
|
2019-09-21 19:15:02 +00:00
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
LocateStableStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Storage LocateStableStorage()
|
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
|
|
|
var path = findStablePath();
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2019-09-21 19:15:02 +00:00
|
|
|
var beatmapId = int.Parse(sr.ReadLine());
|
|
|
|
var mods = int.Parse(sr.ReadLine());
|
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
|
|
|
{
|
2019-09-21 19:15:02 +00:00
|
|
|
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-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))
|
|
|
|
{
|
|
|
|
ChatChannel.Value = sr.ReadLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
State.Value = (TourneyState)Enum.Parse(typeof(TourneyState), sr.ReadLine());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
Score1.Value = int.Parse(sr.ReadLine());
|
|
|
|
Score2.Value = int.Parse(sr.ReadLine());
|
|
|
|
}
|
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-05-20 15:25:53 +00:00
|
|
|
public bool checkExists(string p) => File.Exists(Path.Combine(p, "ipc.txt"));
|
2020-05-16 00:59:48 +00:00
|
|
|
|
2020-05-08 01:38:31 +00:00
|
|
|
private string findStablePath()
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2020-05-08 01:38:31 +00:00
|
|
|
string stableInstallPath = string.Empty;
|
2018-11-07 16:23:00 +00:00
|
|
|
|
2020-05-08 01:38:31 +00:00
|
|
|
try
|
|
|
|
{
|
2020-05-16 00:59:48 +00:00
|
|
|
List<Func<string>> stableFindMethods = new List<Func<string>>
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2020-05-16 00:59:48 +00:00
|
|
|
findFromJsonConfig,
|
|
|
|
findFromEnvVar,
|
|
|
|
findFromRegistry,
|
|
|
|
findFromLocalAppData,
|
|
|
|
findFromDotFolder
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var r in stableFindMethods)
|
|
|
|
{
|
|
|
|
stableInstallPath = r.Invoke();
|
2019-06-21 06:07:04 +00:00
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
if (stableInstallPath != null)
|
|
|
|
{
|
2018-11-10 15:45:48 +00:00
|
|
|
return stableInstallPath;
|
2020-05-16 00:59:48 +00:00
|
|
|
}
|
2020-05-08 01:38:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 15:13:35 +00:00
|
|
|
return null;
|
2020-05-16 00:59:48 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Logger.Log($"Stable path for tourney usage: {stableInstallPath}");
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 16:23:00 +00:00
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
private void saveStablePath()
|
|
|
|
{
|
2020-05-20 14:30:38 +00:00
|
|
|
using (var stream = tournamentStorage.GetStream(StableInfo.STABLE_CONFIG, FileAccess.Write, FileMode.Create))
|
2020-05-16 00:59:48 +00:00
|
|
|
using (var sw = new StreamWriter(stream))
|
|
|
|
{
|
|
|
|
sw.Write(JsonConvert.SerializeObject(stableInfo,
|
|
|
|
new JsonSerializerSettings
|
|
|
|
{
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
DefaultValueHandling = DefaultValueHandling.Ignore,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 16:23:00 +00:00
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
private string findFromEnvVar()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Logger.Log("Trying to find stable with environment variables");
|
|
|
|
string stableInstallPath = Environment.GetEnvironmentVariable("OSU_STABLE_PATH");
|
2020-05-08 01:38:31 +00:00
|
|
|
|
|
|
|
if (checkExists(stableInstallPath))
|
2020-05-16 00:59:48 +00:00
|
|
|
{
|
|
|
|
stableInfo.StablePath.Value = stableInstallPath;
|
|
|
|
saveStablePath();
|
2020-05-08 01:38:31 +00:00
|
|
|
return stableInstallPath;
|
2020-05-16 00:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-08 01:38:31 +00:00
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
private string findFromJsonConfig()
|
|
|
|
{
|
2020-05-17 22:47:31 +00:00
|
|
|
try
|
2018-11-07 16:23:00 +00:00
|
|
|
{
|
2020-05-17 22:47:31 +00:00
|
|
|
Logger.Log("Trying to find stable through the json config");
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(stableInfo.StablePath.Value))
|
2020-05-17 20:27:44 +00:00
|
|
|
return stableInfo.StablePath.Value;
|
2020-05-17 22:47:31 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2018-11-07 16:23:00 +00:00
|
|
|
}
|
2020-05-17 20:46:43 +00:00
|
|
|
|
2020-05-16 00:59:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string findFromLocalAppData()
|
|
|
|
{
|
|
|
|
Logger.Log("Trying to find stable in %LOCALAPPDATA%");
|
|
|
|
string stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"osu!");
|
|
|
|
|
|
|
|
if (checkExists(stableInstallPath))
|
|
|
|
{
|
|
|
|
stableInfo.StablePath.Value = stableInstallPath;
|
|
|
|
saveStablePath();
|
|
|
|
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");
|
|
|
|
|
|
|
|
if (checkExists(stableInstallPath))
|
|
|
|
{
|
|
|
|
stableInfo.StablePath.Value = stableInstallPath;
|
|
|
|
saveStablePath();
|
|
|
|
return stableInstallPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string findFromRegistry()
|
|
|
|
{
|
|
|
|
Logger.Log("Trying to find stable in registry");
|
|
|
|
|
|
|
|
string stableInstallPath;
|
|
|
|
|
|
|
|
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey("osu"))
|
|
|
|
stableInstallPath = key?.OpenSubKey(@"shell\open\command")?.GetValue(string.Empty).ToString().Split('"')[1].Replace("osu!.exe", "");
|
|
|
|
|
|
|
|
if (checkExists(stableInstallPath))
|
|
|
|
{
|
|
|
|
stableInfo.StablePath.Value = stableInstallPath;
|
|
|
|
saveStablePath();
|
|
|
|
return stableInstallPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-11-07 16:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|