// Copyright (c) 2007-2018 ppy Pty Ltd . // 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; using osu.Framework.Logging; 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 { public enum TourneyState { Initialising, Idle, WaitingForClients, Playing, Ranking } public class FileBasedIPC : Component { [Resolved] protected APIAccess API { get; private set; } [Resolved] protected RulesetStore Rulesets { get; private set; } public readonly Bindable Beatmap = new Bindable(); public readonly Bindable Mods = new Bindable(); public readonly Bindable State = new Bindable(); private int lastBeatmapId; public int Score1; public int Score2; [BackgroundDependencyLoader] private void load() { var stable = new StableStorage(); 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"; 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) { lastBeatmapId = beatmapId; 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. } 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. } }, 250, true); } /// /// A method of accessing an osu-stable install in a controlled fashion. /// private class StableStorage : WindowsStorage { protected override string LocateBasePath() { bool checkExists(string p) { return File.Exists(Path.Combine(p, "ipc.txt")); } string stableInstallPath = string.Empty; try { 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; stableInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".osu"); if (checkExists(stableInstallPath)) return stableInstallPath; return null; } finally { Logger.Log($"Stable path for tourney usage: {stableInstallPath}"); } } public StableStorage() : base(string.Empty, null) { } } } }